// 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	
	layer_bottom = document.getElementById('bottomLayer');
	layer_bottom.style.visibility = "visible";
	layer_bottom.style.display = "none";
	layer_bottom.style.height = getPageHeight() + "px"; //getWindowHeight()
	layer_bottom.style.width  = getPageWidth() + "px"; //getWindowWidth()
}

//----------------------------------------------------------
			
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;
	var scrollHeight = document.body.scrollHeight;
	var offsetHeight = document.body.offsetHeight;
	alert(scrollHeight + " " + 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 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 = getViewportWidth(); // document.body.scrollWidth;
	} else { // Explorer Mac, Explorer 6 Strict, Mozilla and Safari
		pageWidth = getViewportWidth(); //document.body.offsetWidth; 
	}
	return pageWidth;
}	

getViewportWidth = function() {
  var width = 0;
  if( document.documentElement && document.documentElement.clientWidth ) {
    width = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth ) {
    width = document.body.clientWidth;
  }
  else if( window.innerWidth ) {
    width = window.innerWidth - 18;
  }
  return width;
};	

//----------------------------------------------------------
			
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--------------------------------

function setLyr(obj,lyr,xMove,yMove,controlToFocus)
{
	//if(!layer_bottom) return; // onLoad was not loaded yet
	if(!layer_bottom){
		onLoad();
	}
	layer_top = document.getElementById(lyr);
	layer_top.style.visibility = "visible";
    layer_top.style.display = "none";
	
	var newX = 0;
	var newY = 0;
	if(obj != window){
		newX = findPosX(obj);
		newY = findPosY(obj);
	}
	if (lyr.substring(0, 8) == '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';
	} 
	
	layer_bottom.style.display = "block";
	window.setTimeout(function () {
		setTopLayerVisible();
		if (controlToFocus != null) {
			document.getElementById(controlToFocus).focus();
		}
	}, 1);
}

function setLyrCentered(obj,lyr,width,top,controlToFocus) {
	//if(!layer_bottom) return; // onLoad was not loaded yet
	if(!layer_bottom){
		onLoad();
	}
	layer_top = document.getElementById(lyr);
	layer_top.style.visibility = "visible";
	layer_top.style.display = "none";	

	hideSelects();
	
	var pageWidth = getPageWidth();
	var pageHeight = getWindowHeight();
	var divTop = Math.max((pageHeight - $(lyr).getDimensions().height)/2, 0);
	var divLeft = Math.max((pageWidth - $(lyr).getDimensions().width)/2, 0);

	layer_top.style.top = divTop + 'px';
	layer_top.style.left = divLeft + 'px';
	
	//layer_top.style.top = (pageHeight - $(lyr).getDimensions().height)/2 + 'px';
	//layer_top.style.left = (pageWidth - $(lyr).getDimensions().width)/2 + 'px';
	
	layer_bottom.style.display = "block";
	window.setTimeout(function () {
		setTopLayerVisible();
		if (controlToFocus != null) {
			document.getElementById(controlToFocus).focus();
		}
	}, 1);
}

function scrollToCenteredLyr(obj,lyr,width,top,controlToFocus) {
	setLyrCentered(obj,lyr,width,top,controlToFocus);
	$(lyr).scrollTo();
}

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;
	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";
	if(typeof(window.__thumbMmg)==="object" && templateControls.length > 1) window.__thumbMmg.setFrameInvisible = true;
}

function setLayerOff() {
	layer_bottom.style.display = "none";
	layer_top.style.display = "none";
	showSelects();
	if(typeof(window.__thumbMmg)==="object" && templateControls.length > 1) window.__thumbMmg.selectionFrame.setVisible(true);
}


// 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;
}