$(document).ready(function(){
	$.currency_array;
	$.currency_display;
	$.currency_sign;
	add_to_piggy(1);
	$("p#amount").draggable({helper: 'clone', cursor: 'move'}); 
    $(".pig_content").droppable({
    	accept: "p#amount", 
 		hoverClass: 'choose_pig',
    	drop: function() {
 			add_to_piggy();
		}
	});
});

$(document).keyup(function(e) {
	var increment = 0;
	var charCode;
	if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }
	switch(charCode){
		case 37 :
			$("ul#currency li").eq($.currency_pointer).removeClass('current');
			$.currency_pointer = $.currency_pointer > 0 ? $.currency_pointer - 1 : $.currency_array.length - 1;
			$("ul#currency li").eq($.currency_pointer).addClass('current');
		break;
		case 38 :
			increment = $.currency_array[$.currency_pointer];
		break;
		case 39 :
			$("ul#currency li").eq($.currency_pointer).removeClass('current');
			$.currency_pointer = $.currency_pointer < ($.currency_array.length - 1) ? $.currency_pointer + 1 : 0 ;
			$("ul#currency li").eq($.currency_pointer).addClass('current');
		break;
		case 40 :
			increment = -$.currency_array[$.currency_pointer];
		break;
	}
   	if(increment){
   		increment_amount(increment);
	}
	return false;
});

function increment_amount(amount){
	var orign = current_amount();
   	var newamount = parseFloat(orign) + amount;
   	if(newamount > 9999.00)	newamount = 9999.00;
   	if(newamount < 0)	newamount = 0;
   	var nstr = number_format(newamount);
	$("#amount span").html(nstr);
	if(orign.length != nstr.length){
		if(nstr.length >= 6){
			var opening = orign.length < nstr.length ? true : false ;
			var base_width = 504;
			var width_px = base_width+((nstr.length-6)*50);
	   		$('#content').animate({width: width_px}, 500);
	   		li_width = (width_px - 4) / 10;
	   		if(opening){
	   			$('ul#currency').css("width",width_px);
	  			$('ul#currency li a').animate({width: li_width}, {queue: false, duration: 500});			
   			}else{
 	  			$('ul#currency li a').animate({width: li_width}, {queue: false, duration: 500}, function(){
 	  				$('ul#currency').css("width",width_px);
   				});
   			}
   		}
	}
	return false;
}

function current_amount(){
	return $("#amount span").html().replace(",", "");
}

function localisation(localID){
	$("#options").slideUp("800");
	switch(localID){
		case 1 :
			$.currency_array = [0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.00, 2.00, 5.00, 10.00];
			$.currency_display = ['1p', '2p', '5p', '10p', '20p', '50p', '&pound;1', '&pound;2', '&pound;5', '&pound;10'];
			$.currency_sign = "&pound;";
		break;
		case 2 :
			$.currency_array = [0.01, 0.05, 0.10, 0.25, 0.50, 1.00, 2.00, 5.00, 10.00, 20.00];
			$.currency_display = ['1&cent;', '5&cent;', '10&cent;', '25&cent;', '50&cent;', '&#36;1', '&#36;2', '&#36;5', '&#36;10', '&#36;20'];
			$.currency_sign = "&#36;";
		break;
		case 3 :
			$.currency_array = [0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.00, 2.00, 5.00, 10.00];
			$.currency_display = ['1&cent;', '2&cent;', '5&cent;', '10&cent;', '20&cent;', '50&cent;', '&#8364;1', '&#8364;2', '&#8364;5', '&#8364;10'];
			$.currency_sign = "&#8364;";
		break;
	}
	$(".loader").addClass('hide');
	$("#amount").removeClass('hide');
	$("#amount strong").html($.currency_sign);
	$("#amount span").html("0.00");
	$("#currency").html("");
	for(var i=0; i<($.currency_array.length); i++){
		var content = '<li><a href="#" onclick="changefocus('+i+'); return false;">'+$.currency_display[i]+'</a></li>';
		$("#currency").append(content);
	}
	$("ul#currency li").removeClass('current');
	$(".select a").html($.currency_sign);
	$(".pig_content p.amount strong").html($.currency_sign);
	$("#previous_payments li span").html($.currency_sign);
	$("ul#currency li:first").addClass('current');
	$("ul#currency li:first a").addClass('round_left');
	$("ul#currency li:last a").addClass('round_right');
	$.currency_pointer = 0;
	$.post("/scripts/ajax/localisation.php", {l: localID});
	return false;
}

function changefocus(focus){
	$("ul#currency li").removeClass('current');
	$("ul#currency li").eq(focus).addClass('current');
	if($.currency_pointer == focus){
		var increment = $.currency_array[$.currency_pointer];
   		increment_amount(increment);
	}
	$.currency_pointer = focus;
	return false;
}

function empty_bank(){
   	var alt = $("#previous_payments li:first").hasClass('alt') ? true : false;
   	var bankamount = parseFloat($(".pig_content p.amount span").html());
   	if(bankamount > 0){
		$.post("/scripts/ajax/emptybank.php", {alt: alt}, function(data){
			$(".pig_content p.amount span").html('0.00');
			$("#previous_payments").prepend(data).slideDown(400);
			$("#previous_payments li:last").remove();
			$(".pig_content p.amount").addClass('hide');
			$(".pig_content p.description").removeClass('hide');
		});
	}
}

function c_tt(code){
	if(code != $('#options p').html()){
		$('#options p').fadeOut(200, function(){
			$('#options p').html(code).fadeIn(400);
		});
	}
}

function add_to_piggy(noshow){
	var currentamount = parseFloat(current_amount());
   	var newamount = parseFloat($(".pig_content p.amount span").html().replace(",", "")) + currentamount;
   	var alt = $("#previous_payments li:first").hasClass('alt') ? true : false;
	if(currentamount > 0){
		$.post("/scripts/ajax/sendbank.php", {a: currentamount, alt: alt}, function(data){
			$("#previous_payments").prepend(data);
			$("#previous_payments li:last").remove();
		});	
		$(".pig_content p.amount span").html(number_format(newamount));
		$("#amount span").html("0.00");
		var base_width = 504;
	   	$('#content').animate({width: base_width}, 500);
	   	li_width = (base_width - 4) / 10;
 	  	$('ul#currency li a').animate({width: li_width}, {queue: false, duration: 500}, function(){
 	  		$('ul#currency').css("width",base_width);
   		});
	}
	if(newamount == 0){
		$(".pig_content p.amount").addClass('hide');
		$(".pig_content p.description").removeClass('hide');
	}else{
		$(".pig_content p.amount").removeClass('hide');
		$(".pig_content p.description").addClass('hide');
	}
	if(!noshow){
		$("#piggy").slideDown("800");
	}
}

function toggle_content(id){
	$("#"+id).slideToggle("800");
	return false;
}

function show_id_input(){
	$(".login").slideDown(400);
	return false;
}

function hide_id_input(){
	$(".login").slideUp(400);
	return false;

}

function number_format(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) i = 0.00;
	var minus = '';
	if(i < 0)	minus = '-';
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0)	s += '.00';
	if(s.indexOf('.') == (s.length - 2))	s += '0';
	s = minus + s;
	s += '';
	x = s.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
