﻿/* bascon - js */
$(document).ready(function() {
	onReadyHandler();
	}
)
$(function(){
	$('.scroll-pane').jScrollPane();
	$('.scroll-pane-arrows').jScrollPane(
		{
			showArrows: true,
			horizontalGutter: 10
		}
	);
});

$(function(){
	$('.scroll-pane').each(
		function()
		{
			$(this).jScrollPane(
				{
					showArrows: $(this).is('.arrow')
				}
			);
			var api = $(this).data('jsp');
			var throttleTimeout;
			$(window).bind(
				'resize',
				function(){
					
					// IE fires multiple resize events while you are dragging the browser window which
					// causes it to crash if you try to update the scrollpane on every one. So we need
					// to throttle it to fire a maximum of once every 50 milliseconds...
					if (!throttleTimeout) {
						throttleTimeout = setTimeout(
							function()
							{
								
								api.reinitialise();
							
								throttleTimeout = null;
							},
							300
						);
					}								
				}
			);
			
		}
	)

});

function onReadyHandler(){
	
	//alert("bascon");
	
	var hash=location.hash;
	if (hash){
		if(contentmode =='projekter' && showallitems!=true && hash.indexOf("#all")!=-1){
			showallitems=true;
		}
	}
	
	if(contentmode=='medarbejdere'){
		window.setTimeout(setPersonImages,1000);
	}
	
	updateContentHeight();
	rewriteMail();
	
	
	$('.scroll-pane').each(
		function()
		{
		$(this).jScrollPane(
				{
					showArrows: $(this).is('.arrow')
				}
			);
		try{
			var api = $(this).data('jsp');
			api.reinitialise();
		}catch(e){
		
			//alert($(this));
		}
		}
	)
//secondarycontent-pane
	$('#preloader').hide();
	
	// fjern alle mouse over tekster på siden
	
	$("a").removeAttr("title");
	$("img").removeAttr("title");
	
	if(contentmode!='home' && bgslides.length>0){
		window.setTimeout(loadBgImage,500);
	}
	updateContentHeight();
	var isloadedTimeout;
	if (!isloadedTimeout) {
		isloadedTimeout = setTimeout(
			function(){								
				var promt_str='isloadedTimeout \n';
				var scrollcount=0;
				$('.scroll-pane').each(
					function(){
						$(this).jScrollPane(
							{
								showArrows: $(this).is('.arrow')
							}
						);
						var api = $(this).data('jsp');
						promt_str+=scrollcount++;
						if(api){
							promt_str+=' - api found';
						}else{
							promt_str+=' - no api found';
						}
						promt_str+='\n';
						api.reinitialise();
						/*var throttleTimeout;
						$(window).bind(
							'resize',
							function(){
								
								// IE fires multiple resize events while you are dragging the browser window which
								// causes it to crash if you try to update the scrollpane on every one. So we need
								// to throttle it to fire a maximum of once every 50 milliseconds...
								if (!throttleTimeout) {
									throttleTimeout = setTimeout(
										function()
										{
											
											api.reinitialise();
										
											throttleTimeout = null;
										},
										300
									);
								}								
							}
						);*/
					
					}
				)
				//alert(promt_str);
			}
			,
			2000 );
	}			
	

}

function loadBgImage(){
	$("img#bg").load(function()
			{
		
				biw=$("img#bg").width();
				bih=$("img#bg").height();
			
				var dh=$(document).height();
				var dw=$(document).width();
				
				ratio = bih / biw;	
					
				// Get browser window size
				
				// Scale the image
				if ((dh/dw) > ratio){
		   		 	$("img#bg").height(dh);
		   		 	$("img#bg").width(dh / ratio);
				} else {
		   			$("img#bg").width(dw);
		    			$("img#bg").height(dw * ratio);
				}
				/*
				// Center the image
				$("img#bg")).css('left', (dw - $("img#bg")).width())/2);
				$("img#bg")).css('top', (dh - $("img#bg")).height())/2);
	*/
				/* Fade in during 3 seconds */
			
				$("img#bg").fadeTo(500,1);
				
				
			}
			)
			$("img#bg").attr("src", bgslides[0].image);

}
function setPersonImages(){

	$('.personimage').each(
		function(){
			var src=$(this).attr('alt');
			$(this).attr('src',src);
		
		
		}
	
	)

}
function onLoadHandler(){

	/* Assign the image to the background */
	
	
	
	
}
function rewriteMail(){
	$('.mail').each(
		function()
		{
		var orgmail_str = $(this).html();
		
		var mail=orgmail_str.split('bascon')[0]+'@bascon.dk';
		
		var maillink='<a href="mailto:';
		maillink+=mail+'" >'+mail+'</a>';
		
		$(this).html(maillink);
		
		}
	)
}
function onResizeHandler(){
	updateContentHeight();
}

