// ===================================================================================
//
// 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:
//  	checkUrlVars();
//  	gb();
//  	gd( STRING );
//  	gds( STRING );
// 		getGoing();
// 		getWindowSize( STRING );
//  	happyIEFix();
//  	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 = 611;  //the width of the popup
var IpopupTop = 200;    //the height of the popup
var fadeDuration = 0.5; //length of the effects
var fadeVal = 0.6;		//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);
Event.observe(window, 'load', happyIEFix, false); //Fix padding on Timeline page( see bottom of document for function happyIEFix();

// -----------------------------------------------------------------------------------
// Generic Functions
function gd( o )                                                    //a quick way to get an element by ID
{ 
    if( document.getElementById( o ) )
    {
        return document.getElementById( o );
    }
    return gd('popupSorry');
}			
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
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Timeline Hover Function
function hoverTime( o, b )  
{ 
	for( var io = 1900; io < 2010; io += 10 )
	{
		if( gd( "Hover" + io ) != null )
		{
			gds( "Hover" + io ).display = 'none';
		}
	}
	if( o != 'all' )
	{
		gds( "Hover" + o ).display =  'block';
	}
}

// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//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
		checkUrlVars();
	}
	//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";
		gds( currPop ).top = IpopupTop + "px";
		gds( currPop ).left = ((getWindowSize( 'w' ) - IpopupWidth)/2) + "px";
	}
		
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Toggle Popup Function
function togPop( o, b )
{
	if( b )
	{	//show popup
		currPop = o;
		gds( currPop ).top = 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 = 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 );
	}
}

//Hide Popup Function
function hidePop( 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.
	togPop( ('popup' + o), false ); 
}

//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 });
	}
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//Check URL Vartiables.
function checkUrlVars()
{   //If we were passed a specific popup call lets run that here
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    if( vars['popup'] )
    {
        if( gd( 'popup' + vars['popup'] ) != gd( 'popupSorry' ) )
        {
            showPop( vars['popup'] );
        }
        else
        {
            showPop( 'Sorry' );
        }
    }
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
//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;
	}
}
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------
// Detect browser and apply specific padding
function happyIEFix()
{
    var browser     = '';
    // get browser
    if (browser == '')
    {
        if (navigator.appName.indexOf('Microsoft') != -1) { browser = 'IE'; }
        else if (navigator.appName.indexOf('Netscape') != -1) { browser = 'Netscape'; }
        else { browser = 'IE'; }
    }
    // do work
    if (browser == 'IE') 
    {
        gds( 'padme1' ).paddingLeft = "0px";
        gds( 'padme1' ).width = "80px";
        gds( 'padme2' ).paddingLeft = "10px";
        gds( 'padme2' ).width = "80px";
        gds( 'padme3' ).paddingLeft = "0px";
        gds( 'padme3' ).width = "80px";
    }
}
// -----------------------------------------------------------------------------------