﻿$(document).ready(function(){
	
	$('#deliveryAddress').hide();
	// billing details different from delivery
	if ($('#shareaddr2').attr('checked')) {
		$('#deliveryAddress').show();
	}
	
	$('#shareaddr1').click(function () {
		if ($('#shareaddr1').attr('checked')) {
			$('#deliveryAddress').slideUp();
		} else {
			$('#deliveryAddress').slideDown();
		}
	});

	$('#shareaddr2').click(function () {
		if ($('#shareaddr2').attr('checked')) {
			$('#deliveryAddress').slideDown();
		} else {
			$('#deliveryAddress').slideUp();
		}
	});

$('.categories .dynamic form').submit( function () {
	var form = $(this);
	
	$(this).find("#addToBasket_dynamicRequest").val('true');
	$.post(this.action, $(this).serialize(),function(data){
		//success - add item added message
		form.parents(".cat").find(".order-message").show()
		form.parents(".cat").find(".order-message").html(data)
	});
	
	// update top basket.
	$.ajax({
		url: "/basket/displaybasketlink",
		dataType: "text",
		success: function(data) {
			$('.basketLink').html(data);
		}
	});
	
	return false;

});

// setup ul.tabs to work as tabs for each div directly under div.panes
$("ul.tabs").tabs("div.panes > div");

// initialize fancybox lightbox
$(".lightbox").fancybox();

// hover on category images grid to fade out non-focused items
function fadeOthers(){
$(this).animate({'opacity':1},400);
	$('.banner .item').not(this).animate({"opacity":0.4},500);
}
function fadeNone(){ $('.banner .item').animate({'opacity':1},500);}

$('.banner .item').hoverIntent({
	interval: 45,
	over: fadeOthers,
	out: $.noop
});

$('.banner').mouseleave(function(){
fadeNone();
}); 

// increment qty

	$('.incrementQty').click(function(e){
			var txtfield = $(this).parent().siblings('#qtyBox');
			var value = parseInt(txtfield.val()) + 1;
			txtfield.val(value);
	});
	$('.decrementQty').click(function(e){
			var txtfield = $(this).parent().siblings('#qtyBox');
			var value = parseInt(txtfield.val()) - 1;
			if(value>0)
				txtfield.val(value);
			else
				txtfield.val(0);
	});


// set min-height of main content to match the left col (minus 50px padding and margins) when navi goes over 890px
if (($('.navi').height()) >= 890){
	$('.main').css({'minHeight':(($('.navi').height()) - 45)});
}
// check height of long desc text
if (($('.long-desc').height()) >= 402){
	$('.more').show(); // show the toggle button if it's equal or more than 402 px
}

// show more product details
$('.more').click(function(){ // click on more details
	$('.prod-desc-text').toggleClass('show'); // expand div
	$(this).toggleClass('toggle');	// add toggle class to button to switch bg image
	
	if ($(this).hasClass('toggle')){ // if it has the toggle class
		$(this).text('Less details'); // change the text to less details
	}	
	else{
		$(this).text('More details'); // change it back to more details
	}
	return false; // stop page jumping to top
});

// close add to basket popup

$('.close-order-message').live('click', function(){
	$(this).closest('.order-message').hide(); // travel up DOM tree to parent div and hide it
	return false; // stop page jerk
});


// CALL BACK POPUP

function popup(){ // call-back popup
	var escClose = function(e) { 
		if (e.which === 27 ) { // if esc is pressed
			$('#callback-overlay').hide();  // hide callback
			$(document).unbind('keyup', escClose); // unbind escape key
		} 
	}; 
	$('#callback-overlay').show(); // else show callback
	$(document).bind('keyup', escClose); // bind escape key
}
// call back popup window
$('.callback-btn').live('click', function() { 
	popup();
});

$('.callback-close').live('click', function(){
	$('#callback-overlay').hide(); // hide form on click
});

if ($('#callback-overlay').is(':visible')){ // if form is visible
	popup();
}

}); // document ready close
