jQuery().ready(function($){

    $("input:text, textarea, input:password").each(function(){
        if(this.value == '')
            this.value = this.title;
    });
    $("input:text, textarea, input:password").focus(function(){
        if(this.value == this.title)
            this.value = '';
    });
    $("input:text, textarea, input:password").blur(function(){
        if(this.value == '')
            this.value = this.title;
    });
    $("input:image, input:button, input:submit").click(function(){
        $(this.form.elements).each(function(){
            if(this.type =='text' || this.type =='textarea' || this.type =='password' ){
                if(this.value == this.title && this.title != ''){
                    this.value='';
                }
            }
        });
    });


	jQuery("input.innerlabel").labelify();
		
	jQuery(".nav-side").treeview({
		animated: "fast",
		collapsed: true,
		unique: true,
		persist: "cookie"
	});
		
	jQuery( "#tabs" ).tabs();
					jQuery('ul.sf-menu').superfish();

	//Select all anchor tag with rel set to tooltip
	$('a[rel=tooltip]').mouseover(function(e) {
		
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');	
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title','');
		
		//Append the tooltip template and its value
		$(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');		
				
		//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn('500');
		$('#tooltip').fadeTo('10',0.9);
		
	}).mousemove(function(e) {
	
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 10 );
		$('#tooltip').css('left', e.pageX - 30 );
		
	}).mouseout(function() {
	
		//Put back the title attribute's value
		$(this).attr('title',$('.tipBody').html());
	
		//Remove the appended tooltip template
		$(this).children('div#tooltip').remove();
		
	});
	
	//Innititate Pretty Photo
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
	
	jQuery('.form_changer').live('change',function(){
		$('#form_submit').click();
	})
    
    jQuery('#slider').nivoSlider({
        pauseTime:8000,
        controlNav:false,
        effect: 'fade',
        directionNav: false
    });
    
    
    jQuery('#finance_period').bind('change',function(){
    
    	var finance_period= jQuery('#finance_period').val();
    	var finance_amount= jQuery('#finance_amount').val();
    	if(finance_period==2){var financerate =0.055; var howmanymonth = '24';}
    	if(finance_period==3){var financerate =0.0499;var howmanymonth = '36';}
    	if(finance_period==5){var financerate =0.0399;var howmanymonth = '60';}
   	var fresult =parseInt(finance_amount)*parseFloat(financerate)*parseInt(finance_period);
   	var totalam=parseInt(finance_amount)+parseInt(fresult);//alert(totalam);
   	var monthpay = parseInt(totalam)/parseInt(howmanymonth);
   	var fin =roundNumber(monthpay,2);

   	var res =howmanymonth+' monthly payments @ £'+fin;//alert(res);
    	jQuery('#repay').html(res);
    
    })
   jQuery('#finance_amount').bind('keyup',function(){
    
    	var finance_period= jQuery('#finance_period').val();
    	var finance_amount= jQuery('#finance_amount').val();
    	if(finance_period==2){var financerate =0.055; var howmanymonth = '24';}
    	if(finance_period==3){var financerate =0.0499;var howmanymonth = '36';}
    	if(finance_period==5){var financerate =0.0399;var howmanymonth = '60';}
   	var fresult =parseInt(finance_amount)*parseFloat(financerate)*parseInt(finance_period);
   	var totalam=parseInt(finance_amount)+parseInt(fresult);//alert(totalam);
   	var monthpay = parseInt(totalam)/parseInt(howmanymonth);
   	var fin =roundNumber(monthpay,2);

   	var res =howmanymonth+' monthly payments @ £'+fin;//alert(res);
    	jQuery('#repay').html(res);
    
    })
 
    
    jQuery("#home_email").validate({
                    expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
                    message: "Email is invalid"
                });
                 jQuery("#home_name").validate({
                     expression: "if (VAL) return true; else return false;",
                    message: "Letters only"
                });
});

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=500,height=500,left = 590,top = 275');");
}

