/*********first order of business**********/
/* (fancy jiggery-pokery to set the width of the menu 
	using special loading events so that it is set 
	without having to wait for images to load */
/*****/
var preload = function(){
	var updateOthers = function(){$("project_content").style.left = $("topnavbox").clientWidth+$("navboxes").clientWidth+"px"; $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("project_content").style.left)+"px";};
	if ($("topnavbox").style.width > 100){
		var topnavboxStatus = "open";	
	} else {
		var topnavboxStatus = "closed";	
	}
	if (topnavboxStatus != GetCookie("topnavboxstatus")){
		//Effect.Shrink2("topnavbox",{direction:"top-left",duration:2.5,scaleTo:scaleAmount,scaleMode: { originalWidth: startW },afterUpdate:updateOthers});
		$("topnavbox").style.width = "170px";
	} else {
		$("topnavbox").style.width = "18px";
	}
	updateOthers();	
}

// Dean Edwards/Matthias Miller/John Resig

function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);

	// do stuff
	preload();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
Behaviour.addLoadEvent(init);
/***** end first order of business! *****/
function pageNavDefaults(){
		// link colours
		$("company_link").style.color = "#FFF";
		$("products_link").style.color = "#FFF";
		$("projects_link").style.color = "#FDF47C";	
		// nav boxes open and closed
		$("company_navbox").style.width = "0px";
		$("products_navbox").style.width = "0px";
		$("projects_navbox").style.width = "180px";
}
function loadStateReset(){
	pageNavDefaults();
	if (document.body && (initStateStatus >= 2)){
		var startW = parseInt($("topnavbox").style.width);
		if(startW>99){
			//alert("reset");
			var scaleAmount = (18/startW)*100;
// 			var updateOthers = function(){if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px";}};
// 			Effect.Shrink2("topnavbox",{direction:"top-left",duration:0.67,scaleTo:scaleAmount,scaleMode: { originalWidth: startW },afterUpdate:updateOthers});
			SetCookie("topnavboxstatus","closed");
                        $("topnavbox").style.width = (parseInt($("topnavbox").style.width) / scaleAmount) + 'px';
                        if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px"; $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("project_content").style.left)+"px";}
		}
		if(parseInt($("navboxes").clientWidth)>179){
// 			var updateOthers2 = function(){if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px";}};
// 			Effect.Shrink2("navboxes",{direction:"top-left",duration:0.67,scaleTo:((18/180)*100),scaleMode: { originalWidth: 180 },afterUpdate:updateOthers2});
                        $("navboxes").style.width = ((parseInt($("navboxes").style.width) || parseInt($("navboxes").clientWidth)) / 10) + 'px';
                        if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px"; $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("project_content").style.left)+"px";}
		} else if(parseInt($("navboxes").style.width)<1){
/*			var updateOthers2 = function(){if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px";}};*/
			$("company_navbox").style.width = "0px";
			$("products_navbox").style.width = "0px";
			$("projects_navbox").style.width = "180px";
// 			Effect.Shrink2("navboxes",{direction:"top-left",duration:0.67,scaleTo:((18/180)*100),scaleMode: { originalWidth: 180 },afterUpdate:updateOthers2});
                        $("navboxes").style.width = ((parseInt($("navboxes").style.width) || parseInt($("navboxes").clientWidth)) / 10) + 'px';
                        if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px"; $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("project_content").style.left)+"px";}
		}}
}
function rollOffTop(){
	pageNavDefaults();
	if (document.body && (initStateStatus >= 2)){
		var startW = parseInt($("topnavbox").style.width);
		if(startW>99){
			//alert("reset");
			var scaleAmount = (18/startW)*100;
// 			var updateOthers = function(){if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px";}};
// 			Effect.Shrink2("topnavbox",{direction:"top-left",duration:0.67,scaleTo:scaleAmount,scaleMode: { originalWidth: startW },afterUpdate:updateOthers});
                        $("topnavbox").style.width = (parseInt($("topnavbox").style.width) / scaleAmount) + 'px';
                        if ($("projectimage")){$("project_content").style.left = parseInt($("topnavbox").style.width)+parseInt($("navboxes").clientWidth)+"px";}
		}}
}
Behaviour.addLoadEvent(function() {
	pageNavDefaults();	
	if (document.body){
		var startW = 170;
		var scaleAmount = (18/startW)*100;
		var updateOthers = function(){$("project_content").style.left = $("topnavbox").clientWidth+$("navboxes").clientWidth+"px"; $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("project_content").style.left)+"px";};
		var openFirstProj = function(){thumbClick("img1");initStateReset();}
		openFirstProj();
		//Effect.Shrink2("navboxes",{direction:"top-left",duration:2.5,scaleTo:((18/180)*100),scaleMode: { originalWidth: 180 },afterFinish:initStateReset,afterUpdate:updateOthers});
		if ($("topnavbox").style.width > 100){
			var topnavboxStatus = "open";	
		} else {
			var topnavboxStatus = "closed";	
		}
		if (topnavboxStatus != GetCookie("topnavboxstatus")){
			//Effect.Shrink2("topnavbox",{direction:"top-left",duration:2.5,scaleTo:scaleAmount,scaleMode: { originalWidth: startW },afterUpdate:updateOthers});
			$("topnavbox").style.width = "170px";
		} else {
			$("topnavbox").style.width = "18px";
		}
		updateOthers();
		initStateReset();
	}
		//	
});
function textHide(){
	if (($("hidetext").style.visibility == "hidden") && (parseInt($("project_textbox").style.width)<20)){
		$("project_textbox").style.width = "346px";
		//$("thumbs").style.left = "346px";
// 		Effect.Shrink2("project_textbox",{direction:"top-left",duration:0.5,scaleTo:1922.2,scaleMode: { originalWidth: 18 }});
// 		$("project_textbox").style.width = (parseInt($("project_textbox").style.width) / 1922.2) + 'px';
		$("hidetext").style.visibility = "visible";
	}	
}
function fadeNextImg(){
	var imgFadeStartInit = true;
	var onImgFadeStart = function(){
		if (imgFadeStartInit){
			$("projectimage_container2").style.visibility = "visible";
			$("projectimage_container_vvbg2").style.visibility = "visible";
			imgFadeStartInit = false;
		}
	}
	var onImgFadeComplete = function(){
		$("projectimage").src = $("projectimage2").src;
		$("projectimage_vvbg").src = $("projectimage_vvbg2").src;
		if (isIE){
			$("projectimage").style.filter = $("projectimage2").style.filter;
			$("projectimage_vvbg").style.filter = $("projectimage_vvbg2").style.filter;
			$("projectimage").style.width = $("projectimage2").width+"px";
			$("projectimage_vvbg").style.width = $("projectimage_vvbg2").width+"px";
			$("projectimage").style.height = $("projectimage2").height+"px";
			$("projectimage_vvbg").style.height = $("projectimage_vvbg2").height+"px";
		} else {
			$("projectimage").style.width = $("projectimage2").style.width;
			$("projectimage_vvbg").style.width = $("projectimage_vvbg2").style.width;
			$("projectimage").style.height = $("projectimage2").style.height;
			$("projectimage_vvbg").style.height = $("projectimage_vvbg2").style.height;
		}
		$("projectimage_container2").style.visibility = "hidden";
		$("projectimage_container_vvbg2").style.visibility = "hidden";
	}
// 	new Effect.Opacity('projectimage_container_vvbg2',{ duration: 0.75, fps:12, transition: Effect.Transitions.linear, from: 0.0, to: 1.0});
// 	new Effect.Opacity('projectimage_container2',{ duration: 0.75, fps:12, transition: Effect.Transitions.linear, from: 0.0, to: 1.0, afterUpdate:onImgFadeStart, beforeFinish:onImgFadeComplete});
//         onImgFadeStart();
        $("projectimage_container_vvbg2").style.opacity = 1;
        $("projectimage_container2").style.opacity = 1;
        onImgFadeComplete();
}
var imagesLoaded = " ";
delete projectImage;
var loadingcount;
function thumbClick(curImg){
	//Loading notification
	clearInterval(loadingcount);
	var loadtext = $("loadtext");
	if ($(curImg).className == "vv"){
		loadtext.style.left = $(curImg).clientWidth+"px";
	} else {
		loadtext.style.left = "100%";
	}
	loadtext.innerHTML = "Loading Image ...";
	$(curImg).parentNode.appendChild(loadtext);
	new Effect.Appear(loadtext);
	var count = 0;
	var addcount = function(){
		count++;
		loadtext.innerHTML = "Loading Image ..."+count;
		if ($(curImg).className == "vv"){
			loadtext.style.left = $(curImg).clientWidth+"px";
		}
	}
	loadingcount = setInterval(addcount,1000);
	$("projectimage_container2").style.visibility = "hidden";
	$("projectimage_container_vvbg2").style.visibility = "hidden";
	if (imagesLoaded != " "){
		for (i=1;i<=9;i++){
			if ($("img"+i)){
				var theElement = "img"+i;
				new Effect.Opacity(theElement,{ duration: 0.75, fps:12, transition: Effect.Transitions.linear, to:1.0});
			}
		}
	}
	if (isIE){
		var curImgSrc = $(curImg).lowsrc;
	} else {
		var curImgSrc = $(curImg).src;
	}
	curImgSrc = curImgSrc.slice(0,curImgSrc.lastIndexOf("_thumb"))+".jpg";
	var projectImage = new Image();
	projectImage.src = curImgSrc;
	projectImage.onload = function(){
		imagesLoaded += curImg;
		if (!isIE){
				$("projectimage2").src = curImgSrc;
		} else {
				var curImgGif = curImgSrc.slice(0,curImgSrc.lastIndexOf("."));
				$("projectimage2").src = curImgGif+".gif";
				$("projectimage2").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+curImgSrc+"', sizingMethod='scale')";	
		}
		new Effect.Opacity(curImg,{ duration: 0.75, fps:12, transition: Effect.Transitions.linear, to:0.5});
		new Effect.Fade(loadtext,{ duration: 0.75, fps:12, transition: Effect.Transitions.linear, to:0});
		clearInterval(loadingcount);
		curImgOrient = $(curImg).className;
		//
		//
		$("projectimage2").style.width="10px";
		$("projectimage2").style.height="10px";
		var fixCount = 0;
		function fixIt(){
			winW = (isIE) ? document.body.clientWidth : window.innerWidth;
			winH = (isIE) ? document.body.clientHeight : window.innerHeight;
			var imgW = $("projectimage2").width;
			var imgH = $("projectimage2").height;
			if (curImgOrient != "vv"){
				$("projectimage_vvbg2").src = "../images/spacer.gif";
				if (winH > imgH){
					$("projectimage2").style.width="auto";
					$("projectimage2").style.height="100%";
				} else if (winW > imgW){
					$("projectimage2").style.width=winW+"px";
					$("projectimage2").style.height="auto";
				}
			} else {
				if (isIE){
					$("projectimage_vvbg2").src = curImgGif+".gif";
					$("projectimage_vvbg2").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+curImgSrc+"', sizingMethod='scale')";
				} else {
					$("projectimage_vvbg2").src = curImgSrc;
				}
				$("projectimage2").style.height="100%";
				$("projectimage2").style.width="auto";
				$("projectimage_vvbg2").style.width=winW+"px";
				$("projectimage_vvbg2").style.height="auto";
			}
			fixCount++;
			if (fixCount > 2){fixCount = 0;clearInterval(fixImg);fadeNextImg();}
		}
		var fixImg = setInterval(fixIt,1);
	}
	if ((imagesLoaded.indexOf(curImg)!=-1) || projectImage.complete){
		projectImage.onload();
	}
}
var textboxOver = true;
var projrules = {
	'#project_content' : function(el){
		el.onmouseover = function(){
			if (!textboxOver){
				loadStateReset();
			}
		}
	},
	'#project_textbox' : function(el){
		el.onmouseover = function(){
			textboxOver = true;
		}
		el.onmouseout = function(){
			textboxOver = false;
		}
		el.onclick = function(){
			loadStateReset();
		}
	},
	'#project_textbox_topmargin' : function(el){
		el.onmouseover = function(){
			textHide()
		}
	},
	'#project_textbox_content' : function(el){
		el.onmouseover = function(){
			textHide()
		}
	},
	'#hidetext' : function(el){
		el.onmouseover = function(){
			$("hidetext_arrow").src="../images/left_arrow-over.gif";
		}
		el.onmouseout = function(){
			$("hidetext_arrow").src="../images/left_arrow.gif";	
		}
		el.onclick = function(){
                        //Effect.Shrink2("project_textbox",{direction:"top-left",duration:0.67,scaleTo:5.2,scaleMode: { originalWidth: 346 }});
			$("project_textbox").style.width = "18px";
			//$("thumbs").style.left = $("project_textbox").style.width;
			$("hidetext").style.visibility = "hidden";
			$("hidetext_arrow").src="../images/left_arrow.gif";	
		}
	},
	'img.hh' : function(el){
		el.onclick = function(){
			thumbClick(this.id);
		}
	},
	'img.vv' : function(el){
		el.onclick = function(){
			thumbClick(String(this.id));
		}
	}
};
Behaviour.register(projrules);
winResize();
