jQuery(function($) {
	
	//Nav	
	$('#nav h1 a').each(function(){
		var _text = $(this).text();
		$(this).replaceWith('<span>'+_text+'</span>');
	});
	
	//Alpha numeric
	$('.quantity, .quantity input').numeric();
	
	//Auto update Cart
	var $update = $('#update');
	if ($update[0]) {
		
		//Hide the button
		$update.hide();
		
		//Auto update on quantity change
		$quantity = $('input.quantity');
		$quantity.bind('keyup', function(){
			$update.click();
		});
		
		//Auto delete items 
		$del = $('a.del');
		$del.click(function(){
			$(this).parent().siblings('.price-quant').find('.quantity').val(0);
			$update.click();
			return false;
		})
	}
	
	//Mark Special Price
	var $normalPrice = $('#normal-price');
	var $specialPrice = $('#special-price');
	var $priceLabel = $('#price-label');
	
	if(parseFloat($normalPrice.text()) == parseFloat($specialPrice.text())){
		$normalPrice.addClass('special');
		$priceLabel.text('Tilbudspris:');
	} 
	
	
	var $imageWrap = $('.slideshow .csc-textpic-imagewrap').removeClass('csc-textpic-imagewrap').addClass('csc-textpic-imagewrap-special');
	
	var _width = $imageWrap.width();
	var slideshowHeight = 0;
	
	$('.slideshow')
		.find('img').each(function(){
			imageHeight = $(this).height();
			$(this).parents('dl').css('margin-left',(_width-$(this).parent('dt').width())/2)
			if (slideshowHeight < imageHeight) slideshowHeight = imageHeight;
		});
		
	$imageWrap.cycle({
	    timeout: 3000
	}).height(slideshowHeight+20).width(_width);
	
	//Validation
	$("#tx-ttproducts-pi1-basket form").validate({
		rules: {
            		'recs[personinfo][name]': 'required',
            		'recs[personinfo][address]': 'required',
            		'recs[personinfo][zip]': 'required',
            		'recs[personinfo][city]': 'required',
            		'recs[personinfo][email]': 'required email',
            		'recs[personinfo][telephone]': 'required',
            		'recs[personinfo][agb]': 'required'            		
            	},
            	messages: {
            		'recs[personinfo][zip]': '',
            		'recs[personinfo][agb]': 'Du m� akseptere kj�psvilk�rene'
            	}

	});

	 $.validator.messages = {
	 	'required': 'Dette er et p�krevd felt',
	 	'email': 'Dette er ikke en gydlig e-post adresse'
	 }
	
});

/*
 * Bring Shipping Calculator
 */
jQuery(function($){
  
  var button = $('#bring-shipping-button').parent().hide(),
      pageURL = document.location.href
      submitButton = $('#basket-submit');    

  if(pageURL.indexOf('herbalife.penelene.no') >= 0) {
		button.click(function(){
			$.fancybox({
				padding: 30,
				height: 460,
				width: 650,
				href: 'index.php?id=154',
				type: 'iframe'
			});
			return false;
		});
  }else{
  		button.click(function(){
			$.fancybox({
				padding: 30,
				height: 460,
				width: 650,
				href: 'index.php?id=152',
				type: 'iframe'
			});
			return false;
		});
  }
  
  
  //Active Item
  $('#bring-items').click(function(e){
    var radio = $(':radio:checked', this),
        li = $('li', this);    
    li.removeClass('active');
    radio.parents('li').addClass('active');    
  }); 
  
  //Only show the button when Post is selected
  if ($('#shipping-select').val() === '10') {
    button.show();    
    if ($('#shipping-tax').text() === '0.00') {
      button.click();
      submitButton.hide();
    }
  }
   
});


