/***************************
 * SITE-SPECIFIC FUNCTIONS *
 ***************************/
 var jukebox = null;

function openJukebox() {
	jukebox = window.open( "", "jukebox", "width=420,height=340,scrollbars=no" );
	if ( !jukebox || jukebox.closed || !jukebox.broadcast ) {
		jukebox = window.open( "/jukebox.php", "jukebox", "width=320,height=300,scrollbars=no" );
	}
}

function toggleMusic() {
	 openJukebox();
	 jukebox.document.getElementById('mpl').sendEvent('PLAY');
}

function launchJukebox() {
	jukebox = window.open( "", "jukebox", "width=420,height=340,scrollbars=no" );
	if ( !jukebox || jukebox.closed || !jukebox.broadcast ) {
		jukebox = window.open( "/jukebox.php", "jukebox", "width=420,height=340,scrollbars=no" );
		jukebox.focus();
	}
	else {
		jukebox.focus();
	}
}

function confirmStoreTerms() {
	var msg = 'All orders are built and shipped according to the date submitted (first in = first out).\n\nOnce your order is placed it is non-cancelable and payment is non-refundable.\n\nLead times (quotes) are approximations at best and that being said we will do our best to fill your order in a timely manner.\n\nBy continuing you certify that you understand and agree to these store terms and conditions.';
	if (!confirm(msg)) {
		return false;	
	}
}
 
/*********************
 * UTILITY FUNCTIONS *
 *********************/
 
function sendMail(user,domain,tld,subject,message) {
	if (!user) user = 'info';
	if (!domain) domain = 'swellpedals';
	if (!tld) tld = 'com';
	locationstring = 'mailto:' + user + '@' + domain + '.' + tld;
	if (subject) locationstring += '?subject=' + encodeURIComponent(subject);
	if (message) locationstring += '&body=' + encodeURIComponent(message);
	window.location = locationstring;
}

function sendExternalMail(subject,message,cc) {
	locationstring = 'mailto:';
	if (subject) locationstring += "?subject=" + encodeURIComponent(subject);
	if (message) locationstring += "&body=" + encodeURIComponent(message);
	if (cc) locationstring += "&cc=" + encodeURIComponent(cc);
	window.location = locationstring;
}

function openWindow(url,features) {
	var newWin = window.open(url,'popup',features);
	newWin.focus();
}

function closeWindow() {
	if (window.opener) {
		self.close();	
	}
}

function printPage() {
	if (window.print != null) {
		window.print();
	}
	else {
		alert("To print this page, please select Print from your browser's File menu.");
	}
}

function preload() {
	var path = '/images/';
	var images = new Array(
		'bto/enclosure/banana.png',
		'bto/enclosure/classic-cream.png',
		'bto/enclosure/coco.png',
		'bto/enclosure/cool-mint.png',
		'bto/enclosure/grape.png',
		'bto/enclosure/ht-black.png',
		'bto/enclosure/ht-blue.png',
		'bto/enclosure/ht-gray.png',
		'bto/enclosure/ht-green.png',
		'bto/enclosure/lime.png',
		'bto/enclosure/none-more-black.png',
		'bto/enclosure/orange.png',
		'bto/enclosure/plum.png',
		'bto/enclosure/raw.png',
		'bto/enclosure/tomato-soup.png',
		'bto/enclosure/sno-white.png',
		'bto/enclosure/tru-blue.png',
		'bto/screen/black.png',
		'bto/screen/blue.png',
		'bto/screen/cream.png',
		'bto/screen/red.png',
		'bto/screen/yellow.png',
		'bto/knobs/black.png',
		'bto/knobs/blue.png',
		'bto/knobs/brown.png',
		'bto/knobs/cream.png',
		'bto/knobs/green.png',
		'bto/knobs/grey.png',
		'bto/knobs/orange.png',
		'bto/knobs/purple.png',
		'bto/knobs/red.png',
		'bto/knobs/white.png',
		'bto/knobs/yellow.png'
	);
	var preload = new Array('');
	var j = images.length;
	for (var i=0; i<images.length; i++) {
		preload[j] = new Image;
		preload[j++].src = path + images[i];
	}
}

function externalLinks() {
	$('a[rel*=external]').attr('target','_blank');
	$('.menu a[rel*=external], .sf-menu a[rel*=external]').append('&nbsp;<img src="/images/external.png" alt="External Link" />');
	$('a[rel*=external]:not(.sf-menu a, .menu a, a.image)').after('&nbsp;<img src="/images/external.png" alt="External Link" />');
}

function filesToBlank() {
	$('a[href$=.pdf],a[href$=.PDF]').attr('target','_blank');//.append(' [PDF]');
	$('a[href$=.doc],a[href$=.DOC]').attr('target','_blank');//.append(' [DOC]');
	$('a[href$=.ppt],a[href$=.PPT]').attr('target','_blank');//.append(' [PPT]');
	$('a[href$=.pps],a[href$=.PPS]').attr('target','_blank');//.append(' [PPS]');
}

function makeRollovers(){
	$('a.rollover').each(function() {
		var img = new Image();
		var upimage = $(this).children().eq(0).attr('src');
		img.src = upimage.replace('_up','_ov');
	});
	$('a.rollover').hover(
		function(){
			var overimage = $(this).children().eq(0).attr('src').replace('_up','_ov');
			$(this).children().eq(0).attr('src',overimage);
		},
		function(){
			var upimage = $(this).children().eq(0).attr('src').replace('_ov','_up');
			$(this).children().eq(0).attr('src',upimage);
		}
	);
}

function actionLinks() {
	$('a.action').after(' &#187;');
}

function collapseDefinitionLists() {
	$('dl.collapse dd').hide();
	$('dl.collapse dt').css('cursor','pointer');
	$('dl.collapse dt').hover(function(){$(this).addClass('sfHover');},function(){$(this).removeClass('sfHover');});
	$('dl.collapse dt').click(function(){
		$('dl.collapse dd:visible').slideUp('fast');
		$(this).next('dd').slideDown('fast');
	});
}

function makeBigLinks() {
	$('.biglink').each(function(){
		var url = $(this).find('a:first').attr('href');
		$(this).hover(function(){
				$(this).css('borderColor','#c5c5c5').css('cursor','pointer');
				window.status = url;
			},
			function(){
				$(this).css('borderColor','#292929').css('cursor','arrow');
				window.status = '';
		});
		$(this).click(function(){window.location=url;});
	});
}

function styleBlockquotes() {
	//$('blockquote p.cite').prev().append('&#8221;');
	$('blockquote p:not(:first-child, .cite)').prepend('&#8220;');
	$('.cite').prepend('&#8212;');
}

function validate() {
	var errors = false;
	$('p.error').remove();
	$('#contact-form .required').each(function() {
		$(this).removeClass('error');
		if ($(this).val() == '') {
			$(this).addClass('error');
			$(this).after('<p class="error">This field is required.</p>');
			errors = true;
		}
	});
	if (! errors) return true;
	return false;
}

function init() {
	externalLinks();
	filesToBlank();
	makeRollovers();
	actionLinks();
	$('#nav').superfish();
	//$('.sf-menu li li, .sf-menu li li li').css('opacity','0.9');
	collapseDefinitionLists();
	makeBigLinks();
	styleBlockquotes();
	$('table.data tr:even').addClass('alt');
	$('#music-on-off').hide().slideDown('slow');
	//$('.storeform').submit(function(){return confirmStoreTerms()});
	preload();
}
