var scrollSpeed = 500;
var portfolioOpacity = 0.2;
var portfolioScrollEasing = 'easeOutBack';
var contentEnabled = 0;

window.log = function(){
	log.history = log.history || [];  
	log.history.push(arguments);
	arguments.callee = arguments.callee.caller;  
	if(this.console) console.log(Array.prototype.slice.call(arguments));
};

function contentAreaSize() {
	$('#contentWrapper').tinyscrollbar();
	
	function initWindowSize() {
		$("#portfolio").width($(window).width()-$("#navColumn").width());
		$("#content").width($(window).width()-$("#navColumn").width());
		if(contentEnabled == 0) $("#content").css("right", -$(window).width()+$("#navColumn").width());
		$("#portfolioMask").width($(window).width()-$("#navColumn").width());
		$('#contentWrapper').height($(window).height()-110);
		$('#contentWrapper').tinyscrollbar_update();
	} initWindowSize();
	
	$(window).resize(function() {
		initWindowSize();
	});	
}

function portfolioItemSize() {
	function initPortfolioItemSize() {
		$(".portfolioItem").each(function() {
			$(this).width($("#portfolio").width());
		});
	} initPortfolioItemSize();
	
	$(window).resize(function() {
		initPortfolioItemSize();
	});	
}

function portfolioItemBaseSize() {
	$(".portfolioItem").each(function() {
		$(this).css("opacity", portfolioOpacity);
		if($(this).hasClass("active")) { $(this).css("opacity", 1); }
		var itemCount = $(this).find(".item").size()+parseInt($(window).width()/600)+1;
		$(".portfolioItemBase").width((itemCount)*600);
	});
}

var scrollToLeftGlobal;

