$(function() {

	$('.prettyGallery').prettyGallery({
		'navigation':'bottom',
		'itemsPerPage':4
	});

	$(window).resize();
	$("#wishlist").hide();
	
	$("#Wishlist").click( function(e) {
		e.preventDefault();
		$("#wishlist_div").slideToggle('slow', function() {
			if ($("#wishlist_div").is(':hidden')) {
			} else { getCart(); }
    });       
	});
	
	$("#hideme").click( function(e) {
		e.preventDefault();
		$("#wishlist_div").slideToggle('slow', function() {
			if ($("#wishlist_div").is(':hidden')) {
			} else { getCart(); }
    });
		
		centerBottom("#bottomThing");
	});
	
	$(".buycart").click( function(e) {
		e.preventDefault();
		var id = $(this).attr("id");
		var newhref = $(this).attr("href");
		var extra = "";
		
		if(newhref == "#") { extra = ""; } else { extra = newhref; }
		var divid = "#div" + id + " .buycart";

		$(divid).html("<img src='img/loading.gif' alt='' />");
		$(divid).css("background", "#fff");
		
		$.get("addincart.php",{
			type: "add",
			pid: id,
			ext: extra
		}, function(data) {
			if(data == "0") {
				alert("Selected product not found. Please choose another.");
				$(divid).html("Failed");
			}
			else if(data == "-1") {
				alert("The product is not in stock.");
				$(divid).html("Failed");
			}
			else {
				//$(divid).html("Added");
				//$(divid).css("background", "#000000");
				$(divid).html("<img src='img/added-in-cart.gif' alt='' />");
				
				$("#wishlist_div").slideDown("slow", function() {
					if ($("#wishlist_div").is(':hidden')) {
					} else { getCart(); countCart(); }
				});
			}
		});
	});

	centerBottom("#bottomThing");

	$(window).resize(function() {
		centerBottom("#bottomThing");
	});
	
	$("#review ul").simplyScroll({
		autoMode: 'loop',
		pauseOnHover: true,
		frameRate: 20,
		speed: 1,
		horizontal: false
	});

});

function centerBottom(selector) {
	var newTop =   $(window).height() - $(selector).height();
	var newLeft = ($(window).width()  - $(selector).width()) / 2;
	$(selector).css({
		'position': 'fixed',
		'left': newLeft,
		'top': newTop
	});
}

function getCart() {
	$.get("wishlist.php",{
		action: "view"
	}, function(data) {
		$("#prohere").html(data);
		$("#wishlist_div").attr({ scrollTop: $("#wishlist_div").attr("scrollHeight") });
	});
}

function countCart() {
	$.get("countlist.php",{
		action: "count"
	}, function(data) {
		$("#bottomCart").html(data);
		$("#viewcart").html(data);
	});
}

function updateProduct(elm, pid) {
	$(elm).attr("disabled", true);
	var val = document.getElementById("input"+pid).value;
	$.get("updateincart.php",{
		type: "update",
		pid: pid,
		quan: val
	}, function(data) {
		if(data == "0") { alert("The quantity you have requested is more than we currently have in stock."); $(elm).attr("disabled", false); }
		else if(data == "-1") { alert("Sorry, but the product you want is no longer in stock"); $(elm).attr("disabled", false); }
		else { getCart(); countCart(); }
	});
}

function deleteProduct(elm, pid) {
	$(elm).attr("disabled", true);
	$.get("deleteincart.php",{
		type: "delete",
		pid: pid,
	}, function(data) {
		getCart(); countCart();
	});
}

function checkIt(evt) {
	evt = (evt) ? evt : window.event
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		status = "This field accepts numbers only."
		return false
	}
	status = ""
	return true
}

function showRating(r, div, classname) {
	$("#"+div).html('<span class="'+classname+'">'+parseFloat(r)+'</span>');
	$("span."+classname).stars();
}

$.fn.stars = function() {
	return $(this).each(function() {
		$(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
	});
}
