$(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 && size !=undefined) {
			search_key += colour
		}
		if (colour != undefined && size == 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('<img src="images/products/250x250/' + image + '" alt="Produt Image" />')
							
							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="Produt 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|Debenture level / Upper Grand Stand') {
			$('#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())
	})
	
	// 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));
		
	});
	
	// product zoom
	$("#enlarge_image").click(function(event) {
		
		event.preventDefault();
		
		if($("#zoom_overlay").length == 0)
		{
			draw_reticle();
			image_name = $('#product_view_main_image img').attr('id');

			display_zoom_image(image_name);
		}
		else if($("#zoom_overlay:visible").length == 1)
		{
			$("#reticle").remove();
			$("#zoom_overlay").fadeOut(500,function() {$(this).remove();});
		}
		else
		{
//			$("#zoom_overlay").fadeIn();
		}
	});
	
	$('#zoom_overlay').live('click',function() {
		$('#enlarge_image').trigger('click');
	});
	
	/* 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;
	});
});

function draw_reticle() {
	target_img = $("#product_view_main_image");
	reticle = $("#reticle");
	
	if(reticle.length == 0)	{
		reticle = target_img.append("<div id=\"reticle\"></div>").find("#reticle");
	}
	
	reticle.fadeTo(0,0.33);
		
	reticle.append('<span>Drag</span>').find('span').css({
		position:'absolute',
		top:'40%',
		left:'30%'
	});
			
	reticle.trigger('mousedown');
		
	reticle.draggable( {
		containment:'parent',
		drag:function(event,ui) {
			$("#zoom_overlay img").css({"top":-(ui.position.top*4),"left":-(ui.position.left*4)});
		}
	});
}
function display_zoom_image(image_name) {

	$(".top_content").append("<div id=\"zoom_overlay\"><img class=\"product\" src=\"\" /></div>");
	$("#zoom_overlay").hide().fadeIn();
	
	/* set loader gif */
	$("#zoom_overlay img")
		.attr("src","images/ajax-loader.gif")
		.attr("alt","loading..")
		.css({width:"25px",height:"25px",margin:"50%"});
	
	/* Load Image*/
	img_loader = new Image();

  	$(img_loader).load(function () {
  		
      $('#zoom_overlay img').remove();
      $('#zoom_overlay').append(this);
		$('#zoom_overlay img').css('position','absolute');
	
		if(browser_is_ie6()) {
			$('#zoom_overlay').supersleight();
		}							
		
   }).attr('src', "images/products/1000x1000/" + image_name);
	
	$('#zoom_overlay').append('<span>Click image to close</span>')
	
	
}
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');
			}
			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());
			}
			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());
			}
			else {
				$("p.availability span").html("This item is usually dispatched within " + data + " of order");
				$('#product_notification').hide();
				$('#product_notification #child_id').val("");
			}
		},
		'html'
	);	
}
function show_voucher_forms(count)
{
	vouchers = $("div.voucher").clone();
}