// JavaScript Document
function init()
{
	if( $("#menu_helper").length>0 )
	{
		menuUp();
		/*$("#menu_helper").fadeIn(1500);
		$("#menu_helper").hide(0);
		setTimeout(function(){	
								$("#menu_helper").fadeIn(1500, function(){ menuUp(); }); 
							}
					, 250) ;*/
	}
	else
	{
		resizeHandler();
		setTimeout('hideCover()', 500);
	}
	
}
function hideCover(){
	$('#cover').hide();
}
function resizeHandler()
{
	if(getWindowWidth()<MIN_WIDTH)
	{
		$("#container").width(MIN_WIDTH);
	}
	else
	{
		$("#container").css({width:"100%"});
	}
	moveMenu();
	moveWorkDetail();
	moveWork();	
	moveSectionHome();
	
}
function moveWorkDetail()
{
	if($("#work_detail").length > 0)
	{
		if($("#work_detail .secondary_info").length > 0 )
		{
			//$("#work_detail .secondary_info").height( $("#work_detail #primary_info").height() ) ;	
			$("#work_detail .title").width($("#work_detail #primary_info").width()+$("#work_detail .secondary_info").width());
		}
		else
		{
			if($("#work_detail .title").width() > $("#work_detail #primary_info").width()+50)
			{
				$("#work_detail .title").width($("#work_detail #primary_info").width()+70);			
			}
			else
			{
				$("#work_detail .title").width($("#work_detail #primary_info").width());
			}
		}
		
		var w = $("#work_detail .image").outerWidth(true) +$("#work_detail .detail").outerWidth(true);
		var marginX = "-"+(w/2)+"px" ;
		
		
		
		$("#work_detail").width (w);
		
		var h = $("#work_detail").height();	
		var marginY = "-"+(h/2)+"px" ;
		
		$("#work_detail").css({marginLeft: marginX, marginTop: marginY, visibility: "visible"});
		
		
		
		/*$("#work_detail").animate({
						 marginLeft: margin
						  }, 100 );	*/
	}
	
}

function moveWork()
{
	if($("#work_image").length > 0)
	{
		if ( $("#reference_container #quickreference").length > 0 )
		{
			$("#work_image").width( ($("#work_image").width() > 500 )? 500 : $("#work_image").width() );
		}
		
		if($("#work_image #bibliography_description").length > 0)
		{			
			if( $("#work_image #bibliography_description").width() > 300 )
			{
				 $("#work_image #bibliography_description").width(300);
			}			
		}
		
		$("#work_image").css({display:"block"});
		
		if ( $("#reference_container #image_reference").length > 0 )
		{
			$("#work_image").width( 650 ) ;
			
			//$("#reference_container").height(330) ;
			
			$("#reference_container #reference_text").width(300) ;

			$("#work_image #image_reference .thumbs").width($("#main_image img").width()) ;
			
			
			if($("#main_image img").width() + $("#reference_container #reference_text").width() < $("#work_image").width() )
			{
				
				$("#work_image").width( $("#main_image img").width() + $("#reference_container #reference_text").width()+30 ) 
			}
			var contentH = $("#work_image #year_body").height() + $("#reference_container #year_title").height() + 22 ;
			
			if(contentH > $("#work_image").height() )
			{
				var textH =  $("#reference_container #reference_text").height() + $("#reference_container #year_title").height()+22 ;
				var imageH =  $("#reference_container #image_reference").height() + $("#reference_container #year_title").height()+22 ;
				if(imageH >= $("#work_image").height() )
				{
					$("#reference_container #year_body").css({overflow: "visible"});
				}
				else 
				{
					$("#reference_text").css({overflow:"visible"});
					$("#reference_text").height( 250 );
				
				}
			}

		}
		else if($("#reference_text").length > 0 )
		{
			
			$("#reference_text").width( ($("#reference_text").width() > 500 )? 500 : $("#reference_text").width() );
			$("#work_image").width( ($("#work_image").width() > 500 )? 500 : $("#work_image").width() );	
		}
		
		
		
		
		var w = $("#work_image").width();
		
		var marginX = "-"+(w/2)+"px" ;
		
		var h = $("#work_image").height();
		var marginY = "-"+(h/2)+"px" ;
		
		$("#work_image").css("margin-left", marginX).css("margin-top", marginY) ;
		
		
	}
	
}

function moveSectionHome()
{
	if($("#section_home").length > 0)
	{
		$("#section_home").css({display:"block"});
		var w = 590; 
		var marginX = "-"+(w/2)+"px" ;
		
		var h = 365-20;
		var marginY = "-"+(h/2)+"px" ;
		
		$("#section_home").css("margin-left", marginX).css("margin-top", marginY) ;
	}
	
}

function moveMenu()
{ 
	var winH = getWindowHeight();
	var winW = ( getWindowWidth() > MIN_WIDTH ) ? getWindowWidth() : MIN_WIDTH;
	$("#main_menu").css({width: winW+"px"});
	if(winH<MIN_HEIGHT)
	{
		$("#container").height(MIN_HEIGHT); 
		$("body").css({overflow: "auto"});
	}
	else
	{
		$("#container").height(winH);

	}
}

function menuUp()
{
	var winH = getWindowHeight();
	if(winH<MIN_HEIGHT)
	{
		var t = MIN_HEIGHT-$(window).scrollTop()-12-26;
		$("#main_menu").stop(); 
		$("#main_menu").animate({
						  top: t+"px"
						  }, 350 );	
	}
	else
	{
		$("#main_menu").stop();
		$("#main_menu").animate({
						  bottom: "0px"
						  }, 350 );	
		
		
	}
}
function menuDown() {
	var winH = getWindowHeight();
	if(winH<MIN_HEIGHT)
	{
		if( $("#menu_helper").length <= 0 )
		{
			var t = MIN_HEIGHT-$(window).scrollTop()-12;
			$("#main_menu").stop();
			$("#main_menu").animate({
							  top: t+"px"
							  }, 350 );	
		}
	}
	else
	{
		if( $("#menu_helper").length <= 0 )
		{
			$("#main_menu").stop();
			$("#main_menu").animate({
							  bottom: "-26px"
							  }, 350 );	
		}
		
		
	}
}

function showLanguageSelection()
{
	$('#language_selection').fadeIn("slow");
	
}
	

function hideYears()
{
	$(".item").hide();
}
function showYear(obj)
{
	
	$(obj).parent().siblings("dd").show() ;
	$(obj).parent().innerWidth( 1600 ) ;
}

function getWindowHeight(){
	return  $(window).height () ;
}
function getWindowWidth(){
	return  $(window).width () ;
}


/** SCROLLER **/
function initScroller(){
	
	var itemSize  = 0;
	var countPoss = 0;
	var wItem = 40;
	var wWin = ( getWindowWidth() > MIN_WIDTH ) ? getWindowWidth() : MIN_WIDTH;
	
	while(wItem <= wWin-80){
		wItem += 43; 
		itemSize++; 
	}
	
	var i = 1;
	$('#timeline li').each(function(e){
		i++;
		
		if($(this).find('a').hasClass('currentYear')){
			countPoss = i;		
		}
	});
		
	$("#scrollable").scrollable({
		horizontal:true,
		size: itemSize,
		poss: countPoss
	});

	var wTotal = (itemSize * 40) + $("#scrollable .prev").width() + $("#scrollable .next").width() ;
	var left = ( wWin - wTotal ) /2 ;
	
	$("#scrollable").width(wTotal);
	$("#scrollable").css({paddingLeft:left+"px"});
}
/** SCROLLER **/
				
			
