var loadStatus=false;
var canHide=false;
var mouseExited=true;
var bOverlayW=0;
var bOverlayH=0;
var menuCustomPos;
var menuPosX=0;
var menuPosY=0;
var xScroll, yScroll,pageHeight,pageWidth;
$(document).ready(function() {updateDim();});
function findImage(name, doc) {
	var i, img;
	for (i = 0; i < doc.images.length; i++) {
    	if (doc.images[i].name == name) {
			return doc.images[i];
		}
	}
	for (i = 0; i < doc.layers.length; i++) {
    	if ((img = findImage(name, doc.layers[i].document)) != null) {
			img.container = doc.layers[i];
			return img;
    	}
	}
	return null;
}
function getImage(name) {
	if (document.layers) {
    	return findImage(name, document);
	}
	return null;
}
function getImageWidth(myImage) {
	var x, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.width;
	} else {
		return getElementWidth(myImage);
	}
	return -1;
}
function getImageHeight(myImage) {
	var y, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.height;
	} else {
		return getElementHeight(myImage);
	}
	return -1;
}
function getElementHeight(Elem) {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetHeight;
		return xPos;
}

function getElementWidth(Elem) {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetWidth;
		return xPos;
}
function pw() {
	return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
}; 
function mouseX(evt) {
	return evt.clientX ? evt.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) : evt.pageX;
} 
function mouseY(evt) {
	return evt.clientY ? evt.clientY + (document.documentElement.scrollTop || document.body.scrollTop) : evt.pageY
} 
function popDown(oi) {
	var wp = pw(); 
	dm = document.getElementById(oi); 
	ds = dm.style; st = ds.visibility; 
	ds.visibility = "hidden"; 
}
function popUp(evt,oi) {
	if (document.getElementById) {
		var wp = pw();		
		dm = document.getElementById(oi); 
		ds = dm.style; st = ds.visibility; 
		if (dm.offsetWidth) 
			ew = dm.offsetWidth; 
		else if (dm.clip.width) 
			ew = dm.clip.width; 
		if (st == "visible" || st == "show") { 
			ds.visibility = "hidden"; 
		} 
		else {
			tv = mouseY(evt) + 20; lv = mouseX(evt) - (ew/4); 
			if (lv < 2) 
				lv = 2; 
			else if (lv + ew > wp) 
				lv -= ew/2;
			lv += 'px';tv += 'px';  
			ds.left = lv; 
			ds.top = tv;
			//alert('day1');
			if(menuCustomPos==true) {
				updateDim();
				ds.left=(((pageWidth/2.0)-(616/2.0))+25)+"px";
				//alert('day');
				ds.top="20px";
			}
			else {
				var imgOffset=$("#pageImage").offset({relativeTo: "body"});
				var pgImgW=getImageWidth("pageImage");
				var pgImgH=getImageHeight("pageImage");					
				ds.left=((imgOffset.left + (pgImgW/2.0)-(616/2.0))+16)+"px";
				ds.top = (imgOffset.top+20)+"px";			
			}
			ds.visibility = "visible";
		}
	}
}
function menuTriggerStart(event) {
	popDown('swissCommMenu');
	popUp(event,'swissCommMenu');
	mouseExited=false;
	canHide=false;	
}
function menuTriggerEnd() {
	mouseExited=true;
	setTimeout("checkAndHide();",500);
}
function checkAndHide() {
	canHide=true;
	if(mouseExited==true && canHide==true) {
		popDown('swissCommMenu');
		//alert('passed');
	}
	else {
		setTimeout('checkAndHide();',500);
		//alert('failed');
	}
}
function updateDim() {	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}		
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	$('.black_overlay').css({'height':pageHeight+"px"});
	$('#menuDIV').css({'left':((pageWidth-124)/2)+"px"});	
	$('#menuDIV').css({'display':"block"});	
}
function checkSet() {
	if(loadStatus==true) {
			$('.black_overlay').css({'display':'none'});
			$('.white_content').css({'display':'none'});			
	}
	else {
			setTimeout('checkSet();',2000);			
	}
}
function updateOverlayDim() {
	updateDim();
	return;
/*
	if( typeof( window.innerWidth ) == 'number' ) {
		bOverlayW = window.innerWidth;
		bOverlayH = window.innerHeight; 
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
		//IE 6+ in 'standards compliant mode' 
		bOverlayW = document.documentElement.clientWidth; 
		bOverlayH = document.documentElement.clientHeight; 
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 
		//IE 4 compatible 
		bOverlayW = pageWidth; //document.body.clientWidth; 
		bOverlayH = pageHeight; //document.body.clientHeight;
	}
*/	
}
function bodyLoaded() {
	loadStatus=true;
	$(document).pngFix();
}
