var RazsorLayoutJS = {};

RazsorLayoutJS.init = function() {
    RazsorLayoutJS.globalJS();
    RazsorLayoutJS.stocklistFinanceInformationDisplay();
    RazsorLayoutJS.addSocialAndPrintButtons();
    RazsorLayoutJS.contactPageReadMore();
    RazsorLayoutJS.fullFinanceForm();
    RazsorLayoutJS.setToBiggestWidth('ul.showcase-specs li strong');
    RazsorLayoutJS.setNumberOnlyInputs('.text-year'); // CSS name
    RazsorLayoutJS.createDateTimePicker('.selected-date');
    RazsorLayoutJS.techSpecBehaviour();
    RazsorLayoutJS.techFinanceFunctionality();
    RazsorLayoutJS.createPrintDescription();
    RazsorLayoutJS.showCaseVehicleHideWelcomeText();
    RazsorLayoutJS.stocklistFinanceCalculator();
};

RazsorLayoutJS.globalJS = function() {
    // mark as javascript enabled
    $("body").addClass("js");
    balanceHeightOfPromotionsAfterTimeout(250);
};


RazsorLayoutJS.createPrintDescription = function() {
    // Detail page, truncate text
    $(".printversion").html($('.rzp-used-cars .rzc-advert .description .more p').html());
    $('.rzp-used-cars .rzc-advert .description .more p').trunc(320, '... <span class="trunc">[[More]]</span>', '<span  class="trunc hide">[[Hide]]</span>');
};

RazsorLayoutJS.setNumberOnlyInputs = function() {
    // Set the year text box to only accept numbers
    for (i = 0; i < arguments.length; i++) {
        $(arguments[i]).keyup(function() {
            this.value = this.value.replace(/[^0-9\.]/g, '');
        });
    }
};

RazsorLayoutJS.createDateTimePicker = function() {
    for (i = 0; i < arguments.length; i++) {
        $(arguments[i]).datepicker({
            showOn : "button",
            dateFormat : 'dd/mm/yy'
        });
    }
};

RazsorLayoutJS.setToBiggestWidth = function() {
    for (i = 0; i < arguments.length; i++) {
        var max = 0;
        $(arguments[i]).each(function() {
            if ($(this).width() > max)
                max = $(this).width();
        });
        $(arguments[i]).width(max + 1);
    }
};

RazsorLayoutJS.addSocialAndPrintButtons = function() {
    // Add print button to main menu
    $(".rzc-main-menu ul.social-menu").append('<li class="print"><a href="#"><span>Print page</span></a></li>');

    // Open print dialog
    $(".rzc-main-menu ul .print").click(function() {
        window.print();
        return false;
    });

    $(".stockFacebook, .stockTwitter").click(function() {
        return false;
    });
};

RazsorLayoutJS.fullFinanceForm = function () {

    // hide full enquiry form
    $(".rzc-finance .full").hide();
    $(".rzp-finance .submit-and-warning button.submit span").text("Send my finance enquiry");

    // show correct form on page load
    var formToShow = $("#formToShow").attr("value");

    if (formToShow !== null && formToShow === "full") {
    	$("#full-enquiry-form").attr("value","true");
        $(".rzc-finance .quick").hide();
        $(".rzc-finance .full").show();
    } else {
    	$("#full-enquiry-form").attr("value", "false");
    	$(".rzc-finance .quick").show();
        $(".rzc-finance .full").hide();
    }

    // show hide forms on click
    var fullLink = $(".quick .switch a");
    fullLink.click(function () {
        $(".quick").fadeOut('fast',function () {
            $(".rzc-finance .full").show();
            $("#fields > .full").slideDown(1000);
            formToShow = "full";
            $("#full-enquiry-form").attr("value", "true");
            $('.common-field').removeClass("highlight-error");
            $('#finance-form .form-warning').empty();
        });
    });
    var quickLink = $(".full .switch a");
    quickLink.click(function () {
        $(".full").fadeOut('fast', function () {
            $(".quick").show();
            $("#fields > .quick").slideDown();
            $("#full-enquiry-form").attr("value","false");
            formToShow = "quick";
            $('.common-field').removeClass("highlight-error");
            $('#finance-form .form-warning').empty();
        });
    });

    $(".current-address").change(function () {
        var yearsCheckAddress = parseInt($(".current-address").val(), 10);
        if (yearsCheckAddress > 3) {
            $(".previous-address input").attr("disabled", "disabled");
            $(".previous-address input").attr("style", "background:#EBEBE4;");
        } else {
            $(".previous-address input").removeAttr("disabled");
            $(".previous-address input").removeAttr("style");
        }
    });

    $(".current-employer").change(function () {
        var yearsCheckEmp = parseInt($(".current-employer").val(), 10);
        if (yearsCheckEmp > 2) {
            $(".previous-employer input").attr("disabled", "disabled");
            $(".previous-employer input").attr("style", "background:#EBEBE4;");
        } else {
            $(".previous-employer input").removeAttr("disabled");
            $(".previous-employer input").removeAttr("style");
        }
    });
    
    $(".dob").datepicker({
		dateFormat : 'dd/mm/yy' ,
		changeMonth: true,
		changeYear: true,
		yearRange: '-111:+0'
	});

};

