if (typeof window.lpb == "undefined") {
	window.lpb = {};
}

lpb.captchachecks = {};

$(document).ready(function () {
	//$(".switch-banner .header").tabs({selected: 0});
	//$("#rhs-form").tabs({selected: 0});
    
    $("#home-why-use-lpb .content .items").jCarouselLite({
        btnNext: "#home-why-use-lpb .next-slide",
        btnPrev: "#home-why-use-lpb .prev-slide",
        visible: 5
    });
    $("#home-how-we-help .content .items").jCarouselLite({
        btnNext: "#home-how-we-help .next-slide",
        btnPrev: "#home-how-we-help .prev-slide",
        visible: 5
    });

	// Hidden by default
	$(".collapse-panel .content").hide();

	// Shown by default
	$(".collapse-panel-expanded .content").show();

	$(".collapse-panel .header").click(function () {
		// Toggle between expanded / collapsed state
		$(this).parent().toggleClass("collapse-panel-expanded");
		$(".content", $(this).parent()).slideToggle("fast");
	});
    //fancy box

    $(".video").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});
		return false;
	});


    $('.fancybox').fancybox({
        'type'          :   'iframe',
        'showCloseButton'	: false,
		'speedIn'		:	600,
		'speedOut'		:	200,
        'width'         :   506 ,
        'height'        :   548,
        'padding'       :   0,
        'titleShow'     :   false,
        'scrolling'     :   'no',
        'centerOnScroll':   false,
        'autoScale'         :   false
	});
    $('.fancybox3').fancybox({
        'type'          :   'iframe',
        'showCloseButton'	: false,
		'speedIn'		:	600,
		'speedOut'		:	200,
        'width'         :   506 ,
        'height'        :   588,
        'padding'       :   0,
        'titleShow'     :   false,
        'scrolling'     :   'no',
        'centerOnScroll':   false,
        'autoScale'         :   false
	});
    $('.fancybox2').click(function(){
        $.fancybox({
        'type'          :   'iframe',
        'showCloseButton'	: false,
		'speedIn'		:	600,
		'speedOut'		:	200,
        'width'         :   506 ,
        'height'        :   627,
        'padding'       :   0,
        'titleShow'     :   false,
        'scrolling'     :   'no',
        'href'          :   'property-valuation-report.php?postcode='+$('#postcode').val(),
        'centerOnScroll':   false,
        'autoScale'         :   false
        });
    });
    

/*
    $('.toggle').click(function() {
            $(this).next().toggle('fast');
            $(this).parents('.content').addClass('opened');
            $(this).parents('li').css('overflow','visible');
            return false;
    }).next().hide();
*/
    var contentHeight = $('#home-how-we-help .content').height();
    $('#home-how-we-help .toggle').click(function(){
        showClass = $('.'+$(this).attr('title'));
        $('#home-how-we-help .banner-content:not(.'+$(this).attr('title')+')').removeClass('open');
        $('#home-how-we-help .banner-part:not(#'+$(this).attr('id')+')').removeClass('opened');
        showClass.toggleClass('open');
        $(this).toggleClass('opened');
        
        if($('#home-how-we-help .banner-content').hasClass('open')) $(this).parents('.content').height(contentHeight+showClass.height()+25);
        else
            $(this).parents('.content').height(contentHeight);
    });

    var contentHeight2 = $('#home-why-use-lpb .content').height();
    $('#home-why-use-lpb .toggle').click(function(){
        showClass = $('.'+$(this).attr('id'));
        $('#home-why-use-lpb .banner-content:not(.'+$(this).attr('id')+')').removeClass('open');
        $('#home-why-use-lpb .banner-part:not(#'+$(this).attr('id')+')').removeClass('opened');
        showClass.toggleClass('open');
        $(this).toggleClass('opened');

        if($('#home-why-use-lpb .banner-content').hasClass('open')) $(this).parents('.content').height(contentHeight2+showClass.height()+25);
        else
            $(this).parents('.content').height(contentHeight2);
    });
    
    
	var clearFocusFN = function () {
		if ($(this).val() == this.defaultValue) {
			$(this).val("");
		}
	};
	var fillBlurFN = function () {
		if ($(this).val() == "") {
			$(this).val(this.defaultValue);
		}
	};

	$('input[type="text"]').focus(clearFocusFN);
	$('input[type="text"]').blur(fillBlurFN);

	$("#main-nav ul").hide();

	$("#main-nav > li").hover(function () {
		//$("ul", $(this)).slideDown(100);
		$("ul", $(this)).show();
	},
	function () {
		$("ul", $(this)).hide();
	});

	$("#main-nav ul li").hover(function () {
		$(this).addClass("hover");
	},
	function () {
		$(this).removeClass("hover");
	});
	
	$("#blog-post-comments").hide();
	$("#blog-view-comments").click(function () {
		$("#blog-post-comments").slideToggle("fast");
	});
	
	$("#offer-viewing-date").datepicker({
        minDate: 0
    });
	
	var bfn = function () {
		var valueField = $("#valuation-property-value");
		if ((valueField.parse() != "NaN") && (valueField.parse() != "0")) {
			$(valueField).val(valueField.parse());
			$(valueField).format({format: "#,###.00", local:"uk"});
			$("#valuation-property-value-hidden").val($(valueField).parse());
		}
	};

	$("#valuation-property-value").blur(bfn);
	$("#valuation-form").submit(bfn);
	$("#valuation-form").submit(function () {
		$("#valuation-property-value").attr("disabled", "disabled");
	});
	
	var props = {
        rules: {
			captcha: {
				required: true,
				remote: "validate-captcha.php"
			},
			captcha1: {
				required: true,
				remote: "validate-captcha1.php"
			}
		},
		errorPlacement: function(error, element) {
			element.parent().addClass("error");
			element.parent().parent().append(error);
			//error.insertAfter(element.parent());
		},
		unhighlight: function (element) { 
			$(element).parent().removeClass("error");
		},
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			var errHolder = $(this).find(".errorholder")
			if (errors) {
				var message = "Some fields have not been filled correctly.";
				errHolder.html(message);
				errHolder.show();
			} else {
				errHolder.hide();
			}
		},
		messages: {
			captcha: "Entered captcha is not correct.",
			captcha1: "Entered captcha is not correct."
		},
        onkeyup: false

	};
	
	$("#refer-form").validate(props);
	$("#offer-form").validate(props);
	$("#contact-form").validate(props);
	
    $("#started_step1").validate(props);
    $("#started_step2").validate(props);
    $("#started_step3").validate(props);
    $("#started_step4").validate(props);
    $("#request-a-callback").validate(props);
    $("#get-started").validate(props);


});

jQuery.validator.addClassRules("captcha123", {
  required: true,
  remote: "validate-captcha.php"
});


jQuery.validator.addMethod("captcha32", function( value, element ) {
	if (value.length != 5) {
		return false;
	}
	
	if (typeof lpb.captchachecks[value] != "undefined") {
		return lpb.captchachecks[value];
	}

	$.get(
		"validate-captcha.php",
		{
			captcha:value
		},
		function(data) {
			lpb.captchachecks[value] = data;
			if (data) {
				$(element).parent().removeClass("error");
				$(element).removeClass("error");
				
				$(element).parent().parent().find("label.error").remove();
			}
			else {
				$(element).parent().addClass("error");
				$(element).addClass("error");
			}
		},
		"json"
	);
	
	
	return lpb.captchachecks[value];
}, "Entered captcha is not correct.");

jQuery.validator.addMethod("nondefault", function( value, element ) {
	return value != element.defaultValue;
}, "This field is required.");
