	
// Reformats a number by inserting commas and padding out the number of digits
// and decimal places.
//
// Parameters:
//     number:        The number to format. All non-numeric characters are
//                    stripped out first.
//     digits:        The minimum number of digits to the left of the decimal
//                    point. The extra places are padded with zeros.
//     decimalPlaces: The number of places after the decimal point, or zero to
//                    omit the decimal point.
//     withCommas:    True to insert commas every 3 places, false to omit them.
function formatNumber(number, digits, decimalPlaces, withCommas)
{
        number       = number.toString();
    var simpleNumber = '';

    // Strips out the dollar sign and commas.
    for (var i = 0; i < number.length; ++i)
    {
        if ("0123456789.".indexOf(number.charAt(i)) >= 0)
            simpleNumber += number.charAt(i);
    }

    number = parseFloat(simpleNumber);

    if (isNaN(number))      number     = 0;
    if (withCommas == null) withCommas = false;
    if (digits     == 0)    digits     = 1;

    var integerPart = (decimalPlaces > 0 ? Math.floor(number) : Math.round(number));
    var string      = "";

    for (var i = 0; i < digits || integerPart > 0; ++i)
    {
        // Insert a comma every three digits.
        if (withCommas && string.match(/^\d\d\d/))
            string = "," + string;

        string      = (integerPart % 10) + string;
        integerPart = Math.floor(integerPart / 10);
    }

    if (decimalPlaces > 0)
    {
        number -= Math.floor(number);
        number *= Math.pow(10, decimalPlaces);

        string += "." + formatNumber(number, decimalPlaces, 0);
    }

    return string;
}
////////////////////////////////

	
	 $(document).ready(function(){
	 //Set initial values for loan
	 updateValues(80,20);
	 
	//add animation to navigation bar
	 $("#nav-cont li ")
		.css( {backgroundPosition: "-20px 50px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-20px 103px)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(40px 50px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-20px 50px"})
			}})
		})

			
	//Bind click funvtion to add button
    $("#add").click(function(){ 
    	var curVal=$('#slider').slider('option', 'value');
    	if(curVal!=143){
    	value=curVal+1;
       	 $('#slider').slider('option', 'value', value);
       	 
       	 updateValues((value*10)+70,17.5+(value*2.5))
       	 
   	 }		
    });
    
    //Bind click funciton to minus
     $("#minus").click(function(){ 
    	var curVal=$('#slider').slider('option', 'value');
    	if(curVal!=1){
    	value=curVal-1;
       	 $('#slider').slider('option', 'value', value);
       	 
       	 updateValues((value*10)+70,17.5+(value*2.5))
       	 
   	 }
    });
    
    //bind slider to slider div
    $("#slider").slider({ 
  value: 1,
    		min: 1,
    		max: 143,
    		step: 1,

    slide: function(e,ui){
    var value = ui.value;
	updateValues((value*10)+70,17.5+(value*2.5))
	
    } 
    });
    
    
	//Add tool tip to slider
	 $("#slider").easyTooltip({
			content: "<div id='toolContent'></div>"		   
		});
  });
  //////////End of document.ready
  
  
//Update values on slide
function updateValues(amount, fee){
$("#borrowAmt").html("&pound;"+formatNumber(amount,0,2,true));
$("#chargeAmt").html("&pound;"+formatNumber(fee,0,2,true));
$("#repayAmt").html("&pound;"+formatNumber((fee+amount),0,2,true));
$("#toolContent").css({'background-color': '#4485AA','border-style':'solid','border-width':'1px','border-color':'#ffffff'});
$("#toolContent").html("Borrowing: &pound;"+formatNumber(amount,0,2,true));
$("#loanAmtHid").val(formatNumber(amount,0,0,true));

}