RazsorLayoutJS.showCaseVehicleHideWelcomeText = function() {
	 if ($('.left-content .showcase-vehicle-wrapper #showcaseVehicleTitleComponent').is(":visible")) {
         $('.left-content .showcase-vehicle-wrapper').show();
         $('.left-content .showcase-vehicle-wrapper').siblings().hide();
     }
     else $('.left-content .showcase-vehicle-wrapper').hide();
};

RazsorLayoutJS.techSpecBehaviour = function() {
    // Detail page, tech specs
    $('.rzc-advert .more-details h3:eq(0)').addClass('on');
    $('.rzc-advert .more-details .box:not(:eq(0))').hide();
    $('.rzc-advert .more-details h3').click(function() {
        if ($(this).hasClass('on')) {
            $(this).removeClass('on');
            $(this).next().slideUp();
        } else {
            $(this).parent().find('h3').removeClass('on');
            $(this).parent().find('.box').slideUp();
            $(this).addClass('on');
            $(this).next().slideDown();
        }
    });
    $(".rzc-advert .more-details table tbody tr:even").addClass('alt');
};

RazsorLayoutJS.techFinanceFunctionality = function() {
	  // Tabs for advert detail page
    $(".rzc-advert .rzc-c-s-strip, .rzc-finance-calculator").hide();
    $(".tab-tech-spec").addClass("tab-on");
    $(".more-details").show();

    $(".advert-detail-more-info-tabs li a").click(function() {
        if (!$(this).hasClass("tab-on")) {
            $(".more-details, .rzc-c-s-strip, .rzc-finance-calculator").slideUp();
            $(".advert-detail-more-info-tabs li a").removeClass("tab-on");
            var moreInfoArea = $(this).attr("class");
            moreInfoArea = moreInfoArea.split(" ");
            switch (moreInfoArea[0]) {
            case "tab-tech-spec":
                $(".tab-tech-spec").addClass("tab-on");
                $(".more-details").slideDown();
                break;
            case "tab-similar-cars":
                $(".tab-similar-cars").addClass("tab-on");
                $(".rzc-c-s-strip").slideDown();
                break;
            case "tab-finance-calculator":
                $(".tab-finance-calculator").addClass("tab-on");
                $(".rzc-finance-calculator").slideDown();
                break;
            }
        }
        return false;
    });

    if ($(".tab-tech-spec").length == 0) {
        $(".tab-similar-cars").addClass("tab-on");
        $(".rzc-c-s-strip").slideDown();
    }
};

RazsorLayoutJS.stocklistFinanceInformationDisplay = function(){
	$(".rzc-stocklist .financeIllustration").prepend('<a class="finance-info-button-outer"><span>View finance information</span></a>');
    
    $(".financeIllustrationWrapper").hide();    
    $(".advert-services-inner .other").hide();
    
    $(".advert-services-title").live('click',function(){ 	
    	if ($(this).hasClass('on')) {
            $(this).removeClass('on');
            $(this).parent().find(".advert-services-inner").slideDown();
        } else {
            $(this).addClass('on');
            $(this).parent().find(".advert-services-inner").slideUp();
        }
    });
};

RazsorLayoutJS.contactPageReadMore = function() {
	
	  // The more information section
    $('.container2 .rzc-opening-times .additionalInformation').after('<div class="read-more"><a href="#">Read more</a></div>');
    $('.container2 .rzc-opening-times .additionalInformation').css('height', '44px');

    $(".read-more").click(function() {
        if ($(".read-more").hasClass("expanded")) {
            $("div.additionalInformation").css("height", "44px");
            $(".read-more").removeClass("expanded");
            $(".read-more a").html("Read more");
        } else {
            $("div.additionalInformation").css("height", "auto");
            $(".read-more").addClass("expanded");
            $(".read-more a").html("Read less");
        }
        return false;
    });
};

RazsorLayoutJS.stocklistFinanceCalculator = function() {
	if (!$(".finance-calculator .box-calculator").hasClass("rzc-hidden")) {
		$(".finance-calculator").append('<div class="finance-calculator-show-hide"><span>Adjust finance options</span</div>');
		$(".finance-calculator .wrapper").hide();
		$(".finance-calculator-show-hide").live('click',function(){
		 	if($(".finance-calculator .wrapper").hasClass("down")){		
				$(".finance-calculator .wrapper").slideUp().removeClass("down");
				$(this).find("span").html("Adjust finance options");
				$(this).css("bottom","-20px");
			} else {
				$(".finance-calculator .wrapper").slideDown().addClass("down");
				$(this).find("span").html("Close finance options");
				$(this).css("bottom","-30px");
			}
		});
	}
	$(".rzc-stocklist .calculateFinance").click(function(){		
		$(this).val("Updating...");	
	});
};


$(document).ready(function() {
    RazsorLayoutJS.init();
});



