// init vars
loaded=false;
hgLoad=false;
footerLoad=false;
historyLoaded=false;
thisID="";
thisPath="";
oldMenu=new Array();
oi="";
oldSection="";
oldHigh="";
oldThumb="";
hideLight=true;
trans={transition: Fx.Transitions.Expo.easeInOut};
trans2={transition: Fx.Transitions.Expo.easeInOut,duration: 'long'};
///
function firstRun(){
	//alert(hgLoad +" - "+ historyLoaded);
	if(hgLoad && historyLoaded && footerLoad){
		if(!loaded){
			el=document.getElementById('nav').getElementsByTagName('div');
			for(e=0;e<el.length;e++){
				thisDiv=String(el.item(e).id);
				if(thisDiv.substr(thisDiv.length-3)=="sub"){
					el.item(e).style.height="0px";
				}
			}
			thisPath=zHistory.getValue().substr(1);
			if(thisPath==""){
				thisPath="mercury/about_us";
				goto(thisPath);
			}
			document.getElementById('nav').style.visibility="visible";
			var body = $$('body')[0];
			body.addEvent('mousemove',tracer);
			loaded=true;
		}
	}else{
		setTimeout("firstRun()",500);
	}
}
function goto(p){
	thisPath=p;
	thisID=str_replace(thisPath,"/","-");
	highlight(thisID);
	resetNav();
	thisFile="assets/content/"+thisPath+".php";
	$('main').load(thisFile);
	sections=thisID.split("-");
	section=sections[0];
	if(oldSection!=section){
		setTimeout("updateSection()",300);
	}
	oldSection=section;
	zHistory.setValue(thisPath);
	zHistory.setTitle("Mercury Studio - 248.414.7300 | "+getTitle(thisID));
}
function updateSection(){
	document.getElementById('HG').updateSection(section);
	document.getElementById('foot').updateSection(section);
}
function updateScroller(){
	document.getElementById('mainBG').style.height=$('main').getHeight()+100;
}
function contentLoaded(){
	fadein();
	updateScroller();
}
zHistory.onChange = function() {
    lastPath=zHistory.getValue().substr(1);
	if(lastPath!=thisPath){
		fadeout();
		setTimeout("goto(lastPath)",500);
	}
}
zHistory.onInit = function() {
	historyLoaded=true;
}
function hgLoaded(){
	hgLoad=true;
}
function footerLoaded(){
	footerLoad=true;
}
function openNav(nav){
	//open the selected menu
	n1=0;
	menu=nav.split("-");
	if(menu[0]!=undefined){
		thisSub=document.getElementById(menu[0]+'-sub');
		if(thisSub!=null){
			n0=getLength(document.getElementById(menu[0]+'-sub').childNodes);
			$(menu[0]+'-sub').set('tween', trans);
			$(menu[0]+'-sub').tween('height', (n0*24)+'px');
		}
	}
	//close the last menu
	if(oldMenu[0]!=undefined && oldMenu[0]!=menu[0]){
		thisOldSub=document.getElementById(oldMenu[0]+'-sub');
		if(thisOldSub!=null){
			$(oldMenu[0]+'-sub').set('tween', trans);
			$(oldMenu[0]+'-sub').tween('height', '0px');
		}
	}
	//store the last menu
	oldMenu=menu;
}
function resetNav(){
	if(thisID != undefined){
		s1=thisID.split("-");
		s1.pop();
		s2=s1.join("-");
		openNav(s2);
	}
}
function hideNavBlocker(){
	document.getElementById('navBlocker').style.display="none";
	document.getElementById('contentBlocker').style.display="block";
}
function showNavBlocker(){
	document.getElementById('navBlocker').style.display="block";
	document.getElementById('contentBlocker').style.display="none";
	if(loaded){
		resetNav();
	}
}
function highlight(i){
	high=i.split("-");
	if(oi!="" && oi!=i){
		if(oldHigh.length>1){
			$(oi).morph('.'+oldHigh[0]+'_sub_off');
			//alert('.'+oldHigh[0]+'_sub_off');
		}
		$(oldHigh[0]+"Butt").morph('.'+oldHigh[0]+'_off');
	}
	if(high.length>1){
		$(i).morph('.'+high[0]+'_sub_on');
	}
	$(high[0]+"Butt").morph('.'+high[0]+'_on');
	oi=i;
	oldHigh=high;
}
function getLength(obj){
	kids=new Array();
	for(i=0;i<obj.length;i++){
		if(obj[i].nodeType==1){
			kids.push(obj[i]);
		}
	}
	return kids.length;
}
function getTitle(t){
	words=str_replace(t,"-"," | ");
	words=str_replace(words,"_"," ");
	return camelCase(words);
}
function camelCase(t) {
        cc = '';
        t = t.split(' ');
        for(var c=0; c < t.length; c++) {
                cc += t[c].substring(0,1).toUpperCase() + t[c].substring(1,t[c].length) + ' ';
        }
        return(cc);
}
function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}
//animations
function fadeout(){
	$('main').set('tween', {duration: 500});
	$('main').fade('out');
}
function fadein(){
	$('main').set('tween', {duration: 500});
	$('main').fade('hide');
	$('main').fade('in');
}
function showShowcase(p){
	//reset
	oldThumb="";
	hideLight=false;
	$('shadowBlock').fade('hide');
	$('lightBox').fade('hide');
	//title
	thisTitle=$('title'+p).get('html');
	$('showcaseHeader').set('html',thisTitle);
	//images
	thisThumbs=$('thumbs'+p).get('html');
	thumbs=thisThumbs.split(",");
	thumbHTML="";
	bodyHTML="";
	for(i=0;i<thumbs.length;i++){
		thumbHTML='<div id="thumb'+i+'" class="showcaseThumb" onclick="updateThumb('+i+')"></div>'+thumbHTML;
		bodyHTML+='<div class="showcaseWrap"><img src="assets/images/showcase/large/'+thumbs[i]+'" width="607" height="302" /></div>';
	}
	if(thumbs.length==1){
		thumbHTML='<div id="thumb0"></div>';
	}
	$('showcaseThumbs').set('html',thumbHTML);
	$('showcaseImage').set('html',bodyHTML);
	$('showcaseImage').style.left="607px";
	updateThumb(0);
	//link
	thisHref=$('href'+p).get('html');
	$('showcaseLink').set('html','<a href="'+thisHref+'" target="_blank">'+thisHref+'</a>');
	//fade in
	$('shadowBlock').fade(.90);
	setTimeout("showLightBox()",600);
}
function showLightBox(){
	if(!hideLight){
		$('lightBox').fade('in');
	}
}
function updateThumb(i){
	$('showcaseImage').set('tween',trans2);
	$('showcaseImage').tween('left',i*-620+"px");
	$('thumb'+i).tween('background-color','#CCC');
	if(oldThumb!="" && oldThumb != ("thumb"+i)){
		$(oldThumb).tween('background-color','#111');
	}
	oldThumb='thumb'+i;
}
function hideShowcase(){
	hideLight=true;
	$('shadowBlock').fade('out');
	$('lightBox').fade('out');
}
function tracer(e){
	var posX = e.client.x; 
	var posY = e.client.y;
	document.getElementById('HG').tracer(posX,posY);
}
