/**
 * @copyright  Copyright (c) 2007 August Ash Inc. (http://www.augustash.com)
 * @version    $Id: aailib.js 6 2008-05-27 20:13:11Z eanderson $
 */

/**
 * Prepare Links - using jQuery
 * 
 * Checks the document, when ready, for all link nodes with a class
 * name "external" and opens them in a new window when clicked.
 */
$(document).ready(function(){
	$("a").filter(".external").click(function (){
		var NewWindow = new OpenWindow($(this).attr("href"));
		return NewWindow.open();
	})
    .end();
});



/**
 * OpenWindow Class
 * 
 * Creates an OpenWindow object that allows you to define the URL, 
 * window name, and features for firing a pop-up window.
 * 
 * @param {String} href
 */
function OpenWindow(href)
{
	// Set default values
	var _href     = href;
	var _name     = "external";
	var _features = "";
	
	function __construct() {
		// Define methods		
		this.getHref     = function() { return _href; }
		this.setHref     = function(href) { _href = href; }
		this.getName     = function() { return _name; }
		this.setName     = function(name) { _href = name; }
		this.getFeatures = function() { return _features; }
		this.setFeatures = function(features) { _features = features; }
		
		this.open = function() {
			window.open(_href, _name, _features);
			return false;
		}
	};
	
	return new __construct();
}

/**
 * Son of Suckerfish Drop Down Menu
 * http://www.htmldog.com/
 */
$(document).ready(function(){
	var sfEls = $("ul#nav li");
	for (var i = 0; i < sfEls.length; i++) {
		$(sfEls[i]).mouseover(function(){
			$(this).addClass("sfhover");
		});
		$(sfEls[i]).mouseout(function(){
			$(this).removeClass("sfhover");
		});
	}
});


/**
 *	"How did your hear about us?" form functionality
 *
 *	Displays the answer box with dynamic value
 * 	based upon which option is selected
 */
