var chosenSubpage = "#subpage1";
var imgWidths_array = Array();
var firstLeft = 0;
var activeImg = 1;

function windowHeight()
{
	if(jQuery.browser.opera) return document.documentElement.clientHeight;
	else if(jQuery.browser.msie) return document.documentElement.clientHeight;
	else return window.innerHeight;
}
function windowWidth()
{
	if(jQuery.browser.opera) return document.documentElement.clientWidth;
	else if(jQuery.browser.msie) return document.documentElement.clientWidth;
	else return window.innerWidth;
}


function moveSubpageLead(newPos, btn, subpage)
{
	$("#inner2-subpageLead ul").animate({"top": newPos }, "normal" );
		
	$(chosenSubpage).fadeOut("fast", function(){$(subpage).fadeIn("fast")});
	chosenSubpage = subpage;
	
	$(".subpageBtn1").removeClass("active");
	$(".subpageBtn2").removeClass("active");
	$(".subpageBtn3").removeClass("active");
	$(".subpageBtn4").removeClass("active");
	$(".subpageBtn5").removeClass("active");
	$(".subpageBtn6").removeClass("active");
	
	btn.addClass("active");
	
	
}

function changeProjectGallerySize()
{
	var newHeight = Math.round(windowHeight() - $("#projectDetailsDescription").height() - 20);
	//alert(newHeight);
	if(newHeight < 580)
	{
		$("#projectGallery img").each(function()
										   {
											   newWidth = Math.round((newHeight/$(this).height()) * $(this).width());
											   //alert($(this).width());
											   //$(this).width(newWidth);
											   $(this).height(newHeight);
										   }
								  );
		$("#projectGallery").css({"top": "10px" })
	}
	else
	{
		$("#projectGallery img").each(function()
										   {
											   $(this).height(580);
										   }
								  );
		
		var newTopPos = (windowHeight() - $("#projectDetailsDescription").height() - 580) / 2;
		$("#projectGallery").css({"top": newTopPos + "px" })
	}
	imgWidths_array = Array();
	$("#projectGallery ul li").each( function(index){ imgWidths_array[index] = $(this).width() } );
	firstLeft = (windowWidth() / 2) - (imgWidths_array[0] / 2);
}

function hideProjectGallery()
{
	$("#projectDetails .bg").hide();
	$("#projectDetailsDescription").css({"bottom": -$("#projectDetailsDescription").height()-20});
	$("#projectDetails .nav").css({"right": -$("#projectDetails .nav").width()-40});
	$("#projectGallery").hide();
}

function showProjectGallery(projId)
{
	var activeImg = 1;
	
	$("body").append("<div id=\"projectDetails\"></div>");
	
	$("#projectDetails").load("../layout/projectDetails.php", {'projId':projId}, function(){						
							
					//    ZASTAP LOAD GET!!!!!!!!!
							
	//changeProjectGallerySize();
	hideProjectGallery();
	
	$("#projectGallery").show();
	$("#projectGallery ul").css({"left": windowWidth()+"px"});
	
	$("#projectGallery ul li").not($("#projectGallery ul li").eq(activeImg-1)).animate( {opacity:.2}, 500);
	
	
	
	//alert(imgWidths_array[0]+"-"+firstLeft);
	
	
	$("#projectDetails .bg").fadeIn("slow", function(){
		$("#projectDetailsDescription").animate( { bottom:"0px" }, 600 );
		changeProjectGallerySize();
		setTimeout(function(){$("#projectGallery ul").animate( { left:firstLeft } , 1000 ) }, 600);

		setTimeout(function(){$("#projectDetails .nav").animate( { right:"0px" } , 1000 )  }, 1600)
		;})
																		   
	$("#projectDetails .exitBtn").click( function(){ hide2ProjectGallery() } );
	$("#projectDetails .nextBtn").click( function(){
			if(activeImg < imgWidths_array.length)
			{
				changeProjectGallerySize();
				activeImg++;
				var newPosition = 0;
				for(i = 0; i < activeImg-1; i++) { newPosition += imgWidths_array[i]; }
				$("#projectGallery ul").animate( { left: (windowWidth() / 2) - newPosition - (imgWidths_array[activeImg-1] / 2) } , 800 );
				$("#projectGallery ul li").eq(activeImg-2).animate( {opacity:.2}, 800);
				$("#projectGallery ul li").eq(activeImg-1).animate( {opacity:1}, 800);
			}
		} );
	$("#projectDetails .prevBtn").click( function(){
			if(activeImg > 1)
			{
				changeProjectGallerySize();
				activeImg--;
				var newPosition = 0;
				for(i = 0; i < activeImg; i++) { newPosition += imgWidths_array[i]; }
				$("#projectGallery ul").animate( { left: (windowWidth() / 2) - newPosition + (imgWidths_array[activeImg-1] / 2) } , 800 );
				$("#projectGallery ul li").eq(activeImg).animate( {opacity:.3}, 800);
				$("#projectGallery ul li").eq(activeImg-1).animate( {opacity:1}, 800);
			}
		} );
	
																		   
																		   });
	
}


