// BASKET

var basket = {
	
	init: function() {
		// bind to quantity input
		$('input.basketitemquantity').live('change', function() {
			basket.changequantity($('input[name*="productid"]', $(this).parent()).val(), $('input[name*="basketitemquantity"]', $(this).parent()).val(), $('input[name*="productoptions"]', $(this).parent()).val());
			return false;
		});
		// bind to remove product
		$('a.basketremoveitembutton').live('click', function() {
			basket.removeproduct($(this).attr('class').match(/productid\-.+?\b/).toString().replace('productid-', ''), $('input[name*="productoptions"]', $(this).parent()).val());
			return false;
		});
		
	},
	
	additem: function(productid) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=additem' 
				+ '&productid=' + productid 
				+ '&quantity=' + '1',
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});			
	},
	
	removeproduct: function(productid, options) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=removeproduct' 
				+ '&productid=' + productid + '&options=' + options,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});	
	},
		
	changequantity: function(productid, quantity, options) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=changequantity' 
				+ '&productid=' + productid 
				+ '&quantity=' + quantity
				+ '&options=' + options,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});	
	},
	
	setdeliverycountryid: function(deliverycountryid) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setdeliverycountryid&deliverycountryid=' + deliverycountryid,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						break;
					default:
						alert('Unexpected Response ' + $("message", xml).text(), 'Message');
					break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
	},	
	
	setgiftwrap: function(giftwrap) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setgiftwrap&giftwrap=' + giftwrap,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						break;
					default:
						alert('Unexpected Response ' + $("message", xml).text(), 'Message');
					break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
	},	

	setnextday: function(nextday) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setnextday&nextday=' + nextday,
			success: function(xml) {
				switch ($("status", xml).text()) {
				case '0':
					alert($("message", xml).text(), 'Error');
					break;
				case '1':
					var bodyhtml = $("body", xml).text();
					$('#basket').html(bodyhtml);
					break;
				default:
					alert('Unexpected Response ' + $("message", xml).text(), 'Message');
				break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
	},
	
	setpromotionalcode: function(promotionalcode) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setpromotionalcode&promotionalcode=' + promotionalcode,
			success: function(xml) {
				switch ($("status", xml).text()) {
				case '0':
					alert($("message", xml).text(), 'Error');
					break;
				case '1':
					var bodyhtml = $("body", xml).text();
					$('#basket').html(bodyhtml);
					break;
				default:
					alert('Unexpected Response ' + $("message", xml).text(), 'Message');
				break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
	}		
	
};

basket.init();	

var new_products_interval;
var interval = 5000; // for teh homepage latest products slider, time between slides
			
function shift_new_products() {
	$('.productlist.mini li:first').animate({ marginLeft: -180 },1500,'swing',function() {
		$(this).css('margin-left','-1px').appendTo('.productlist.mini');
	});
}

$(function() {
	
	if($('.productlist.mini li').length > 5) {
		new_products_interval = window.setInterval(shift_new_products,interval);
		$('.productlist.mini').hover(function() {
			clearInterval(new_products_interval);
		},function() {
			new_products_interval = window.setInterval(shift_new_products,interval);
		});
	}
	
	$.ajaxSetup({
		type: 'POST',
		cache: false
	});	
	
	$('body').ajaxStart(function() {
		$(this).addClass('isloading');
		$('.hideonajax').hide();
		$('.visibilityonajax').css({ opacity: 0 });
		$('.fadeonajax').stop().animate({opacity:0});
		$('.ajax-status').fadeIn()
		
	});

	$('body').ajaxStop(function() {
		$(this).removeClass('isloading');
		$('.ajax-status').stop().fadeOut()
		$('.hideonajax').show();
		$('.visibilityonajax').css({ opacity: 1 });
		$('.fadeonajax').animate({opacity:1});
	});	
	
	$('form.jfinder select.finder-category').live('change', function() {
		$('form.jfinder').attr('action', $(this).val());
	});
	
	$("#control").bind('dblclick', function() {
		$("#control").fadeOut();
		return false;
	});
	
	$('.clickable').live('click', function() {
		if ($('a', this).attr('target')) {
			window.open($('a', this).attr('href'), $('a', this).attr('target'));
		} else {
			location.href = $('a', this).attr('href');
		}
		return false;
	});

	$('form.newsletter input').live('focus', function() {
		if ($(this).val() == 'Enter your email here...') {
			$(this).val('');
		}
	});

	$('form.search input').live('focus', function() {
		if ($(this).val() == 'Search Asquiths...') {
			$(this).val('');
		}
	});
	
	$('div.gallery ul.thumbnails li').live('mouseover', function() {
		var fullsizeurl = "url('" + $('a', $(this)).attr('href') + "')";
		var fullsizeurlzoom = fullsizeurl.replace('535/468', '948/948');
		$('div.large', $(this).parent().parent()).css('backgroundImage', fullsizeurl);
		$('div.largezoom', $(this).parent().parent()).css('backgroundImage', fullsizeurlzoom);
	});
	$('div.gallery ul.thumbnails li').live('click', function() {
		return false;
	});
	$('div.gallery div.large').live('mouseenter', function() {
		$('div.gallery div.largezoomloader').show();
		$('div.gallery div.largezoom').show();
	});
	$('div.gallery div.largezoom').live('mouseleave', function() {
		$('div.gallery div.largezoomloader').hide();
		$('div.gallery div.largezoom').hide();
	});
	
	$('div.gallery div.largezoom').live('mousemove', function(e) {
	    var vW=$(this).width();
	    var vH=$(this).width();
	    var vL=$(this).offset().left;
	    var vT=$(this).offset().top;
	    var xpos=e.pageX-vL;
	    var ypos=e.pageY-vT;
	    var x=(Math.round(xpos/vW*100));
	    var y=(Math.round(ypos/vW*100));
	    $(this).css({backgroundPosition:x + '% ' + y + '%'});
	});
	
	if ($('#sidebar').children().size() == 0) {
		$('#sidebar').remove();
		$('body').addClass('nosidebar');
	}
	
	$("body").ajaxStart(function () {
		$('body').addClass('loading');
	});
	
	$("body").ajaxStop(function () {
		$('body').removeClass('loading');
		$('.ajaxtrigger').removeClass('ajaxtrigger');
	});
	
	$('.shop-filters select').live('change',function() {
		if($('#price').val() == 'any' && $('#metal').val() == 'any' && $('#color').val() == 'any') {
			$('.clear-filters').fadeOut();
		} else {
			$('.clear-filters').fadeIn();
		}
		$('.filters-loader').show();
		$.ajax({
			url: '/ajax/filters.php',
			data: 'productcategory=' + product_category + '&price=' + $('#price').val() + '&metal=' + $('#metal').val() + '&color=' + $('#color').val(),
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('.ajax-filters-container').replaceWith(bodyhtml);
						$('.filters-loader').hide();
						break;
					default:
						alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
	});
	
	$('.shop-filters .clear-filters').live('click',function() {
		$('#price,#metal,#color').val('any');
		$('.clear-filters').fadeOut();
		$('.filters-loader').show();
		$.ajax({
			url: '/ajax/filters.php',
			data: 'productcategory=' + product_category + '&clearfilters=yes',
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('.filters-loader').hide();
						$('.ajax-filters-container').replaceWith(bodyhtml);
						break;
					default:
						alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});				
	});
	
	
	var hdslideshow = {
			
		slides: $('ul.featured-designer-slides li'),
		numslides: $('ul.featured-designer-slides li').length,
		currentslide: 0,
			
		init: function () {
			if (hdslideshow.numslides > 1) {
				hdslideshow.slides.css({'opacity':0, 'z-index':1});
				hdslideshow.next(false); 
				setInterval(function() { hdslideshow.next(true); },	10000);				
			}
		},
		
		next: function(crossfade) {
			var csn = hdslideshow.currentslide;
			var nsn = (csn == hdslideshow.numslides ? 1 : csn + 1);
			var cs = hdslideshow.slides.eq(csn-1);
			var ns = hdslideshow.slides.eq(nsn-1);
			cs.css({'z-index':1});
			ns.css({'z-index':2});
			if (crossfade) {
				ns.animate({'opacity':1}, 1000, function() {
					cs.css({'opacity':0});	
				});
			} else {
				ns.css({'opacity':1});
				cs.css({'opacity':0});
			}
			hdslideshow.currentslide = nsn;
		}
	}
	
	hdslideshow.init();
	
	$('a.lightbox').colorbox({rel:'gal', photo:true});
	
});