$(document).ready(function(){
	// set variables
	var hearAboutSelect = $("div#cwrap div#content form div.hearAbout_select select");

	var hearAboutAnswer = $("div#cwrap div#content form div.hearAbout_answer input");
	var hearAboutAnswerDefaultValue = $("div#cwrap div#content form div.hearAbout_answer input[value$='']");
	
	// start with the answer field hidden
	hearAboutAnswer.hide();

	
	// get the Options of the Select Box
	var printYellowPages 				= $("div#cwrap div#content form div.hearAbout_select select option:contains('Print Yellow Pages')");
	var onlineYellowPages 			= $("div#cwrap div#content form div.hearAbout_select select option:contains('Online Yellow Pages')");
	var onlineDirectory 				= $("div#cwrap div#content form div.hearAbout_select select option:contains('Online Directory')");
	var website 								= $("div#cwrap div#content form div.hearAbout_select select option:contains('Another Website')");
	var searchEngine 						= $("div#cwrap div#content form div.hearAbout_select select option:contains('Internet Search Engine')");
	var tvCommercial 						= $("div#cwrap div#content form div.hearAbout_select select option:contains('TV Commercial')");
	var radioCommercial 				= $("div#cwrap div#content form div.hearAbout_select select option:contains('Radio Commercial')");
	var insuranceAgency 				= $("div#cwrap div#content form div.hearAbout_select select option:contains('Insurance Agency')");
	var dealerServiceCollision 	= $("div#cwrap div#content form div.hearAbout_select select option:contains('Dealer/Service/Collision')");
	var fleetReferral 					= $("div#cwrap div#content form div.hearAbout_select select option:contains('Fleet Referral')");
	var rgVan 									= $("div#cwrap div#content form div.hearAbout_select select option:contains('Rapid Glass Van')");
	var rgSalesman 							= $("div#cwrap div#content form div.hearAbout_select select option:contains('Rapid Glass Salesman')");
	var rgEmployee 							= $("div#cwrap div#content form div.hearAbout_select select option:contains('Rapid Glass Employee')");
	var other 									= $("div#cwrap div#content form div.hearAbout_select select option:contains('Other')");
	

	if ($.browser.msie) 
	{
		$(hearAboutSelect).click(function (){
			hearAboutAnswer.show();
		});
		
		// IE doesn't recognize option.click so we'll use the change function
		$(hearAboutSelect).change(function () {
			
			var str = "";

			$("div#cwrap div#content form div.hearAbout_select select option:selected").each(function () {
      	str += $(this).text();

				// compare the str variable and assign a new default value for the answer box
				if (str == 'Print Yellow Pages') { $(hearAboutAnswerDefaultValue).val("Page #"); }
				if (str == 'Online Yellow Pages') { $(hearAboutAnswerDefaultValue).val("Which one?"); }
				if (str == 'Online Directory') { $(hearAboutAnswerDefaultValue).val("Which one?"); }
				if (str == 'Another Website') { $(hearAboutAnswerDefaultValue).val("Which one?"); }
				if (str == 'Internet Search Engine') { $(hearAboutAnswerDefaultValue).val("Which one?"); }
				if (str == 'TV Commercial') { $(hearAboutAnswerDefaultValue).val("Which channel?"); }
				if (str == 'Radio Commercial') { $(hearAboutAnswerDefaultValue).val("Which station?"); }
				if (str == 'Insurance Agency') { $(hearAboutAnswerDefaultValue).val("Agency Name"); }
				if (str == 'Dealer/Service/Collision') { $(hearAboutAnswerDefaultValue).val("Shop Name"); }
				if (str == 'Fleet Referral') { $(hearAboutAnswerDefaultValue).val("Fleet Name"); }
				if (str == 'Rapid Glass Van') { $(hearAboutAnswerDefaultValue).val("Location"); } 
				if (str == 'Rapid Glass Salesman') { $(hearAboutAnswerDefaultValue).val("Name"); }
				if (str == 'Rapid Glass Employee') { $(hearAboutAnswerDefaultValue).val("Name"); }
				if (str == 'Other') { $(hearAboutAnswerDefaultValue).val("Who/How"); }
				if (str == "") { $(hearAboutAnswerDefaultValue).val("Please Fill In"); }
			})
			.trigger('change');
		});
	}
	else
	{
		$(printYellowPages).mousedown(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Page #");
		});
		$(onlineYellowPages).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which one?");
		});
		$(onlineDirectory).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which one?");
		});
		$(website).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which one?");
		});
		$(searchEngine).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which one?");
		});
		$(tvCommercial).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which channel?");
		});
		$(radioCommercial).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Which station?");
		});
		$(insuranceAgency).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Agency Name");
		});
		$(dealerServiceCollision).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Shop Name");
		});
		$(fleetReferral).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Fleet Name");
		});
		$(rgVan).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Location");
		});
		$(rgSalesman).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Name?");
		});
		$(rgEmployee).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Name?");
		});
		$(other).click(function (){
			hearAboutAnswer.show();
			$(hearAboutAnswerDefaultValue).val("Who/How");
		});
	}

	// clear out any default values set above
	$(hearAboutAnswer).click(function (){ $(this).val(""); });
	
});




//Style Sheet Switcher version 1.1 Oct 10th, 2006
//Author: Dynamic Drive: http://www.dynamicdrive.com
//Usage terms: http://www.dynamicdrive.com/notice.htm

var manual_or_random="manual" //"manual" or "random"
var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.

//////No need to edit beyond here//////////////

function getCookie(Name) { 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
setCookie(name, "moot")
}


function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
var i, cacheobj, altsheets=[""]
for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
cacheobj.disabled = true
altsheets.push(cacheobj) //store reference to alt stylesheets inside array
if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
cacheobj.disabled = false //enable chosen style sheet
}
}
if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
var randomnumber=Math.floor(Math.random()*altsheets.length)
altsheets[randomnumber].disabled=false
}
return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
}

function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
if (document.getElementById){
setStylesheet(styletitle)
setCookie("mysheet", styletitle, days)
}
}

function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
var element=(element.type=="select-one") ? element.options : element
for (var i=0; i<element.length; i++){
if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
if (element[i].tagName=="OPTION") //if this is a select menu
element[i].selected=true
else //else if it's a radio button
element[i].checked=true
break
}
}
}
}

if (manual_or_random=="manual"){ //IF MANUAL MODE
var selectedtitle=getCookie("mysheet")
if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored
setStylesheet(selectedtitle)
}
else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE
if (randomsetting=="eachtime")
setStylesheet("", "random")
else if (randomsetting=="sessiononly"){ //if "sessiononly" setting
if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty
document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value
else
setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie
}
else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting
if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value
setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
}
else
setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie
} 
}