/*
	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(/^imgjQuery/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(/^ajQuery/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 = {
	counter: 0,
	gTimerNo: 0,
	targets: [],
	
	start: function(target, timeout, dotUlClass) {
		jQuery('a.external').attr('target','_blank');
		var e = jQuery(target + ':first');
        this.targets[this.targets.length] = target;
        
        jQuery(target).data('current',0);
        this.gTimerNo = 0;
        
        jQuery(target).not(':first').animate({opacity: 0}, 1).css({'z-index': '88'});
        var c = jQuery(target).size();
        this.round(target, timeout, this.gTimerNo );
        jQuery(target + ':last').after(
			'<dt class="dotted-ul '+dotUlClass+'"><ul></ul></dt>'
        );
        jQuery('dt.dotted-ul').css('padding-top','0');
        
        
        var jQuerydottedMenu = jQuery(target).parent().find('dt.dotted-ul ul');
        var vthis = this;
        jQuery(target).each(
			function(i)
			{
				jQuerydottedMenu.append('<li><a href="javascript:void(0);"></a></li>');
				jQuerydottedMenu.find('a:last').click(
					function()
					{
// 						jQuery(target).data('lock', 1);
						timerNo = vthis.gTimerNo+1;
						vthis.gTimerNo = timerNo;
						slide.animateTo(target, i, timeout, timerNo);
						for(var ci =0; ci<vthis.targets.length; ci++)
						{
							if(vthis.targets[ci] == target)
								continue;
							
							slide.animateTo(vthis.targets[ci], jQuery(vthis.targets[ci]).data('current'), timeout, timerNo);
						}
						return false;
					}
				);
			}
        );
        jQuerydottedMenu.find('li:first').addClass('active');
        
    },


	// sprawdzamy, gdzie dac lock, a gdzie nie.. i czy/jak wyw
    round: function(t, timeout, timerNo) {
		var vthis = this;
        setTimeout(function() {
			if(timerNo != vthis.gTimerNo)
				return true;
				
			var next = jQuery(t).data('current')+1;
			slide.animateTo(t, next, timeout, timerNo);
        }, timeout);
    },
    
    animateTo: function(t, newIndex, timeout, timerNo) {
		var current = jQuery(t).data('current');
		var e = jQuery(t).eq(current);
		var n = jQuery(t).eq(newIndex);
		n = (jQuery(t).index(n) == -1) ? jQuery(t + ':first') : n;
		newIndex = jQuery(t).index(n);
        jQuery(t).data('current', jQuery(t).index(n));
        this.gTimerNo = timerNo;
			
        var jQuerydottedMenu = jQuery(t).parent().find('dt.dotted-ul ul');
        jQuerydottedMenu
			.find('li')
			.removeClass('active')
			.eq(newIndex)
				.addClass('active');
			
        
		e.animate({opacity: 0}, {queue: false},  200).css({'z-index': '88'});
		n.animate({opacity: 1}, {queue: false}, 1000).css({'z-index': '99'});
		this.round(t, timeout, timerNo);
    }
}

homepage = {
    start: function() {
        
        this.centerPage();
        this.decoration();
        this.loginform();
        this.videoplayer();
        this.checkbox();
        if(document.location.href.indexOf('szk.pl') > 0)
        {
			slide.start('#tutorial dd', 3000, '');
			slide.start('#inspiration dd', 3000, 'photo-ul');
			slide.start('#discussion dd', 3000, 'insp-ul');
        } else {
			slide.start('#tutorial dd', 10000, '');
			slide.start('#inspiration dd', 10000, 'photo-ul');
			slide.start('#discussion dd', 10000, 'insp-ul');
        }
        
        jQuery('.news-wrapper').jScrollPane( { 
			horizontalGutter: 10
        } );
        
        jQuery(window).resize(
		function()
		{
			homepage.centerPage();
			return true;
		}
		);
        
    },
    
    
	getPageSizeY: function()
	{
		var dE = document.documentElement;
		return window.innerHeight || self.innerHeight || (dE && dE.clientHeight) || document.body.clientHeight;
	},
	
    centerPage: function()
    {
		var jQuerywCenter = jQuery('#wCenter');
		var h = (homepage.getPageSizeY()-jQuerywCenter.height())/2;
		if(h>0)
		{
			jQuerywCenter.css('padding-top',h+'px');
		}
	},
    
    
    checkbox: function() {
        var jQuerylabel = jQuery('label.checkbox-home');
        var jQueryinput = jQuerylabel.find('input');
        jQueryinput.hide();
        jQueryinput.wrap('<span class="jcheckbox"></span>');
        
        var jQueryspanJC = jQuerylabel.find('span.jcheckbox');
        
		if (jQueryinput.is(':checked')) {
			jQuerylabel.addClass('checked');
		};
		
        jQuerylabel.find('span').click(function(e) {
            if (jQuerylabel.hasClass('checked')) {
                jQuerylabel.removeClass('checked');
                jQueryinput.attr('checked', false);
            } else {
                jQuerylabel.addClass('checked');
                jQueryinput.attr('checked', true);
            }
            return false;
        });
    },
    
    parse: function(target) {
        var input = [];
        jQuery(target).each(function() {
            if (jQuery(this).is("[type='checkbox']")) {
                if (jQuery(this).is(':checked')) {
                    input.push(this.name + '=' + encodeURI(this.value));
                }
            } else {
                input.push(this.name + '=' + encodeURI(this.value));
            }
        })
        return input.join('&');
    },

    password: function() {
        var Layer = new Windows();
        Layer.close('');
        Layer.open('#reminder',
            function() {
                jQuery.get('/windows/password.thtml', function(content) {
                    Layer.init(content);
                    jQuery('#loginform p.error').remove();

                })
            }, function() {
                movable = true;
                jQuery('#window form .proced').click(function() {
                    if (movable) {
                        submitElement();
                    }
                    movable = false;
                    return false;
                });
                jQuery('#window form').submit(function() {
                    if (movable) {
                        submitElement();
                    }
                    movable = false;
                    return false;
                })
                function submitElement() {
                    jQuery.ajax({
                        data: inputs = homepage.parse('#window input'),
                        timeout: 2000,
                        type: "POST",
                        dataType: "json",
                        url: '/ajax/password/',
                        cache: false,
                        success: function(data) {
                            movable = true;
                            if (data.status == 'true') {
                                Layer.terminate();
                            } else {
                                jQuery('#window .message').empty().append(data.message);
                            }
                        }
                    });
                }
            }
        );
    },

    
    
    decoration: function() {
    	Cufon.replace('#vignette ul a', {fontFamily: 'GlasgowPl RR ExtraBold'});
        Cufon.replace('#user-profile h4 small, .claim-header p', {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('div.hdr strong,h3 strong,#account dt big', {fontFamily: 'HelveticaNeue'});
        Cufon.replace('td.tresc a strong', {fontFamily: 'HelveticaNeue'});
        
        jQuery(".image img").reflect({height: 0.15, opacity: 0.23});
        jQuery('#inspiration dd, #discussion dd, #tutorial dd').click(function() {
            link = jQuery(this).find('a').attr('href');
            document.location.href = link;
        })
        Cufon.replace('#tutorial a.dt-title', {fontFamily: 'GlasgowPl RR ExtraBold', color: '-linear-gradient(#8FCFDF, #65bbd2)'}); 
        Cufon.replace('#inspiration a.dt-title', {fontFamily: 'GlasgowPl RR ExtraBold', color: '-linear-gradient(#94CC5B, #6DB944)'}); 
        Cufon.replace('#discussion a.dt-title', {fontFamily: 'GlasgowPl RR ExtraBold', color: '-linear-gradient(#81C7C2, #59aea6)'}); 
        Cufon.replace('#account a.dt-title', {fontFamily: 'GlasgowPl RR ExtraBold', color: '-linear-gradient(#D0CF99, #BEBD73)'});
        Cufon.now();
    },
    
    videoplayer: function() {
        
        jQuery('#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() {
//         jQuery("#loginform input[name='data[username]']").val('Login').one('click focus', function() {
//             jQuery(this).val('');
//         });
// 		jQuery("#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);
//             input2.focus();
//             jQuery(input2).unbind('focus');
//             jQuery(input2).unbind('click');
//             return false;
// //             if($input1.val() == 'Hasło')
// 
// 
//         });

		var fakeInput = document.createElement("input"),
		placeHolderSupport = ("placeholder" in fakeInput),
		clearValue = function () {
			if (searchField.val() === originalText) {
				searchField.val("");
			}
		};
		
		if(!placeHolderSupport || location.href.indexOf('?placeholderJS') > 0)
		{
			jQuery("#loginform p.text").each(
				function()
				{
					
					var $p = jQuery(this);
					var $inp = $p.find('input');
					
					var placeholder = $inp.attr('placeholder');
					$inp.attr('placeholder','');
					if(placeholder)
					{
						$inp.wrap('<label></label>');
						$p.find('label')
							.append('<span>'+placeholder+'</span>')
							.find('span')
								.click(
									function()
									{
										$inp.trigger('focus');
										jQuery(this).hide();
										return true;
									}
								);
								
						$inp.focus(
							function()
							{
								$p.
									find('label span')
									.hide();
								return true;
							}
						);
						
						
						
						$inp.bind('blur',
							function()
							{
								if($inp.val() == '')
									$p.find('label span').fadeIn(500);
							}
						);
					}
					
				}
			);
		}
		
		if(location.href.indexOf('?le') > 0)
		{
// 			jQuery('#loginform .error').remove();
//             jQuery('#loginform p.forgotten').after('<p class="error">Błąd. Nie pamiętasz hasła? <a href="javascript::void(0);" id="reminder">Kliknij tutaj</a></p>');
			homepage.password();
		}
		
		jQuery('#loginform input').change(
			function()
			{
				jQuery('#loginform p.error').remove();
				return true;
			}
		);
		
        jQuery('form#loginform').submit(function() {
			jQuery('#loginform .loader input.submit')
				.hide();
			jQuery('#loginform .loader').append('<img src="/template/image/common/loading.black.gif" />');
            jQuery('#loginform .error').remove();
            
            var check = 0;
            if(jQuery("#loginform input[name='data[rememberme]']:checked").is('input'))
				check = 1;

			var data = {
                "data[username]": jQuery("#loginform input[name='data[username]']").val(),
                "data[password]": jQuery("#loginform input[name='data[password]']").val(),
                "data[referer]": jQuery("#loginform input[name='data[referer]']").val(),
                "data[js]": 1,
                "data[rememberme]": check,
                "data[homepage]": true
            }
            
            jQuery.post('/ajax/login/', data, function(data) {
                if (data.status == "true") {
                    window.location.href = data.redirect;
                } else {
                    jQuery('#loginform p.forgotten').after('<p class="error">' + data.message + '</p>');
                    homepage.password();
                }
                jQuery('#loginform .loader img').remove();
                jQuery('#loginform .loader input.submit').fadeIn(60);
            }, 'json');
            return false;
        });
    }
}

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

