/*
	display.js
	
	Javascripts for ~~ website
*/

function catalogApp() {
	// initialize all the buttons
	$('.colorswatch img').each(function() {
		var thisrc = $(this).attr('src');
		$(this).attr('rsrc',thisrc);
		var thisosrc = thisrc.replace(/\.(jpg|gif|png)/,"_on.$1");
		$(this).attr('osrc',thisosrc);
	});
	// set the button relevant to the displayed product pic
	var splashsrc = $('#case_splash_img').attr('src');
	var splashcolor = splashsrc.substring(splashsrc.lastIndexOf('_')+1,splashsrc.lastIndexOf('.'));
	var buttonsrc = $('#button_'+splashcolor+' img').attr('osrc');
	$('#button_'+splashcolor+' img').attr('src',buttonsrc);
	// configure the click action for all buttons
	$('.colorswatch').click(function() {
		// first neutralize all the other buttons...
		var thisid = $(this).attr('id');
		$('.colorswatch img').not('#'+thisid+' img').each(function() {
			$(this).attr('src',$(this).attr('rsrc'));
		});
		// ...then set this button.
		var newsrc = $('#'+thisid+' img').attr('osrc');
		$('#'+thisid+' img').attr('src',newsrc);
		// ...then set the main image.
		var color = thisid.substring(thisid.lastIndexOf('_')+1);
		srcreplace = '_'+color+'.';
		var oldsplashsrc = $('#case_splash_img').attr('src');
		var oldreplace = oldsplashsrc.substring(oldsplashsrc.lastIndexOf('_'),oldsplashsrc.lastIndexOf('.')+1);
		var newsplashsrc = oldsplashsrc.replace(oldreplace,srcreplace);
		$('#case_splash_img').attr('src',newsplashsrc);
		$('#add_to_cart_right input[name=product]').val('Voi Lorem ' + $(this).text());
		$('#add_to_cart_right #case_color').text('Color: ' + $(this).text());
		
	});
	// now generate the magic linky BUY NOW buttons!
	$('#add_to_cart_right').html($('#add_to_cart').html()); // #add_to_cart must be in content area
	$('#add_to_cart').empty();
	$('#add_to_cart_right input[value^=Voi]').val('Voi Lorem ' + $('#button_'+splashcolor).text());
	$('#add_to_cart_right #case_color').text('Color: ' + $('#button_'+splashcolor).text());
	// preload all the other images
	var srctemp = '';
	var colors = [];
	var preloadreplace = [];
	var buttonlist = $('.colorswatch');
	for(var a=0;a<buttonlist.length;a++) {
		colors[a] = buttonlist[a].id.substring(buttonlist[a].id.lastIndexOf('_')+1);
		srctemp = $('#case_splash_img').attr('src');
		srcreplace = '_'+splashcolor+'.';
		preloadreplace[a] = new Image();
		preloadreplace[a] = srctemp.replace(srcreplace,'_'+colors[a]+'.');
	}
}

// preload random images needed by various things.
function extraPreloader (piclist) {
	picPre = new Array();
	for (a=0;a<piclist.length;a++) {
		picPre[a] = new Image();
		picPre[a].src = imgPath + piclist[a];
	}
	return;
}

// emulates :hover effect for IE6.
function hoverFixjQ() {
	$('.buttons *').hover(function(){$(this).addClass('over');},function(){$(this).removeClass('over');});
	return;
}

// Tests for MSIE version. Returns version whole number
function isIE() {/*@cc_on
if(@_jscript_version < 5.6){return 5;}if(@_jscript_version < 5.7){return 6;}if(@_jscript_version < 5.8){return 7;}if(@_jscript_version < 5.9){return 8;}
@*/ return 0;}
// variable display for debugging. Requires jquery
function vt() {
	if (!document.getElementById('debug')) { return; }
	var bar = vt.arguments;
	var foo = new Array();
	for (var a=0;a<bar.length;a++) {
		var p = a;
		foo[a] =  '<span style="color:#900;">'+p+' =></span>\r     '+bar[a].toString()+'\r';
	}
	$('#debug').html('<pre>'+foo.join('\r')+'</pre>');
	return;
}
function launchAll() {
	if (isIE && isIE<7) { hoverFixjQ(); }
	if (document.getElementById('case_splash')) { catalogApp(); }
}
window.onload = launchAll;

/* EOF */

