Site = {
	ChangePageTime: 1200,
	PageScrollTime: 400,
	OfertaTime: 1800,
	PolecAnimateTime: 600,
	PolecMessageTime: 1500,
	Href: '',
	RealSliderHandle: '',
	RealSliderTime: 5000,
	RealAnimate: 1400,
	TitleAddress: ' - Addictive Studio',
	
	
	run: function(){
		if (/MSIE (5\.5|6)/.test(navigator.userAgent))
		{
			this.ieHacks();
		}
		else
		{
			this.hrefUpdate();
			
			if (this.Href != '/' && self.location.href.indexOf('#')==-1)
			{
				self.location.href = 'http://www.addictive-studio.com/#'+this.Href;
			}
			$("#load").css("height", $("#load").height()+"px");
			$("#load .content").css("display", "none");
			
			if (this.Href != '/' && this.Href != '/Glowna.html')
			{
				this.siteChange(this.Href);
			}
			
			if (this.Href == '/'){
				self.location.href = '#/Glowna.html';
				this.hrefUpdate();
			}
			
			$("body").append('<div id="test"></div>');
			
			$("a[class!=online]").each(function(){
				$(this).attr("adres", $(this).attr("href"));
				$(this).removeAttr("href");
			});
			$("a[adres=/]").attr("adres", "/Glowna.html");
			
			this.setMenuActive();
			
			$("h1 a, ul.menu a").click(function(){
				if($(this).attr("class").indexOf("active")==-1 && $(this).attr("adres")!='/Polec.html')
				{
					Site.siteChange($(this).attr("adres"));
				}
			});
			
			
			this.selectSite();
			this.polec();
			
		}
	},
	
	glowna: function(){
		
		$("#oferta_glowna a, #ostatnia_realizacja a.online").each(function(){
			$(this).attr("adres", $(this).attr("href"));
			$(this).removeAttr("href");
		});
		
		$(".content ul.menu a, #ostatnia_realizacja a.online").click(function(){
			Site.siteChange($(this).attr("adres"));
		});
		
		if($("a.online").text()=='zobacz online')
		{
			$("a.online").get(0).onclick = function(){
				window.open($(this).attr("href")).focus(); return false
			};
		}
		
		document.title = 'Addictive Studio - Tworzenie Stron Internetowych';
		this.siteShow();
	},
	
	oferta: function(){
		$(".oferta li a").removeAttr("href");
		if(Site.Href != '/Oferta.html'){
			var tab_id = Site.Href.substr(Site.Href.indexOf(',')+1, Site.Href.indexOf('.')-(Site.Href.indexOf(',')+1));
			$(this).load("/oferta/"+tab_id+".html", null, function(){
				self.location.href = '#'+Site.Href;
				$('.oferta ul a[class='+tab_id+']').addClass("active");
				$("div.oferta_cat").attr("id", tab_id);
			});
		}
		this.siteShow();
		document.title = $('.oferta_cat h2').html()+' - Oferta'+Site.TitleAddress;
		$(".oferta li a").click(function(){
			var active_now = $(this);
			if(active_now.attr("class").indexOf("active")==-1)
			{
				var tab_id = active_now.attr("class");
				$(".oferta li a.active").removeClass("active");
				$(".oferta").css("height", $(".oferta").height());
				$("div.oferta_cat div").fadeOut(Site.OfertaTime/3, function(){
					$(this).load("/oferta/"+tab_id+".html", null, function(){
						$("div.oferta_cat div").css({"visibility":"hidden", "display":"block"});
						
						$("#load").animate({height: $(".content").height()}, Site.OfertaTime/3, null, function(){
							$("div.oferta_cat div").css({"visibility":"visible", "display":"none"});
							document.title = $('.oferta_cat h2').html()+' - Oferta'+Site.TitleAddress;
							self.location.href = '#/Oferta,'+tab_id+'.html';
							active_now.addClass("active");
							$("div.oferta_cat").attr("id", tab_id);
							$("div.oferta_cat div").fadeIn(Site.OfertaTime/3);
						});
					});
				});
			}
		});
		$(".oferta_cat a").click(function(){
			Site.siteChange($(this).attr("href"));
			return false;
		});
	},
	
	realizacje: function(){
		$(".content div.scroll li a").removeAttr("href");
		$(".content ul.menu-real li a").removeAttr("href");
		$("#realizacja li a").removeAttr("href");
		$("#test").html('<img src="'+$("#load .content .photo img").attr("src")+'"/>');
		$("#test img").get(0).onload = function(){
			Site.siteShow();
		};
		setTimeout('Site.siteShow();', 2000);
		if($("a.online").attr("class"))
		{
			$("a.online").get(0).onclick = function(){
				window.open($(this).attr("href")).focus(); return false
			};
		}
		
		if($("#realizacja div.opis").html())
		{
		document.title = $("#realizacja div.opis").html().substring($("#realizacja div.opis").html().indexOf('>')+1, $("#realizacja div.opis").html().indexOf('</'))+' - '+$('ul.menu-real a.active').attr('class').substring(0, $('ul.menu-real a.active').attr('class').indexOf(' ')).replace('_', ' ')+' - Realizacje'+Site.TitleAddress;
		}
		refreshMenu();
		refreshMins();

		$(".content ul.menu-real li a").click(function(){
			var active_now = $(this);
			if(active_now.attr("class").indexOf("active")==-1){
				self.location.href = '#/Realizacje,'+active_now.attr('class')+'.html';
				var tab_id_now = active_now.attr("class");
				$(".content ul.menu-real li a.active").removeClass("active");
				$("div.scroll").fadeOut(Site.RealAnimate/2, function(){
					$("div.scroll").html('').load('/RealizacjeJS.php?co=menu&cat='+tab_id_now, null, function(){
					$("div.scroll").fadeIn(Site.RealAnimate/2);
					refreshMenu();
					});
				});
				realChange(active_now.attr("class"), 0);
				active_now.addClass("active");
			}
		});
		function refreshMenu(){
			$(".content div.scroll li a").removeAttr("href");
			$("div.scroll ul a[adres="+Site.Href+"]").addClass("active");
			if(!$("div.scroll ul a.active").attr("class")){
				$("div.scroll ul li:first-child a").addClass("active");
			}

			$("div.scroll li a").click(function(){
				if($(this).attr("class").indexOf("active")==-1){
					self.location.href = '#/Realizacje,'+$(this).attr('class')+','+$(this).attr("id")+'.html';
					$("div.scroll li a.active").removeClass("active");
					realChange($(this).attr("class"), $(this).attr("id"));
					$(this).addClass("active");
				}
			});
		}
		function realChange(cat, id){
			window.clearInterval(Site.RealSliderHandle);
			$("#realizacja div.opis p").fadeOut(Site.RealAnimate/2);
			$("#realizacja div.opis a").fadeOut(Site.RealAnimate/2);
			$("#realizacja div.opis h2").fadeOut(Site.RealAnimate/2, function(){
				$("#realizacja div.opis").html('').load('/RealizacjeJS.php?co=opis&cat='+cat+'&id='+id, function(){
				document.title = $("#realizacja div.opis").html().substring($("#realizacja div.opis").html().indexOf('>')+1, $("#realizacja div.opis").html().indexOf('</'))+' - '+$('ul.menu-real a.active').attr('class').substring(0, $('ul.menu-real a.active').attr('class').indexOf(' '))+' - Realizacje'+Site.TitleAddress; 
				$("#realizacja div.opis h2").fadeIn(Site.RealAnimate/2);
				$("#realizacja div.opis a").fadeIn(Site.RealAnimate/2);
				$("#realizacja div.opis p").fadeIn(Site.RealAnimate/2);
				if($("a.online").attr("class"))
				{
					$("a.online").get(0).onclick = function(){
						window.open($(this).attr("href")).focus(); return false
					};
				}
				});
			});
			$("#realizacja ul").fadeOut(Site.RealAnimate/2, function(){
				$(this).html('').load('/RealizacjeJS.php?co=mins&cat='+cat+'&id='+id, null, function(){
					$("#realizacja ul").fadeIn(Site.RealAnimate/2);
					refreshMins();
				});
			});
			$("#realizacja .photo img").fadeOut(Site.RealAnimate/2, function(){
				$("#realizacja .photo").html('').load('/RealizacjeJS.php?co=img&cat='+cat+'&id='+id, null, function(){
					$('#test').html('<img src="'+$("#realizacja .photo img").attr("src")+'"/>');
					if($('#test img').height()==516){
						$("#realizacja .photo img").fadeIn(Site.RealAnimate/2);
					}
					else{
						$("#realizacja .photo img").get(0).onload = function(){$(this).fadeIn(Site.RealAnimate/2);};
					}
					

					
				});
			});
			
				
			
		}
		function refreshMins(){
			$("#realizacja li a").removeAttr("href");
			$("#realizacja li:first-child a").addClass("active").append('<span class="ramka"></span>');
			$("#realizacja li a").each(function(){
				$(this).get(0).onclick = function(){
					window.clearInterval(Site.RealSliderHandle);
					var id = $(this).children().attr("alt");
					if($("#realizacja .photo img").attr("src")!="files/normal/"+id+".jpg"){
					$("#realizacja li a.active span.ramka").remove();
					$("#realizacja li a.active").removeClass("active");
					$(this).addClass("active").append('<span class="ramka"></span>');
					$("#realizacja .photo img").fadeOut(Site.RealAnimate/2, function(){
						$(this).attr("src", "files/normal/"+id+".jpg");
						$(this).attr("alt", id);
						$('#test').html('<img src="files/normal/'+id+'.jpg"/>');
						if($('#test img').height()==516){
							$(this).fadeIn(Site.RealAnimate/2);
						}
						else{
							$(this).get(0).onload = function(){$(this).fadeIn(Site.RealAnimate/2);};
						}
					});
					}
				};
			});
		}
	},
	
	oNas: function(){
		document.title = 'O Nas'+Site.TitleAddress;
		this.siteShow();
	},
	
	kontakt: function(){
		this.inputsUpdate();
		this.selectsUpdate();
		document.title = 'Kontakt'+Site.TitleAddress;
		this.siteShow();
		$(".content form").submit(function(){
			var validate=true;
			if(document.forms[0].email.value.match(/^[\w.]{2,}@(?:\w{1,}\.)+\w{2,}$/)==null){
				$(".content input[name=email]").css("color", "#00deff");
				validate=false;
			}
			if(document.forms[0].dane.value==null || document.forms[0].dane.value=="Imię i Nazwisko"){
				$(".content input[name=dane]").css("color", "#00deff");
				validate=false;
			}
			if(document.forms[0].temat.value==null || document.forms[0].temat.value=="Temat"){
				$(".content input[name=temat]").css("color", "#00deff");
				validate=false;
			}
			if(document.forms[0].tresc.value==null || document.forms[0].tresc.value=="Treść Wiadomości"){
				$(".content textarea").css("color", "#00deff");
				validate=false;
			}
			if(validate){
				var typ= $(".select li:first-child").attr("value");
				var dane=document.forms[0].dane.value;
				var email=document.forms[0].email.value;
				var temat=document.forms[0].temat.value;
				var tresc=document.forms[0].tresc.value;
				$(".content #formularz").fadeOut(300, function(){
					$(this).load('index.php?p=Kontakt&only_cnt=true&onlyshow=1', {typ: typ, dane: dane, email: email, temat: temat, tresc: tresc}, function(){
						$(this).fadeIn(300, function(){	
						Site.kontakt();
						});
					});
				});
			}
			return false;
		});
	},
	
	polec: function(){
		$("body").append('<div id="polec"></div>');
		$("a[href=/Polec.html]").each(function(){
			$(this).attr("adres", $(this).attr("href"));
			$(this).removeAttr("href");
		});
		$("#polec").css("display", "none");

		$("a[adres=/Polec.html]").click(function(){
			$("#polec").load("/Polec.html,c=true", function(){
				$('body').attr('tytuł', document.title);
				document.title = 'Poleć Nas'+Site.TitleAddress;
				Site.inputsUpdate();
				$("#polec a.close").removeAttr("href");
				$("#polec a.close").click(function(){
					Site.polecHide();
				});
				
				$("html").css("height", "100%");
				$("html").css("overflow", "hidden");
				$("#polec .inner").css("display", "none");
				if(navigator.userAgent.indexOf("IE")==-1)
				{
					$(this).fadeIn(Site.PolecAnimateTime/2, function(){
						$(this).css("display", "table");
						$("#polec .okno").css("display", "none");
						$("#polec .inner").css("display", "table-cell");
						$("#polec .okno").fadeIn(Site.PolecAnimateTime/2);
					});
				}
				else
				{
					$(this).css("display", "block");
				}
				$("#polec form").submit(function(){
					var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					if(reg.test(document.forms[0].email.value)==false || reg.test(document.forms[0].twoj_email.value)==false){
						if(document.forms[0].email.value.match(/^[\w.]{2,}@(?:\w{1,}\.)+\w{2,}$/)==null){
						$("#polec input[name=email]").css("color", "#00deff");
						}
						if(document.forms[0].twoj_email.value.match(/^[\w.]{2,}@(?:\w{1,}\.)+\w{2,}$/)==null){
						$("#polec input[name=twoj_email]").css("color", "#00deff");
						}
					}
					else{
						$("#test").load('/Polec.html,c=true', {dane: document.forms[0].dane.value, twoj_email: document.forms[0].twoj_email.value, email: document.forms[0].email.value},function(){
							$("#polec .okno").html('<p>Email został wysłany. Dziękujemy.</p><a class="close">Zamknij Okno</a>');
							setTimeout('Site.polecHide();', Site.PolecMessageTime);
						});
					}
					return false;
				});
			});
		});
	},
	
	polecHide: function(){
		document.title = $('body').attr('tytuł');
		if(navigator.userAgent.indexOf("IE")==-1)
		{
		$("#polec .okno").fadeOut(Site.PolecAnimateTime/2, function(){
			$("#polec").fadeOut(Site.PolecAnimateTime/2);
		});
		}
		setTimeout(function(){
			$("#polec").css("display", "none");
			$("html").css("height", "auto");
			$("html").css("overflow", "visible");
		}, Site.PolecAnimateTime);
	},
	
	setMenuActive: function(){
		if (this.Href.indexOf(',')!=-1)
		{
			var adres = this.Href.substring(this.Href.indexOf('/'), this.Href.indexOf(','))+'.html';
		}
		else{
			var adres = this.Href;
		}
		$("#top ul.menu a.active").removeClass("active");
		$("#top ul.menu a[adres="+adres+"]").addClass("active");
		$("#footer ul.menu a.active").removeClass("active");
		$("#footer ul.menu a[adres="+adres+"]").addClass("active");
	},
	
	hrefUpdate: function(){
		this.Href = self.location.href.replace('http://www.addictive-studio.com', '');
		if(this.Href.indexOf('#')!=-1)
		{
			this.Href = this.Href.substring(this.Href.indexOf('#')+1, self.location.href.length);
		}
	},
	
	selectSite: function(){
		this.hrefUpdate();
		var found = false;
		if (this.Href.indexOf("Oferta")!=-1)
		{
			this.oferta();
			found = true;
		}
		if (this.Href.indexOf("Realizacje")!=-1)
		{
			this.realizacje();
			found = true;
		}
		if (this.Href.indexOf("Kontakt")!=-1)
		{
			this.kontakt();
			found = true;
		}
		if (this.Href.indexOf("Polec")!=-1)
		{
			this.polec();
			found = true;
		}
		if (this.Href.indexOf("O_Nas")!=-1)
		{
			this.oNas();
			found = true;
		}
		if (this.Href.indexOf("Glowna")!=-1 || this.Href=='/')
		{
			this.glowna();
			found = true;
		}
		if(!found){
			Site.siteShow();
		}
	},
	
	siteChange: function(adres){
		var destination = $("#wrapper").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, this.PageScrollTime);
		
		self.location.href = '#'+adres;
		this.hrefUpdate();
		this.setMenuActive();
		
		
		if($("#load").css("height"))
		{
			var heightOld = $("#load").css("height");
		}
		else
		{
			var heightOld = $("#load").height()+"px";
			$("#load").css("height", heightOld);
		}
		$("#load .content").fadeOut(this.ChangePageTime/3, function(){
		
			$("#load").load(adres+',c=true', null, function(){
				$("#load").css({"height": "auto", "width" : "0", "padding-left" : "100%"});
				var heightNew = $("#load").height()+"px";
				$("#load .content").css("display", "none");
				$("#load").css({"height": heightOld, "width" : "100%", "padding-left" : "0"});
				$("#load").animate({height: heightNew}, Site.ChangePageTime/3, null, function(){
					Site.selectSite();
				});	
			});		
		});
	},
	
	siteShow: function(){
		$("#load .content").fadeIn(this.ChangePageTime/3);
	},
	
	inputsUpdate: function(){
		$("input.text").focus(function(){
			$(this).css("color", "#969696");
			if(!$(this).attr("wartosc"))
			{
				$(this).attr("wartosc", $(this).attr("value"));
			}
			if($(this).attr("wartosc")==$(this).attr("value")){
				$(this).attr("value", "");
			}
		});
		$("input.text").blur(function(){
			if(!$(this).attr("value")){
				$(this).attr("value", $(this).attr("wartosc"));
			}
		});
		$("textarea").focus(function(){
			$(this).css("color", "#969696");
			if(!$(this).attr("wartosc"))
			{
				$(this).attr("wartosc", $(this).html());
			}
			if($(this).attr("wartosc")==$(this).html()){
				$(this).html("");
			}
		});
		$("textarea").blur(function(){
			if(!$(this).attr("value")){
				$(this).html($(this).attr("wartosc"));
			}
		});
	},
	
	selectsUpdate: function(){
		document.body.onclick = function() {
			if($(".select ul").css("height")!="34px"){
				$(".select ul").animate({height : "34px"}, 300);
			}
		};
		$("select option").each(function(){
			$(this).attr("tekst", $(this).html());
		});
		$("select").each(function(){
			var length = $(this).children().length;
			var div = $(this).parent();
			$(this).css("display", "none");
			div.css("background", "none");
			div.html('<ul></ul>'+div.html());
			var ul = div.children('ul');
			for(i=1; i<=length; i++){
				ul.append('<li value="'+$(this).children('option:nth-child('+i+')').attr("value")+'">'+$(this).children('option:nth-child('+i+')').html()+'</li>');
			}
			ul.html('<li value="'+ul.children('li:first-child').attr("value")+'">'+ul.children('li:first-child').html()+'</li>'+ul.html());
		});
		$(".select ul li:first-child").click(function(){
			if($(this).parent().css("height")=="34px"){
				$(this).parent().css("height", "auto");
				var height = $(this).parent().height();
				$(this).parent().css("height", "34px");
				$(this).parent().animate({height : height+"px"}, 300);
			}
			else{
				$(this).parent().animate({height : "34px"}, 300);
			}
		});
		$(".select ul li").click(function(){
			if($(".select li").index(this)>0){
				$(this).parent().children("li:first-child").html($(this).html());
				$(this).parent().children("li:first-child").attr("value", $(this).attr("value"));
				$(this).parent().animate({height : "34px"}, 300);
			}
		});
	},
	
	ieHacks: function(){
		$("body").html('<div style="width:100%;text-align:center;padding: 4px 0 5px 10px;background-color:#efeee8;font-family: Verdana, Sans-serif;font-size:11px;border-bottom:#cdcabb solid 1px;color:#000000;"><strong style="color:red">Uwaga!</strong> Twoja przeglądarka jest w bardzo starej wersji. Zaktualizuj ją natychmiast. <a style="color: #2186b9;" href="http://ie6.pl/howto/">Dowiedz się więcej jak to zrobić.</a></div>'+$("body").html());
		$("#top ul.menu img").each(function(){
					$(this).get(0).runtimeStyle.backgroundImage = "none";
					$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).get(0).src + "', sizingMethod='image')";
					$(this).get(0).src = "/template/images/blank.gif";
					$(parent).css("position", "relative");
		});
		$("#realizacje ul.menu img").each(function(){
					$(this).get(0).runtimeStyle.backgroundImage = "none";
					$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).get(0).src + "', sizingMethod='image')";
					$(this).get(0).src = "/template/images/blank.gif";
					$(parent).css("position", "relative");
		});
	}
}
$().ready(function(){
	Site.run();
});