﻿// JavaScript Document
// Dave's Guitar Shop Javascripts - Scripts in use throughout the site
// Sleeping Giant Studios, LLC
// Created by David Ellenwood - 09/05/2007
// Modified by Jake Holman - 04/06/09

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			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;
	}

}

// 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("/include/site/multimedia/swf/initiate_ExpressInstall.swf","upgradeBox","310","200","8","/include/site/multimedia/swf/expressinstall.swf");
		}
	}
}

// Controls and validates the site Search form
function initSiteSearch(){
	
	if($("#siteSearchBox").attr("value") == ''){$("#siteSearchBox").attr({value:"Search"});}
	
	// Validate the form before submission...
	$("#submitSearchBtn").click(
		function(){ // Validate search field...
			
			var searchValue = jQuery.trim($("#siteSearchBox").attr("value"));
			
			if(searchValue == '' || searchValue == 'Search') {
				alert('Please enter a search term.');
				$("#siteSearchBox").focus();
				return false;
			} else {
				//return false;
				$("#siteSearchForm").submit();
			}
			
		}
	);
	
	$("#siteSearchBox").focus(
		function(){ // highlight and clear the search field
			if($("#siteSearchBox").attr("value") == 'Search'){
				$("#siteSearchBox").attr({value:""});
				$(this).css({color:"#3f2f13"});
				//$("#siteSearch").css({background:"transparent url('/site/img/searchFieldBkDn.gif') no-repeat"});
			}
		}
	);
	$("#siteSearchBox").blur(
		function(){ // highlight and clear the search field
			if($(this).attr("value") == ''){
				$(this).attr({value:"Search"});
				$(this).css({color:"#b4a492"});
				//$("#siteSearch").css({background:"transparent url('/site/img/searchFieldBk.gif') no-repeat"});
			}
		}
	);
	
}

// Show/Hides Product navigation sub menus
function initProductNav() {
	//junk = $("#productArea .sidebar ul.l1 > li").size();
	//alert(junk);
	$("#productArea .sidebar > ul > li").addClass("top");
	$("#productArea .sidebar > ul > li span").click(
		function(){
			//alert('blah!');
			parentLi = $(this).parent().get(0);
			$(parentLi).find("ul").toggle("fast");
			$(parentLi).siblings().find("ul").hide("fast");
		}
	);
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 			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...
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

$(document).ready(function(){
	initSiteSearch();	// Initializes the Site Search events
	testFlash();		// Test for & initializes upgrade requests for Flash Player
	//initProductNav();	// Initializes the product navigation events
});
