// Vox Nativa Association, Evadne @ evadnia.wordpress.com


// TODO: 
// integrate RSH

var vxDebugSwitch = false;
var vxShowDoorpage = false;
var vxDoorpageLoaded = false;
var vxSidebarLoaded = false;
var vxSidebarLinkShown = false;
var vxSidebarApplShown = false;
var vxFirstpageLoaded = true;
var vxAftermathLaunched = false;
var vxSufferHardandLong = false; // hope so
var vxInnerwidth = 0, vxInnerheight = 0;


function vxDebug(message){if(vxDebugSwitch)alert(message);}

function vxHackLayout(){	

	//Determination of size
  	if( typeof( window.innerWidth ) == 'number' ){
		vxInnerwidth = window.innerWidth; vxInnerheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
    	vxInnerwidth = document.documentElement.clientWidth;
		vxInnerheight= document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		vxInnerwidth = document.body.clientWidth;
		vxInnerheight = document.body.clientHeight;
	}
	
	vxHackLayoutCheck();

	// pageContentBox.
	
	with(document.getElementById('pageContentBox').style){
		height = (vxInnerheight - 85) + "px";
		if(vxSufferHardandLong) width = (vxInnerwidth > 1240)? (1240 + "px") : (vxInnerwidth + "px");
	}
	
	with(document.getElementById('pageMaindivision').style){
		height = (vxInnerheight - 200) + "px";
		width = (vxInnerwidth > 1240)? "940px" : ((vxInnerwidth - 300) + "px");
	}
	
	with(document.getElementById('pageGuidance').style){
		left = (vxInnerwidth > 1240)? (((vxInnerwidth - 1240)/2) + "px") : "0px";
	}
	
	
	
	if(document.getElementById('titleBox')) with(document.getElementById('titleBox').style){
			width = (vxInnerwidth > 1240)? "940px" : ((vxInnerwidth - 300) + "px");
			
			if(vxSufferHardandLong){
				position = "absolute";
				top = "60px";
				right = (vxInnerwidth > 1240)? ((vxInnerwidth - 1240)/2) + "px" : "0px";
			}
	}
	
	with(document.getElementById('loadingIndicator').style){
		right = (vxInnerwidth > 1240)? ((vxInnerwidth - 1240)/2) + "px" : "0px";
	}
	vxHackLayoutCheck();
}

function vxHackLayoutCheck(){
	vxDebug('vxHackLayout: the document is ' + vxInnerwidth + ' x ' + vxInnerheight);
	vxDebug('vxHackLayout: pageMaindivision is ' + document.getElementById('pageMaindivision').style.width + ' x ' + document.getElementById('pageMaindivision').style.height);
	vxDebug('vxHackLayout: pageContentBox is ' + document.getElementById('pageContentBox').style.width + ' x ' + document.getElementById('pageContentBox').style.height);
}



// INITIALIZATION - DOM: vxDomInit / BODY: vxInitialize

function vxInitialize(){
	if (navigator.appName == "Microsoft Internet Explorer"){
		vxSufferHardandLong = true;
		vxDebug ('Damn IE goes to inferno.');
	}
	
	vxHackLayout();
}	

function vxDoorpageLoad(){
	vxDebug('vxDoorpageLoad: called.  componentDoorpage is loaded.');
	vxDoorpageLoaded = true;
	vxAftermath();
}

function vxSidebarLoad(){	//called upon sidebar finished loading	
	vxDebug('vxSidebarLoad: called.  componentLogo is loaded.');
	vxSidebarLoaded = true;
	vxAftermath();
}

function vxAftermath(){ // called by vxinitalizeDoorpage and vxInitializeSidebar.  If the two components were both loaded, run the initialization.
	if (vxAftermathLaunched) return;
	if (vxSidebarLoaded && vxDoorpageLoaded){
		//We could do something now.
		vxDebug('vxAftermath: called.  Both componentDoorpage and componentLogo were loaded.');
		
		if(vxShowDoorpage){
			vxDebug('vxAftermath: vxShowDoorpage is true.  Animating.'); 
			vxDefineFlash('componentDoorpage').Play(); // Flash will call vxShowSidebar.
		}else{
			vxRemoveDoorpage();
			vxPage('about'); // This shall be traced by RHS.  If the user enters with another anchor, this param shall differ.
			vxShowSidebar();
		}
		
		vxAftermathLaunched = true;	
	}
}

