function addToFavorites()
{ 
	if (window.external) 
	{ 
		window.external.AddFavorite(urlAddress,pageName)
	} 
	else 
	{ 
		alert("Sorry! Your browser doesn't support this function."); 
	} 
}
function animate() {
	var first = $($('.vslider').children()[0]);
	first.slideUp('slow',function() {	
		first.remove();
		first.show();
		$('.vslider').append(first);
		
		setTimeout(animate,2000);
	});
}
function animateCycle() {
    $('.cycling a').each(function() {
        var self = $(this);
        var last = $('*:last-child',self); //find the last child in the context of the current element
    
        last.fadeOut(1000,function() {
            var img = $(this);
            var a = img.parent();

            img.remove();
            a.prepend(img);                
            
            var nextImg = $('*:last-child',a);
            nextImg.animate({left:'0',width:'169',top:'0',height:'129'},0);
            nextImg.fadeIn(1000);
        })
    });
    
    setTimeout(animateCycle,4000);
}

//Code to load the models after the Make has been selected
$(document).ready(function() {

	$('.location img').live('mouseenter',function() {
	    $(this).animate({left:'-20',width:'209',top:'-20',height:'169'});
	    $(this).parent().parent().removeClass('cycling');
	    });
	$('.location img').live('mouseleave',function() {
	    $(this).animate({left:'0',width:'169',top:'0',height:'129'});
	    if ($(this).parent().parent().hasClass('cycle')) {
	        $(this).parent().parent().addClass('cycling');
	    }
	    }); 
	
	$('.cycle').addClass('cycling');
	animateCycle();
        
	var navDuration = 150; //time in miliseconds
	var navJumpHeight = "0.45em";
	
	$(function() {
        $( "#tabs" ).tabs();
    });
	
	$(function(){
	    //Get our elements for faster access and set overlay width
	    var div = $('div.sc_menu'),
	                 ul = $('ul.sc_menu'),
	                 // unordered list's left margin
	                 ulPadding = 15;

	    //Get menu width
	    var divWidth = div.width();

	    //Remove scrollbars
	    div.css({overflow: 'hidden'});

	    //Find last image container
	    var lastLi = ul.find('li:last-child');

	    //When user move mouse over menu
	    div.mousemove(function(e){

	      //As images are loaded ul width increases,
	      //so we recalculate it each time
	      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

	      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	      div.scrollLeft(left);
	    });
	});
	
	
	$(".resultImage").hover(
	  function () {
		  $(this).find(".imagehover").show();
	  },function() {
		  $(this).find(".imagehover").hide();
	  }
	);
	
    $("ul.topnav li").hover(function() { //When trigger is clicked...  
        $(this).find("ul.subnav").slideDown('fast'); //Drop down the subnav on click  
    },function(){
    	$(this).find("ul.subnav").slideUp('fast');
    });

    setTimeout(animate,2000);
    
	$("#slider").easySlider({
        auto: true,
        continuous: true,
        numeric: true
    });
	$('#calcsubmit').bind('click',function()
	{
		var cashprice = $('#cashprice').val();
		var deposit = $('#deposit').val();
		var loanlength = $('#loanlength').val();
		var loanvalue = cashprice - deposit;
		loanvalue = loanvalue.toFixed(2);
		var totalrepayment = (loanvalue * 1.07)+300;
		totalrepayment = totalrepayment.toFixed(2);
		var monthrepayment = totalrepayment / loanlength;
		monthrepayment = monthrepayment.toFixed(2);
		$('#loanvalue').val(String.fromCharCode(163)+loanvalue);
		$('#monthrepayment').val(String.fromCharCode(163)+monthrepayment);
		$('#totalrepayment').val(String.fromCharCode(163)+totalrepayment);
		return false;
	});
	
	pageurl = window.location.search.substring(1);

	function querySt(qs) 
	{
		pageurl = window.location.search.substring(1);
		qssplit = pageurl.split("&");
		for (i=0;i<qssplit.length;i++) 
		{
			qssplitval = qssplit[i].split("=");
			if (qssplitval[0] == qs) 
			{
				return qssplitval[1];
			}
		}
	}

	var koko = querySt("page");

	$('#make').change(function(){
		$('#model').html('<option value="">Select a Model</option>');
		url = "remotesearch.php?type=model&make="+$("#make").attr("value");
        
        $.getJSON(url + '&callback=?', function(data) {
        	for(x in data) {
        		model = data[x];
        		opt = $('<option/>').attr('value', model).html(model);
        		$('#model').append(opt);
        	}
        });		
	});

	$('#make').trigger('change');
	$('#model').show();
    
    
    
    /******** FANCYBOX ********/
    /* Date picker */
    	$("#bookingdate").datepicker({
			changeMonth: true,
			changeYear: true
		});
    
	/* Apply fancybox to multiple items */
	
	$(".imagebox").fancybox({
		//'transitionIn'	:	'elastic',
		//'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'width'			:	640,
		'height'		:	480
		//'overlayShow'	:	false
	});
	
	$('#frpage').attr('value', document.location);
	
	$("#sendtofriend").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'width'			: 220,
		'onClosed'		: function() {
		    $("#friend_error").hide();
		}
	});
	
	$("#friend_form").bind("submit", function() {
		if ($("#frname").val().length < 1 || $("#fremail").val().length < 1 || $("#frpage").val().length < 1) {
		    $("#friend_error").show();
		    $.fancybox.resize();
		    return false;
		}

		$.fancybox.showActivity();
		
		$.post("modules/friendmailer.php", {name: $("#frname").val(), 
											   email: $("#fremail").val(), 
											   page: $("#frpage").val(), 
											   message: $("#frmessage").val()},
			function(data){$.fancybox(data);}
		);
		
		return false;
	});
	
    /******** Change Image ********/

	$(".unselected").live('click',function(){
		$(this).parent().attr('rel', '');
		$(".selected").parent().attr('rel', 'imagegroup');
		$("#mainimageimg").attr('src', $(this).attr('src'));
		$("#mainimage .imagebox").attr('href', $(this).attr('src'));
		$(".selected").removeClass('selected').addClass('unselected');
		$(this).removeClass('unselected').addClass('selected');
	});
	
	$('.accordion').live('click',function(){
		$(this).next().toggle();
		return false;
	}).next().hide();
	
	$('#partex').bind('change',function()
	{
		$('#partexlocatecontent').toggle(400);
		return false;
	});
	
	/***** Image Slideshow ****/
	$('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	
	/***** Details Tabs Change *****/
	$('.tabUnselected').live('click',function(){
		$('.tabSelected').addClass('tabUnselected').removeClass('tabSelected');
		$(this).addClass('tabSelected').removeClass('tabUnselected');
		for(i=1;i<=3;i++)
		{
			if($(this).hasClass('tab'+i))
			{
				$('.content'+i).addClass('changer');
				$('.selectedContent').addClass('unselectedContent').addClass('temp').removeClass('selectedContent');
				$('.changer').addClass('selectedContent').removeClass('unselectedContent').removeClass('changer');
			}
		}
		$('.temp').removeClass('selectedContent').addClass('unselectedContent').removeClass('temp');
	});
	
	
	
});
var RecaptchaOptions = {
		   theme : 'clean'
		};
