/**
 * Using jQuery in Magento requires noConflict() mode
 */
 (
	 /** @param {jQuery} $ jQuery Object */
 	function($) {
	$(document).ready(function(){	
		//Preload any hover images
		$('img.wh-roll').each(function(){
			this.ExtraImage= new Image();
			this.ExtraImage.src = addSuffix(this.src,'_on');
		});
		//Create roll over images on any image with wh-roll as its class
		$('img.wh-roll').hover(
			function(){
				//Over function
				this.src = addSuffix(this.src,'_on');
			},
			function(){
				//Out function
				this.src = remSuffix(this.src,'_on');
			});
		//If the user clicks in the subscribe inputs then clear out the default text
		$('.block-mailing input[type="text"]')
			.one("click", function(){
			$(this).attr("value","");
		});	
		//Animate the designers menu
		$('#cat-designers ul')
			.prepend('<li class="first open">choose a designer</li>');
		$('#cat-designers ul li:gt(0)')	
			.hide();
		$('#cat-designers ul li:eq(0)')	
			.toggle(
				function (){
					$('#cat-designers ul li:gt(0)').slideDown();
					$(this).removeClass('open')
						.addClass('close');
				},
				function(){
					$('#cat-designers ul li:gt(0)').slideUp();
					$(this).removeClass('close')
						.addClass('open');
				});
				
		//Animate the product groups list
		$('.product-classification-box li p').hide();
		var label = $('.product-classification-box ul').append('<li class="label_icon"></li>').find('li:last');
		$('.product-classification-box li').hover(
			function(){
				$(label).text( $(this).children('p').text() );
			},
			function(){
				$(label).text('');
			});		
		//Product page - gallery switcher
		$('.more-views a').each(function(i){
			$(this).bind('mouseover', {index:i},function(){
				imgArray = $('.product-image a');
				$('.product-image a').hide();
				//$('.product-image a')[i].show();
				$(imgArray[i]).show(); //IE doesn't like index addressing the jQuery array directly
				})
		}) 		
	});			
	/**
	* Helper functions
	*/
	function remSuffix(a,b){
		//removes suffix b from filename a without removing the file extension
		var lastdot = a.lastIndexOf(b + '.');
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot+b.length,a.length);
			return a.slice(0,lastdot) + ext;
		}		
	}
	function addSuffix(a,b){
		//adds suffix b to filename a without removing the file extension
		var lastdot = a.lastIndexOf(".");
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot,a.length);
			return a.slice(0,lastdot) + b + ext;
		}		
	}  
 })(jQuery)