//*********************************************
//GreenLight Association Dynamic Actions
//Author: John Gerdsen
//BlackShirt Media
//www.blackshirtmedia.com
//*********************************************

//Variable Declaration
var MENU_POSITION_X = null;
var MENU_POSITION_X_MOVED = null;
var MENU_GRAPHIC_WIDTH = 300;
var LOGO_POSITION_Y = 300;
var LOGO_POSITION_X_MOVED = null;
var CONTENT_POSITION_X = null;
var MENU_MOVED = false;

//AJAX Variables
var NORMAL_STATE = 4;
var PREFIX = 'http://www.greenlightassn.com/';
var http = getHTTPObject();

//Object Declaration
var contentObj = null;
var logoObj = null;
var shadowObj = null;
var emailObj = null;

//Menu Tween Variable
var menuTween = null;
var logoTween = null;
var contentOpacity = null;
var emailOpacity = null;

//Email Variables
var eName;
var eEmail;
var eQuestion;
var eRec = "info@greenlightassn.com";
var eSub = "Question from GreenLight Website";
var EMAILPREFIX = 'http://www.greenlightassn.com/cgi-sys/FormMail.cgi?';
var dataMissing = false;

//Image Preloading
var g1BG = new Image(700,500);
g1BG.src = PREFIX + "images/mission.jpg";
var g2BG = new Image(700,500);
g2BG.src = PREFIX + "images/membership.jpg";
var g3BG = new Image(700,500);
g3BG.src = PREFIX + "images/services.jpg";
var g4BG = new Image(700,500);
g4BG.src = PREFIX + "images/products.jpg";
var g5BG = new Image(700,500);
g5BG.src = PREFIX + "images/contactus.jpg";

function sendEmail(){
	eName = document.getElementById('name').value;
	eEmail = document.getElementById('email').value;
	eQuestion = document.getElementById('question').value;
	//Check for missing data in the text fields
	for(i=0; i<document.mailForm.elements.length; i++)
	{
		if(document.mailForm.elements[i].type=="text" || document.mailForm.elements[i].type=="textarea")
		{
			if(document.mailForm.elements[i].value=='')
			{	//Make border orange to show that data is missing
				document.mailForm.elements[i].style.border = "solid #FF9933 1px";
				dataMissing = true;
			}
			else
			{	//Change back to normal if text is okay
				document.mailForm.elements[i].style.border = "";
			}
		}
	}
	if(!dataMissing){
		email();
	}
}

function email()
{
	document.getElementById('send').disabled = true;
	http.open('GET', EMAILPREFIX + 'realname=' + eName +'&email=' + eEmail +'&subject=' + eSub +'&question='+eQuestion+'&recipient='+eRec, true);
	http.onreadystatechange = emailSent;
	http.send(null);
}
	
function emailSent()
{
	if (http.readyState == NORMAL_STATE) {
		document.getElementById('name').value = "";
		document.getElementById('email').value = "";
		document.getElementById('question').value = "";
		document.getElementById('status').innerHTML = "E-mail Sent.";
		emailOpacity.continueTo(0, 1);
		document.getElementById('send').disabled = false;
	}
}
	
//**********************************************
//Initial Load Functions
//Functions required to gather information about elements in the site
//Arrange Element properly depending on resolution of browser.
//Initialize Tweens
//**********************************************
function elementScout()
{
	MENU_POSITION_X = findPosX(document.getElementById('menuDiv'));
	MENU_POSITION_X_MOVED = ((pageWidth()-800) / 2);
	CONTENT_POSITION_X = (MENU_POSITION_X_MOVED + MENU_GRAPHIC_WIDTH) + 'px';
	LOGO_POSITION_X_MOVED = ((pageWidth()-745) / 2) + 'px';
	
	logoObj = document.getElementById('logoDiv');
	logoObj.style.left = LOGO_POSITION_X_MOVED;
	logoObj.style.visibility = 'visible';
	
	contentObj = document.getElementById('contentDiv');
	shadowObj = document.getElementById('shadowDiv');
	contentObj.style.left = CONTENT_POSITION_X;
	shadowObj.style.left = CONTENT_POSITION_X;
	
	menuTween = new Tween(document.getElementById('menuDiv').style,'left',Tween.strongEaseOut, MENU_POSITION_X, MENU_POSITION_X_MOVED, 1.5,'px');
	menuTween.stop();
	logoTween = new Tween(document.getElementById('logoDiv').style,'top',Tween.strongEaseOut, LOGO_POSITION_Y, 0, 1.5,'px');
	logoTween.stop();
	contentOpacity = new OpacityTween(contentObj, Tween.regularEaseOut, 0, 100, .5);
	contentOpacity.stop();
}

