// JavaScript Document

var layer_bottom;
var layer_top;

//--------------------------INITIALIZE VARIABLES AND GET PAGE HEIGHT--------------------------------

function onLoad() {
	// load/initialise variables in the HTML doc so can be read
	layer_bottom = document.getElementById('bottomLayer');
	layer_top = document.getElementById('topLayer');
	layer_bottom.style.height = getPageHeight() + "px"; //getWindowHeight()
	layer_bottom.style.width = getPageWidth() + "px"; //getWindowWidth()
} 

//----------------------------------------------------------
			
window.onresize = resizeBottomLayer;

function resizeBottomLayer()
{
	layer_bottom = document.getElementById('bottomLayer');
	layer_bottom.style.height = getPageHeight() + "px";
	layer_bottom.style.width  = getPageWidth() + "px"; 
}

function getPageHeight() {
	var pageHeight;
	var scrollHeight = document.body.scrollHeight;
	var offsetHeight = document.body.offsetHeight;
	if (scrollHeight >= offsetHeight) {
		pageHeight = document.body.scrollHeight;
	} else { // Explorer Mac, Explorer 6 Strict, Mozilla and Safari
		pageHeight = document.body.offsetHeight;
	}
	var windowHeight = getWindowHeight();	
	if (windowHeight > pageHeight) {
		return windowHeight;
	} else {
		return pageHeight;
	}
}

function getPageWidth() {
	var pageWidth;
	var scrollHeight = document.body.scrollHeight;
	var offsetHeight = document.body.offsetHeight;
	if (scrollHeight >= offsetHeight) {
		pageWidth = document.body.scrollWidth;
	} else { // Explorer Mac, Explorer 6 Strict, Mozilla and Safari
		pageWidth = document.body.offsetWidth; 
	}
	return pageWidth;
}			

//----------------------------------------------------------

function getWindowHeight() {
	var windowHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
	    windowHeight = window.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	    //IE 6+ in 'standards compliant mode'
	    windowHeight = document.documentElement.clientHeight;
	} else if( document.body && document.body.clientHeight ) {
		//IE 4 compatible
		windowHeight = document.body.clientHeight;
  }
  return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
	}
	return windowWidth;
}

var _info		= navigator.userAgent;
var _isSafari	= (_info.indexOf("Safari") >= 0);

function formSubmit(submitURL) { 
	document.uploadForm.action = submitURL; 
	document.uploadForm.submit(); 
	
	// Reload your animated GIF image (otherwise GIF doesn't animate in IE)
	if (document.images){
		animatedGif = new Image(); 
		animatedGif.src = "../images/popup/progress_dial.gif"; 
		if (!_isSafari) { // Safari doesn't like this IE hack
			document.getElementById('animated_gif').src = animatedGif.src;
		}
	}
	
	return;
} 

function formSubmitContest(submitURL) { 
	document.uploadForm.action = submitURL; 
	document.uploadForm.submit(); 
	
	// Reload your animated GIF image (otherwise GIF doesn't animate in IE)
	if (document.images){
		animatedGif = new Image(); 
		animatedGif.src = "../images_v2/contest/popup_animated_arrows.gif"; 
		if (!_isSafari) { // Safari doesn't like this IE hack
			document['animated_gif'].src = animatedGif.src;
		}
	}
	
	return;
} 

//--------------------------GET CLICK POSITION--------------------------------

function setLyr(obj,lyr,xMove,yMove)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	if (lyr == 'topLayer'){
		newY += 20;
		hideSelects();
	}
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	
	if (xMove != 0) {
		x.style.left = newX + xMove + 'px';
	}else {
		x.style.left = newX + 'px';
	}
	
	if (yMove != 0) {
		x.style.top = newY + yMove + 'px';
	}else {
		x.style.top = newY + 'px';
	}
	
	setLayerOn();
}

function hideSelects()
{
	var selects = document.getElementsByTagName('select');
	for (x = 0; x <	selects.length; x++) {
	selects[x].style.visibility = 'hidden';
	}
}

function showSelects()
{
	var selects = document.getElementsByTagName('select');
	for (x = 0; x <	selects.length; x++) {
	selects[x].style.visibility = 'visible';
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	window.status = printstring;
	return curtop;
}


function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.topLayer.layers[name];
	    this.style = document.layers.topLayer.layers[name];
	   }
 }
}

function setLayerOn() {
	layer_bottom.style.visibility = "visible";
	layer_top.style.visibility = "visible";
}

function setLayerOff() {
	layer_bottom.style.visibility = "hidden";
	layer_top.style.visibility = "hidden";
	showSelects();
}


