// JavaScript Document
// ===================================================================================
//
// Amplify Popup Functions
//
// File: popups.js
// Author: John Norton
// Version: 1.2.0
// Company: Amplify Studios
//
// Copyright: C 2008 Amplify Studios. All Rights Reserved.
//
// ===================================================================================
//
// How to Use:
//		showPopup( POPUP );
//		hidePopup( POPUP );
//			where POPUP is the name of the popup minus 'popup'.
//				ex. popupNotes you would pass showPopup( 'Notes' )
//      swchPop( NEXT POPUP );
//
// How To call A Popup On Page Load:
//		http://siteurl.com/somepage.html?popup=POPUP
//
// Requires:
//		scriptaculous librarys.
//
// Functions Contained in Package:
//  	gb();
//  	gd( STRING );
//  	gds( STRING );
// 		getGoing();
// 		getWindowSize( STRING );
//  	hidePop( STRING );
//  	hoverTime( STRING, BOOL );
//  	setPop( BOOL );
//  	showPop( STRING );
//  	swchPop( STRING );
//  	togPop( STRING, BOOL );
//  	v();
//
// Reserved Variables:
//		currPop
//		fadeDuration
//		fadeVal
//		IpopupTop
//		IpopupWidth
//
// ===================================================================================

// ===================================================================================
//                                VARIABLES TO EDIT
// ===================================================================================
// Values to modify. See comments for details

var IpopupWidth = 775;  //the width of the popup
var IpopupHeight = 550; //the height of the popup
var IpopupTop = 20;    //the height offset of the popup
var fadeDuration = 0.1; //length of the effects
var fadeVal = 0.8;		//transparency of the grey mask

// ===================================================================================
//                                DO NOT TOUCH BELOW
// ===================================================================================

var currPop = 'null';   //dont touch. used to maintin order.

function getGoing() { setPop( true ); }
Event.observe(window, 'load', getGoing, false);

