var bindModels={};
var selectModel={};
var clearModels={};
var arrModels={};
var make='#find_make';
var model='#find_model';
var gallery_options={panel_width:484,panel_height:322,frame_width:60,frame_height:40,background_color:'transparent',border:'none',transition_interval:0,nav_theme:'dark',filmstrip_size:2,slide_method:'pointer'};

$(document).ready(function(){

	/* Makes & models */
	$.getJSON('/_utils/json.php?do=makes-models',function(j){arrModels=j;});
	$(make).change(function(){bindModels(make,model);});
	clearModels=function(dd_model){$(dd_model)[0].options.length=1;};
	bindModels=function(dd_make,dd_model){
		clearModels(dd_model);
		var arr=$.grep(arrModels,function(a){return a.make_link_name==$(dd_make).val();});
		$(arr).each(function(i){$("<option>").attr("value",arr[i].link_name).text(arr[i].model).appendTo(dd_model);});
		if($(arr).length>0){if($(arr).length==1){$(dd_model).attr('selectedIndex',1);}else{$(dd_model).attr('selectedIndex',0);}}
	};

	$('#form-find').submit(function() {
		var url = '/vehicles/'+$('#find_section').val()+'/';
		var make = $('#find_make').val();
		var model = $('#find_model').val();
		if (make != '') { url += make+'/'; }
		if (model != '') { url += model+'/'; }
		window.open(url,'_self');
	});

	/* Callback */
	var callbackValidate=function(){var err=1;var name=$("#callback_name").val();var phone=$("#callback_phone").val();if (name!=''&&phone!=''){err=0;}if (err==0){$("#callback-form").hide();$("#callback-complete").hide();$("#callback-loading").show();return true;}else{alert("Please enter your name and telephone number so that we can call you.");return false;}};
	var callbackComplete=function(){
 	$.get('/thanks-callback.php');
	$("#callback-loading").hide();
	$("#callback-complete").show();
	setTimeout(callbackRevert,4500);
	};var callbackRevert=function(){$("#callback-loading").hide();$("#callback-complete").fadeOut('slow');$("#callback-form").fadeIn('slow');};$('#form-callback').ajaxForm({beforeSubmit:callbackValidate,success:callbackComplete,clearForm:true});

	/* Alerts */
	var alertsValidate=function(){
		var err=1;
		var name=$("#alerts_firstname").val();
		var phone=$("#alerts_surname").val();
		var email=$("#alerts_email").val();
		var terms = $("input[name=terms]").attr('checked');

		if (name!=''&&phone!=''&&email!=''&&terms==true&& $("input[name='vehicles[]']:checked").length > 0){
			err=0;
		}
		if (err==0){
			$("#alerts-form").hide();
			$("#alerts-complete").hide();
			$("#alerts-loading").show();
			return true;
		}else{
			alert("An error occured. The following details are required:\n\n- First name\n- Surname\n- E-mail address\n\nAlso you must select at least one type of vehicle and confirm you have read our Terms & Conditions.");
			return false;
		}
	};

	var alertsComplete=function(){
	 	$.get('/thanks-alerts.php');
		$("#alerts-loading").hide();
		$("#alerts-complete").show();
		setTimeout(alertsRevert,10000);
	};

	var alertsRevert=function(){
		$("#alerts-loading").hide();
		$("#alerts-complete").fadeOut('slow');
		$("#alerts-form").fadeIn('slow');
	};
	
	$('#form-alerts').ajaxForm({
		beforeSubmit:alertsValidate,success:alertsComplete,clearForm:true
	});

	/* Finance */
	$("#recalc_finance").click(function() {
		var v = $("#vehicle_id").val();
		var d = $("#finance_deposit").val();
		var r = $("#finance_repayments").val();

		if (d > 0 && r > 0) {
			$.get("/_utils/calc.php",{id:v,deposit:d,repay:r},function(data){
				var finance = data.split("|");
				$("#current_deposit").html('&pound;'+finance[0]+' <span style="font-weight:normal;">('+d+'%)</span>');
				$("#current_months").html(r+' months');$("#current_monthly").html('&pound;'+finance[1]);$("#current_weekly").html('&pound;'+finance[2]);
				$("#amount_to_pay").attr('value',finance[0]);
			});
		} else {
			alert('Please select a deposit amount and repayment period.');
		}
	});

	/* Gallery */
	$("#gallery-nav a").click(function() {
		var nav_array = $(this).attr('id').split("_");
		$('div#gallery_1').load('/_utils/gallery.php',
		{id:$('#vehicle_id').val(),gallery:nav_array[2]},function() {
			$('.gallery-link').removeClass('on');
			$('#gallery_link_'+nav_array[2]).addClass('on');
        	$('div#gallery_1 .gallery').galleryView(gallery_options);
        });
	});

	/* FAQs */
	$('div.faqs h2 span').click(function() {
		var c = $(this).attr('id').split("_");
		$('div.c_'+c[1]).toggle();
	});
	$('div.faqs p.q').click(function() {
		var q = $(this).attr('id').split("_");
		$('div#a_'+q[1]).toggle();
	});

	/* Buy Options */
	$('.buy-option a').hover(function() {
		var bits = $(this).attr('id').split("_");
		$('#op'+bits[1]).css('background-position','0 0');
	},function() {
		var bits = $(this).attr('id').split("_");
		$('#op'+bits[1]).css('background-position','0 -86px');
	});

	/* Order Form */
	$('#del_same').click(function() {
		var s = $(this).attr('checked');
		if (s) {
			$('#del_add1').attr('value',$('#bill_add1').val());
			$('#del_add2').attr('value',$('#bill_add2').val());
			$('#del_add3').attr('value',$('#bill_add3').val());
			$('#del_town').attr('value',$('#bill_town').val());
			$('#del_county').attr('value',$('#bill_county').val());
			$('#del_country').attr('value',$('#bill_country').val());
			$('#del_postcode').attr('value',$('#bill_postcode').val());
		} else {
			$('input.del').attr('value','');
		}
	});

	/* Order Receipt */
	$('#button-invoice').click(function() {
		window.open('/index.php?do=order-invoice','usedcars2u_invoice','width=600,height=600,scrolling=yes');
	});

	/* Misc */
	if($.browser.msie&&jQuery.browser.version=='6.0'){$(document).pngFix();}
	$("a[rel=external]").attr('target','_blank');

});
