/**
 * postit.functions.js
 * 
 * Various functions to allow screen position to be determined.
 */


/**
 * Gets the number of pixels that the page has been scrolled from the top
 * 
 * @param e
 * @return int
 */
function getScrollY() 
{
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
	} else if( document.body && document.body.scrollTop) {
		scrOfY = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY;
}

/**
 * Gets the number of pixels that the page has been scrolled from the left
 * 
 * @param e
 * @return int
 */
function getScrollX() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof(window.pageYOffset ) == 'number' ) {
		scrOfX = window.pageXOffset;
	} else if(document.body && document.body.scrollLeft ) {
		scrOfX = document.body.scrollLeft;
	} else if(document.documentElement && document.documentElement.scrollLeft ) {
		scrOfX = document.documentElement.scrollLeft;
	}

	return scrOfX;
}


/**
 * Gets Y position of the mouse in pixels from the top of the page.
 * 
 * @param event e
 * @return int
 */
function getPosY(e){
	var ev = (!e)?window.event:e;//IE:Moz
	var posy;
	
	if (ev.pageY)//Moz
	{
		posy=ev.pageY;
	}
	else if(ev.clientY)//IE
	{
		posy =ev.clientY + getScrollY();
	}
	if(!posy)
	{
		return 0;
	}
	else
	{
		return posy;
	}
}

/**
 * Gets X position of the mouse in pixels from the left side of the page.
 * 
 * @param event e
 * @return int
 */
function getPosX(e){
	var ev = (!e)?window.event:e;//IE:Moz
	var posx;
	
	if (ev.pageX)//Moz
	{
		posx=ev.pageX;
	}
	else if(ev.clientX)//IE
	{
		posx=ev.clientX;
	}
	if(!posx)
	{
		return 0;
	}
	else
	{
		return posx;
	}
}



/**
 * Gets the height of the current page in pixels
 * 
 * @return int
 */
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

/**
 * Gets the width of the current page in pixels
 * 
 * @return int
 */
function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}


//tells if a query variable exists
function var_exists(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return 1;
    }
  } 
  return 0;
}


/**
 * Sets the Flyerviewer to be in the middle of the screen
 * 
 * @return int
 */
function centerContent(divID) {

	var windowHeight = getWindowHeight();
	var windowWidth = getWindowWidth();
	var contentElement = document.getElementById(divID);


	if (windowHeight > 0) {
		
		var contentHeight = contentElement.offsetHeight;
		var top = ((windowHeight / 2) - (contentHeight / 2)) + getScrollY();
		if(((windowHeight / 2) - (contentHeight / 2)) + getScrollY() < 0)
		{
			top = 0;
		}	
		
		if(top < 0)
		{
			top = 0;
		}
		
		contentElement.style.top =  top + 'px';
		
	}
	if(windowWidth > 0) {
		var contentWidth = contentElement.offsetWidth;
		var left =  ((windowWidth / 2) - (contentWidth / 2));
		if(((windowWidth / 2) - (contentWidth / 2)) < 0)
		{
			left = 0;
		}
			
		contentElement.style.left = left  + 'px';
	}
	
}
