$("#orderstatus").live("click", function(){
	$.post(PATH+"sapi", {action: "orders"}, function(data){
		$("#main").html(data);
	});
	return false;
});

$(".orderline").live("click", function(){
	var id = $(this).attr("id").replace(/orderline/,"");
	$("#orderdesc"+id).toggle();
});

$(".menuitem").live("click", function(){
	var type = $(this).attr("id").replace(/menu/,"");
	$.post(PATH+"sapi", {type: type, action: "browse"}, function(data){
		$("#main").html(data);
	});
	return false;
});

$("#checkoutlink").live("click", function(){
	$.post(PATH+"sapi", {action: "checkout"}, function(data){
		$("#main").html(data);
	});
	return false;
});

$("#search").live("submit", function(){
	var searchterm = $("#searchtxt").val();
	if (searchterm.length >= 3)
	{
		$.post(PATH+"sapi", {action: "search", query: searchterm}, function(data){
			$("#main").html(data);
		});
	}
	return false;
});

$("#clearcart").live("click", function(){
	$.post(PATH+"sapi", {action: "cartclear"}, function(){
		$("#cartitemcount").text("0 item");
		$("#cartcr").text("0");
		$("#cartbit").text("0");
	});
	return false;
});

$("form[name='additem']").live("submit",function(){
	var id = $(this).children("input[name='id']").val();
	$.post(PATH+"sapi", {action: "cartadd", id: id}, function(data){
		if (data.error)
		{
			alert(data.error);
		}else{
			switch (true)
			{
				case (data.cartitemcount > 1): $("#cartitemcount").text(data.cartitemcount+" items"); break;
				case (data.cartitemcount == 1): $("#cartitemcount").text("1 item"); break;
				case (data.cartitemcount == 0): $("#cartitemcount").text("0 item"); break;
			}
			$("#cartcr").text(data.cr);
			$("#cartbit").text(data.bit);
		}
	},"json");
	return false;
});


$(document).ready( function(){
	$.ajax({
		dataType: 'jsonp',
		data: '',
		jsonp: 'session',
		url: 'http://hacktheplanet.no/session/',
		success: function (data) {
			$.post(PATH+"sapi", {action: "session", id: data.s}, function(d){
				if (d.status == "ok") window.location = "http://spendmore.com";
			},"json");
		},
	});
	if ($('#coin-slider').attr("id") !== undefined)
	{
		$('#coin-slider').coinslider({
			width: 750, // width of slider panel
			height: 200, // height of slider panel
			spw: 5, // squares per width
			sph: 5, // squares per height
			delay: 4200, // delay between images in ms
			sDelay: 32, // delay beetwen squares in ms
			opacity: 0.8, // opacity of title and navigation
			titleSpeed: 0, // speed of title appereance in ms
			effect: 'rain', // random, swirl, rain, straight
			navigation: false, // prev next and buttons
			links : true, // show images as links 
			hoverPause: true // pause on hover
		});
	}
});

$("input[type=radio]").live("change", function(){
	var id = $(this).val();
	$.post(PATH+"sapi", {action: "deliveryoption", id: id}, function(data){
		switch(id)
		{
			case "1": $("#totalcr").text(number_format(totalcr,0,""," ")); $("#totalbit").text(number_format(totalbit,0,""," ")); break;
			case "2": $("#totalcr").text(number_format((totalcr+1000),0,""," ")); $("#totalbit").text(number_format(totalbit,0,""," ")); break;
			case "3": $("#totalcr").text(number_format((totalcr+5000),0,""," ")); $("#totalbit").text(number_format(totalbit,0,""," ")); break;
			case "4": $("#totalcr").text(number_format(totalcr,0,""," ")); $("#totalbit").text(number_format((totalbit+10),0,""," ")); break;
		}
	});
});

var sent = false;

$("#checkoutbutton>a.order").live("click", function(){

	if (sent == false) {
		sent = true;
		$("#checkoutbutton>a").css("background-color","#CCC").css("border-color","#BBB").text("Processing order");
		$("#shippingbox input[type=radio]").attr('disabled',true);
		
		$.post(PATH+"sapi", {action: "order"}, function(data) {
			if (data.status == "ok") {
				$("#cartitemcount").text("0 item");
				$("#cartcr").text("0");
				$("#cartbit").text("0");
				$("#playercr").text(data.playercr);
				$("#playerbit").text(data.playerbit);
				$("#orderoutput>.time").text(data.deliverytime);
				$("#orderoutput").fadeIn();
			}else{
				$("#checkoutbutton>a").css("background-color","#00A6D6").css("border-color","#333").text("Place Order");
				$("#orderoutputerror").text("Something went wrong with your order. Please verify your credit status");
			}
			sent = false;
		},"json");
	}
	return false;
});
function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

