var Site = {
	lang: "pl",
	init: function () {
		if (window.location.pathname.length > 1) {
			window.location = "http://" + window.location.host + "#" + window.location.pathname;
		}
		News.init();
		Site.addressMenu();
		$("a.launchmovie").unbind().bind("click", Site.launchMovie);
		$(".loader").remove();
		Site.newsletter();
		Vimeo.init();
	},
	addressMenu : function () {
		if(!($(".ajax-off").length)){
			$("a:not([href^='http://']):not([href^='mailto:'])").address(function () {
				return $(this).attr('href').replace(location.pathname, '').replace("#", '');
			});
		}
	
	},
	changeLang : function(){
		$.ajax({
		url: "index.php",
		global: false,
		type: "GET",
                data: {langchange : 1, l : Site.lang},
		dataType: "html",
		async: false,
		success: function (data) {
			$("#container").html($("#container",data).html());
			$("#container").removeClass("pl").removeClass("en").addClass(Site.lang);
		}
		});
	},
	setAddress : function(){
	
			if($('.awards').length){
				$('.awards a').address(function () {
					return $(this).attr('href').replace(location.pathname, '').replace("#", '');
				});
			}
			if($('a.internal').length){
				$('a.internal').address(function () {
					return $(this).attr('href').replace(location.pathname, '').replace("#", '');
				});
			}
			Projects.setAddress();
	
	},
	launchMovie: function (e) {
		e.preventDefault();
		$(e.currentTarget).fadeOut(300, function () {
			$(e.currentTarget).next().fadeIn(300, function(){Vimeo.play($(e.currentTarget).next().attr("id"))});
		});
	},
	onChange: function (e) {
		
		if(!($(".ajax-off").length)){
		if(e.pathNames[0] == "pl"){
				Site.lang = "pl";
			}
			else if(e.pathNames[0] == "en"){
				Site.lang = "en";
			}
			else {
				e.pathNames[0] = "pl";
				e.pathNames[1] = "aktualnosci";
			}
			if($("#container").attr("class") !== Site.lang){
				Site.changeLang();
			}
			
		
		if (!$(".js-off").length) {
			Site.cleanonChange();

			var page;
			if (e.pathNames[1] != undefined) {
				$("#menu li a").removeClass("selected");
				var whatPage = e.pathNames[1];
				if(whatPage == "archiwum"){
					whatPage = "aktualnosci";
				}
				else if(whatPage == "archives"){
					whatPage = "news";
				}
				$("#menu li a[href*='" + whatPage + "']").addClass("selected");
				if ((e.pathNames[2] == undefined)||((e.pathNames[2]>0)&&((e.pathNames[1] == "archiwum")||(e.pathNames[1] == "archives")))) {
					if (e.pathNames[1] == "aktualnosci") {
						e.pathNames[1] = "aktualnosci";
					}
					page = e.pathNames[1];
					//console.log(url);
					Site.removeContent();
					setTimeout(Site.loadSite, 300, page);
				} else if ((e.pathNames[1] == "projekty") || (e.pathNames[1] == "projects")) {
					if ($(".projects").length) {
						Projects.loadProject(e);
					} else {

						page = e.pathNames[1];
						Site.removeContent();
						setTimeout(Site.loadSite, 300, page, Projects.loadProject, e);
					}
				}

			} else if (!($("section.front").length)) {
					if (Site.lang == "pl") {
						page = "aktualnosci";
					} else {
						page = "news";
					}
					
					Site.loadSite(page);
				
			}
			
			else {
				$("section.front").slideDown(600);
			}
		}
		}
	},
	removeContent: function () {
		if ($("section").length) {
			$("footer").fadeOut(300);
			$("section").fadeOut(300, function () {
				$(this).remove()
			});
		}
	},
	loadSite: function (page, callback, vars) {
		$("#container").prepend("<img src=\"/images/ajax-loader.gif\" alt=\"\" class=\"loader\" />");
		var url = "/index.php";
		//console.log(page);
		$.ajax({
			url: url,
			global: false,
			type: "GET",
			data: ({
				page: page,
				l: Site.lang,
				ajax: 1,
				lang: Site.lang
			}),
			dataType: "html",
			async: false,

			success: function (data) {
				document.title = $("h1",data).html()+" - Novimedia";
				$("header").after(innerShiv($("div", data).html(), false));
				$("section, footer").hide().fadeIn(500);
				Site.setAddress();				
				Site.init();
				if (callback != undefined) {
					callback(vars);
				}
			}
		});
	},
	cleanonChange: function () {
		$(".products").children().not("#ajax").removeClass("selected");
		$(".loader").remove();
	},
	newsletter : function(){
		$("#subscribe").unbind().bind("click",Site.subscribe);
		$("#newsletter").focus(Site.newsletterOnFocus).focusout(Site.newsletterOnFocusOut);
        },
	newsletterOnFocus : function(){
		if($("#newsletter").val()=="e-mail"){
			$("#newsletter").val("");
		}
	},
	newsletterOnFocusOut : function(){
		if($("#newsletter").val()==""){
			$("#newsletter").val("e-mail");
		}
	},
	subscribe : function(){
		
		var val = $("#newsletter").val();
                var url = "/php/sendit.php";
                $(".res").remove();
                $(".emailform").slideUp(300);
		$.ajax({
			url: url,
			global: false,
			type: "POST",
			data: ({
				email: val
			}),
			dataType: "html",
			async: false,
			success: function (data) {
				 $('.emailform').after(data);
                                 $(".res").hide().fadeIn(800);
                                 $(".emailform").slideDown(400);
                                 setTimeout(Site.clearResp, 2500);
                        }
		});             	
		
	},
        clearResp : function(){
            $(".res").slideUp(300, function(){$(".res").remove()});
        }

};
var News = {
	init: function () {
		var aV = $.address.pathNames();
		
		$(".front .news h3 a, .archives .news h3 a").unbind().bind("click", News.accordeon);
		
		if(aV[2]>0){
			$(".news li div").hide();
			$("li#news"+aV[2]).find("div").show();
		}
		else {
			$(".news li").not(":first-child").find("div").hide();
		}
		if($(".video").length){
			setTimeout(News.adjustLayout,300);
		}
		
	},
	accordeon: function (e) {
		e.preventDefault();
		e.stopPropagation();
		var li = $(e.currentTarget).parent().parent();
		if ($(li).find("div.newsbody").is(":visible")) {
			$(li).parent().find("li").removeClass("selected").find("div.newsbody:visible").stop(true, true).slideUp(300);
		} else {
			$(li).parent().find("li").removeClass("selected").find("div.newsbody:visible").stop(true, true).slideUp(300);
			$(li).addClass("selected").find("div.newsbody").stop(true, true).slideDown(300);
		}
		if($(".video").length){
			setTimeout(News.adjustLayout,300);
		}
	},
	adjustLayout : function(){
		var n = $(".news").height();
		var v = $(".video").height();  //603
		//var d = Math.abs(n-v); 
		if(n>=603){			
			$(".mainboxes").stop(true,true).animate({marginTop: (n-603)+"px"},300);
		}		
	}
};
var Projects = {
	setAddress : function () {
		if(!($(".ajax-off").length)){
			$(".projectlist a, .projectsmenu a, .projectscontent a").address(function () {
				return $(this).attr('href').replace(location.pathname, '').replace("#", '');
			});
		}
	},
	loadProject: function (e) {
		var category = e.pathNames[3];
		var project = e.pathNames[5];
		var page = e.pathNames[1];
		var classNr;
		switch (e.pathNames[2]){
			case "zewnetrzne" || "external":
				classNr = "bg1";
			break;
			case "wewnetrzne" || "internal":
				classNr = "bg2";
			break;
			case "newsletter" :
				classNr = "bg3";
			break;
		}
		if(!($("."+classNr).length)){
			$("#bar").fadeOut(200, function(){$("#bar").attr("class","").addClass("projectsbar").addClass(classNr).fadeIn(400)});	
		}
		$("#projectajax").stop(true, true).slideUp(300);
		setTimeout(function () {
			$.ajax({
				url: "/index.php",
				global: false,
				type: "GET",
				data: ({
					kid: category,
					id: project,
					page: page,
					ajax: 1,
					l: Site.lang
				}),
				dataType: "html",
				async: false,

				success: function (data) {

					$("#projectajax").html(innerShiv($("div",data).html(), false)).slideDown(500);
					document.title = $("h1",data).html()+" - Novimedia";
					if (project > 0) {

						$(".projectlist a").removeClass("selected");
						$(".projectlist a[href*='" + e.pathNames[4] + "/" + project + "/']").addClass("selected");
					}
					if (category > 0) {
						$(".projectsmenu a").removeClass("selected");						
						$(".projectsmenu a[href*='/" + category + "/']").addClass("selected");
					}
					Projects.setAddress();
				}
			});
		}, 300);
	}
};
var Vimeo = {
	init : function(){
		var addEvent = function(element, eventName, callback){
			if (element.addEventListener) {
                        element.addEventListener(eventName, callback, false);
                    }
                    else {
                        element.attachEvent(eventName, callback, false);
                    }
		};
		if( $("#vimeo").length && !(navigator.userAgent.match(/like Mac OS X/i)) ) $f("vimeo").addEvent('ready', Vimeo.play)
		else $(".launchmovie").hide();
	},
        play : function(){
		$(".launchmovie").fadeOut(600);
		console.log("is ready");
                var froogaloop = $f("vimeo");
                froogaloop.api('play');
            }
};