function portfolioScrolling() {
	
	var scrollCentering;	
	var scrollMessageTimeout;
		
	function scrollMessage() {
		var message = $("#scrollNavMessage");
		var fadeTime = 500;
			
		clearTimeout(scrollMessageTimeout);
		message.stop(true, true).fadeIn(fadeTime);
		scrollMessageTimeout = setTimeout(function() {
			message.fadeOut(fadeTime*5);
			scrollMessageVisible = 0;
		}, 3000);
	}
	
	// Portfolio Scrolling Left/Right START
	$('#portfolio .portfolioItem:first').before($('#portfolio .portfolioItem:last'));
	
	function portfolioScrollTopInitialize() {
		scrollCentering = ($(window).height()-500)/2;
		$('#portfolio').scrollTop(500-scrollCentering);
		$('#portfolioTriggers .scrollTop').height(scrollCentering);
		$('#portfolioTriggers .scrollTop').width($(window).width()-$("#navColumn").width());
		$('#portfolioTriggers .scrollBottom').height(scrollCentering);
		$('#portfolioTriggers .scrollBottom').width($(window).width()-$("#navColumn").width());
		$('#portfolioTriggers .scrollPrev').css("top", scrollCentering);
		$('#portfolioTriggers .scrollNext').width($(window).width()-$("#navColumn").width()-700);
		$('#portfolioTriggers .scrollNext').css("top", scrollCentering);
	} portfolioScrollTopInitialize();
	$(window).resize(function() { portfolioScrollTopInitialize(); });	
	
	var animatedPortfolio = 0;
	
	function scrollPortfolio(direction) {
		scrollMessage();
		if(animatedPortfolio == 0) {
			animatedPortfolio = 1;
			if(direction == "-=") {
				$('#portfolio .portfolioItem:first').before($('#portfolio .portfolioItem:last'));
				$('#portfolio').scrollTop(1000-scrollCentering);
			}
			$('.portfolioItem').each(function() {
				$(this).scrollLeft(500);
			});
			var temp = $('.portfolioItem.active');
			temp.animate({opacity: portfolioOpacity}, scrollSpeed);
			temp.removeClass("active");
			if(direction == "+=") {
				temp.next().animate({opacity: 1}, scrollSpeed);
			} else {
				temp.prev().animate({opacity: 1}, scrollSpeed);
			}
			$('#portfolio').animate({scrollTop: direction+'500px'}, scrollSpeed, portfolioScrollEasing, function() {
				if(direction == "+=") {
					$('#portfolio .portfolioItem:last').after($('#portfolio .portfolioItem:first'));
					$('#portfolio').scrollTop(500-scrollCentering);
					temp.next().addClass("active");
				} else {
					temp.prev().addClass("active");
				}
				animatedPortfolio = 0;
			});
		}
	}
	// Portfolio Scrolling Left/Right END
	
	// Portfolio Items Scrolling Left/Right START
	$('.portfolioItem').each(function() {
		$(this).find('.item:first').before($(this).find('.item:last'));
		$(this).scrollLeft(500);
	});
	
	var animatedItem = 0;
	
	function scrollItem(direction) {
		scrollMessage();
		if(animatedItem == 0 && animatedPortfolio == 0) {
			animatedItem = 1;
			if(direction == "-=") {
				$('.portfolioItem.active .item:first').before($('.portfolioItem.active .item:last'));
				$('.portfolioItem.active').scrollLeft(1100);
			}		
			$('.portfolioItem.active').animate({scrollLeft: direction+'600px'}, scrollSpeed, portfolioScrollEasing, function() {
				if(direction == "+=") {
					$('.portfolioItem.active .item:last').after($('.portfolioItem.active .item:first'));
					$('.portfolioItem.active').scrollLeft(500);
				}
				animatedItem = 0;
			});
		}
	}
	// Portfolio Items Scrolling Left/Right END
		
	$("#scrollTop, #portfolioTriggers .scrollTop").click(function() { if(contentEnabled == 0) scrollPortfolio("-="); });	
	$("#scrollBottom, #portfolioTriggers .scrollBottom").click(function() { if(contentEnabled == 0) scrollPortfolio("+="); });
	$("#scrollNext, #portfolioTriggers .scrollNext").click(function() { if(contentEnabled == 0) scrollItem("+="); });
	$("#scrollPrev, #portfolioTriggers .scrollPrev").click(function() {	if(contentEnabled == 0) scrollItem("-="); });
		
	$(document).keyup(function(event) {
		if (event.keyCode == 38) { if(contentEnabled == 0) scrollPortfolio("-="); } // Top
		if (event.keyCode == 40) { if(contentEnabled == 0) scrollPortfolio("+="); } // Bottom
		if (event.keyCode == 37) { if(contentEnabled == 0) scrollItem("-="); } // Prev
		if (event.keyCode == 39) { if(contentEnabled == 0) scrollItem("+="); } // Next
	});
	
	scrollToLeft = function() { scrollItem("+="); };
	scrollToLeftGlobal = scrollToLeft;
	
}

function navAnimations() {
	var speed = 280;

	$("#nav li a").each(function() {
		$(this).html($(this).html()+"<span>"+$(this).html()+"</span>");
	});
	
	$("#nav li").mouseenter(function() {
		$(this).find("a").stop(true, false).animate({top: "-30px"}, speed, 'easeOutCirc');
	});
	
	$("#nav li").mouseleave(function() {
		$(this).find("a").stop(true, false).animate({top: 0}, speed, 'easeOutCirc');
	});
}

