﻿/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />
var HomePage = {
    Init: function () {
        HeroPlayerMediaCarousel();

        BranchFinderBranchFinderSearch();

        // Popular Product carousel
        $('#carouselHold').jcarousel();

        $("#emailSignUp").val("");
        $("#signUpRequest").click(function () {
            SubscribeNewsletter($(this));
        });
    }
}

function SubscribeNewsletter(that) {
    var email = $("#emailSignUp");
    var emailValue = $.trim(email.val());
    if (emailValue !== "") {
        that.attr('disabled', true);
        email.val("processing...");
        $.post("/Home/SubscribeNewsletter", { email: emailValue }, function (result) {
            that.removeAttr('disabled');
            if (result.success) {
                email.val("Your request has been sent");
            }
            else {
                if (result.invalidEmail) {
                    email.val("Invalid email")
                } else {
                    email.val("Temporarily unavailable")
                }
            }
        });
    }
}

function HeroPlayerMediaCarousel() {
    $("ul.homeTabs").tabs("div.homeCarousel > div", {
        effect: 'fade',
        fadeOutSpeed: "slow",
        fadeInSpeed: "slow",
        rotate: true,
        current: 'selected',
        tabs: 'a'
    }).slideshow({ autoplay: true, interval: 5000 });
}


function BranchFinderBranchFinderSearch() {
    function Search(value) {
        if (value.replace(/\s/g, "") != "") {
            var href = window.location.href;
            return href + "depot-finder/" + value;
        }

        return;
    }

    $("#cityPostcode").keyup(function (e) {
        e.preventDefault();
        if (e.keyCode == 13) {
            $('.goSearch').trigger("click");
        }
    });


    $('.goSearch').click(function (e) {
        e.preventDefault();
        var value = $("#cityPostcode").val();
        if (value == "Type your City or Postcode") {
            return;
        }

        var newLocation = Search(value);
        if (newLocation) {
            window.location = newLocation;
            return;
        }
    });
}

