//  If you think this is sloppy, you haven't seen my php code (or my desk for that matter).


var linkTimeOut = 1000; // freeze brand/title after kclicking link (in ms)
var linkTime = 0; // 0 = no linktime set, go right ahead

var TimeToFade = 70;

var aBrand = "";
var aTitle = "";
var aBack = "";

var nHeight = 1;
var nWidth = 2;
var nControllerHeight = 0;

var ResVers = new Array();
var ControllerHeight = 0;

var info_state;

window.onload=getBT;
window.onresize=setSize;


//Helpfunction to show/hide ID
function IdVisible(ID, s)
{
	if(x=document.getElementById(ID)){
		if(s==1){
			x.style.visibility = 'visible'; 
			x.style.opacity = 1;
		} else if(s==0) {
			x.style.visibility = 'hidden';
		} else if(s==-1){
			x.style.opacity = 0.6;
		}
	}
}

function NavMouseOver(ID,active,url)
{
	if(ID == 'info'){
		if(info_state == 1){
			active = 1;
		}
		IdVisible(ID,active);
		setSize();
	} else if(linkTime == 0){
		if(document.getElementById("works")){
			IdVisible("works", active);
			IdVisible("m1", active);
			IdVisible("player", active);
			if(active ==1 && info_state == 0){
				IdVisible("info", 0);
				}
			else{
				IdVisible("info", active);
			}
		}
   		SetBT(ID,url);
   		IdVisible("back",1);
   	}
}

function SetBT(ID,url)
{
	if(ID!=='active'){
		innerText =  document.getElementById(ID).innerHTML;
		bt = innerText.split(' | ');
    	brand = bt[0].split('>');
    	brand = brand[1].split('<');
    	brand = brand[0];
    	title = bt[1];
    	burl = url; 
    }
    else{
    	brand = aBrand;
    	title = aTitle;
    	burl = aBack;
    }
    
    //alert (aBack + " " + burl);
    
    if(brand){document.getElementById("brand").innerHTML = brand;}
   		else {document.getElementById("brand").innerHTML ="";};
    if(title){document.getElementById("campaign_title").innerHTML = title;}
    	else {document.getElementById("campaign_title").innerHTML ="";};
    if(burl){document.getElementById("back").src = burl;}
    	else {document.getElementById("back").src="http://jeroentiebout.net/helpers/vertical_spacer.png";};
    	
    /*back = document.getElementById("back");
    //alert(url);
    back.src = url;
	*/
}



function getBT(){
	
	if(document.getElementById("brand")){aBrand = document.getElementById("brand").innerHTML;}
		else {aBrand = "";};
	if(document.getElementById("campaign_title")){aTitle = document.getElementById("campaign_title").innerHTML;}
		else {aTitle = "";};
	if(document.getElementById("back")){aBack = document.getElementById("back").src;}
		else {aBack = "";};
	
	//evt andere velden (description?) ook ophalen
	
	linkTime = 1;
	
	
	
   	setTimeout("linkTime = 0",linkTimeOut);
   
	if( document.getElementById("m1") ) {
		m1_ = document.getElementById("m1");
   	
		nHeight = m1_.height - nControllerHeight;
		nWidth = m1_.width;
	
		setSize();
	}
	
	///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS 
	shortcut.add("right", function() {
		url = document.getElementById("nextw").href;
		//alert("left: " + url);
		window.location = url;
	});	
	
	shortcut.add("left", function() {
		url = document.getElementById("prevw").href;
		//alert("left: " + url);
		window.location = url;
	});	
	
	shortcut.add("up", function() {
		url = document.getElementById("prevc").href;
		//alert("left: " + url);
		window.location = url;
	});	
	
	shortcut.add("down", function() {
		url = document.getElementById("nextc").href;
		//alert("left: " + url);
		window.location = url;
	});	
	
	shortcut.add('h', function() {
		window.location = 'http://jeroentiebout.net';
	});	
	
	shortcut.add('i', function() {
		sndReq('toggleInfo');
		//NavMouseOver('info',info_state);
	//	setSize();
	});	
	
	//kuler kleurtje halen?
	shortcut.add('b', function() {
		sndReq('background');
		//window.location.reload();
	});	
	

	
	///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS ///// SHORTCUTS 
}