function loadContent(content) {
	$("#contentWrapper").animate({'opacity': 0}, 300, function(){
		$.ajax({
			url: content,
			success: function(data) {
				$("#contentWrapper .overview").html(data).parents("#contentWrapper").animate({'opacity': 1}, 800);
				$('#contentWrapper').tinyscrollbar_update();
				$("#contentWrapper .overview a").click(function() {
					
					if($(this).attr("href").substr(0, 7) == 'mailto:') return true;
					if(!$(this).parent().hasClass("quizForm")) {
						var contentHref = $(this).attr("href").replace("#", "");
						if(contentEnabled == 0) {
							$("#content").animate({right: 0}, speed, 'easeOutCirc', function() {
								loadContent(contentHref);
							});
							$("#scrollNav").fadeOut(speed);
							$("#portfolioButton").fadeIn(speed);
							contentEnabled = 1;
						} else {
							if($(this).is(":not(.rra)")) {
								loadContent(contentHref);
							}
						}
					}
				});
			},
			error: function() {
				var data = '<h3>404 nie można odnaleźć rządanej strony</h3><p class="large">Z przykrością muszę stwierdzić, że strona o podanym adresie nie może zostać odnaleziona.</p><p>Najprawdopodobniej plik, do którego się odwołujesz nie istnieje. Jeśli wpisywałeś adres ręcznie spróbuj zrobić to ponownie, być może popełniłeś błąd. Jeśli jednak odwołałeś się tu poprzez inną stronę spróbuj przejść do <a href="index.html">strony głównej</a> i ręcznie znajdź interesujący Cię element.</p>';
				$("#contentWrapper .overview").html(data).parents("#contentWrapper").animate({'opacity': 1}, 800);
				$('#contentWrapper').tinyscrollbar_update();
			}
		});
	});
}

function initContentHash() {
	if(window.location.hash.length > 0) {
		contentEnabled = 1;
		$("#content").css("right", 0);
		loadContent(window.location.hash.replace("#", ""));
		$("#scrollNav").fadeOut(500);
		$("#portfolioButton").fadeIn(500);
	}
}

function contentContainer() {
	var speed = 500;
	
	$("#portfolioButton").hide();
	
	$("#nav li:not(#portfolioButton) a").click(function() {
		if($(this).attr("href") != window.location.hash) {
			var contentHref = $(this).attr("href").replace("#", "");
			if(contentEnabled == 0) {
				$("#content").animate({right: 0}, speed, 'easeOutCirc', function() {
					loadContent(contentHref);
				});
				$("#scrollNav").fadeOut(speed);
				$("#portfolioButton").fadeIn(speed);
				contentEnabled = 1;
			} else {
				loadContent(contentHref);
			}
		}
	});
	
	$("#portfolioButton a, #logo").click(function() {
		if(contentEnabled == 1) {
			$("#content").animate({right: -$(window).width()+$("#navColumn").width()}, speed, 'easeOutCirc');
			$("#contentWrapper").animate({'opacity': 0}, 300);
			$("#scrollNav").fadeIn(speed);
			$("#scrollNavMessage").hide();
			$("#portfolioButton").find("a").stop(true, false).css("top", 0);
			$("#portfolioButton").fadeOut(speed);
			contentEnabled = 0;
		}
	});
}