//*********************************
//Function to display email form
//**********************************
function showEmail(id){
	document.getElementById('status').innerHTML = "";
	emailObj = document.getElementById(id);
	emailObj.style.display = "block";
	emailOpacity = new OpacityTween(emailObj, Tween.regularEaseInOut, 0, 100, 1.25);
	emailOpacity.start();
}

//******************************************************************
//Menu Functions
//Perform functions required to move menu and load section data.
//******************************************************************
function sectionSelect(id)
{

	if(!MENU_MOVED)
	{
		//Make call to load information into DIV
		retrieveInformation(id);
	}
	else{
		//If Menu is open clear information and continue to closing.
		contentObj.innerHTML = '';
		contentOpacity.onMotionFinished = null;
		shadowObj.style.display = "none";
		contentOpacity.continueTo(0, .5);
		menuTween.continueTo(MENU_POSITION_X, 1.5);
		logoTween.continueTo(LOGO_POSITION_Y, 1.5);
		menuTween.onMotionFinished = function(){if(!MENU_MOVED){retrieveInformation(id);}};
		MENU_MOVED = false;
	}
}

//**********************************
//Close Menu back to original state
//**********************************
function closeMenu(){
	//Only allow if menu has been opened
	if(MENU_MOVED){
		contentObj.innerHTML = '';
		menuTween.onMotionFinished = null;
		menuTween.continueTo(MENU_POSITION_X, 2);
		logoTween.continueTo(LOGO_POSITION_Y, 2);
		shadowObj.style.display = "none";
		contentOpacity.onMotionFinished = null;
		contentOpacity.continueTo(0, .5);
		MENU_MOVED = false;
	}
}

//**************************************************
//Function to retrieve section information and place into div
//*********************************************************
function retrieveInformation(page){
	var imageURL = "images/" + page + ".jpg";
	if(page == "products"){
		contentObj.style.backgroundAttachment = "fixed";
	}
	else{
		contentObj.style.backgroundAttachment = "scroll";
	}
	page = page + ".html";
	//Assigning new background to contentDiv
	if (document.layers)
		contentObj.background.src = imageURL == 'none' ? null : imageURL;
	else if (document.all)
		contentObj.style.backgroundImage = imageURL == 'none' ? 'none' : 'url(' + '"' + imageURL + '"' + ')';
	else if (document.getElementById)
		contentObj.style.backgroundImage = imageURL == 'none' ? 'none' : 'url('+ '"' + imageURL + '"'+')';
	http.open('GET', PREFIX + page, true);
	http.onreadystatechange = handleRetrieve;
	http.send(null);
}

//**************************************************
//Function to handle retrieved information and place
//properly into DIV
//***************************************************
function handleRetrieve()
{
	if (http.readyState == NORMAL_STATE) {
		contentObj.innerHTML = http.responseText;
		menuTween.continueTo(MENU_POSITION_X_MOVED, 2);
		logoTween.continueTo(0, 2);
		contentOpacity.continueTo(100, 2.5);
		contentOpacity.onMotionFinished = function() { shadowObj.style.display = "block"; };
		MENU_MOVED = true;
	}
}

//***************************************************************
//Misc Functions
//BROWSER INFORMATION FUNCTIONS
//***************************************************************
//Find Page Width
function pageWidth() {
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 
//Find Page Height
function pageHeight() {
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : 
	null;
}

//------------------------------------------------
//Find X Coordinate of Element and return
//------------------------------------------------
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

//--------------------------------------
//Grab URL Variables
//--------------------------------------
function getUrlVars(){    
	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];    
	}     
	return vars;
}