﻿// Functions to show and hide the email form
var PopUpScreen = null;
var PopUpDialog = null;
var PopUpIsShown = false;

window.onresize = startUp;

function startUp(){
	
	PopUpScreen = getRefToDiv('GMEmailThisPage_PopUpScreen');
	PopUpDialog = getRefToDiv('GMEmailThisPage_Dialog');
	//get a reference as above ...
	if( !PopUpScreen ) {
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}else{
		//now we have a reference to it
		if( PopUpScreen.style ) {
			//DOM & proprietary DOM
			PopUpScreen.style.height = document.body.scrollHeight;
			PopUpScreen.style.width = document.body.scrollHeight;
			
			PopUpDialog.style.height = GM5EmailPageDialogHeight;
			PopUpDialog.style.width = GM5EmailPageDialogWidth;
		} else {
			//layers syntax
			PopUpScreen.height = document.body.scrollHeight;
			PopUpScreen.width = document.body.scrollHeight;
			
			PopUpDialog.height = GM5EmailPageDialogHeight;
			PopUpDialog.width = GM5EmailPageDialogWidth;
		}
	}
}


function getRefToDiv(divID,oDoc) {
	if( document.getElementById ) {
		return document.getElementById(divID); 
	}
	
	if( document.all ) {
		return document.all[divID];
	}
	
	if( !oDoc ) {
		oDoc = document;
	}
	if( document.layers ) {
		if( oDoc.layers[divID] ) {
			return oDoc.layers[divID]; 
		} else {
			//repeatedly run through all child layers
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				//on success, return that layer, else return nothing
				y = getRefToDiv(divID,oDoc.layers[x].document); 
			}
			return y; 
		} 
	}
	return false;
}
function showEmailForm(height,width) {
	//get a reference as above ...
	
	PopUpScreen = getRefToDiv('GMEmailThisPage_PopUpScreen');
	PopUpDialog = getRefToDiv('GMEmailThisPage_Dialog');
	if( !PopUpScreen ) {
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	PopUpIsShown = true;
	//now we have a reference to it
	if( PopUpScreen.style ) {		
		//DOM & proprietary DOM
		setMaskSize();
		centerDialog(GM5EmailPageDialogHeight,GM5EmailPageDialogWidth)
		PopUpScreen.style.visibility = 'visible';
		PopUpDialog.style.visibility = 'visible';
	} else {
		//layers syntax
		PopUpScreen.height=document.body.scrollHeight;
		PopUpScreen.visibility = 'show';
		PopUpDialog.visibility = 'show';
	}
}

function hideEmailForm() {
	//get a reference as above ...
	
	PopUpScreen = getRefToDiv('GMEmailThisPage_PopUpScreen');
	PopUpDialog = getRefToDiv('GMEmailThisPage_Dialog');
	if( !PopUpScreen ) {
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	//now we have a reference to it
	if( PopUpScreen.style ) {
		//DOM & proprietary DOM
		PopUpScreen.style.visibility = 'hidden';
		PopUpDialog.style.visibility = 'hidden';
	} else {
		//layers syntax
		PopUpScreen.visibility = 'hide';
		PopUpDialog.visibility = 'hide';
	}
	PopUpIsShown = false;
}

function setMaskSize() {

	PopUpScreen = getRefToDiv('GMEmailThisPage_PopUpScreen');
	var theBody = document.body;
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	PopUpScreen.style.height = popHeight + "px";
	PopUpScreen.style.width = popWidth + "px";
}

function centerDialog(width, height) {
	if (PopUpIsShown) {
		if (width == null || isNaN(width)) {
			width = PopUpDialog.offsetWidth;
		}
		if (height == null) {
			height = PopUpDialog.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.body;
		//theBody.style.overflow = "hidden";
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		//setMaskSize();
		
		//window.status = PopUpScreen.style.top + " " + PopUpScreen.style.left + " " + gi++;
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		PopUpDialog.style.top = ((scTop + (fullHeight / 2)) - GM5EmailPageDialogHeight/2) + "px";
		PopUpDialog.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + PopUpDialog.style.left);
	}
}
/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}