function initQuiz(e) {
	var speed = 700;
	var quizForm = $("#"+e);
	
	function allFilled() {
	    var filled = true;
	    $('.screen.active input:text').not(".other").each(function() { if($(this).val() == '') filled = false; });
	    $('.screen.active input:radio').each(function() { if($(this).attr("checked") == "undefined") filled = false; });
	    return filled;
	}
	
	quizForm.find(".screen:first").addClass("active").show();
	
	quizForm.find(".next").click(function() {
		if($(this).hasClass("active")){
			if(quizForm.find(".screen.active").next().length > 0) {
				if(allFilled()) {
					var temp = quizForm.find(".screen.active");
					temp.fadeOut(speed).removeClass("active");
					temp.next().fadeIn(speed).addClass("active");
					
					if(quizForm.find(".screen:first").hasClass("active")) { quizForm.find(".prev").removeClass("active").fadeOut(speed); } else { quizForm.find(".prev").addClass("active").fadeIn(speed); }
					if(quizForm.find(".screen:last").hasClass("active")) { quizForm.find(".next").removeClass("active").fadeOut(speed); } else { quizForm.find(".next").addClass("active").fadeIn(speed); }
					if(quizForm.find(".screen:last").hasClass("active")) { quizForm.find(".submit").fadeIn(speed); } else { quizForm.find(".submit").fadeOut(speed); }
				}
			}
		} return false;
	});
	
	quizForm.find(".prev").click(function() {
		if($(this).hasClass("active")){	
			if(quizForm.find(".screen.active").prev().length > 0) {
				var temp = quizForm.find(".screen.active");
				temp.fadeOut(speed).removeClass("active");
				temp.prev().fadeIn(speed).addClass("active");
				
				if(quizForm.find(".screen:first").hasClass("active")) { quizForm.find(".prev").removeClass("active").fadeOut(speed); } else { quizForm.find(".prev").addClass("active").fadeIn(speed); }
				if(quizForm.find(".screen:last").hasClass("active")) { quizForm.find(".next").removeClass("active").fadeOut(speed); } else { quizForm.find(".next").addClass("active").fadeIn(speed); }
				if(quizForm.find(".screen:last").hasClass("active")) { quizForm.find(".submit").fadeIn(speed); } else { quizForm.find(".submit").fadeOut(speed); }
			}
		} return false;
	});
	
	/*quizForm.parents("form").submit(function() {
		// $.post("test.php", );
		$.ajax({  
		    type: "POST",  
		    url: "content/brief.php",
		    data: quizForm.parents("form").serialize(),  
		    success: function(){  
		        quizForm.parents("form").find("input[type='submit']").fadeOut(1000);
		        quizForm.find(".prev").fadeOut(1000);
		    }  
		});
		return false;  
	});*/
}

function hideButtons() {
	setTimeout(function() {
		$("#portfolioTriggers .button").fadeOut(500);
	}, 5000);
}

function languageOption() {
	var language = window.navigator.userLanguage || window.navigator.language;
	if(language == "pl-pl") {
		$("#languageInfo").remove();
	}
	
	$("#closeLanguageInfo").click(function() {
		$("#languageInfo").remove();
	});
}

function christmas() {
	
	var wishes = $("#wishes");
	var tree = $("#xMasTree");
	var treeStar = $("#xMasStar");
	var treeL1 = $("#xMasLevel1");
	var treeL2 = $("#xMasLevel2");
	var treeL3 = $("#xMasLevel3");
	var bg = $("#xMasBackground");
	
	treeStar.css("top", -0.5*$(window).height());
	treeL2.css("left", 1*$(window).width());
	treeL1.css("left", -1*$(window).width());
	treeL3.css("left", -1*$(window).width());
	
	contentEnabled = 1;
	
	wishes.delay(1500).fadeOut(500, function() {
		bg.fadeIn(2000);
		setTimeout(function() {
			treeL3.animate({left: "0"}, 500, function() {
				treeL2.animate({left: "24px"}, 500, function() {
					treeL1.animate({left: "43px"}, 500, function() {
						treeStar.animate({top: "0"}, 300, function() {
							setTimeout(function() {
								scrollToLeftGlobal();
								$("#xMasBG1").fadeIn(300);
								setTimeout(function() {
									scrollToLeftGlobal();
										$("#xMasBG2").fadeIn(300);
										setTimeout(function() {
											scrollToLeftGlobal();
											$("#xMasBG3").fadeIn(300);
											setTimeout(function() {
												scrollToLeftGlobal();
												contentEnabled = 0;
											}, 3000);
										}, 3000);
								}, 3000);
							}, 1000);
						});
					});
				});
			});
		}, 100);
	});
		
}

$(document).ready(function() {
	
	navAnimations();
	contentAreaSize();
	portfolioItemSize();
	portfolioItemBaseSize();
	portfolioScrolling();
	contentContainer();
	initContentHash();
	hideButtons();
	languageOption();
	// if($("html").attr("lang")!="en") christmas();
		
});

