
//This code helps to read the query string for jury-rigging the different templates
/*
jqURL
by Josh Nathanson

various manipulations on url strings and windows.  
all functions can also take a window object as an argument,
for example {win:opener}
but will default to current window if none is passed.

public functions:

-------------------------
.url({ 
	 win:window object 
	 })
-------------------------
returns the whole url string
like win.location.href

	so if the current window href is "http://www.mysite.com?var1=1&var2=2&var3=3"
	
	$.jqURL.url() returns "http://www.mysite.com?var1=1&var2=2&var3=3"
	
	
------------------------------
.loc(urlstr:string, 
	 { 
	 win:window object, 
	 w:integer, 
	 h:integer, 
	 t:integer,
	 l:integer,
	 wintype:string('_top'[default],'_blank','_parent') )
	})
------------------------------																			 
- directs passed in window to urlstr, which is required
- works like window.location.href = 'myurl'
- but you can also use it to pop open a new window by passing in "_blank" as the wintype
- if popping open a window, defaults to center of screen

	so
	$.jqURL.loc('http://www.google.com',
				{w:200,h:200,wintype:'_blank'});
	would open Google in a new centered 200x200 window
	
	or, locate an url to any named window:
	$.jqURL.loc('http://www.google.com',{ win:mywindow });
	opens Google in mywindow


------------------------------
.qs({ 
	ret:string('string'[default],'object'), 
	win:window object })
------------------------------
returns querystring, either string (pass ret:'string' [default])
or object (pass ret:'object') 

	so if the current window href is "http://www.mysite.com?var1=1&var2=2&var3=3"

	$.jqURL.qs();
	returns "var1=1&var2=2&var3=3"
	
	$.jqURL.qs({ ret:'object' });
	returns Object var1=1,var2=2,var3=3


------------------------------
.strip({ keys:string(list of keys to strip), win:window object })
------------------------------
if passed with no arguments, returns url with '?' and query string removed
if you pass in list of keys, it returns url with the specified key-value pairs removed

	so if the current window href is "http://www.mysite.com?var1=1&var2=2&var3=3"

	$.jqURL.strip();
	will return
	"http://www.mysite.com"
	
	$.jqURL.strip({ keys:'var1,var2' });
	will return
	"http://www.mysite.com?var3=3"
	
	
-------------------------------------
.get(key, {win:window object})
-------------------------------------
returns value of passed in querystring key

	so if the current window href is "http://www.mysite.com?var1=1&var2=2&var3=3"
	$.jqURL.get('var2');
	will return 2

--------------------------------------
.set(hash, {win:window object})
--------------------------------------
returns the window's url, but with the keys/values set in the query string
if the keys already exist, re-sets the value
if they don't exist, they're appended onto the query string

*/

