// JavaScript Document

var layer_bottom;
var layer_top;
var page_height;

//--------------------------INITIALIZE VARIABLES AND GET PAGE HEIGHT--------------------------------

function onLoad() {
	// load/initialise variables in the HTML doc so can be read	
	documentLoaded = true; 
	layer_bottom = document.getElementById('bottomLayer');
	layer_bottom.style.visibility = "hidden";
	layer_bottom.style.display = "none";
	layer_bottom.style.height = getPageHeight() + "px";
	layer_bottom.style.width  = getPageWidth() + "px";
}

//----------------------------------------------------------
			
window.onresize = resizeBottomLayer;

function resizeBottomLayer()
{
	layer_bottom = document.getElementById('bottomLayer');
	if (layer_bottom != null) {
		layer_bottom.style.height = getPageHeight() + "px";
		layer_bottom.style.width  = getPageWidth() + "px"; 
	}
}

function getPageHeight() {
	var pageHeight;
	if( window.innerHeight && window.scrollMaxY ) // Firefox 
	{
		pageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
		pageHeight = document.body.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		pageHeight = document.body.offsetHeight + document.body.offsetTop; 
	}
	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;
}

//--------------------------GET CLICK POSITION--------------------------------
var ctrlToFocus = null;
function setLyr(obj,lyr,width,top,controlToFocus) {
	if(!layer_bottom) return; // onLoad was not loaded yet
	layer_bottom.style.visibility = "visible";
	layer_bottom.style.display = "block";

	layer_top = document.getElementById(lyr);
	layer_top.style.visibility = "visible";
	//hideSelects();
	
	var pageWidth = getPageWidth();
	layer_top.style.top = top + 'px';
	layer_top.style.left = (pageWidth - width)/2 + 'px';
	
/*	xMove = 0;
	yMove = 0;
	var newX = 0;
	var newY = 0;
	if(obj != window){
		newX = findPosX(obj);
		newY = findPosY(obj);
	}

	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';
	}
	*/

	window.setTimeout(function () {
		setTopLayerVisible();
		if (controlToFocus != null) {
			var control = document.getElementById(controlToFocus);
			control.style.visibility = 'visible';
			ctrlToFocus = control;
			control.focus();
		}
	}, 1);
}

function setLyr2(obj, lyr, width, top, controlToFocus) {
    if (!layer_bottom) return; // onLoad was not loaded yet
    layer_bottom.style.visibility = "visible";
    layer_bottom.style.display = "block";

    layer_top = document.getElementById(lyr);
    layer_top.style.visibility = "visible";
    //hideSelects();

    var pageWidth = getPageWidth();
    if(document.documentElement)
        if (document.documentElement.scrollTop)
            top = parseInt(top) + parseInt(document.documentElement.scrollTop);
    layer_top.style.top = top + 'px';
    layer_top.style.left = (pageWidth - width) / 2 + 'px';

    window.setTimeout(function() {
        setTopLayerVisible();
        if (controlToFocus != null) {
            var control = document.getElementById(controlToFocus);
            control.style.visibility = 'visible';
            ctrlToFocus = control;
            control.focus();
        }
    }, 1);
}


