var language_array = null;
var language_select = null;

jQuery.fn.loadPage = function()
{
	var args = arguments[0] || {};
	var url = args.url;
	var identificator = args.identificator;
	var first = args.first;
	$.post(url, { element: identificator, language: language_select }, function(data) {
		$("#container").html(data);
		if(first == "true")
		{
			$("#main").css({display:"block"});
			$("#logo").css({display:"block"});
			$("#menu").css({display:"block"});
			$("#preloader").delay(1000).fadeOut(function() {
				$(window).unbind("CenterCanvas");
				$("#preloader").remove();
			});
		}
		else $("#loading").delay(500).fadeOut();
		$(window).resizeWindow();
	});
};

jQuery.fn.createLinks = function()
{
	$("a.link").click(function() {
		var array = new String($(this).attr("href")).replace("#", "").split("!");
		var link = array[0];
		var request = array[1];
		var current = new String(link).split("/");
		var currentClass = "menu"+(current[1]).charAt(0).toUpperCase()+(current[1]).slice(1);
		$("#loading").fadeIn(function() {
			if(current[1] == "blog") {
				$("#logo").stop().animate({ opacity: 0 }, 500, function() {
					$("#menu").stop().animate({ marginBottom: ($(window).height()-$("#menu").outerHeight())+"px" }, 1500, function() {
						$(document).loadPage({ url: link, identificator: request, first: "false" });
					});
				});
			}
			else {
				$("#menu").stop().animate({ marginBottom: "0px" }, 1500, function() {
					$("#logo").stop().animate({ opacity: 1 }, 500, function() {
						$(document).loadPage({ url: link, identificator: request, first: "false" });
					});
				});
			}
		});
		$("#menu ul.left li a").removeClass("active");
		$("#menu ul.left li a."+currentClass).addClass("active");
		$(document).attr("title", "2om Studio :: Interactive Systems :: "+$("#menu li a."+currentClass).text());
		Cufon.refresh('.myriadcond');
	});
}

jQuery.fn.resizeWindow = function()
{
	var imageratio = 1600/900;
	var currentratio = $(window).width()/$(window).height();
	if(imageratio < currentratio) {
		$("img.fit").css({width:"100%",height:"auto",marginTop:((100-(currentratio/imageratio)*100)/8)+"%",marginLeft:"0"});
		$("img.fitWidth").css({width:"auto",height:"100%",margin:"auto"});
	}
	else {
		$("img.fit").css({width:"auto",height:"100%",marginTop:"0",marginLeft:((100-(imageratio/currentratio)*100)/2)+"%"});
		$("img.fitWidth").css({width:"100%",height:"auto",marginTop:(((100-(imageratio/currentratio)*100)/4)*-1)+"%",marginLeft:"0"});
	}
	$("#main").css({width:$(window).width(),height:$(window).height()});
	if(parseInt($("#menu").css("margin-bottom")) != 0) $("#menu").css({ marginBottom: ($(window).height()-$("#menu").outerHeight())+"px" });
	$("body").css({ fontSize: Math.round(($(window).height()/900)*15)+"px" });
	$(".maxWidthHeight").css({width:$(window).width(),height:$(window).height()});
	$(".maxWidth").css({width:$(window).width()});
	$(".maxHeight").css({height:$(window).height()});
	$(".maxHeightMenu").css({height:$(window).height()-$("#menu").height()});
	if($(".customerItem").length != 0) {
		if($(window).height() >= 925) { $(".customersWrapper").height(748); $(".customersWrapper").width(183*Math.ceil($(".customerItem").length/4)); }
		else if($(window).height() >= 738) { $(".customersWrapper").height(561); $(".customersWrapper").width(183*Math.ceil($(".customerItem").length/3)); }
		else if($(window).height() >= 551) { $(".customersWrapper").height(374); $(".customersWrapper").width(183*Math.ceil($(".customerItem").length/2)); }
		else { $(".customersWrapper").height(187); $(".customersWrapper").width(183*Math.ceil($(".customerItem").length/1)); }
	}
	$("#preloader div.preloader").css("margin-top", Math.ceil(($(window).height() - $("#preloader div.preloader").outerHeight()) / 2));
};

$(window).load(function() {
	$("#preloader div.preloader div").html5Loader({
		color: "#ffffff",
		glowColor: "#ffffff",
		lineWidth: 5,
		fullScreen: false,
		onComplete: function() {
			$("#preloader").addClass("loading");
		}
	});
	$(window).resizeWindow();
});

$(window).bind("resize", function(){
	$(window).resizeWindow();
});

$(document).ready(function() {
	$("a.lang").click(function() {
		//if($("#preloader").hasClass("loading")) {
			if($(this).hasClass("polish")) language_select = "polish";
			if($(this).hasClass("english")) language_select = "english";
			$.post("/translation.json", { language: language_select }, function(data) {
				language_array = data;
				$(language_array).find("translations").find("translation").each(function()
				{
					$("."+$(this).find("class").text()).html(new String($(this).find("name").text()).replace("|","<br/>"));
					if($(this).find("url").text() != "") $("."+$(this).find("class").text()).attr("href", "#"+$(this).find("url").text());
				});
				$(document).loadPage({ url: "/home", identificator: "", first: "true" });
				$("#menu ul.left li a.menuHome").addClass("active");
			}, "xml");
		//}
	});
	$(".coverDown").hover(function() {
		$(this).parent().find(".slideDown").stop().animate({ marginTop: "-52px", height: "52px" }, 200);
    },function(){
        $(this).parent().find(".slideDown").stop().animate({ marginTop: "0px", height: "0px" }, 500);
    });
	$(document).createLinks();
});