jQuery.jqURL={url:function(args){args=jQuery.extend({win:window},args);return args.win.location.href},loc:function(urlstr,args){args=jQuery.extend({win:window,w:500,h:500,wintype:'_top'},args);if(!args.t){args.t=screen.height/2-args.h/2}if(!args.l){args.l=screen.width/2-args.w/2}if(args['wintype']=='_top'){args.win.location.href=urlstr}else{open(urlstr,args['wintype'],'width='+args.w+',height='+args.h+',top='+args.t+',left='+args.l+',scrollbars,resizable')}return},qs:function(args){args=jQuery.extend({ret:'string',win:window},args);if(args['ret']=='string'){return jQuery.jqURL.url({win:args.win}).split('?')[1]}else if(args['ret']=='object'){var qsobj={};var thisqs=jQuery.jqURL.url({win:args.win}).split('?')[1];if(thisqs){var pairs=thisqs.split('&');for(i=0;i<pairs.length;i++){var pair=pairs[i].split('=');qsobj[pair[0]]=pair[1]}}return qsobj}},strip:function(args){args=jQuery.extend({keys:'',win:window},args);if(jQuery.jqURL.url().indexOf('?')==-1){return jQuery.jqURL.url({win:args.win})}else if(!args.keys){return jQuery.jqURL.url({win:args.win}).split('?')[0]}else{var qsobj=jQuery.jqURL.qs({ret:'object',win:args.win});var counter=0;var url=jQuery.jqURL.url({win:args.win}).split('?')[0]+'?';var amp='';for(var key in qsobj){if(args.keys.indexOf(key)==-1){amp=(counter)?'&':'';url=url+amp+key+'='+qsobj[key];counter++}}return url}},get:function(key,args){args=jQuery.extend({win:window},args);qsobj=jQuery.jqURL.qs({ret:'object',win:args.win});return qsobj[key]},set:function(hash,args){args=jQuery.extend({win:window},args);var qsobj=jQuery.jqURL.qs({ret:'object',win:args.win});for(var i in hash){qsobj[i]=hash[i]}var qstring='';var counter=0;var amp='';for(var k in qsobj){amp=(counter)?'&':'';qstring=qstring+amp+k+'='+qsobj[k];counter++}return jQuery.jqURL.strip({win:args.win})+'?'+qstring}};