//----------------------------------------------------                   sizing up stuff
function ElmSize(elmID) {
	s=new Array();
	if(s=document.getElementById(elmID).clientHeight) {} 
  	else if(s=document.getElementById(elmID).offsetHeight){}
	
	return s;
}

//http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
//----------------------------------------------------                   /sizing up stuff

//----------------------------------------------------                   set size
function getSizes(blurb,type){
	ResVers = blurb;
	
	switch(type){
		case 2: nControllerHeight = 25;
				break;
		case 6:	nControllerHeight = 16;
				break;
		default: nControllerHeight = 0;
	}
	
	
}

function CheckRes(height){
	
	nurl = "";
	m1_ = document.getElementById("m1");
	
	
	lastindex = ResVers.length-1;
	l_height = ResVers[lastindex]["h"]; // get largest size . if its set bigger set it to max res again.
	
	//alert (l_height);
	
	if(height>l_height && !document.getElementById("m2")){
		nurl = ResVers[lastindex]["u"];
		
		m1_.width = ResVers[lastindex]["w"];
		m1_.height = ResVers[lastindex]["h"];
		if(nControllerHeight == 16){
			m1_.height = parseInt (m1_.height) + parseInt (nControllerHeight);
			}
		
	} else {
		for(i=0;i<ResVers.length;i++){
			if(height<=ResVers[i]["h"]){
				nurl = ResVers[i]["u"];
				
				break;
			}
		}
	}
	m1_.src = "files/" + nurl;
	

}

function setSize(){

	em = ElmSize("wl0");  // meet 1.5 em in px
	
	H = f_clientHeight(); // browservenster hoogte
	W = f_clientWidth(); // browservenster breedte
	
	info = document.getElementById('info');
	if( info.style.visibility == 'visible' || info_state == 1){
		H = H - info.offsetHeight;
	}
		
	// If there's an audioplayer and a transcript in the game...
	
	if (transcript = document.getElementById('transcript')){ 
		if (player = document.getElementById('player')){ H = H - player.offsetHeight;}
		//transcript.style.maxWidth = W - 8 + "px";
		
	
		tmph = H - (8.5 * em);
		transcript.style.maxHeight = tmph + "px";
		//H = H - transcript.offsetHeight;
		}
	
	
	//else{
	
	maxHeight =  H - (7.5 * em); 
	maxWidth = W - 8;
	
	

	maxHeightPctOfmedia =  (maxHeight  - nControllerHeight) / nHeight;
	maxWidthPctOfmedia =  maxWidth / nWidth;
	
	newWidth = nWidth * maxHeightPctOfmedia;
	newHeight = (nHeight * maxHeightPctOfmedia) + nControllerHeight;
	
	
	
	if (newWidth > maxWidth){
	
		//alert("new W: " + newWidth + "  maxwidth: " + maxWidth);
		newWidth = nWidth * maxWidthPctOfmedia;
		newHeight = (nHeight * maxWidthPctOfmedia) + nControllerHeight;
	}
	
	if(newWidth < 1)newWidth = 1;
	

		m1_ = document.getElementById("m1");
		m1_.height = newHeight;
		m1_.width = newWidth;
	
	//alert(newWidth + "x" + newHeight);

	
	if(document.getElementById("m2")){
		m2_ = document.getElementById("m2");
		m2_.height = newHeight;
		m2_ .width = newWidth;
		}

	if(!transcript){CheckRes(newHeight);}
	
	

}



//----------------------------------------------------                   /set size

function mouseOutMain(){
	IdVisible('works',1);
	SetBT('active');
	IdVisible('m1',1);
	IdVisible('player',1);
	IdVisible('info',info_state);
	IdVisible('back',-1);
	//opacity: 0.49;
}



