﻿// JavaScript Document
// Mathews Javascripts - Scripts in use throughout the site
// Sleeping Giant Studios, LLC
// Created by David Ellenwood - 09/05/2007


// Checks to make sure visitors have minimum version of Flash installed.
function testFlash() {
	if(!$.cookies.get('FlashUpgradeDeclined')) {
		// We must not have checked recently...Do the check.
		if(swfobject.hasFlashPlayerVersion("8.0.0")) {
			// Don't need to do anything.  The world is good today.
		} else {
			// Show the Flash Intall/Upgrade DIV...
			$('#flashUpgrade').css({display:"block"});
			//Add close button functionality to the Install/Upgrade DIV...
			$('.closeUpgradeBox').click(function() {
				$('#flashUpgrade').css({display:"none"});
				$.cookies.set('FlashUpgradeDeclined','true',{hoursToLive:24});
			});
			// If they have Flash v6.5 or greater, but less than our required version (8), start Adobe's Express Install script...
			swfobject.embedSWF("/site/swf/initiate_ExpressInstall.swf","upgradeBox","310","200","8","/site/swf/expressinstall.swf");
		}
	}
}

// Adds a class to hovered items on the site nav to overcome IE6's crappy CSS ':hover' support
function setupNav() {
	$("#primaryNavTop li").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
}

$(document).ready(function(){
	testFlash(); // Test for & initializes upgrade requests for Flash Player
	setupNav(); // Setup site nav drop downs for IE6
});

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			BROWSER POP-UPS
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

//Function to show and hide pop-ups
//REQUIRES: STATIC POP-UP WIDTH OF 350PX!
function showHidePopUp() {
	if(document.getElementById(this.rel) && document.getElementById('overlay')) {
	
		// Get some objects...
		var popUp	= document.getElementById(this.rel);
		var overlay	= document.getElementById('overlay');
		
		//show or hide it the popup...
		if(popUp.style.display == 'none') {
		
			//First, show the overlay...
			overlay.style.display	= 'block';
			
			//Get the L-R positioning of the popup based on the width of the overlay...
			var popLeft = (overlay.offsetWidth - 350) / 2 + 'px';
			
			//Set the L-R positioning...
			popUp.style.left = popLeft;
			
			//Set the close buttons...
			var links = popUp.getElementsByTagName('a');
			for(i=0;i<links.length;i++) {
				if(links[i].className == 'closeBtn') {
					links[i].onclick = showHidePopUp;
				}
			}
			
			// Show the popup!
			popUp.style.display		= 'block';
		} else {
		
			//Hide it!
			overlay.style.display	= 'none';
			popUp.style.display		= 'none';
		}
		
	} else {
		alert('Sorry, this link is currently unavailable.')
	}
}

// Initialize the Standards link in the site footer...
function intStandardsPopUp() {
	
	//If the link is avaliable, then add an action to it.
	if(document.getElementById('whyStandardsBtn')) {
		var btn = document.getElementById('whyStandardsBtn');
		btn.onclick = showHidePopUp;
	}

}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			SITE SEARCH: Required Scripts
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// Function to show and hide the search box
function showHideSearchBox() {

	var searchBox = document.getElementById('siteSearchPopUp');
	var searchBtn = document.getElementById('siteSearchBtn');

	if(searchBox.style.display == "none") {
		searchBox.style.display = "block";
		searchBtn.className	= "sel";
	} else {
		searchBox.style.display = "none";
		searchBtn.className	= "";
	}
}


// Function to validate the search form
function validateSearch() {
	
	//alert(this.previousSibling.previousSibling.value);
	
	var searchForm = this.parentNode;
	
	if(this.previousSibling.previousSibling.value ==''){
		window.alert("Please enter your search terms.")
		return false;
	}
	searchForm.submit();
	return true;
}


// Function to initialize the search tab on the bottom of each page.
function setSiteSearch() {
	
	//Get some objects...
	var searchBtn			= document.getElementById('siteSearchBtn');
	var searchCloseBtn		= document.getElementById('siteSearchCloseBtn');
	var popUpSubmitBtn		= document.getElementById('submitSearchBtn');
	var resultsSubmitBtn	= document.getElementById('resultsSubmitBtn');
	
	//Set some events...
	searchBtn.onclick		= showHideSearchBox;
	searchCloseBtn.onclick	= showHideSearchBox;
	popUpSubmitBtn.onclick	= validateSearch;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			GENERAL SCRIPTS
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// Adds multiple events to any event handler - created by Peter Paul Koch @ http://www.quirksmode.org
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

// Simple function to create a browser cookie - created by Peter Paul Koch @ http://www.quirksmode.org
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Simple function to read a browser cookie - created by Peter Paul Koch @ http://www.quirksmode.org
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Simple function to delete a browser cookie - created by Peter Paul Koch @ http://www.quirksmode.org
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			INITIALIZATION CALLS...
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// Initialize scripts that need it...
//addEventSimple(window,"load",setSiteSearch);

function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

function showChart(imgSrc, resultsDivId, maxWidth)
{
    document.getElementById(resultsDivId).innerHTML = '<img src="' + imgSrc + '" width="' + maxWidth + '" title="Draw Length Chart" />';
    return false;
}