//This code runs when the page loads
$(document).ready(function(){
	$(".googlemaps").fancybox({
	'frameWidth':425,
	'frameHeight':350,
 	'zoomSpeedIn': 500, 
	'zoomSpeedOut': 500, 
	'overlayShow': true }); 
	
	$("a").each(function(){
	var linkplace = $(this).attr('href');
	if(linkplace){
		if((linkplace.toLowerCase().search(".jpg") != -1) || (linkplace.toLowerCase().search(".gif") != -1) || (linkplace.toLowerCase().search(".png") != -1)){
			$(this).fancybox({'overlayShow': true });
		}
	}
	});
        $('.searchbutton').click(function(){
            var keyword = $('#KEYWORDS').val();
            keyword = keyword.replace(" ", " and ");
            keyword = keyword.replace(" and and ", " and ");
            $('#KEYWORDS').val(keyword);
        });


	//This bit determines when to load a certain template based on the query string argument, templatename.
	var template = $.jqURL.get('templatename');
	if (template)
	{
		$('#store_content').addClass(template);	
		$('#store_content .brownbox').removeClass('brownbox');
		temp = template.substring(0,1).toUpperCase();
		newtemplate = temp + template.substring(1);
		$('.section h2:first').html(newtemplate);
		
		//make sure the template is "passed" when a user pages through
		$('.paging a').each(function(){
			var ref = $(this).attr('href');
			if(ref != ""){
				ref += ("&amp;templatename=" + template);
				$(this).attr('href', ref);
			}
		});
	}
	
	
	$('li.feat').css('display','none');
	$('#featured_basses li.feat:first-child').show("fast").addClass("active"); 
	$('#featured_amps li.feat:first-child').show("fast").addClass("active"); 
	$('#featured_parts li.feat:first-child').show("fast").addClass("active"); 
	$('#featured_accessories li.feat:first-child').show("fast").addClass("active"); 
	$('#featured_specials li.feat:first-child').show("fast").addClass("active"); 
	$('#featured_exclusives li.feat:first-child').show("fast").addClass("active");
	
	//starts the image rotation
	t=setTimeout("fadeFeatured()", 7500);
	
	//sIFR stuff for headline replacement
	if(typeof sIFR == "function" && !($.browser.msie && parseInt($.browser.version) < 7)){
		sIFR.bHideBrowserText = false;
		sIFR.setup();
		
		//master head h1
		sIFR.replaceElement("h1", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#4f3209", sWmode: "transparent"}));
		//master head phone number
		sIFR.replaceElement("#master_head_phone", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#4f3209", sWmode: "transparent"}));
		//brownbox h2s
		sIFR.replaceElement(".brownbox h2", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#4f3209", sWmode: "transparent"}));
		//bass h2
		sIFR.replaceElement(".basses h2", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#bf2026", sWmode: "transparent"}));
		//amps h2
		sIFR.replaceElement(".amps h2", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#549235", sWmode: "transparent"}));
		//accessories h2
		sIFR.replaceElement(".accessories h2", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#479ca5", sWmode: "transparent"}));
		//parts h2
		sIFR.replaceElement(".parts h2", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#c36d11", sWmode: "transparent"}));
		//sidebar h3s
		sIFR.replaceElement("#sidebar h3", named({sFlashSrc: "http://www.thebassplace.com/javascript/calcitepro.swf", sColor: "#4f3209", sWmode: "transparent"}));

		
	};
});

function fadeFeatured(){	
		var featuredBassCount = $('#featured_basses li.feat').size();
		var featuredAmpsCount = $('#featured_amps li.feat').size();
		var featuredAccessoriesCount = $('#featured_accessories li.feat').size();
		var featuredPartsCount = $('#featured_parts li.feat').size();
		var featuredSpecialsCount = $('#featured_specials li.feat').size();
		var featuredExclusivesCount = $('#featured_exclusives li.feat').size();
		var fadespeed="slow";
		//for basses
		if(featuredBassCount>1){
			$('#featured_basses li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_basses li').hasClass("active"))){
						$('#featured_basses li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}
		//for amps
		if(featuredAmpsCount>1){
			$('#featured_amps li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_amps li').hasClass("active"))){
						$('#featured_amps li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}	
		//for accessories
		if(featuredAccessoriesCount>1){
			$('#featured_accessories li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_accessories li').hasClass("active"))){
						$('#featured_accessories li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}	
		//for parts
		if(featuredPartsCount>1){
			$('#featured_parts li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_parts li').hasClass("active"))){
						$('#featured_parts li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}
		//for specials
		if(featuredSpecialsCount>1){
			$('#featured_specials li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_specials li').hasClass("active"))){
						$('#featured_specials li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}
		//for exclusives
		if(featuredExclusivesCount>1){
			$('#featured_exclusives li.active').fadeOut(fadespeed, function(){
					$(this).removeClass("active").next(".feat").addClass("active").fadeIn(fadespeed)
					if(!($('#featured_exclusives li').hasClass("active"))){
						$('#featured_exclusives li.feat:first-child').fadeIn(fadespeed).addClass("active");
					}
			});	
		}
	//loop it!
	t=setTimeout("fadeFeatured()", 7500);
}

//Formatting functions that were built in to PDG cart
function FormatCurrency (value) {
	var Precision = 2;

	var valStr = "" + value;
	var valInt = Math.floor(value);
	var valFrac = value - valInt;
	var multiplier = " 1000000000000000000".substring(1,Precision+2);
	var valRoundedFrac = Math.floor (valFrac*multiplier + 0.5);
	valInt = Math.floor (Math.floor (value*multiplier + 0.5) / multiplier);
	var valFracStr = "000000000000000000" + valRoundedFrac;
	valFracStr = valFracStr.substring (valFracStr.length-Precision, valFracStr.length);
	
	valStr = "$" + valInt + "." + valFracStr;
	return valStr;
}
function FormatEuro (value) {
	var Precision = 2;

	var valStr = "" + value;
	var valInt = Math.floor(value);
	var valFrac = value - valInt;
	var multiplier = " 1000000000000000000".substring(1,Precision+2);
	var valRoundedFrac = Math.floor (valFrac*multiplier + 0.5);
	valInt = Math.floor (Math.floor (value*multiplier + 0.5) / multiplier);
	var valFracStr = "000000000000000000" + valRoundedFrac;
	valFracStr = valFracStr.substring (valFracStr.length-Precision, valFracStr.length);
	
	valStr = "" + valInt + "." + valFracStr + " EU";
	return valStr;
}

