﻿jQuery(document).ready(function ($) {

    var displayVSRNow = true;
    if (typeof (displayVSR) != "undefined" && displayVSR != null)
        displayVSRNow = displayVSR;
    /*else
    displayVSRNow = false;*/

    var foundScreen = $('#screen');

    //VSR
    if (displayVSRNow && foundScreen != null) {

        $('#slideshow').infiniteCarousel({
            displayProgressBar: false,
            showControls: false,
            displayThumbnails: false,
            mainImageHeight: 330,
            mainImageWidth: 980,
            offset: 22,
            imagePath: '/Style%20Library/sylvania/js/jQuery/infinitecarousel/images/',
            onSlideStart: function (pos, dir) {
                $('.active .largeimg').hide();
                $('.active .smallimg').show();

                $('.active').removeClass('active');
                var parentEls = $('.promo-gallery').children('li');

                $(parentEls[pos - 1]).addClass('active');
                $('.active .smallimg').hide();
                $('.active .largeimg').show();
                //alert('onSlideStart fired for position: ' + pos);
            }
        });

        $('#navigation a').click(function () {
            var $id = $(this).attr("id");
            var $index = 1;

            if ($id == "3")
                $index = 2;
            else if ($id == "5")
                $index = 3;
            else if ($id == "7")
                $index = 4;
            else if ($id == "9")
                $index = 5;

            $('#slideshow').trigger('goto', [$index]);

        });

        $('#slideshow a').click(function () {
            var ref = $(this).attr('href');
            if (ref.length > 0) {
                window.location.href = ref;
            }
        });

        if (displayVSRNow) {
            //hide large images at the beginning
            $('.largeimg').hide();
            $('.active .largeimg').show();
            $('.active .smallimg').hide();
        }
    }


    // Start the HTML 2 image rotators.
    var twoRotators = new Array();
    $(".html-2-rotator").each(function (index) {
        twoRotators[index] = $(this).children().attr("id");
    });

    if (twoRotators.length > 0) {
        for (var i = 0; i < twoRotators.length; i++) {
            $("#" + twoRotators[i]).jsCarousel({ autoscroll: false, circular: true, masked: false, itemstodisplay: 2, orientation: 'h' });
        }
    }
    // Start the HTML 5 image rotators.
    var fiveRotators = new Array();
    $(".html-5-rotator").each(function (index) {
        fiveRotators[index] = $(this).children().attr("id");
    });

    if (fiveRotators.length > 0) {
        for (var i = 0; i < fiveRotators.length; i++) {
            $("#" + fiveRotators[i]).jsCarousel({ autoscroll: false, circular: true, masked: false, itemstodisplay: 5, orientation: 'h' });
        }
    }
    // Start the HTML 5 image rotator CQWPs.
    var fiveRotatorCQWPs = new Array();
    $(".html-5-rotator-cqwp").each(function (index) {
        fiveRotatorCQWPs[index] = $(this).children().attr("id");
    });

    if (fiveRotatorCQWPs.length > 0) {
        for (var i = 0; i < fiveRotatorCQWPs.length; i++) {
            $("#" + fiveRotatorCQWPs[i]).jsCarousel({ autoscroll: false, circular: true, masked: false, itemstodisplay: 5, orientation: 'h', cqwpvariant: 't' });
        }
    }

    $(".subtitlefield input").change(function () {
        if (($(".shortdescfield textarea").length > 0)) {
            if (($(".shortdescfield textarea").val() == null) || ($(".shortdescfield textarea").val() == "")) {
                $(".shortdescfield textarea").val(this.value);
            }
        }
    });

    $("#searchbutton").click(function () {
        var searchtype = $(".search .AdvancedSearchDetail a.checked").attr("id");
        if (searchtype == "advSearch")
            window.location.href = '/' + $.url.segment(0) + '/search/Pages/results.aspx?k=' + $("input#searchterm").val() + "&s=All%20Sites";
        else if (searchtype == "productSearch")
            onClickTriggerSearch_SearchBusiness('searchterm');
        else if (searchtype == "documentSearch")
            onClickTriggerSearch_SearchDocuments('searchterm');
        else
            onClickTriggerSearch_SearchAutomotive('searchterm');
    });

    $('#searchterm').keyup(function (e) {
        if (e.keyCode == 13) {
            var searchtype = $(".search .AdvancedSearchDetail a.checked").attr("id");
            if (searchtype == "advSearch")
                window.location.href = '/' + $.url.segment(0) + '/search/Pages/results.aspx?k=' + $("input#searchterm").val() + "&s=All%20Sites";
            else if (searchtype == "productSearch")
                onClickTriggerSearch_SearchBusiness('searchterm');
            else if (searchtype == "documentSearch")
                onClickTriggerSearch_SearchDocuments('searchterm');
            else
                onClickTriggerSearch_SearchAutomotive('searchterm');
        }
    });

    $("#productCatalogSearchButton").click(function () {
        onClickTriggerSearch_SearchBusiness('productSearchValue');
    });

    $("#productSearchValue").keyup(function (e) {
        if (e.keyCode == 13) {
            onClickTriggerSearch_SearchBusiness('productSearchValue');
        }
    });

    $("#pressrelsearchbutton").click(function () {
        window.location.href = '/' + $.url.segment(0) + '/search/Pages/results.aspx?k=' + $("input#pressrelSearchValue").val() + "&s=Press%20Release";
    });

    $("ul.languageListing .more").click(function () {
        var value = 'en-us';
        if ($.url.segment(0) == 'en-us')
            value = 'fr-ca';

        pathArray = window.location.href.split('/');
        var newValue = pathArray[0] + '//' + pathArray[2] + '/' + value;

        window.location.href = newValue;
    });

    $('#supportOptions').change(supportChange);

    //fixes for scrolling issue
    if ($("#s4-ribbonrow").length > 0) {
        $("#whole_body").css("overflow-y", "hidden");
    }

    $(".selectBox").selectbox({
        onOpen: function () { jQuery(".selectBox").addClass('Opening'); }
    });

    $(document).bind('click', function () {
        var elm = jQuery(".selectBox");
        if (elm.hasClass("Opening")) {
            elm.removeClass('Opening');
            elm.addClass('Open');
        } else if (elm.hasClass('Open')) {
            elm.removeClass('Open');
            elm.selectbox('close');
        }
    });

    //Set email link
    if ($(".Email") != null) {
        var path = encodeURIComponent(window.location);
        var lnk = "mailto:?subject='" + path + "'";
        $(".Email").attr("href", lnk);
    }
})