function setProjectsFront(pro_max,columns){
	pro_max--;
	pro_count=0;
	
	$('.projects-item').each(
		function(){
			pro_count++;
		
		}
	)
	
	if(pro_count>pro_max){
	
		pro_count=0;
		$('.projects-item').each(
			function(){
				pro_count++;
				if(pro_count<=pro_max){
					$(this).show();
				}else{
					$(this).hide();
				}
		
			}
		)
		$('#all-link').show();

	}else{
		$('.projects-item').each(
			function(){
				$(this).show();
			}
		)
		$('#all-link').hide();
	
	
	
	}
	
	$('#list-content').width(columns*(195+6));
	
	

}

function personOver(id){
	
	$('.people-item .personimage .mouseover-pane').each(
		function(){
			$(this).hide();
		}
	)
	
	$('#'+id+' .personimage .mouseover-pane').show();
	

}

function scrollToLetter(id){
	var pane = $('#maincontent-pane');
     var api = pane.data('jsp');
     api.reinitialise();
     api.accordion_callback=openDone;
     api.scrollToElement("#letter_"+id, true, true);

}

function personClick(id){

	var pane = $('#maincontent-pane');
     var api = pane.data('jsp');
     api.reinitialise();
     api.accordion_callback=openDone;
     api.scrollToElement("#item_"+id, true, true);
     
}
function showallprojects(){
	//alert("vil du virkelig?");
	showallitems=true;
	updateContentHeight();
	
	$('.scroll-pane').each(
		function()
		{
		var api = $(this).data('jsp');
		api.reinitialise();
		}
	)
}

function startslideshow(){
	var currentimage=frontpageslides[0].image;
	jQuery(function($){
		$.supersized({
			slides : [ {image: currentimage } ]
			});
	});
	
	//$.doTimeout(4500,function() {

	//nextslide();
//});

}