function hide2ProjectGallery()
{
	setTimeout(function(){$("#projectDetailsDescription").animate( { bottom:-$("#projectDetailsDescription").height()-20 }, 600 )}, 500);
	$("#projectGallery ul").animate( { left: windowWidth()+"px" } , 700 )
	$("#projectDetails .nav").animate( { right: -$("#projectDetails .nav").width()-40 } , 700 )
	setTimeout(function(){$("#projectDetails .bg").fadeOut(800)}, 1000);
	setTimeout(function(){$("#projectGallery").hide()}, 2000);
}


$(
	function()
	{
		//if($("#portfolioCont")) portfolio = true;
		
		$("#mainMenu>ul>li>a").click(function(){  } );
		$(".subpageBtn1").click( function(){ moveSubpageLead("0", $(this), "#subpage1") } );
		$(".subpageBtn2").click( function(){ moveSubpageLead("-245px", $(this), "#subpage2") } );
		$(".subpageBtn3").click( function(){ moveSubpageLead("-490px", $(this), "#subpage3") } );
		$(".subpageBtn4").click( function(){ moveSubpageLead("-735px", $(this), "#subpage4") } );
		$(".subpageBtn5").click( function(){ moveSubpageLead("-980px", $(this), "#subpage5") } );
		$(".subpageBtn6").click( function(){ moveSubpageLead("-1225px", $(this), "#subpage6") } );
		
		//$("#portfolioCont li a").click( function(){ $(this).hide("slow") } );
		//$("#showAll").click( function(){$("#portfolioCont li a").fadeIn("slow");} )
		
		
		$("#portfolioCont li a").click( function(){ showProjectGallery($(this).attr('href')) } );
		
		$(window).resize(function(){
			changeProjectGallerySize();
		});
		//changeProjectGallerySize();
		
				
		$(".printPage").click( function(){ window.print() } );
		$(".changeFontSize .normal").click( function(){ $("#subpageCont *").css("fontSize", "100%").css("marginBottom", "3%"); $("#subpageCont h3").css("fontSize", "20px").css("marginBottom", "10px"); $("#subpageCont h4").css("fontSize", "16px").css("marginBottom", "10px") } );
		$(".changeFontSize .medium").click( function(){ $("#subpageCont *").css("fontSize", "110%").css("marginBottom", "4%"); $("#subpageCont h3").css("fontSize", "24px").css("marginBottom", "3%"); $("#subpageCont h4").css("fontSize", "20px").css("marginBottom", "3%") } );
		$(".changeFontSize .big").click( function(){ $("#subpageCont *").css("fontSize", "120%").css("marginBottom", "6%"); $("#subpageCont h3").css("fontSize", "30px").css("marginBottom", "3%"); $("#subpageCont h4").css("fontSize", "24px").css("marginBottom", "3%") } );
		
		$("#newsModule .news").click( function(){ $("#newsDetailsModule .news[title!="+$(this).attr("title")+"]").hide("slow"); $("#helloModule").hide("slow"); $( "#newsDetailsModule ." + $(this).attr("title") ).show("slow"); } )
		
	}
)