// -----------------------------------------------------------------------------------
// Generic Functions
function gd( o ) { return document.getElementById( o ); }	        //a quick way to get an element by ID		
function gds( o ) { return gd( o ).style; }	                        //a quick way to refrence an object's style by ID
function gb() { return document.getElementsByTagName("body"); } 	//a quick way to get the body element
function v() { /*this function actualy does nothing at all....*/ }	//a quick way to void the href
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Set Popup (runs on resize aswell as for a first go)
function setPop( b )
{
	//Initialization Script
	if( b ) //Only runs once! To manualy force a rerun pass 'setPop( true );'
	{
	    //get body tag
		var bo = gb().item(0);
		bo.onresize = function() { setPop( false ); }   //add a resize script for super centering technology!

		var objS = document.createElement("div");
		objS.setAttribute('id','lightbox');
		//set styleish stuff
		objS.style.zIndex = 100;
		objS.style.position = 'absolute';
		objS.style.lineHeight = '0px';
		objS.style.left = '0px';
		objS.style.backgroundColor = '#000000';
		objS.style.display = 'none';
		objS.style.top = '0px';
		//plop the div in place
		bo.appendChild(objS);
		//just for the initial go
		gds( 'lightbox' ).width = getWindowSize( 'w' ) + "px";
		gds( 'lightbox' ).height = getWindowSize( 'h' ) + "px";
		//check to see if when they first visit the page they wanted a popup
	}
	//so we never get a box thats to small or to big
	if( currPop != 'null' )
	{
		gds( 'lightbox' ).width = getWindowSize( 'w' ) + "px";
		gds( 'lightbox' ).height = getWindowSize( 'h' ) + "px";
		IpopupHeight = ( currPop == "popupenews_signup" ) ? 286 : 550;
		var CenterAmt = (getScrollHeight() < IpopupHeight) ? ((getWindowSize('wh') - IpopupHeight)/2) : ((getScrollHeight() - IpopupHeight)/2);
		IpopupHeight = 550;
		gds( currPop ).top = (CenterAmt > 0) ? CenterAmt + "px" : 0 + "px";
		gds( currPop ).left = ((getWindowSize( 'w' ) - IpopupWidth)/2) + "px";
	}
		
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Toggle Popup Function
function togPop( o, b )
{
	if( b )
	{	//show popup
		currPop = o;
		//alert("Debug mode has been enabled. \nSorry for the inconvieninece.\nReport: " + getScrollHeight() );
		IpopupHeight = ( currPop == "popupenews_signup" ) ? 286 : 550;
		var CenterAmt = (((getWindowSize('wh') - IpopupHeight)/2) + getScrollHeight());
		IpopupHeight = 550;
		gds( currPop ).top = (CenterAmt > 0) ? CenterAmt + "px" : 0 + "px";
		
		//gds( currPop ).top = ( getScrollHeight() + IpopupTop ) + "px";
		gds( currPop ).left = ((getWindowSize( 'w' ) - IpopupWidth)/2) + "px";
		togLB( true );
		gds( o ).zIndex = 101; //so we dont have to do it in the css
		new Effect.Appear( o, { duration: fadeDuration, from: 0.0, to: 1.0 });
	}
	else
	{	//hide popup
		currPop = 'null';
		togLB( false );
		new Effect.Fade( o, { duration: fadeDuration, from: 1.0, to: 0.0 });
	}
}

//Switch the popups
function swchPop( n )
{	//This function does NOT remove the grey mask. it simply calls a new popup and hides the old one.
	//Grey mask STAYS UP.
	var c = currPop;
	currPop = 'popup' + n;
	gds( currPop ).top = ( getScrollHeight() + IpopupTop ) + "px";
	gds( currPop ).left = ((getWindowSize( 'w' ) - IpopupWidth)/2) + "px";
	gds( currPop ).zIndex = 101; //so we dont have to do it in the css
	new Effect.Fade( c, { duration: fadeDuration, from: 1.0, to: 0.0 });
	new Effect.Appear( currPop, { duration: fadeDuration, from: 0.0, to: 1.0 });
}

//Show Popup Function
function showPop( o ) 
{ 	//This is a simple go-between. you dont need it but it makes it easier to call popups.
	//See togPop for actual popup calls.
	if( currPop == 'null' )
	{	//Make sure a popup is not being called from a popup(that would mess things up)
		togPop( ('popup' + o), true );
	}
	else
	{	//If a popup is called from a popup this code runs so the grey mask stays.
		//Yay for error checking.
		swchPop( o );
	}
}

//Show Popup Function
function showPopup( o ) 
{ 	//This is a simple go-between. you dont need it but it makes it easier to call popups.
	//See togPop for actual popup calls.
	if( currPop == 'null' )
	{	//Make sure a popup is not being called from a popup(that would mess things up)
		togPop( ('popup' + o), true );
	}
	else
	{	//If a popup is called from a popup this code runs so the grey mask stays.
		//Yay for error checking.
		swchPop( o );
	}
}

//Hide Popup Function
function hidePopup( o ) 
{ 	//This is a simple go-between. you dont need it but it makes it easier to call popups.
	//See togPop for actual popup calls.
	if( o == 'popupTakeATour' )
	{
		//alert("debug enabled. sorry for the inconvienience");
		togPop(( o), false );
		
	}
	else
	{
		togPop( ('popup' + o) , false );
	}
}

//Show Product Tour Function
function showTour()
{	//This function uses flash variables to insert the flash file into the site then calls 
	//showPop() for the actual display function.
		var so = new SWFObject("http://www.sendlabs.com/Flash/TakeATour.swf", "tat", "775", "550", "9", "#000");
		so.addVariable("whereAt", "popupTakeATour" );
		so.addParam("wmode", "transparent");
		so.write("tourcontent");
		showPop( "TakeATour" );
}

//Toggle Lightbox Function
function togLB( b )
{	//Note: calling this function alone will result in an inacesable site. 
	//		This displays NO close buttons. Just the mask.
	if( b )
	{
		new Effect.Appear('lightbox', { duration: fadeDuration, from: 0.0, to: fadeVal });
	}
	else
	{
		new Effect.Fade('lightbox', { duration: fadeDuration, from: fadeVal, to: 0.0 });
	}
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Get Window Dimensions
function getWindowSize( m )
{   //m accepts 'w' = width , 'h' = height or 'wh' = window height(used to vert center popup)
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth + 20;
		yScroll = document.body.offsetHeight + 30;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth + 20;
		windowHeight = document.documentElement.clientHeight + 30;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth + 20;
		windowHeight = document.body.clientHeight + 30;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	if( m == 'w' )
	{
		return pageWidth;
	}
	else if( m == 'h' )
	{
		return pageHeight;
	}
	else if( m == 'wh' )
	{
		return windowHeight;
	}
}

//Get Scrolled Height Function
function getScrollHeight()
{	//Function returns the scrolled height of the page so the popup is 
	//always visable.
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}
// -----------------------------------------------------------------------------------
