//-------------------------------------------------------------------------------------------
// 		Tinie Tempah
//		01 Augutst 2011
//-------------------------------------------------------------------------------------------

$(document).ready(function(){

	///////----------------------------------------------------------------------------------
	// Section Jump Menu - This will jump to different sections of the website in dropdown
	// it won't fire if the first item is selected. TODO : Clean this function up a little.
	/////////////////////////////////////////////////////////////////////////////////////////
	$("#sectionSelector").change(function() {
		var e_first = $("#sectionSelector option:first").val();
		var e_selected = $("#sectionSelector option:selected").val();
		if(e_first != e_selected) { $("#categorySearch").submit(); }
	});
		
	
	///////----------------------------------------------------------------------------------
	// Currency Popups - if an element has a class of .currInfo - it will alert the message
	// below. This is a generic popup. One message fits all. This could be used to target ids
	// if different messages are required.
	/////////////////////////////////////////////////////////////////////////////////////////

	$('.currInfo').click(function(){
		var curr_pop_text = "This conversion is for guideline purposes only, because exchange rates vary constantly. We will charge you in British Pounds Sterling. Your bank / financial institution will then convert this into your currency.";
		alert(curr_pop_text);
	});
	
	
	///////----------------------------------------------------------------------------------
	// Product mini gallery - changes the source of #productImg to the source of the hovered
	// thumb image in #thumbs li img - on mouseout swaps it back to the origianl image src 
	/////////////////////////////////////////////////////////////////////////////////////////
	
	$('.images img:first').addClass('prdImage');
	
	// get the original product image source
	var origImage = $('.prdImage').attr('src');
	
	// do the swapping here
	$('#thumbnails li img').hover(function(){
		$('.prdImage').attr('src', this.src);
		$(this).css('cursor', 'pointer');
	},function() { 
		$('.prdImage').attr('src', origImage); // reset back to the original image src
	});



	
	///////----------------------------------------------------------------------------------
	// Search field - focus & blur - this uses the title attribute of the input for the text
	/////////////////////////////////////////////////////////////////////////////////////////
	
	// set the default text
	$('#searchField').val($('#searchField').attr('title'));
	
	// clear it out on focus
	$('#searchField').focus(function(){
		$(this).val("");
	});
	
	// if it's blank then reset back the title attribute
	$('#searchField').blur(function(){
		if(!$(this).val().length){
			$('#searchField').val($('#searchField').attr('title'));
		}
	});
	
});

///////----------------------------------------------------------------------------------
// Existing js - TODO - Move over to jquery
/////////////////////////////////////////////////////////////////////////////////////////

// productdetail.jsp - changing variant size

function changeVariantInProductInfo_Old(itemCount, variantBasePK, newVariant, oldVariant)
{
   	var variantFormName = "variantInBundle" + variantBasePK;
	var concreteProducts2 = document.forms["productOptions"].elements["concreteProducts2"].value;
	var search = concreteProducts2.search(oldVariant);
	alert(concreteProducts2);
	var newConcrete = concreteProducts2.replace(oldVariant,newVariant);
	alert(oldVariant + " : " + newVariant + " : " + newConcrete);
	document.forms[variantFormName].elements["SELCTEDVARIANT"].value=newVariant;
	alert(document.forms[variantFormName].elements["SELCTEDVARIANT"].value);
	document.forms["productOptions"].elements["concreteProducts2"].value=newConcrete;
}

function changeVariantInProductInfo(variantBasePK, newVariant)
{
   	var variantFormName = "variantInBundle" + variantBasePK;
	var concreteProducts2 = document.forms["productOptions"].elements["concreteProducts"].value;
	var oldVariant = document.forms[variantFormName].elements["SELCTEDVARIANT"].value;
	var newConcrete = concreteProducts2.replace(oldVariant,newVariant);
	document.forms[variantFormName].elements["SELCTEDVARIANT"].value=newVariant;
	document.forms["productOptions"].elements["concreteProducts"].value=newConcrete;
}


// productdetail.jsp - end

function changeVariantInBundle(itemCount, variantBasePK, newVariant, oldVariant)
{
	var productOptionsFormName = "productOptions"+itemCount;
	var variantFormName = "variantInBundle" + variantBasePK;
	var concreteProducts = document.forms[productOptionsFormName].elements["concreteProducts"].value;
	var search = concreteProducts.search(oldVariant);
	var newConcrete = concreteProducts.replace(oldVariant,newVariant);
	document.forms[variantFormName].elements["SELCTEDVARIANT"].value=newVariant;
	document.forms[productOptionsFormName].elements["concreteProducts"].value=newConcrete;
}
