$(document).ready(function() {

	// Get the rel value if set, and set it as the 
	$('input').each(function() {
    	if ($(this).attr('rel')) {
    		$(this).css('color', '#ccc');
    		$(this).val( $(this).attr('rel') );
    		$(this).bind('click focus', function(e) {
    			e.stopPropagation();
    			if ($(this).val() == $(this).attr('rel')) {
    				$(this).css('color', '#000');
    				$(this).val('');
    			}
    		});
    	}
  	});
  	
  	$('input').each(function() {
  		$(this).bind('blur', function() {
	    	if ($(this).attr('rel')) {
	    		if ($(this).val() == '') {
	    			$(this).css('color', '#ccc');
	    			$(this).val( $(this).attr('rel') );
	    		}
	    	}
	    });
	});
  	
  	
  	
  	// Main product image swap
  	if ($('#subImages').length) {
  		$('#subImages .alt').each(function() {
  			$(this).click(function() {
  				imgurl = $(this).attr('href');
  				$('#mainImage img').attr('src', imgurl);
  				return false;
  			});
  		});
  	}
  
  	
  	// If there's a temporary message fade it out.
  	if ($('#tempMessage').length) {
  		$('body').ready(function() {
  			$('#tempMessage').delay(3000).slideUp();
  		});
  	}
  	
  	
  	// Checkboxes on delivery page
  	if ($('#deliveryOptions').length) {
  		$('#deliveryOptions .inputCheck input').each(function() {  		
  			$(this).click(function() {
  				postalValue = $(this).attr('id');
  				postalAmnt  = parseFloat($(this).val());
  				postalID    = postalValue.replace('check_', '');
  				$('#deliveryOptions .inputCheck input').each(function() {
  					$(this).attr('checked', false);
  				});
  				$(this).attr('checked', true);
  				$('#delivery_option_id').val(postalID)
  				$('#delValue').html(postalAmnt.toFixed(2));
  				newTotal = parseFloat(postalAmnt + parseFloat($('#OP_full_total').val())).toFixed(2);
  				$('#absoluteValue').html('&pound;'+newTotal);
  			});
  		});
  	}
  	orgPostage = parseFloat($('#OP_default_postage_price').val()).toFixed(2);
  	$('#delValue').html(orgPostage);


	// Submit a form on enter.
	if ($('#password').length) {
		$("#password").keyup(function(e) { 
			if ($(this).hasClass('enterSubmit')) {
				if(e.keyCode == 13) { 
					$(this).closest("form").submit(); 
				}
			}
		}); 
	}

});


// product submit
function submitProduct(gobackto) {
	if (typeof gobackto == 'undefined') {
		$('#productForm').submit();		
	} else {
		$('#productForm').append('<input type="hidden" value="'+gobackto+'" name="returnurl" />');
		$('#productForm').submit();	
	}
}




/* BASIC HP SLIDER -------------------------------*/

	var slideWidth  = 0;
	var onThisSlide = 0;
	var tmer; 
	var numOfSlides = 0;
	var Sinterval   = 5000;
	var Stransition = 500;
	
	$(window).load(function () {
	    if ($('#slider').length) {
			slideWidth      = $('#slider').width();
			slideHeight     = $('#slider').height();
			numOfSlides     = $('.slide').length;
					
			$('#sliderContainer').height(slideHeight);
			
			if (numOfSlides > 1) {
				
				$('#dots').css({'left':(slideWidth+15)+'px'});
				for (i = 0; i < numOfSlides; i++) {
					$('<a href="" id="slideDot'+i+'"></a>').appendTo('#dots');
				}
				$('#dots a#slideDot0').addClass('on');
			
				$('#sliderContainer').width(slideWidth * 3);
				$('#sliderContainer').css({'left':'-'+slideWidth+'px'});
			
				$('.slide').each(function(index, value) {
					$(this).attr('id', 'mySlide'+index);
					$(this).css({'width':(slideWidth)+'px','height':(slideHeight)+'px'});
					if (index == 0) {
						$(this).css({'top':'0','left':(slideWidth)+'px','opacity':'1'});
					} else {
						$(this).css({'top':'0','left':(slideWidth*2)+'px','opacity':'0'});
					}
				});
				
				tmr = setInterval("goToSlide()",Sinterval);
				
			} else {
				$('.slide').css({'top':'0','left':'0','opacity':'1'});
			}
			
	    }
	    
	    $('#slider').hover(function () {
				pauseSlide();
			},
			function () {
				unPauseSlide();
			}
		);
	    
	});
	
	function goToSlide () {
		
		$('#mySlide'+onThisSlide).animate({
			'left':'0',
			'opacity':'0'
			}, Stransition, function() {
				$('#mySlide'+onThisSlide).css({'left':(slideWidth*2)+'px', 'opacity':'0'});
			});
		
		if (onThisSlide + 1 == numOfSlides) {
			nextSlide = 0;
		} else {
			nextSlide = onThisSlide + 1;
		}
		
		$('#mySlide'+nextSlide).animate({
			'left':slideWidth+'px',
			'opacity':'1'
			}, Stransition, function() {
				onThisSlide++;
				if (onThisSlide == numOfSlides) onThisSlide = 0;
				$('#dots a').each(function(index, value) {
					$(this).removeClass('on');
				});
				$('#dots a#slideDot'+nextSlide).addClass('on');
				pauseSlide();
				unPauseSlide();
			});
	}
	
	function pauseSlide () {
		clearInterval(tmr);
	}
	function unPauseSlide () {
		tmr = setInterval("goToSlide()",Sinterval);
	}

/*------------------------------------------------*/






/*------------------------------------------------*/
/* http://www.adamcoulombe.info/lab/jquery/select-box/ 
   -- custom select box */
   
(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);

$(document).ready(function(){
	$('select').customStyle();
});

/*------------------------------------------------*/


