﻿var highlightInterval = null;

//doc ready
$(document).ready(function () {
    //image sliders
    $(".slider ul").anythingSlider({
        resizeContents: true,
        buildArrows: true,
        buildNavigation: true,
        autoPlay: true,
        pauseOnHover: true,
        delay: 6000,
        stopAtEnd: false
    });
    //highlight menus
    $('.RadTabStrip_MainMenu .rtsLink .rtsTxt').each(function () {
        if ($('.cNavigationTree .root') != null && $('.cNavigationTree .root').html() != null) {
            if ($('.cNavigationTree .root').html().toLowerCase() == $(this).html().toLowerCase()) {
                $(this).parents('.rtsLink').addClass('rtsSelected');
            }
        }
    });
    //attach lightboxes
    $('.slider .panel:not(.cloned) .lightbox').lightBox();
    //front page highlight
    if ($('.Highlights .Highlight')) {
        $('.Highlights .Highlight:first').show();
        $('.Highlights .Tabs a').attr('href', '');
        $('.Highlights .Tabs a').click(function () {
            var elements = $('.Highlights .Highlight');
            var showIndex = parseInt($(this).attr('rel')) - 1;
            for (var i = 0; i < elements.length; i++) {
                if (i != showIndex && $(elements[i]).css('display') != 'none') {
                    $(elements[i]).fadeOut(600);
                } else if (i == showIndex) {
                    $(elements[showIndex]).fadeIn(600);
                }
            }
            $('.Highlights .Tabs a').removeClass('selected');
            $(this).addClass('selected');
            //reset cycler
            clearInterval(highlightInterval);
            highlightInterval = setInterval('CycleHighlight();', 6500);
            return false;
        });
        highlightInterval = setInterval('CycleHighlight();', 6500);
    }
    //product detail tabs
    if ($('.Main .PageBase .ProductTabs')) {
        $('.Main .PageBase .ProductTabs .TabContent:first').show();
        $('.Main .PageBase .ProductTabs .Tabs a').attr('href', '');
        $('.Main .PageBase .ProductTabs .Tabs a').click(function () {
            var elements = $('.Main .PageBase .ProductTabs .TabContent');
            var showIndex = parseInt($(this).attr('rel'));
            for (var i = 0; i < elements.length; i++) {
                if (i != showIndex && $(elements[i]).css('display') != 'none') {
                    $(elements[i]).hide();
                } else if (i == showIndex) {
                    $(elements[showIndex]).show();
                }
            }
            $('.Main .PageBase .ProductTabs .Tabs a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        });
    }
    //hide detail bar
    if ($('.Main .PageBase .ProductDetail .DetailHeader div div')) {
        if ($('.Main .PageBase .ProductDetail .DetailHeader div div').val() == "") {
            $('.Main .PageBase .ProductDetail').hide();
        }
    }
    //attach address events
    $('.Main .PageBase .address .sameAsBilling input').click(function () {
        AddressShipSameAsBilling($(this).is(':checked'));
    });
    $('.Main .PageBase .address .country select').change(function () {
        AddressCountrySwitch($(this));
    });
    //apply post form styles
    $('.Main .PageBase .address .country select').each(function (index) {
        AddressCountrySwitch($(this));
    });
    $('.Main .PageBase .sfFormsEditor .sfError').each(function (index) {
        $(this).parents('.sfFormsEditor .sfFormBox, .sfFormsEditor .sfFormDropdown, .sfFormsEditor .sfFormBlock, .sfFormsEditor .sfFormCheckboxlist').addClass('invalid');
    });
    //attach hints
    $('.sfsearchBox .sfsearchTxt').attr('title', 'Search').addClass('hinted').val('Search');
    $('.SideContent .ContactUsDemoMachine .contactname input').attr('title', 'Your Name').addClass('hinted').val('Your Name');
    $('.SideContent .ContactUsDemoMachine .contactemail input').attr('title', 'Email Address').addClass('hinted').val('Email Address');
    setTimeout("$('.Footer .NewsletterSubscribe .sfFormBox input').attr('title', 'Enter Your Email Address').addClass('hinted').val('Enter Your Email Address');", 200);
    setTimeout("$('input[title!=\"\"]').hint();", 250);
    if ($.browser.msie) {
        if (parseInt($.browser.version, 10) <= 6) {
            DD_belatedPNG.fix('img, .button_go, .button_go2, .Highlights .Highlight .sfContentBlock, .anythingSlider .arrow a');
        }
    }
    //add language class for css customization
    $('body').addClass(__cultureInfo.name);
    //fix language dropdown
    String.prototype.capitalize = function () { return this.charAt(0).toUpperCase() + this.slice(1); }
    var capTxt = $('.languageselect option')[1];
    capTxt.val(capTxt.val().capitalize());
});

function CycleHighlight() {
    var currentIndex = parseInt($('.Highlights .Tabs a.selected').attr('rel'));
    var found = false;
    var elements = $('.Highlights .Highlight');
    var tabs = $('.Highlights .Tabs a');
    $('.Highlights .Tabs a').removeClass('selected');
    for (var i = 0; i < elements.length; i++) {
        if (i != currentIndex && $(elements[i]).css('display') != 'none') {
            $(elements[i]).fadeOut(600);
        } else if (i == currentIndex) {
            $(elements[currentIndex]).fadeIn(600);
            $(tabs[currentIndex]).addClass('selected');
            found = true;
        }
    }
    if (found == false) {
        $('.Highlights .Tabs a:first').addClass('selected');
        $('.Highlights .Highlight:first').fadeIn(600);
    }
}

function AddressCountrySwitch(countryElement) {
    var value = countryElement.val().toLowerCase();
    var parentAddressElement = $(countryElement).parents('.address');
    if (value != "united states" && value != "usa" && value != "united states of america" && value != "america") {
        parentAddressElement.find('.region').show();
        parentAddressElement.find('.state').hide();
    } else {
        parentAddressElement.find('.region').hide();
        parentAddressElement.find('.state').show();
    }
}

function AddressShipSameAsBilling(issame) {
    if (issame) {
        $('.Main .PageBase .shipto .addressline1 input').val($('.Main .PageBase .billto .addressline1 input').val());
        $('.Main .PageBase .shipto .addressline2 input').val($('.Main .PageBase .billto .addressline2 input').val());
        $('.Main .PageBase .shipto .city input').val($('.Main .PageBase .billto .city input').val());
        $('.Main .PageBase .shipto .state select').val($('.Main .PageBase .billto .state select').val());
        $('.Main .PageBase .shipto .region input').val($('.Main .PageBase .billto .region input').val());
        $('.Main .PageBase .shipto .zipcode input').val($('.Main .PageBase .billto .zipcode input').val());
        $('.Main .PageBase .shipto .country select').val($('.Main .PageBase .billto .country select').val());
        AddressCountrySwitch($('.Main .PageBase .shipto .country select'));
    }
}

function OpenVideo(videoUrl, posterUrl) {
    if (posterUrl != undefined && posterUrl != null) {
        window.open("/notifications/video-player?v=" + videoUrl + "&p=" + posterUrl, "VideoWindow", "menubar=no,width=650,height=484,toolbar=no,scrollbars=no");
    } else {
        window.open("/notifications/video-player?v=" + videoUrl, "VideoWindow", "menubar=no,width=650,height=484,toolbar=no,scrollbars=no");
    }
}
