/*
	reflection.js for jQuery v1.02
	(c) 2006-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/
(function(a){a.fn.extend({reflect:function(b){b=a.extend({height:0.33,opacity:0.5},b);return this.unreflect().each(function(){var c=this;if(/^img$/i.test(c.tagName)){function d(){var j,g=Math.floor(c.height*b.height),k,f,i;if(a.browser.msie){j=a("<img />").attr("src",c.src).css({width:c.width,height:c.height,marginBottom:-c.height+g,filter:"flipv progid:DXImageTransform.Microsoft.Alpha(opacity="+(b.opacity*100)+", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy="+(b.height*100)+")"})[0]}else{j=a("<canvas />")[0];if(!j.getContext){return}f=j.getContext("2d");try{a(j).attr({width:c.width,height:g});f.save();f.translate(0,c.height-1);f.scale(1,-1);f.drawImage(c,0,0,c.width,c.height);f.restore();f.globalCompositeOperation="destination-out";i=f.createLinearGradient(0,0,0,g);i.addColorStop(0,"rgba(255, 255, 255, "+(1-b.opacity)+")");i.addColorStop(1,"rgba(255, 255, 255, 1.0)");f.fillStyle=i;f.rect(0,0,c.width,g);f.fill()}catch(h){return}}a(j).css({display:"block",border:0});k=a(/^a$/i.test(c.parentNode.tagName)?"<span />":"<div />").insertAfter(c).append([c,j])[0];k.className=c.className;a.data(c,"reflected",k.style.cssText=c.style.cssText);a(k).css({width:c.width,height:c.height+g,overflow:"hidden"});c.style.cssText="display: block; border: 0px";c.className="reflected"}if(c.complete){d()}else{a(c).load(d)}}})},unreflect:function(){return this.unbind("load").each(function(){var c=this,b=a.data(this,"reflected"),d;if(b!==undefined){d=c.parentNode;c.className=d.className;c.style.cssText=b;a.removeData(c,"reflected");d.parentNode.replaceChild(c,d)}})}})})(jQuery);
(function(a){a.fn.center=function(b){var c={top:function(){return window.pageYOffset||document.documentElement&&document.documentElement.scrollTop||document.body.scrollTop;},height:function(){return window.innerHeight||document.documentElement&&document.documentElement.clientHeight||document.body.clientHeight;}};return this.each(function(e){if(e==0){var g=a(this);var d=g.height();var f=c.top()+(c.height()/2)-(d/2);g.css({position:"absolute",marginTop:"0",top:f});}});};})(jQuery);
slide = {
	start: function(target, timeout) {
        var e = $(target + ':first');
        $(target + ':last').animate({opacity: 0}, 1).css({'z-index': '88'});
        var c = $(target).size();
        this.round(target, e, c, false, timeout);
    },

    round: function(t, e, c, o, timeout) {
        setTimeout(function() {
            if (o == false) o = e;
            n = e.next();
            n = ($(t).index(n) == -1) ? $(t + ':first') : n;
            e.animate({opacity: 0}, {queue: false},  200).css({'z-index': '88'});
            n.animate({opacity: 1}, {queue: false}, 1000).css({'z-index': '99'});
            slide.round(t, n, c, o, timeout);
        }, timeout);
    }
}

homepage = {
    start: function() {
        
        this.decoration();
        this.loginform();
        this.videoplayer();
        slide.start('#tutorial dd', 12000);
        slide.start('#inspiration dd', 12000);
        slide.start('#discussion dd', 12000);
    },
    
    decoration: function() {
        Cufon.replace('#user-profile h4 small', {fontFamily: 'GlasgowPl RR Light'});
        Cufon.replace('#user-profile h4 strong', {fontFamily: 'GlasgowPl RR ExtraBold'});
        Cufon.replace('.decoration h3 strong', {fontFamily: 'GlasgowPl RR Light'});
        Cufon.replace('h3 strong', {fontFamily: 'GlasgowPl RR Light'});
        $(".image img").reflect({height: 0.25, opacity: 0.33});
        $('#inspiration dd, #discussion dd, #tutorial dd').click(function() {
            link = $(this).find('a').attr('href');
            document.location.href = link;
        })
    },
    
    videoplayer: function() {
        
        $('#openvideo').click(function() {
            openwindow();
            return false;
        })
        
        function openwindow() {
            content = '';
            content += '<div id="window">';
            content += '<div id="videoplayer">';
            content += '</div>';
            content += '</div>';

            var values = {
                'height' : jQuery('body').height(),
                'width'  : jQuery('body').width()
            };

            jQuery('<div id="overlay"></div>').animate({opacity: 0}, 0).prependTo('body').css(values).animate({opacity: 0.6}, 180, function() {
                jQuery(content).animate({opacity: 0}, 0).prependTo('body').animate({opacity: 1}, 260, function() {
                    var so = new SWFObject('/swf/main_vplayer.swf', 'videoplayerflash', '614', '404', '9', '#000000', true);
                    so.addVariable("gateway", "/cake_gateway.php");
                    so.addParam("allowScriptAccess", "always");
                    so.addParam("scale", "noscale");
                    so.addParam('wmode', 'opaque');
                    so.write('videoplayer');
                    setTimeout(function() { document.getElementById('videoplayerflash').playVideo(); }, 1200);
                    jQuery('#overlay').click(function() {
                        jQuery('#window').fadeOut('120', function() {
                            jQuery('#overlay').fadeOut('60', function() {
                                jQuery('#window').remove();
                                jQuery('#overlay').remove();
                            });
                        });
                        return false;
                    });

                }).center();
            });   

        }
        return false;
    },
    
    loginform: function() {
        $("#loginform input[name='data[username]']").val('Login').one('click focus', function() {
            $(this).val('');
        });
        $("#loginform input[name='data[password]']").val('Hasło').one('click focus', function() {
            var input1   = document.getElementById('login-password');
            var input2   = input1.cloneNode(false);
            input2.type  = 'password';
            input2.value = '';
            input1.parentNode.replaceChild(input2, input1);
        });
        $('form#loginform').submit(function() {
            $('#loginform .loader input').fadeOut(60, function() {
                $('<img src="/image/common/loading.black.gif" />').appendTo($(this).parent());
            });
            $('#loginform .error').remove();
            var data = {
                "data[username]": $("#loginform input[name='data[username]']").val(),
                "data[password]": $("#loginform input[name='data[password]']").val(),
                "data[referer]": $("#loginform input[name='data[referer]']").val()
            }
            $.post('/ajax/login/', data, function(data) {
                if (data.status == "true") {
                    window.location.href = data.redirect;
                } else {
                    $('#loginform').append('<p class="error">' + data.message + '</p>');
                }
                $('#loginform .loader img').fadeOut(60, function() {
                    $('#loginform .loader input').fadeIn(60);
                    $(this).remove();
                });
            }, 'json');
            return false;
        });
    }
}

$(document).ready(function() { homepage.start(); });