$.address.change(Site.onChange);
$.address.init(Site.init);

var Froogaloop=function(){function g(a){return new g.fn.init(a)}function h(a,b,c){if(!c.contentWindow.postMessage)return!1;var f=c.getAttribute("src").split("?")[0];a=JSON.stringify({method:a,value:b});c.contentWindow.postMessage(a,f)}function i(a){if(a.origin!=playerDomain)return!1;var b=JSON.parse(a.data);a=b.value;var c=b.data,f=f==""?null:b.player_id;b=f?d[f][b.event||b.method]:d[b.event||b.method];var e=[];if(!b)return!1;a!==void 0&&e.push(a);c&&e.push(c);f&&e.push(f);return e.length>0?b.apply(null,
e):b.call()}function j(a,b,c){c?(d[c]||(d[c]={}),d[c][a]=b):d[a]=b}var d={},k=!1;g.fn=g.prototype={playerDomain:"",element:null,init:function(a){typeof a==="string"&&(a=document.getElementById(a));this.element=a;return this},api:function(a,b){if(!this.element||!a)return!1;var c=this.element,f=c.id!=""?c.id:null,e=!b||!b.constructor||!b.call||!b.apply?b:null,d=b&&b.constructor&&b.call&&b.apply?b:null;d&&j(a,d,f);h(a,e,c);return this},addEvent:function(a,b){if(!this.element)return!1;var c=this.element;
j(a,b,c.id!=""?c.id:null);a!="ready"&&h("addEventListener",a,c);if(k)return this;c=c.getAttribute("src").split("/");for(var d="",e=0,g=c.length;e<g;e++){if(e<3)d+=c[e];else break;e<2&&(d+="/")}playerDomain=d;window.addEventListener?window.addEventListener("message",i,!1):window.attachEvent("onmessage",i,!1);k=!0;return this},removeEvent:function(a){if(!this.element)return!1;var b=this.element,c;a:{if((c=b.id!=""?b.id:null)&&d[c]){if(!d[c][a]){c=!1;break a}d[c][a]=null}else{if(!d[a]){c=!1;break a}d[a]=
null}c=!0}a!="ready"&&c&&h("removeEventListener",a,b)}};g.fn.init.prototype=g.fn;return window.Froogaloop=window.$f=g}();