function nextslide(){
	if(frontpageindex==undefined || frontpageindex==null){
		frontpageindex=0;
	}else{
		frontpageindex++;
	}
	if(frontpageindex>=frontpageslides.length){
		frontpageindex=0;
	}
	
	showslide(frontpageindex);
	

}
function onSlideShowBtn(index){

	stopslideshow();
	showslide(index)
}
function showslide(index){

	if(frontpageslides[index]!=undefined){
	alert('frontpageindex: '+frontpageindex+'\n'+frontpageslides[index].image);
	var currentimage=frontpageslides[index].image;
	$.supersized({
			slides : [ {image: currentimage } ]
			});
	
	}
	$('.indicators').each(
				function(){
					$(this).hide();
				}
	)
	$('#indicator'+frontpageindex).show();
	
	$('#slidetext .headline').html('<a href="'+frontpageslides[frontpageindex].link+'" ><h1>'+frontpageslides[frontpageindex].title+'</h1><p>'+frontpageslides[frontpageindex].text+'</p></a>');
	
	

}
function updateContentHeight(){

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		 myHeight = document.body.clientHeight;
	}
	
	
	
	
	var primary = document.getElementById("primary");
	var mainheight=(myHeight-140)+"px";
	
	if(primary){
		primary.style.height = mainheight;
	}
	var primarywidth=(myWidth-(130+6+6))+"px";
	
	
	var submenu = document.getElementById("submenu");
	if(submenu){
		if(submenu.style.display!='none'){
			submenu.style.height = mainheight;
			submenu.style.left=142+"px";
			primary.style.left=(289)+"px";
		}
	
	}else{
		if(primary){
			primary.style.left=142+"px";
		}
	}
	
	
	var secondary= document.getElementById("secondary");
	$('#content').css({'margin-left':'0px'});
	if(contentmode!='normal' && contentmode!=undefined && contentmode!=null && primary){
		//primary.innerHTML=contentmode;
		var w=myWidth-(getNumber(primary.style.left)+6)-20;
		primary.style.width=w+'px';
		columns=Math.floor((w-17)/(195+6));
		rows=Math.floor((getNumber(mainheight)+6)/(175+6));
		
		if(columns<1){
			columns=1;
		}
		if(rows<1){
			rows=1;
		}
		
		if(contentmode =='projekter'){
			if(showallitems!=true){
				setProjectsFront(rows*columns,columns);
			}else {
				//$('#list-content').width(w-17);
			
				$('#list-content').width(columns*(195+6));
			
				$('.projects-item').each(
				function(){
					$(this).show();
				}
				)
				$('#all-link').hide();
			}
		}else if(contentmode =='medarbejdere'){
			setHeightOnElement(primary,'maincontent-pane',getNumber(mainheight)-30);
			$('#list-content').width(columns*(195+6));
		}else if(contentmode == 'home'){
			
			var menuheight=$('#access').height();
			mainheight= (menuheight-10)+'px';
			primary.style.height=mainheight;
			/*var promt='menuheight: '+menuheight+'<br/>';
			
			$('#content').html(promt);*/
			//alert(promt);
		
			
				var dh=$(document).height();
				var dw=$(document).width();
				
			
					
				// Get browser window size
				
				// Scale the image
				if ((dh/dw) > ratio){
		   		 	$("img#bg").height(dh);
		   		 	$("img#bg").width(dh / ratio);
				} else {
		   			$("img#bg").width(dw);
		    			$("img#bg").height(dw * ratio);
				}
		}
		
		upDateContentWidth(primary,w);
		
	}else if(secondary){
		//if(secondary.style.display!='none'){
			
			//if((myWidth-(getNumber(primary.style.left)+502+6))<(230+6)){
			if(myWidth<1037){
				/*var pw= myWidth-(getNumber(primary.style.left)+6+230+6+40);
				if(pw>423){
					primary.style.width=pw+'px';
					secondary.style.display='block';
				}else{
					primary.style.width=(462)+'px';
					secondary.style.display='none';
				}
				*/
				/*secondary.style.float="right";*/
				/*body.style.['min-width']='1050px';
				'position':'relative',
					'float':'right',*/
				$("#secondary").css({
					
					'right':'0',
					'left':(1037-(230+20+6))+'px'
				});
				
				$("#minimenu").css({
					
					'right':'0',
					'left':(1037-(230+20+6))+'px',
					'display':'block',
					'width':'256px'
					
				});
				
				$("#colophon").css({
					'width':'1037px'
				});
				secondary.style.display='block';
				primary.style.display='block';
				primary.style.width=(472)+'px';
			}else{
				$("#secondary").css({
					/*'left':'0',*/
					'left': (myWidth-(230+20+6))+'px',
					'right':'6px'
					
					
				});
				
				$("#minimenu").css({
					/*'left':'0',*/
					'left': (myWidth-(230+20+6))+'px',
					'right':'6px',
					'display':'block',
					'width':'256px'
					
				});
				
				$("#colophon").css({
					'width':myWidth+'px'
				});
				
				primary.style.width=(472)+'px';
				secondary.style.display='block';
				
			
			}
			
			var w=getNumber(primary.style.width);
			
			upDateContentWidth(primary,w)
			
			
			/*
			var promt='myWidth: '+myWidth+' px<br/>';
			promt+='primary.style.left: '+getNumber(primary.style.left)+' px<br/>';
			promt+='primary.style.width: '+primary.style.width+'<br/><hr/>';
			promt+='pw: '+pw+'<br/><hr/>';
			promt+='secondary.style.width: '+230+' px<br/>';
			
			promt+='space left'+(myWidth-(getNumber(primary.style.left)+502+6))+' px<br/>';
			promt+='space needed'+(230+6)+' px<br/>';
			
			
			secondary.innerHTML=promt;
			*/
		
			//primarywidth=(myWidth-(230+6+130+6+6+20))+"px";
			
			secondary.style.height=mainheight;
				$('#secondarycontent-pane .jspContainer').each(
					function(){
						$(this).height(secondary.style.height);
		
					}
				);
		//}
		//$("#secondarycontent").fadeTo(1000,1);
		$("#secondarycontent").css({'display':'block'});
		var w=getNumber(primary.style.width);
			
			upDateContentWidth(primary,w)
		
	}
	
	
	
	/* primary.style.width=primarywidth;*/
	
	var primarycontent=document.getElementById("primarycontent");
	if(primarycontent){
		primarycontent.style.height = mainheight;
	}
	$("#primary").css({'display':'block'});
	$("#submenu").css({'display':'block'});
	
	
	
}
function upDateContentWidth(primary,w){
	var parent=primary;
	var p=setWidthOnElement(primary,'primarycontent',w);
	var p=setWidthOnElement(p,'maincontent-pane',w);
	var p=setWidthOnElement(p,'jspContainer',w);
	w-=20;
	var p=setWidthOnElement(p,'jspPane',w);
	w-=7;
	var p=setWidthOnElement(p,'content',w);
	
	
	

}
function setHeightOnElement(parent,elementID,h){
	
	var element=document.getElementById(elementID);
	if(element){
		element.style.height=h+'px';
		return element;
	}
	return parent;
	
}
function setWidthOnElement(parent,elementID,w){
	
	var element=document.getElementById(elementID);
	if(element){
		element.style.width=w+'px';
		return element;
	}
	return parent;
	
}
function getNumber(_str){
	
	return Number(_str.split('px').join(''));

}