function hideSelects() {
	var selects = document.getElementsByTagName('select');
	for (x = 0; x <	selects.length; x++) {
		if (selects[x].id.substring(0,11) != "qs_surface_") 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;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	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(xPos,yPos) {
	layer_bottom.style.display = "block";
	window.setTimeout("setTopLayerVisible();", 1);
}

function setTopLayerVisible() {
	layer_top.style.display = "block";
	layer_top.style.visibility = "visible";
	if(typeof(window.__thumbMmg)==="object" && templateControls.length > 1) window.__thumbMmg.setFrameInvisible = true;
}

function setLayerOff() {
	layer_bottom.style.visibility = 'hidden';
	layer_top.style.visibility = 'hidden';
	if(ua.indexOf('msie 8') < 0)
	{
		layer_bottom.style.display = 'none';
		layer_top.style.display = 'none';
	}
	if(ctrlToFocus != null)
		ctrlToFocus.style.visibility = 'hidden';
	showSelects();
	if(typeof(window.__thumbMmg)==="object" && templateControls.length > 1) window.__thumbMmg.selectionFrame.setVisible(true);
}

// specific for upload/contestupload pages.


var _info		= navigator.userAgent;
var _isSafari	= (_info.indexOf("Safari") >= 0);

function formSubmitUpload(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/loading.gif"; 
		if (!_isSafari) { // Safari doesn't like this IE hack
			document['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;
} 

// specific for move/copy album on prints_thumbs.

function setTransferType(transferType) {
	document.getElementById('transferType').value = transferType;
}

function ensureOneAlbumOrPhotoBookChecked(formname) {
	len = eval('document.forms.'+formname+'.elements.length');
    var i = 0;
    var count = 0;
    var mediaCount = 0;
    
    for (i = 0; i < len; i++) {
    
      var str1 = eval('document.forms.'+formname+'.elements['+i+']');
      var str2 = eval('document.forms.'+formname+'.elements['+i+'].id');
      if (str1.checked==1)
      {
          count++;
		  mediaCount += albumMediaCount[str2];
      }      
    }
	
	if (count > 1) {
			alert('You may only share one item at a time.');
			return false;
		}
   
	if (count == 0) {
			alert('Please select an item to share.');
			return false;
		}
		
	if (mediaCount == 0) {
			alert('The album you have selected has no media in it to share.');
			return false;
	}
 
    return true;
}

// specific to share page
function ensureOneAlbumChecked(formname) {
    len = eval('document.forms.'+formname+'.elements.length');
    var i = 0;
    var count = 0;
    var mediaCount = 0;
    
    for (i = 0; i < len; i++) {
    
      var str1 = eval('document.forms.'+formname+'.elements['+i+']');
      var str2 = eval('document.forms.'+formname+'.elements['+i+'].id');
      if (str1.checked==1)
      {
		  if (str2.substring(0, 7) != 'pb_col_') // bypass for photobooks
		  {
	          count++;
			  mediaCount += albumMediaCount[str2];
		  }
      }      
    }
	
	if (count > 1) {
			alert('You may only share one album at a time.');
			return false;
		}
   
	if (count == 0) {
			alert('Please select an album to share.');
			return false;
		}
		
	if (mediaCount == 0) {
			alert('The album you have selected has no media in it to share.');
			return false;
	}
 
    return true;
}

// specific to album main page
function ensureAtLeastOneAlbumChecked(formname) {
    len = eval('document.forms.'+formname+'.elements.length');
    var i = 0;
    var count = 0;
    var mediaCount = 0;
    
    for (i = 0; i < len; i++) {
    
      var str1 = eval('document.forms.'+formname+'.elements['+i+']');
      var str2 = eval('document.forms.'+formname+'.elements['+i+'].id');
      if (str1.checked==1)
      {
		  if (str2.substring(0, 7) != 'pb_col_') // bypass for photobooks
		  {
	          count++;
			  mediaCount += albumMediaCount[str2];
		  }
      }      
    }
	
	if (count > 1) {
			alert('Please select one album at a time.');
			return false;
		}
    return true;
}

// specific to album main page
function ensureAtLeastOneShareAlbumChecked(formname) {
    len = eval('document.forms.'+formname+'.elements.length');
    var i = 0;
    var count = 0;
    var mediaCount = 0;
    
    for (i = 0; i < len; i++) {
    
      var str1 = eval('document.forms.'+formname+'.elements['+i+']');
      var str2 = eval('document.forms.'+formname+'.elements['+i+'].id');
      if (str1.checked==1)
      {
		  if (str2.substring(0, 7) != 'pb_col_') // bypass for photobooks
		  {
	          count++;
			  mediaCount += albumMediaCount[str2];
		  }
      }      
    }
	
	if (count > 1) {
			alert('Please select one album.');
			return false;
		}
    return true;
}

// specific to prints thumbs page
function ensureAtLeastOneAlbumAddedToCart(formname) {
    len = eval('document.forms.'+formname+'.elements.length');
    var i = 0;
    var count = 0;
    
    for (i = 0; i < len; i++) {
    
      var str1 = eval('document.forms.'+formname+'.elements['+i+']');
      var str2 = eval('document.forms.'+formname+'.elements['+i+'].id');
      if (str1.checked==1)
      {
		  if (str2.substring(0, 7) != 'pb_col_') // bypass for photobooks
		  {
	          count++;
		  }
      }      
    }
	
	if (count == 0) {
			alert('Please select at least one album.');
			return false;
		}
    return true;
}