// Functon to Show out Busy Div
function showBusy(){
	$('#busy').show('slow');
}

function loadPhoto(url){
	if(url === ''){ return; }
	var photo = new Image();
	$(photo).load(function(){
		$('#photo-preview').removeClass('loading').html('').append(this);
	}).attr('src', url); 
	
}



/*----------------------------*/	
	
	function validateName(){
		//if it's NOT valid
		if(($("#name").val() == '')){
			alert("Please enter your name");
			return false;
		}
		//if it's valid
		else{
			return true;
		}
	}	
	
	
		function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			return true;
		}
		//if it's NOT valid
		else{
			alert("Please enter a valid email address");
			return false;
		}
	}



function validatePhone(){
		//if it's NOT valid
		if(($("#phone").val() == '')){
			alert("Please enter a contact number");
			return false;
		}
		//if it's valid
		else{
			return true;
		}
	}	
	
	
	function thankyouPage() {
		
		window.location.assign("http://"+document.domain+"/dealers/thankyou?location=dropdown_postcode");
		
	}
	
	
	function  getAddressFromPostCode() {	
		
		function loadIntoHolder(data) {
		$('#postcode-input-loader-icon').removeClass('loading');
		$('#dropdown_contact').append(data);
		$('#dropdown_contact').slideDown("slow");
		
			$('.dropdown-contact .close-button').click(function () { 
			$('#dropdown_contact').slideUp("slow");
			$('#dropdown_contact').html('');
			}); 
			
				$('#submit_dropdown').click(function () { 
						
					
						
						var therePostCode = $('#find-field').val().replace(/ /g,'');

						// $.AJAX Request
						$('#dropdownfrom').submit(function(eve){
							eve.preventDefault();
							
							if(validateName() & validateEmail() & validatePhone() ) {
	
							$('#submit_dropdown').hide();
	
	
							$.ajax({
								url: "/dealers/dropDownSend/"+therePostCode,
								type: "POST",
								dataType: "html",
								data: $('#dropdownfrom').serialize(),
								beforeSend: function(){
									showBusy();
									//console.log('posting...');//log in firebug
								},	
								success: function(html) {
									//processForm(html);
									//console.log('post success');//log in firebug
									$('.dropdown-contact').html('');
									$('.dropdown-contact').append(html);
									
									$('.dropdown-contact .close-button').click(function () { 
									$('#dropdown_contact').slideUp("slow");
									$('#dropdown_contact').html('');
									
									
									// Redirect
									
								
									}); 

								}
								
							});

thankyouPage();							
} else {
				
return false }
		
		
		
		});	
		
		
		});
		}
		
		
	
		
								
		
		
	var therePostCode = $('#find-field').val().replace(/ /g,'');
	
	var baseURL = document.domain;
	
	$.get("http://"+baseURL+"/dealers/postcode/"+therePostCode, loadIntoHolder);
	}

$(document).ready(function(){
	
    $('INPUT.auto-hint').each(function(i, el){
        if($(this).val() === ''){
            $(this).val($(this).attr('title'));
        }
        $(el).focus(function(){
            if ($(this).val() === $(this).attr('title')) {
                $(this).val('');
            }       
        });
        $(el).blur(function(){
            if ($(this).val() === '') {
                $(this).val($(this).attr('title'));
            }
        });
    });
    
    $('#carousel').jCarouselLite({
        visible: 1,
		speed:2500,
		auto:5000,
		circular:true,
        btnNext: ".slider-nav a.next",
        btnPrev: ".slider-nav a.prev"
    });
    
    $('#carousel-main').jCarouselLite({
        visible: 5,
        scroll: 5,
        btnNext: "a.in-right",
        btnPrev: "a.in-left"
    });


	$('#brand-carousel').jCarouselLite({
        visible: 5,
        scroll: 1,
        btnNext: ".brand-right a",
        btnPrev: ".brand-left a"
    });




	$('.dropdown-contact').fadeOut("slow");
	
	$("input[name='lookuptype']").click(function(){
			$('#find-field').val('GB or NI Postcode');
			$('#find-field').attr('title','GB or NI Postcode');
	});

	
	$('.find-btn').click(function (eve) { 
	eve.preventDefault();
	
				$('#postcode-input-loader-icon').addClass('loading');
				$('.dropdown-contact').html('');
				
				var therePostCode = $('#find-field').val().replace(/ /g,'');
				
				if (checkPostCode(therePostCode)) {
				getAddressFromPostCode();
				} 
				else {
				alert ("Postcode has invalid format");
				$('#postcode-input-loader-icon').removeClass('loading');
				}
	
	}); 




    $('UL.thumb LI.current').each(function(i){
		if(!i){
			var photoUrl = $(this).children('a').attr('href');
			loadPhoto( photoUrl );
		}
	});
	$('.thumb A').click(function(){
		$('UL.photo-list LI').removeClass('current');
		$(this).parent('LI').addClass('current');
		var photoUrl = $(this).attr('href');
			$('#photo-preview').fadeOut(1000, function(){
				loadPhoto( photoUrl );
			});	
			$('#photo-preview').fadeIn(1000);
		return false;
	});
	
    
	
	
	//$("#photo-preview" ).mouseenter(function() {
//		$( "#you-tube-link" ).addClass( "you-tube-link-over");
//		$( "#you-tube-link" ).removeClass( "you-tube-link-out");
//		return false;
//	});
//	$("#photo-preview" ).mouseleave(function() {
//		$( "#you-tube-link" ).removeClass( "you-tube-link-over");
//		$( "#you-tube-link" ).addClass( "you-tube-link-out");
//		return false;
//	});
//	
//	
//	
//		$("#you-tube-link" ).mouseenter(function() {
//		$( "#you-tube-link" ).addClass( "you-tube-link-over");
//		$( "#you-tube-link" ).removeClass( "you-tube-link-out");
//		return false;
//	});
//	$("#you-tube-link" ).mouseleave(function() {
//		$( "#you-tube-link" ).removeClass( "you-tube-link-over");
//		$( "#you-tube-link" ).addClass( "you-tube-link-out");
//		return false;
//	});
	
});



