$(document).ready(function(){
	$('.tree > li > a').addClass('category-parent')
	$('.tree > li:first-child').addClass('first')
	$('.category-parent').click(function(){return false;})
	$('#categories_block_left h4').css('display', 'none')
	$('#categories_block_left .tree li ul').addClass('cat-children')
	
	
	
	/** Reorder Christmas Wrapping **/
	//$('.christmas_wrap').prependTo($('#color_to_pick_list'));
	$('.common_wrap').insertAfter($('#color_to_pick_list li:first-child'))
	$('.christmas_wrap').insertAfter($('#color_to_pick_list li:first-child'))
	
	
	/**** MAKE breadcrumbs unclickable ****/

	
	$('#offer-submit').click(function(){
		$('input[name=offer-email]').removeClass('errors')
		if($('input[name=offer-email]').val() != 'email address' && $('input[name=offer-email]').val() != ''){
			$.ajax({
			  type: "POST",
			  url: "themes/johnson/submit.php",
			  data: 'email='+$('input[name=offer-email]').val(),
			  success: function(response){
			  	$('.special-offers-inner').html('')
			  	$('.special-offers-inner').html('<h3>Thanks</h3>')
			  	$('.special-offers-foot').html('')
			  	$('.special-offers-foot').html('<h3>Thanks</h3>')
		    }
});
		}else{
			$('input[name=offer-email]').addClass('errors')
		}
	})
	
	
	$('.breadcrumb .apparel-gear, .occasions, .shopbysize, .shopbyflavor, .corporateorders, .celebrations, .bestsellers, .shopbyprice, #slideshow a').click(function(){
		return false;	
	});
	
		if($('body').attr('id') == 'index'){
		$('#slideshow').cycle({
			fx: 'fade',
			timeout: 6000,
			before: function(){$(this).css('visibility', 'visible')}	
		});
	}
	
	if($('body').attr('id') == 'product'){	
		$("input[type='radio']").custCheckBox();
		
		
			/*** Getting the incoming narrow for a product.  
			Eg. If got to product by looking for Peanut Crunch Products
			***/
			if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

			var how_got_here = $('.breadcrumb').text()
			how_got_here = how_got_here.trim();
			how_got_here = how_got_here.split('>')

				if(how_got_here[how_got_here.length-2] == 'Home' || how_got_here[how_got_here.length-3] == 'Home' ){}else{
				$('input[name=Flavors]').attr('checked', false)
				//console.log(how_got_here[how_got_here.length-2])
				if(how_got_here[how_got_here.length-2] != 'Caramel' && how_got_here[how_got_here.length-2] != 'Chocolate Drizzle' && how_got_here[how_got_here.length-2] != 'Cheddar Cheese' &&  how_got_here[how_got_here.length-2] != 'Peanut Crunch' &&  how_got_here[how_got_here.length-2] != 'Platinum Edition'){
					$('.flavors_group .cust_radio_on').removeClass('cust_radio_on').addClass('cust_radio_off')
					$('input[alt=Caramel]').attr('checked', true).prev().addClass('cust_radio_on').removeClass('cust_radio_off')
				}else{
					$('.flavors_group .cust_radio_on').removeClass('cust_radio_on').addClass('cust_radio_off')
					$('input[alt='+how_got_here[how_got_here.length-2]+']').attr('checked', 'checked').prev().addClass('cust_radio_on').removeClass('cust_radio_off')
				}
		if($('.flavors_group').html()){
				findCombination()
			}
			
			}				
	}

	

	var cart_text = $('.cart_last_product_content .recent_cart_text').text();
	
	cart_text = cart_text.replace('1No Wrap', 'No Wrap')
	$('.cart_last_product_content .recent_cart_text').text('').text(cart_text)
	//console.log($('.cart_last_product_content a').text())
	
	

	
	$('.cart_description_a').each(function(){
		cart_text = $(this).text()
		cart_text = cart_text.replace('1No Wrap', 'No Wrap').replace('4Chocolate Drizzle', 'Chocolate Drizzle').replace('2Caramel', 'Caramel').replace('5Platinum Edition', 'Platinum Edition').replace('2Peanut Crunch', 'Peanut Crunch').replace('3Cheddar Cheese', 'Cheddar Cheese').replace('1Small', 'Small').replace('2Medium', 'Medium').replace(/,/g, '<br />')
		$(this).text('').html(cart_text)
		
	})
	
	
	/** Error Handling **/
	if($('.the_errors').text() != ''){
		$('.errors').removeClass('errors')
		var the_errors = jQuery.trim($('.the_errors').text().replace(/\n/g, ''))

		the_errors = the_errors.split('^')
		//console.log(the_errors)
		for(var i = 0; i < the_errors.length; i++){

			if(jQuery.trim(the_errors[i]) == 'someonehasalreadyregisteredwiththise-mailaddress'){
				$('#email_create').addClass('errors')
				$('#email_create').parent().next().append('<span class="required_field">E-mail address already registered</span>').show()				
			}


			if(jQuery.trim(the_errors[i]) == 'Invalide-mailaddress'){
				if($('#email_create').val() == ''){
					$('#email').addClass('errors')
				$('#email').parent().next().append('<span class="required_field">Invalid E-mail address</span>').show()
				}else{
				
					$('#email_create').addClass('errors')
				
					$('#email_create').parent().next().append('<span class="required_field">Invalid E-mail address</span>').show()
				}
			}
			
			
			
			if(jQuery.trim(the_errors[i]) == 'E-mailaddressrequired'){
				$('#email').addClass('errors')
				$('#email').parent().next().append('<span class="required_field">E-mail Required</span>').show()
			}
			
			
			if(jQuery.trim(the_errors[i]) == 'Authenticationfailed'){
				$('#email').addClass('errors')
				$('#passwd').addClass('errors')
				$('#passwd').parent().next().append('<span class="required_field">Authentication Failed</span>').show()
			}
			
		
			if(jQuery.trim(the_errors[i]) == "thisdiscountisn'tapplicabletothatproductcategory"){
				$('#discount_name').addClass('errors')
				$('.error_area').text('Discount does not apply to this product').show()
				$('.discount_area').css('width', 590)
			}
			
			
			if(jQuery.trim(the_errors[i]) == "vouchernamenotvalid"){
				$('#discount_name').addClass('errors')
				$('.error_area').text('Invalid Voucher').show()
				$('.discount_area').css('width', 457)
			}
			
			
			if(jQuery.trim(the_errors[i]) == 'password'){
					$('#passwd').addClass('errors')
				}
			
			
			
			if($('#'+jQuery.trim(the_errors[i])).val() == ''){
				
				
				$('#'+jQuery.trim(the_errors[i])).addClass('errors')
				//$('#'+jQuery.trim(the_errors[i])).parent().next().append('<span class="required_field">'+$('#'+jQuery.trim(the_errors[i])).parent().prev().find('label').text()+' is required</span>').show()
			}
		}
		
		
		if($('#passwd').val() == ''){
			
			if($('#email_create').val() == ''){
				$('#passwd').addClass('errors')
				$('#passwd').parent().next().append('<span class="required_field">Invalid Password</span>').show()
			}
		}
		
		if($('#customer_firstname').val() == ''){
			$('#customer_firstname').addClass('errors')
		}
		if($('#customer_lastname').val() == ''){
			$('#customer_lastname').addClass('errors')
		}
		
		if($('#phone').val() == ''){
			$('#phone').addClass('errors')
		}
		
	}
	if($('body').attr('id') == 'product'){
		//Highlight selected wrap
		var title = $('#group_8 option:selected').attr('title')
		if(title != undefined){
			title = title.replace('1','')
			$('#color_to_pick_list a[title='+$('#group_8 option:selected').attr('title')+']').addClass('active_wrap')
			$('#current-wrap').text(title)
		}
	}	
	
	$('.cust_checkbox').click(function(){
		var clicked = $(this).parent().attr('class')
		findCombination()
		
	})
	

	
})
