// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


jQuery(document).ready(function($) {
	var counter = 1;	
	var active = false;
	var timerid;
	
	/// TIMER SWITCH - CHANGE VARIABLE TO TRUE TO ACTIVATE PRODUCT ROTATION
	var activatetimer = false;
	
	
	/////////// STARTS LOOP TO DISPLAY ROTATING PRODUCTS
	if(activatetimer){
		$('#testtimer').everyTime(3000, 'controlled', function() {
				
				timerid = ".timer" + counter;
					
					if(counter == 1)
					{
						tempcounter = 5;
						temptimer = ".timer" + tempcounter;
					}
					else
					{
						temptimer = ".timer" + (counter-1);
					}
					
					changeProduct(timerid,temptimer);
					
					
					if(counter == 5)
						counter = 1
					else
						counter++;
			});	
	  };
////////////////////////////// ABOVE HERE JUST START AND STOP BUTTONS - CAN IGNORE THESE
	

////////////////////// MOUSEOVER DOES NORMAL FUNCTIONALITY BUT ALSO ADDED LINES AS FOLLOWS

	jQuery("a.j_bestsellerlinks").mouseover(function (e) { 

		////// STOPS TIMER LOOP
		$('#testtimer').stopTime('controlled');
		
		 var image = $(this).attr("image");
		 var price = $(this).attr("price");
		 var name = $(this).attr("title");
		 var special_price = $(this).attr("special_price");
		 var site_id = $(this).attr("site_id");
		 var imagelink = $(this).attr("href");
		 var jquerycolour = $(this).attr("jquerycolour");
		  
		  /////// CSS LINES - SETS IMAGES USING INSTEAD OF CSS
		 $(timerid).css({
		 "color": "#666",
		 "background": ""		
		 });
		 
		 $(this).css({
		 "color": "#fff",
		 "background": "url(/ui/zoombits/bestseller_rollovers/roll_"+jquerycolour+".gif) repeat-x"		
		 });
		 
		 

		 var currency_symbol = $(".j_currency_symbol").html();
		 
		 $(this).parents(".j_bestsellerleft").next(".j_bestsellerright").find("img.j_bestsellerimage").attr({
			 src : image
		 });
		 
		 $(this).parents(".j_bestsellerleft").next(".j_bestsellerright").find("a.j_image_link").attr({
			 href : imagelink
		 });
		 
		 $(this).parents(".j_bestsellerleft").siblings(".j_bestsellerdescription").html(name);
		 $(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").children(".j_bestsellerprice").html("");
		 
		 
		 
		 switch(site_id) {
			 case "8":	var now = "Seulement ";
			 			var was = "&#201;tait ";
						break;
			 case "2":	var now = "Jetzt ";
			 			var was = "Vorher ";
						break;
			 default:	var now = "Now ";
			 			var was = "Was ";
						break;						
		 }
			 
			
		/*
		 switch(special_price) {
			 
			 case "£0.00" :
			 case "0,00 €" :
			 case "" :
					 	var new_price_string = '<span class="dark-pink price-large">'+price+'</span>';
						$(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_price_string);
						break;
			default:	
						var new_string = '<span class="price-small">'+was+"</span> <span class=\"price-small dark-pink\">"+price+"</span> <span class=\"price-small\">"+now+"</span> <span class=\"price-large dark-pink\">"+special_price+'</span>';
						$(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_string);
						break;
		}
		*/
		
		 check_special_price = stripAlphaChars(special_price);
			 
		 if(check_special_price > 0)
		 {
			 var new_string = '<span class="price-small">'+was+"</span> <span class=\"price-small dark-pink\">"+price+"</span> <span class=\"price-small\">"+now+"</span> <span class=\"price-large dark-pink\">"+special_price+'</span>';
			 $(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_string);				
		 }
		 else
		 {
			var new_price_string = '<span class="dark-pink price-large">'+price+'</span>';
			$(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_price_string);			 
		 }
		 
	})
	
	
	jQuery("a.j_bestsellerlinks").mouseout(function (e) { 
					
		////// SETS IMAGE BACK TO BLANK ONCE MOUSE OFF
		$(this).css({
		 "color": "#666",
		 "background": ""		
		 });
		
		
		/////////// STARTS LOOP TO DISPLAY ROTATING PRODUCTS
		if(activatetimer){
		$('#testtimer').everyTime(5000, 'controlled', function() {
				
				timerid = ".timer" + counter;
					
					if(counter == 1)
					{
						tempcounter = 5;
						temptimer = ".timer" + tempcounter;
					}
					else
					{
						temptimer = ".timer" + (counter-1);
					}
					
					changeProduct(timerid,temptimer);
					
					
					if(counter == 5)
						counter = 1
					else
						counter++;
			});	
		};
		 
	})

});


function changeProduct(timerid,temptimer)
{		
		//////// MAIN LOOP, BUG HERE

		$("a" + timerid).each(function(){ 
		
		 var image = $(this).attr("image");
		 var price = $(this).attr("price");
		 var name = $(this).attr("title");
		 var special_price = $(this).attr("special_price");
		 var site_id = $(this).attr("site_id");
		 var imagelink = $(this).attr("href");		 
		 var jquerycolour = $(this).attr("jquerycolour");

		 var currency_symbol = $(".j_currency_symbol").html();
		 
		 /////// THIS CAN HAVE THE IMAGES FADING IN AND OUT CLASS INSTEAD OF ID - MAYBE .EACH FUNCTION TO HIDE ALL THEN BELOW TO SHOW ALL - COULD BE SLOW?
		 $(".fader").each(function(){ 
		 $(this).hide();
		 });

		 $(this).parents(".j_bestsellerleft").next(".j_bestsellerright").find("img.j_bestsellerimage").attr({
			 src : image
		 });
		 
		 $(this).parents(".j_bestsellerleft").next(".j_bestsellerright").find("a.j_image_link").attr({
			 href : imagelink
		 });
		 
		 $(this).parents(".j_bestsellerleft").siblings(".j_bestsellerdescription").html(name);
		 $(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").children(".j_bestsellerprice").html("");
		 
		 // CODE HERE TO FADE IMAGE IN IF WE WANT TO BRING IT IN LATER
		 $(".fader").each(function(){ 
		 $(this).fadeIn("slow");
		 });
		 
		 ////////// AGAIN CSS HERE TO SET COLOUR IMAGES
		 $(this).css({
		 "color": "#fff",
		 "background": "url(/ui/zoombits/bestseller_rollovers/roll_"+jquerycolour+".gif) repeat-x"		
		 });
		 
		 $(temptimer).css({
		 "color": "#666",
		 "background": ""		
		 });
		 
		 
		 switch(site_id) {
			 case "8":	var now = "Seulement ";
			 			var was = "&#201;tait ";
						break;
			 case "2":	var now = "Jetzt ";
			 			var was = "Vorher ";
						break;
			 default:	var now = "Now ";
			 			var was = "Was ";
						break;						
		 }
			 
			 
		  check_special_price = stripAlphaChars(special_price);
			 
			 if(check_special_price > 0)
			 {
				 var new_string = '<span class="price-small">'+was+"</span> <span class=\"price-small dark-pink\">"+price+"</span> <span class=\"price-small\">"+now+"</span> <span class=\"price-large dark-pink\">"+special_price+'</span>';
				 $(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_string);				
			 }
			 else
			 {
				var new_price_string = '<span class="dark-pink price-large">'+price+'</span>';
				$(this).parents(".j_bestsellerleft").siblings(".bestsellerprice").html(new_price_string);			 
			 }
		
		
		//});
	});
}




function stripAlphaChars(pstrSource) 
{ 
var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}