﻿$(document).ready(function () {

    if (!Modernizr.input.placeholder) {

        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();
        $('[placeholder]').parents('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });

    }

    /* Blend navigation */
    $('#primaryNav a').blend(150);

    // Randomizer
    if ($('#random').size() > 0) {
        var images = ['banner1.jpg', 'banner2.jpg', 'banner3.jpg', 'banner4.jpg'];
        $('<img src="../Content/images/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#random');
    }

    if ($('#random-sizes').size() > 0) {
        var images = ['photo-sizes1.jpg', 'photo-sizes2.jpg', 'photo-sizes3.jpg', 'photo-sizes4.jpg', 'photo-sizes5.jpg'];
        $('<img src="../Content/images/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#random-sizes');
    }

    if ($('#random-results').size() > 0) {
        var images = ['photo-sizes1.jpg', 'photo-sizes2.jpg', 'photo-sizes3.jpg', 'photo-sizes4.jpg', 'photo-sizes5.jpg'];
        $('<img src="../../Content/images/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#random-results');
    }

    // Tire Videos
    $("#videoWrapper li:first").addClass("current").show();
    $(".video-display:first").show();

    $("#videoWrapper li").click(function () {

        $("#videoWrapper li").removeClass("current");
        $(this).addClass("current");
        $(".video-display").hide();

        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn("slow");
        return false;
    });

    if ($('#year').size() > 0) {

        // Select options   
        LoadYear(tireSelectorUrl, vehicleSelect);

    };

    if ($('#contactUsForm').size() > 0) {

        // Validate form
        $('#contactUsForm').validate();

    };

    // Disable select boxes
    $('#makeId, #modelId, #vehicleId').attr('disabled', 'disabled');

    $("#year").change(function () {
        $('#makeId').removeAttr('disabled');
        $('#homeMake, #selectMake, #fitMake').removeClass('disabled');
    });

    $("#makeId").change(function () {
        $('#modelId').removeAttr('disabled');
        $('#homeModel, #selectModel, #fitModel').removeClass('disabled');
    });

    $("#modelId").change(function () {
        $('#vehicleId').removeAttr('disabled');
        $('#homeTrim, #selectTrim, #fitTrim').removeClass('disabled');
    });

    // Form submit without input button
    $('.find-dealers').click(function () {
        if ($('#dealerZIP').val().length == 5) {
            $('#target-zipcode').submit();
        }
    });

    // Ajax spinner
    $('#dealer-search-button').click(function () {
        if ($('#searchZipcode').val().length == 5) {
            $('#dealerList').html("<div class='loader'></div>");
            DealerSearch();
        }
        else {
            $('#zipfield').fadeIn().delay(2400).fadeOut();
        }
    });

    // Accordian
    $('.faq-list h4').click(function () {
        $(this).next().toggle();
        return false;
    }).next().hide();

});



