//
/******cookie functions******/
//
function getCookieVal (offset) {
         var endstr = document.cookie.indexOf (";", offset);
         if (endstr == -1) {
            endstr = document.cookie.length;
         }
         return unescape(document.cookie.substring(offset, endstr));
      }
function FixCookieDate (date) {
         var base = new Date(0);
         var skew = base.getTime(); // dawn of (Unix) time - should be 0
         if (skew > 0) {  // Except on the Mac - ahead of its time
            date.setTime (date.getTime() - skew);
         }
      }
function GetCookie (name) {
         var arg = name + "=";
         var alen = arg.length;
         var clen = document.cookie.length;
         var i = 0;
         while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg) {
               return getCookieVal (j);
            }
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) {
               break;
            }
         }
         return null;
      }
function SetCookie (name,value,expires,path,domain,secure) {
         document.cookie = name + "=" + escape (value) +
            ((expires) ? "; expires=" + expires.toGMTString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
      }
 function DeleteCookie (name,path,domain) {
         if (GetCookie(name)) {
            document.cookie = name + "=" +
               ((path) ? "; path=" + path : "") +
               ((domain) ? "; domain=" + domain : "") +
               "; expires=Thu, 01-Jan-70 00:00:01 GMT";
         }
      }
//
/*****detects and init*********/
//
var isIE = (navigator.appName.indexOf("Microsoft")!=-1);
var isIE7 = (isIE && window.XMLHttpRequest);
var isMac = (navigator.userAgent.indexOf("Mac") != -1);
if (isIE && isMac){document.location = "ie_mac_apology.html";}
var winW, winH;
var curImgOrient = "hh";
var curImgOrient2;
var pjImgW;
var vvbgW;
var projectimageWidthFix = function(){
		alert("called projectimageWidthFix");
};
var initStateStatus = 0;
function initStateReset(){
	initStateStatus++;
}
/********resize**********/
function winResize() {
	if (document.body){
		winW = (isIE) ? document.body.clientWidth : window.innerWidth;
		winH = (isIE) ? document.body.clientHeight : window.innerHeight;
		if ($("topnavbox").style.width!="18px"){
			if (winW<850){
				$("topnavbox").style.width="100px";
			} else {
				$("topnavbox").style.width="170px";
			}
                        if ($("product_content")){$("product_content").style.left = parseInt($("topnavbox").clientWidth)+parseInt($("navboxes").clientWidth)+"px"; $("product_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("product_content").style.left)+"px";}
                        if ($("project_content")){$("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 ($("company_content")){$("company_content").style.left = parseInt($("topnavbox").clientWidth)+parseInt($("navboxes").clientWidth)+"px"; $("company_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-parseInt($("company_content").style.left)+"px";}
		}
		var imgRatio = $("bgimage").width/$("bgimage").height;
		var winRatio = winW/winH;
		if (winRatio > imgRatio){
			if ($("bgimage").style.width!="100%"){
				$("bgimage").style.width="100%";
				$("bgimage").style.height="auto";
			}
		} else {
			if ($("bgimage").style.height!="100%"){
				$("bgimage").style.height="100%";
				$("bgimage").style.width="auto";
			}
		}
		if ($("projectimage")){
			var imgRatio = $("projectimage").width/$("projectimage").height;
			var winRatio = (winW-35)/winH;
			if (curImgOrient != "vv"){
				if (winRatio > imgRatio){
					//$("projectimage").style.width="100%";
					$("projectimage").style.width=(winW-35)+"px";
					$("projectimage").style.height="auto";
				} else {
					$("projectimage").style.height="100%";
					$("projectimage").style.width="auto";
				}
			} else {
				$("projectimage").style.height="100%";
				$("projectimage").style.width="auto";
				$("projectimage_vvbg").style.width=(winW-35)+"px";
				$("projectimage_vvbg").style.height="auto";
			}
		}
	}
}
window.onresize = function() {
	winResize();
}
/*******onload**********/
Behaviour.addLoadEvent(function() {
	bgImageLoader = new Image();
	bgImageLoader.onload = function(){
		if ($("topnavbox").style.width > 100){
			topnavboxStatus = "open";	
		} else {
			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";
		}
		if (!$("home_textbox")){
			var bgImgSrc = bgImageLoader.src;
			if (isIE){
				var bgImgGif = bgImgSrc.slice(0,bgImgSrc.lastIndexOf("."));
				$("bgimage").src = bgImgGif+".gif";
				$("bgimage").style.filter += "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+bgImgSrc+"', sizingMethod='scale')";
				$("imagebg").style.visibility = "visible";
			} else {
				$("bgimage").src = bgImgSrc;
			}
			window.onresize();
		} else {
			var completed = 0;
			var onloadComplete = function(){
				if (completed < 1) {
					var bgImgSrc = bgImageLoader.src;
					if (isIE){
						var bgImgGif = bgImgSrc.slice(0,bgImgSrc.lastIndexOf("."));
						$("bgimage").src = bgImgGif+".gif";
						$("bgimage").style.filter += "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+bgImgSrc+"', sizingMethod='scale')";
						$("imagebg").style.visibility = "visible";
					} else {
						$("bgimage").src = bgImgSrc;
					}
					window.onresize();
					
					completed++;
				}
			}
			//new Effect.Opacity('bgimage',{ duration: 0.5, fps:60,transition: Effect.Transitions.linear, from: 0.0, to: 1.0,afterUpdate:onloadComplete});
                        $('bgimage').style.opacity = 1;
                        onloadComplete();
		}
	}
	
	var filePath = $("logoimage").src.slice(0,$("logoimage").src.lastIndexOf("/"))+"/";
	bgImageLoader.src = filePath + "home83quality.jpg";
	if (isIE && !isIE7){$("logoimage").src = filePath + "spacer.gif";}
	if (isIE){
		for (t=0;t<10;t++){
			if ($("img"+t)){
				var curThumbSrc = $("img"+t).src;
				var curThumbGif = curThumbSrc.slice(0,curThumbSrc.lastIndexOf("_thumb"));
				$("img"+t).lowsrc = curThumbSrc;
				$("img"+t).src = curThumbGif+".gif";
				$("img"+t).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+curThumbSrc+"', sizingMethod='scale')";
			}
		}
	}
});
//
/*******etc********/
var homeNavBoxesOpen = 0;
var openNavBoxes = function(){
	if (!$("home_textbox")){
		if(parseInt($("navboxes").style.width)<19){
			var scaleAmount = (180/18)*100;
			var offsetAmount = "0px";
			var updateOthers = function(){
				offsetAmount = parseInt($("topnavbox").style.width)+parseInt($("navboxes").style.width);
				if ($("product_content")){
					$("product_content").style.left = offsetAmount+"px";
                                        $("product_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
				} else if ($("project_content")){
					$("project_content").style.left = offsetAmount+"px";
                                        $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
				} else if ($("company_content")){
					$("company_content").style.left = offsetAmount+"px";
                                        $("company_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
				}
			}
			if (initStateStatus >= 2){
				//Effect.Shrink2("navboxes",{direction:"top-left",duration:0.67,scaleTo:scaleAmount,scaleMode: { originalWidth: 18 },afterUpdate:updateOthers});
                                $('navboxes').style.width = '180px';
                                updateOthers();
			}
		}
	} else if (homeNavBoxesOpen<1){
		var scaleAmount = (180/1)*100;
		//Effect.Shrink2("navboxes",{direction:"top-left",duration:0.67,scaleTo:scaleAmount,scaleMode: { originalWidth: 1 }});
                $('navboxes').style.width = '180px';
		homeNavBoxesOpen = 1;
	}
}
/********/
/*
var mouseTrigger;
var mouseStopDelay = 50;
var mousemoving = false;
var curMenuOver;
var mouseStopped = function(){
	mousemoving = false;
	Event.stopObserving(window.document, "mousemove", eventHandlerFunction, false);
	curMenuOver.onmouseover();
}
var eventHandlerFunction = function(){
	mousemoving = true;
	clearTimeout(mouseTrigger);
	mouseTrigger = setTimeout("mouseStopped()",mouseStopDelay);
} 
var menuMouseMotion = function(){
	Event.observe(window.document, "mousemove", eventHandlerFunction, false); 
}*/
//Behaviours
var myrules = {
	'#topnavbox' : function(el){
		el.onmouseover = function(){
			SetCookie("topnavboxstatus","open");
			//$("topnavbox").style.width = (winW>850) ? "170px" : "100px";
			if(parseInt($("topnavbox").style.width)>0 && parseInt($("topnavbox").style.width)<19){
				var scaleAmount = (winW>850) ? (170/18)*100 : (100/18)*100;
				var offsetAmount = "0px";
				var updateOthers = function(){
					offsetAmount = parseInt($("topnavbox").clientWidth)+parseInt($("navboxes").clientWidth);
					if ($("product_content")){
						$("product_content").style.left = offsetAmount+"px";
                                                $("product_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
					} else if ($("project_content")){
						$("project_content").style.left = offsetAmount+"px";
                                                $("project_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
					} else if ($("company_content")){
						$("company_content").style.left = offsetAmount+"px";
                                                $("company_content").style.width = parseInt((isIE) ? document.body.clientWidth : window.innerWidth)-offsetAmount+"px";
					}
				}
				/*if (initStateStatus >= 2){*/
// 					Effect.Shrink2("topnavbox",{direction:"top-left",duration:0.67,scaleTo:scaleAmount,scaleMode: { originalWidth: 18 },afterUpdate:updateOthers});
                                        $('topnavbox').style.width = ((winW>850) ? 170 : 100)+'px';
                                        updateOthers();
				/*}*/
			}
		}
		el.onmouseout = function(){
			curMenuOver = this;	
		}
	},
	'#navboxes' : function(el){
		el.onmouseover = function(){
			openNavBoxes();
		}
	},
	'#home_textbox' : function(el){
		el.onmouseover = function(){
			loadStateReset();
		}
	}
};
Behaviour.register(myrules);
