/*****************************************************************************************
Name:			global.js
Copyright:		2007 Breakthrough Design Group.
Description:	Global javascript file for gobdg.com
*****************************************************************************************/

/*********************************************************************************************
	ONLOAD EVENTS
*********************************************************************************************/
window.addEvent('domready',function()
{
	sizeBg(); 				// Sizing the black 'shimmed' background to fit the content (only necessary for content pgs -not the home page)
	convertLinks();			// Converting link custom attributes "rel"
	//initializeHeyThere();	//Startin the welcome message at the top of the page
});

/*********************************************************************************************
	Sizing the black 'shimmed' background to fit the content (only necessary for content pgs -not the home page)
*********************************************************************************************/
function sizeBg()
{
	if($('m_contentBg'))
	{
		$('m_contentBg').style.height = $('m_content').offsetHeight + 'px';
	}
}




/***********************************************************************
	Converting <a rel="external" to <a target="_blank" for standards compliancy
***********************************************************************/
function convertLinks()
{
	var anchors = document.getElementsByTagName('a');
	for(var i=0;i<anchors.length;i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&  anchor.getAttribute("rel") == "external")
		{
			anchor.target = "_blank";
		}
	}
}
/*******************************************************************************************
	SHOWING/HIDING THE BACKGROUND IMAGE
*******************************************************************************************/
function showBg(b)
{
	var x = '';
	var y = 'none';
	if(b)
	{
		x = 'none';
		y = '';
	}	
	if($('contentBg'))$('contentBg').style.display = x;
	if($('cbg'))$('cbg').style.display = x;
	$('content').style.display = x;
	$('click_anywhere').style.display = y;
}

/**********************************************************************************************
		GLOBAL UTILITIES
**********************************************************************************************/

function getWindowWidth()
{
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        myWidth = window.innerWidth;
    } 
    else if( document.documentElement && document.documentElement.clientWidth)
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } 
    else if( document.body &&  document.body.clientWidth ) 
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}


/**********************************************************************************************
* function:     getElementsByTagNames
* description:  Special thanks to quirksmode.org for this script.  
*				This function is basically the same as the getElementByTagNames, it just allows 
				us to grab multiple objects by passing in a comma delimited list.
* params:       list - objects to get (comma delimitd for multiple)
				obj - root object defaults to 'document' if nothing is passed in.
**********************************************************************************************/
function getElementsByTagNames(list,obj) {
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}

/**********************************************************************************************
* function:     findPosition
* description:  Many thanks to quirksmode.org for this one:
				Basically gives us the actual top and left position of an element.
* params:       object whose position we are attempting to define.
* retuns:		Array, where     [0] = left position        [1] = top position
**********************************************************************************************/
function findPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/** Here we use javascript to center elements that are abasolutely positioned. 
Event.observe(window,'load',function()
{
	toCenter = document.getElementsByClassName('absPosCenter');
	toCenter.each( function(div){
			// The end goal here is to calculate the 'left' parameter, 
			So, we need to get the width of the window, and the width of hte div 
			var dWidth = div.offsetWidth;
			var wWidth = getWindowWidth();
			var dLeft = (wWidth-dWidth)/2;
			// Here we are correcting for mozilla being a few pixels off to the left?? 
				if(typeof( window.innerWidth ) == 'number')
				{
					dLeft -= 10;
				}
			div.style.left = dLeft + 'px';
		});
});**/

// After 1minute 30, This function will ask if they need assistance
// If so, it will prompt for their phone number
// if not, it must save a cookie so that this DOESN'T OPEN AGAIN
// loaded in footer.php
function helpMsg(){
	if(confirm('Friendly Helper Message:\n\n You have been reading for 1:30.  Would you like us to contact you for assistance?')){
		var phoneNum = window.prompt('Please enter a phone number that we may reach you on.');
		document.forms['helperForm'].phoneMsg.value = phoneNum;
		document.forms['helperForm'].submit();
		Set_Cookie( 'sentHelperMsg', 'set', 30, '/', '', '' );
		alert('Alright, we will be calling you soon.\n\nYou can reach us immediately at (619)849-8738 or (315)233-9283');
	}
	else{
		Set_Cookie( 'sentHelperMsg', 'set', 30, '/', '', '' );
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function validateContactMsg(){
	var cmsg = document.getElementById('contact_message').value;
	if(cmsg == '' || cmsg == 'Enter comments and contact info here.'){
		alert('Please enter your message and contact info.');
		return false;
	}
	else return true;
}

/*****************************************************************
	BEGINS THE HEY THERE MESSAGING
*****************************************************************/
var messageNumber = 0;
function initializeHeyThere(){	
	// unsetting the display propery first off
	$('heythere').style.display = '';
	
	// Initially hiding all the messages
	for(var i=0;i<=30;i++){
		if($('heytext_'+i)){
			var mySlide = new Fx.Slide('heytext_'+i).hide();
		}
	}
	// If not home page, open the msg
	if(!$('page-home-page')){
		setTimeout('showFirst()',1200);
	}
}
function showFirst(){
	var mySlide = new Fx.Slide('heytext_0').hide().slideIn();
	setTimeout('getNextMessage()',5000);
}
function getNextMessage(){
	// Fade out the last div
	var mySlide = new Fx.Slide('heytext_'+messageNumber).show().toggle();
		
	// Fade in the new one
	messageNumber += 1;
	var newMsgDiv = $('heytext_'+messageNumber);
	if(newMsgDiv)newMsgDivId = $('heytext_'+messageNumber);
	else {
		messageNumber = 0;
		newMsgDivId = 'heytext_0';
	}
	
	var mySlide = new Fx.Slide(newMsgDivId).hide().slideIn();
	setTimeout('getNextMessage()',5000);
}
