function loadImg(src) {
	var img = new Image();
	$("#loader").show();
	//$("#img").height($(window).height());
	$(img).load(function () {
		$("#loader").hide();
		$("#img img").removeAttr("style");

		if(img.width > img.height)
			$("#img img").attr('src', src+'.jpg').width($(window).width()-420);
		else
			$("#img img").attr('src', src+'.jpg').height($(window).height());
	}).attr('src', src+'.jpg');
}

function changeRel() {
/*
	$("a[rel]").each(function(i) {
		href = $(this).attr('href').replace(/.html/g, '');
		$(this)
		.attr('href', '#'+href)
		.removeAttr('rel');
	});
*/
}

function opacityTop() {
	$("#items .item").css({opacity: 0.5}).mouseover(function() {
		$("#items .item").css({opacity: 0.5});
		$(this).css({opacity: 1.0});
	});
}

function slide_up() {
	margin = $('html').scrollTop()+119;
	$('html,body').stop().animate({scrollTop: margin}, 500);
	btnCheck();
}
function slide_down() {
	margin = $('html').scrollTop()-119;
	$('html,body').stop().animate({scrollTop: margin}, 500);
	btnCheck();
}
function btnCheck() {
	margin = $('html').scrollTop();

	if(margin==0) $(".down").hide();
	else $(".down").show();

	if(margin>=($(document).height()-$(window).height())) $(".up").hide();
	else $(".up").show();
}
function setMenu(id) {
	$("#metki a").each(function() {
		y = $(this).attr("href").split(",");
		if(y[1] == id) {
			$(this).addClass("a"); $(this).parent().css('color', '#ff7f00');
		} else {
			$(this).removeClass("a"); $(this).parent().css('color', '#ddd');
		}
	});
}
function setFlash(site) {
	try {
		document.getElementById("menu").napisz(site);
	} catch(e) { setTimeout('setFlash("'+site+'")', 500); }
}

var margin = 0;
var margin_max = 0;
var ile = 3;

$(document).ready(function() {
	var ie7 = false;
	if((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ie7 = true;

	if(!ie7) changeRel();

	var params = {};
	params.wmode = "transparent";

	scale = ($(window).height()-180) / 430;
	n_height = 430 * scale;
	n_width = 160 * scale;

	if($(window).height()<600) {
		swfobject.embedSWF("paski.swf", "menu", n_width, n_height, "9.0.45", "", "", "", params);
		$(".left div").css({'top': (n_height+110)+'px'});
	} else swfobject.embedSWF("paski.swf", "menu", 160, 430, "9.0.45", "", "", "", params);

	if($(window).height() <= 650) ile = 3;
	else if($(window).height() <= 750) ile = 4;
	else ile = 5;

	$(window).scroll(function () {
		btnCheck();
	});

	$.address.change(function(event) {
		if(event.value == "/") {
			try {
				event.value = window.location.pathname.match(/([a-z,0-9]+).html/)[1];
			} catch(e) { }
		}
		x = event.value.split(',');
		//alert(x[0]);
		if(event.value=="ofirmie") setFlash("anim1");
		else if(event.value=="kontakt") setFlash("anim5");
		else if(x[0]=="wizualizacje") setFlash("anim4");
		else if(x[0]=="realizacje") setFlash("anim3");
		else if(x[0]=="projekt") setFlash("anim2");
		else if(x[0]=="linki") setFlash("anim6");
		else if(x[0]=="praca") setFlash("anim7");

		$("#content").removeAttr("style");

		if(event.value == "/") {
			$("#content .bg2").width($(window).width()-800);
			$("#content").height($(document).height());
			if($("#content .content").attr("class")=="content")
				$("#content").addClass("image");
		} else if((x[0] == "wizualizacje" || x[0] == "realizacje") && x[1]==undefined) {
		  $("#content .bg2").width($(window).width()-800);
      $("#content").height($(document).height());
			$("#content").addClass("image2");
			$("#content").load('gen.php?url='+x[0]+'&jx=1', function() {
				$("#content .bg2").width($(window).width()-800);
				$("#content").height($(document).height());
				if(ie7) {
				  $("#items h3").width($(document).width()-335);
				  $("#items").width($(document).width()-290);
				  $("#items > div").css({'paddingRight':'110px'});
				}
				//opacityTop();
				$("#items > div").height($(window).height()-220);
				$("#items .item").css({'opacity':0.6}).hover(function() {
				  $(this).css({'opacity':1});
				}, function() {
          $(this).css({'opacity':0.6});				  
				});
				//if(x[1]==undefined) window.location=$("#items .item a").attr("href");
				$('a[rel=lightbox]').fancybox({
          'titleShow'     : true,
          'showCloseButton' : true,
          'titlePosition' : 'inside',
          'transitionIn'  : 'elastic',
          'transitionOut' : 'elastic',
          'easingIn'      : 'easeOutBack',
          'easingOut'     : 'easeInBack',
          'overlayOpacity': 0.8,
          'overlayColor': '#000000'
        });
			});
		} else if((x[0] == "wizualizacje" || x[0] == "realizacje")  && x[1]!=undefined) {
		  $("#content .bg2").width($(window).width()-800);
      $("#content").height($(document).height());
		  //$("#content").addClass("image");
			$("#content .bg2").width($(window).width()-800);
			//$("#content").height($(document).height());
			margin_max=0;
			$("#items .item").each(function(i) {
				margin_max += 119;
			});
			btnCheck();
			if($("#content .content").attr("class")=="content") window.location="#"+x[0];
			else loadImg(x[0]+"/b"+x[1]);
		} else if(x[0] == "koncepcyjny" || x[0] == "projekt_koncepcyjny" || x[0] == "techniczny" || x[0] == "projekt_techniczny") {
			setFlash("anim2");
			if($("#img").attr("id") != "img") {
				$("#content").load('gen.php?url='+x[0]+'&jx=1', function() {
					$("#content .bg2").width($(window).width()-800);
					//$("#content").height($(document).height());
					if(!ie7) changeRel();
					setMenu(x[1]);
					loadImg("projekt/b"+x[1]);
				});
			} else {
				$("#content .bg1").hide();
				$("#content .bg2").hide();
				$("#content .content").hide();
				$("#content").removeClass("image");
				setMenu(x[1]);
				loadImg("projekt/b"+x[1]);
			}
		} else {
			$("#content").addClass("image");
			$("#content").load('gen.php?url='+event.value+'&jx=1', function() { $("#content .bg2").width($(window).width()-800); $("#content").height($(document).height()); if(!ie7) changeRel(); });
		}
	});
});

