// JavaScript Document
jQuery(document).ready(function(){
	
/* jQUERY goes here  */
	
	// home page bottom gap fix 
	if(jQuery('#home_bottom').length > 0){
		var inner_height = jQuery('#inner_container').height();
		var fixed_height = inner_height - 21;
		jQuery('#inner_container').css('height', fixed_height);
	}
		
	// home page news rotator
	jQuery('#home_rotator_items').cycle({ 
		    fx: 'fade', 
			pager: '#rotator_pager',
			timeout: 3000
		});	
	
	jQuery('#btn_show_long_desc').click(function(e){
		 e.preventDefault();
		 jQuery('#short_desc').slideUp('slow', function(e){
										 jQuery('#long_desc').slideDown('slow');				
														});
		
		 });
	
	jQuery('#btn_show_short_desc').click(function(e){
		 e.preventDefault();
		 jQuery('#long_desc').slideUp('slow', function(e){
										 jQuery('#short_desc').slideDown('slow');				
														});
		 });
	
	jQuery('#ing_list li .ing_ttl').toggle(function(e){
			e.preventDefault();
			jQuery(this).parent().addClass('expanded')			
			jQuery(this).siblings('.ing_info, .btn_ing_close').show();
		}, function(e){
			e.preventDefault();
			jQuery(this).parent().removeClass('expanded');
			jQuery(this).siblings('.ing_info, .btn_ing_close').hide();
		});
	
	jQuery('.btn_ing_close').click(function(e){
		e.preventDefault();									
		jQuery(this).siblings('.ing_ttl').trigger('click');									
		});
	
	/*
	jQuery('.product .link').hover(function(){
		  jQuery(this).siblings('.images').children('.p_image').animate({opacity: 0.6}, 400);
		},function(){
			jQuery(this).siblings('.images').children('.p_image').animate({opacity: 1}, 400);
		}); 
	*/
	
	// about us equal columns
	if (jQuery('#about_us .col').length > 0){
		var highestCol = 0;
		jQuery('#about_us .col').each(function(){
			var colHeight = jQuery(this).height();
			if( colHeight > highestCol){
				highestCol = colHeight;	
			}
		});
		jQuery('#about_us .col').height(highestCol);
	}
	
	
	// expanding faqs menu
	jQuery('#faqs_nav a.cat').click(function(e){
		e.preventDefault();
		var catID = jQuery(this).get(0).id;
		jQuery('#faqs_nav .open').children('ul').slideUp('slow', function(){
			jQuery('#faqs_nav .open').removeClass('open');	
			jQuery('#' +catID).parent().addClass('open');
			jQuery('#' +catID).siblings('ul').slideDown('slow');
			jQuery('#' +catID).parent().children('ul').children('li').children('a.faq_link:first').trigger('click');
		});
	});
	/*
	jQuery('#faqs_nav .open a.cat').click(function(e){
		e.preventDefault();
	});*/
	
	jQuery('#faqs_nav li li a').click(function(e){
		e.preventDefault();
		var linkID = jQuery(this).get(0).id;
		jQuery('.faq').hide();
		jQuery('.faq_' +linkID).show();
		jQuery('#faqs_nav li.active').removeClass('active');
		jQuery(this).parent().addClass('active');
	});
	
	
	jQuery('#ask_the_experts_nav li a').click(function(e){
		e.preventDefault();
		var linkID = jQuery(this).get(0).id;
		jQuery('.faq').hide();
		jQuery('.faq_' +linkID).show();
		jQuery('#ask_the_experts_nav li.active').removeClass('active');
		jQuery(this).parent().addClass('active');
	});
	
	
	jQuery('#close_cart').click(function(e){
		e.preventDefault();								 
		jQuery('#ajax_cart_container').hide();	
		jQuery('#btn_shopping_bag').removeClass('active');
		if (jQuery.browser.msie) {
			jQuery('#home_rotator').css('visibility', 'visible');
		}
	});
	
	jQuery('#btn_shopping_bag').click(function(e){
		e.preventDefault();								 
		jQuery('#ajax_cart_container').show();	
		jQuery('#btn_shopping_bag').addClass('active');	
		var timeout = 'bag_link';
		
		if (jQuery.browser.msie) {
			jQuery('#home_rotator').css('visibility', 'hidden');
		}
	});
	
	jQuery('#referral_source_options').change(function(e){
		if(jQuery('#referral_source_options').val() == 'Other'){
			jQuery('#referral_source_text_container').show();
		}		
		else{
			jQuery('#referral_source_text_container').hide();
		}
	});

	

});

function removePostCodeRequirement(){
	
		if(jQuery('#billing:country_id').val() == 'GB'){
			if(!jQuery('#billing:postcode').hasClass('required-entry')){
				jQuery('#billing:postcode').addClass('required-entry');
			}
		}		
		else{
			jQuery('#billing:postcode').removeClass('required-entry');
			jQuery('#billing:postcode').removeClass('validate-zip-international');
		}
	
}
// input field text reset functions
function resetField(field, value){
	if (field.value==value){
		field.value="";	
		return false;
	}
}

function startValue(field, text){
			if (field.value==""){
			field.value=text;	
			return false;
			}
}



/* ====== Ajax cart functions ====== */


function ajaxCartAddProduct () {
	
		var form = jQuery('#product_addtocart_form');
		var container = jQuery('#ajax_cart_container');
		var loader = jQuery('#ajax_cart_loading');
		var content = jQuery('#ajax_cart_content')
		var inputs = jQuery(':input', form).serialize();
		
		jQuery('#btn_shopping_bag').addClass('active');
		
		jQuery('html,body').animate({scrollTop: 0}, 200);
		
		
		
		container.show(1, function () {
			content.hide();
			loader.fadeIn('slow');
		});
	
		jQuery.post(form.get(0).action+"?isAjax=1", inputs, function (data, textStatus) {
			content.html(data);
			loader.fadeOut('slow', function () {
				content.fadeIn('slow', function() {
					var timeout;
					if(timeout != 'bag_link'){
						ajaxCartSetTimeout(5000);
						timeout = '';
					}
					var body_id = jQuery('body').get(0).id;
					if(body_id == "my_shopping_bag"){
						location.reload(true);
					}
				});
			});
		}, "html");
	
		return false;
		
	
}

function ajaxCartRemoveProduct(a, removeUrl) {
  var message = jQuery('.ajax_cart_message');
  var prodName = jQuery(a).parent().siblings('.name').text();

	jQuery(a).parents('.product_row').fadeOut('slow', function () {
    message.removeClass('bad');
    message.text('Removed ' + prodName);
	});
	jQuery('#ajaxcart_subtotal span').hide();
	jQuery.post(removeUrl+'?isAjax=1', {}, function() {
		jQuery.post(AjaxcartPriceUrl, {}, function(response) {
			jQuery('#ajaxcart_subtotal span').html(response);
			jQuery('#ajaxcart_subtotal span').show();
		}, 'html');
	});
	return false;
}

function ajaxCartSetTimeout(t) {
   window.ajaxCartTimeout=setTimeout("jQuery('#ajax_cart_container').fadeOut('slow'); jQuery('#btn_shopping_bag').removeClass('active');", t);
}

function ajaxCartClearTimeout() {
  if(typeof(window.ajaxCartTimeout)!='undefined'){
    clearTimeout(window.ajaxCartTimeout);
    window.ajaxCartTimeout=null;
  }
}


