$(document).ready(function()
{
	get_availability($("#sku").attr('value'));
	get_price($("#sku").attr('value'));
		
	$('#notify_email_address').keyup(function(data){
		
		var email = (this).value;
		
		$.post("includes/inc.valid_email.php", {email: email}, function(data){

			if(data == 'false')
			{
				$('#notify_me_button').attr('disabled','disabled');
				$('#notify_me_button').attr('value','Incorrect email.');
			}
			else
			{
				$('#notify_me_button').removeAttr('disabled');
				$('#notify_me_button').attr('value','Notify Me');
			}
		});
	});
		
		
	/* Child options selectors */
	$('ul.child_selector input').click(function(e) {
		
		size = $('#size_selector input:checked').val();
		colour = $('#colour_selector input:checked').val();
		
		if(size!=undefined) {
			search_key = size;
		}
		if(colour!=undefined) {
			search_key += colour
		}
		
		// lookup child_id in product_children object
		$.each($.product_children,function() {
			
			if(this.search_key == search_key) {
				
				// set sku field for postback and get child details
				$('#sku').val(this.child_id);
				
				get_availability(this.child_id);
				get_price(this.child_id);
				
				images = (!this.images) ? $.parent_images : this.images; 
				
				if(images) {
					//swap in child_images or replace with parent images  
					$.each(images,function(orientation,image){
						
						if(orientation == 'front') {
							container = $('#product_view_main_image');
							current_path = container.find('img').attr('src');
							
							split_path = current_path.split('/');
							
							split_path[split_path.length-1] = image;
							container.empty();
							
							container.append('<span class="sm_grey">click image to enlarge</span><a title="Product Image" class="blowup" href="images/products/600x600/' + image +'" ><img border="0" src="images/products/250x250/' + image + '" alt="Product Image" /></a>');
							container.append('<script>$(function() { $(".blowup").lightBox(); });</script>');
							
							container.find('img').attr('id',image);

							if(browser_is_ie6()) {
								container.supersleight();
							}
						}
						else {
							container = $('div.extra_image');

							container.empty();
							
							container.append('<img src="images/products/125x125/' + image + '" alt="Product Image" />')
							container.find('img').attr('id',image);
							
							if(browser_is_ie6()) {
								container.supersleight();
							}							
						}
					});
				}
				
				return false;
			}
		});
	});
	
//	$('ul.child_selector input[checked]').trigger('click');
	
	// hide stock notice text
	$('#hamper_notice').hide();
	
	$('#collection_point').change(function() {
		if($(this).val() == 3) {
			$('#hamper_notice').fadeIn();
		}
		else {
			$('#hamper_notice').hide();
		}
	});
	
	// attach live event to stock notification toggle 
	$('#notify_me').live('click',function(event) {
		$('#product_notification').slideToggle();
		event.preventDefault();
	});
	
	$('#sku').change(function() {
		get_availability($(this).val());
		get_price($(this).val());
		get_old_blue($(this).val());
	});
	
	// Extra images
	$('.extra_image img').live('click',function() {
		
		this_name = $(this).attr('src').split('/').pop();
		
		target = $('#product_view_main_image img');
		target_name = target.attr('src').split('/').pop();
		
		target.attr('src',target.attr('src').replace(target_name,this_name));
		target.attr('id',this_name);
		
		$(this).attr('src',$(this).attr('src').replace(this_name,target_name));
		
	});
	
	
	/* Tabs */
	var tabDivs = $("#product_information div.tab_content");
	tabDivs.filter(':not(:first)').hide();
	
	var tabs = $('#product_information ul.tabs a').click(function(event) {
		
		tabDivs.hide();
		$(this.hash).show();

		$('#product_information ul.tabs li a').removeClass('selected');
		$(this).addClass('selected');
		
		event.preventDefault();
		
		return false;
	});
});

// if the sku is related to old blue membership - add the donation link
//function get_old_blue(sku_code) {
//	
//	 if (sku_code == 10484)
//		  {
//		  	 $("#donation_link_holder").html('<a href="?page=shop&option=old_blue_donation&ob=yes">click here to make a donation</a>').hide().fadeIn('fast');
//			 $("#qty").hide();
//			 $(".disable_cart").hide();
//			 $(".toghide").hide();
//		  }
//     if (sku_code != 10484)
//		 {
//		 	$("#donation_link_holder").html('');
//			$("#qty").show();
//			$(".disable_cart").show();
//			$(".toghide").show();
//		 }
//}


function get_price(sku_code) {
	
	$.get	(
		"ajax/index.php",
		{
			id:sku_code,
			page:'shop',
			action:'price'
		},
		function(data) {
			$('#child_price').html(data);
		},
		'html'
	);	
}


function get_availability(sku_code)
{
	$.get
	(
		"ajax/index.php",
		{
			id:sku_code,
			page:'shop',
			action:'availability'
		},
		function(data) {
			
			//inject the lead time into the lead_time span
			if(data == "no_stock") {
				$("p.availability span").html('Out of stock. <a href="#" id="notify_me">Notify me when stock is available</a>');
				$('#product_notification #child_id').val($('#sku').val());
				$('#availability_tab').trigger('click');
				$("div.disable_cart").html('');
			}
			else if(data == "no_stock_notified") {
				$("p.availability span").html('Out of stock. You will be notified when stock is available');
				$('#product_notification #child_id').val($('#sku').val());
				$("div.disable_cart").html('<input type="image" src="images/add_to_cart.png" name="add_now" value="Add to Cart" title="Add to Cart" />');
			}
			else if(data == "non_deliverable")
			{
				$("p.availability span").html('This item is not despatched, please refer to description for further details');
//				$('#product_notification #child_id').val($('#sku').val());
				$("div.disable_cart").html('<input type="image" src="images/add_to_cart.png" name="add_now" value="Add to Cart" title="Add to Cart" />');
			}
			else {
				$("p.availability span").html("This item is usually dispatched within " + data + " of order");
				$('#product_notification').hide();
				$('#product_notification #child_id').val("");
				$("div.disable_cart").html('<input type="image" src="images/add_to_cart.png" name="add_now" value="Add to Cart" title="Add to Cart" />');
			}
		},
		'html'
	);	
}
function show_voucher_forms(count)
{
	vouchers = $("div.voucher").clone();
}