function vxShowSidebar(){ // be called from componentDoorpage or vxInitialize
	vxDebug('vxShowSidebar: started');
	vxDefineFlash("componentLogo").Play();
	new Effect.Appear('guideTextBox', { duration: 1.0 });
}

function vxRemoveDoorpage(){
	document.getElementById('pageContentBox').removeChild(document.getElementById('pageDoorwayBox'));
	vxShowDoorpage = false;
}

// FLASH CONTROL AND DOORPAGE BEHAVIORS

function vxDefineFlash(movieName){
	if (window.document[movieName]) return window.document[movieName];
 	if (navigator.appName.indexOf("Microsoft Internet")==-1){
    	if (document.embeds && document.embeds[movieName]) return document.embeds[movieName];
	} else {
    	return document.getElementById(movieName);
  	}
}

// BEHAVIOR OF SLIDING BOXES ON THE LEFT

function vxToggleSidebarLink(){
	vxSidebarInstanceFetch();
	if (!vxSidebarLinkShown && !vxSidebarApplShown){
		Effect.SlideDown('linkBox', { duration: 0.4 });
	}else if (vxSidebarLinkShown && !vxSidebarApplShown){
		Effect.SlideUp('linkBox', { duration: 0.2 });
	}else if (!vxSidebarLinkShown && vxSidebarApplShown){
		Effect.SlideDown('linkBox', { duration: 0.4 });
		Effect.SlideUp('applBox', { duration: 0.2 });
	}
}

function vxToggleSidebarAppl(){
	vxSidebarInstanceFetch();
	if (!vxSidebarLinkShown && !vxSidebarApplShown){
		Effect.SlideDown('applBox', { duration: 0.4 });
	}else if (!vxSidebarLinkShown && vxSidebarApplShown){
		Effect.SlideUp('applBox', { duration: 0.2 });
	}else if (vxSidebarLinkShown && !vxSidebarApplShown){
		Effect.SlideDown('applBox', { duration: 0.4 });
		Effect.SlideUp('linkBox', { duration: 0.2 });
	}
}

function vxSidebarInstanceFetch(){
	vxSidebarApplShown = (document.getElementById('applBox').style.display == 'none')?false:true;
	vxSidebarLinkShown = (document.getElementById('linkBox').style.display == 'none')?false:true;
}

/*Effect.SlideDown('linkBox', { duration: 0.5 });Effect.SlideUp('applBox', { duration: 0.5 });*/

// AJAX PAGE LOADING

function vxPage(pagename){
	vxDebug('vxPage: load ' + pagename);
	ajaxpage('siteResources/contents/' + pagename + '.html', 'pageMaindivision');
}

// HISTORY FRAMEWORK - integrate RSH!

// SANDBOX










// AJAX LOAD FRAMEWORK

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
	
	Effect.Appear('loadingIndicator');

	var page_request = false;
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
		else if (window.ActiveXObject){ // if IE
			try { page_request = new ActiveXObject("Msxml2.XMLHTTP")}
			catch (e){ try{ page_request = new ActiveXObject("Microsoft.XMLHTTP")}
				catch (e){}}
		}else return false;
	
	
	page_request.onreadystatechange=function(){ loadpage(page_request, containerid);}
	pageTracker._trackPageview(url);
	
	if (bustcachevar) bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	page_request.open('GET', url+bustcacheparameter, true);
	page_request.send(null);
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) with(document.getElementById(containerid)){
		scrollTop = 0;
		innerHTML=page_request.responseText;
		Effect.Fade('loadingIndicator');
		vxHackLayout();
	}
}

function loadobjs(){
	if (!document.getElementById) return;
	for (i=0; i<arguments.length; i++){
		var file=arguments[i];
		var fileref="";
		if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			} else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects+=file+" " //Remember this object as being already added to page
		}
	}
}
