$.fn.center = function() {
    this.css({
        'position': 'fixed',
        'left': '50%',
        'top': '50%',
    });
    this.css({
        'margin-left': -this.width() / 2 + 'px',
        'margin-top': -this.height() / 2 + 'px'
    });

    return this;
}

$(document).ready(function(){
		
	$("#testDriveForm").validate({
		wrapper: "h3",
		submitHandler: function(form) {
			setCookie('thankYou', 'Thanks You<br />Your test drive booking request has been sent<br />We shall contact you soon.', 1);
   			form.testDriveForm.submit();
 		}
	});
	
	$("#vehicleEnquiryForm").validate({
		wrapper: "h3",
		submitHandler: function(form) {
			setCookie('thankYou', 'Thanks You<br />Your vehicle enquiry has been sent<br />We shall contact you soon.', 1);
   			form.vehicleEnquiryForm.submit();
 		}
	});
	
	$("#tellAFriendForm").validate({
		wrapper: "h3",
		submitHandler: function(form) {
			setCookie('thankYou', 'Your \'Tell a friend\' email has been sent<br />Thank you for passing this vehicles information onto your friend.', 1);
   			form.tellAFriendForm.submit();
 		}
	});
	
	$("#vehicleFinderForm").validate({
		wrapper: "h3",
		submitHandler: function(form) {
			setCookie('thankYou', 'Thanks You<br />Your vehicle finder request has been sent<br />We shall contact you soon.', 1);
   			form.tellAFriendForm.submit();
 		}
	});
	
	$('.formLink').click(function(){
		var formName = $(this).attr('data-formname');
		
		if($('.listForm:visible').size() > 0){
			var openForm = $('.listForm:visible').attr('id');
			$('.listForm:visible').slideToggle('fast', function(){
				if(formName != openForm.replace('Slider', '')){
					$('#' + formName + "Slider").slideToggle('fast');
				}
			});
		} else {
			$('#' + formName + "Slider").slideToggle('fast');
		}
	});
	
	if(getCookie('thankYou') != null){
		$('#thankYouMessage').html(getCookie('thankYou'));
		$('#thankYouNotification').delay(1000).slideDown('slow').delay(5000).slideUp('fast');
		deleteCookie('thankYou')
	 }
	 
	var vidOpen = "false";
	
	$('img.videoLink').click(function(){
		if(vidOpen == "false"){
			$('img.videoLink').fadeTo('fast', 0.10);
			$('img.videoLink').removeClass('myPointer');
			vidOpen = "true";
			vidPath = "";
			if($(this).hasClass('hp')){
				vidPath = "http://player.vimeo.com/video/35366953?autoplay=1";
			}
			if($(this).hasClass('pcp')){
				vidPath = "http://player.vimeo.com/video/35317900?autoplay=1";
			}
			if($(this).hasClass('lp')){
				vidPath = "http://player.vimeo.com/video/35367943?autoplay=1";
			}
   			$(this).after('<div id="videoContainer"><div class="vidClose"></div><iframe src="'+vidPath+'" width="640" height="360" frameborder="0"></iframe></div>');
			$('#videoContainer').center();
			$('#videoContainer .vidClose').click(function(){
				$('img.videoLink').fadeTo('fast', 1);
				$('img.videoLink').addClass('myPointer');
				vidOpen = "false";
				$("#videoContainer").remove();
			});
		}
	});
});
