
var featuredTimer;
//ONLOAD CALLS
$(document).ready(function () {

    //Homepage stuff: "featured"
    var featuredInstruments = $('#featuredInstruments ul li');
    if (featuredInstruments.length > 0) {

        featuredInstrumentShow(featuredInstruments.first());
        //fix the first item, once
        if (featuredInstruments.first().length > 0) {
            //featuredInstrumentCenter(featuredInstruments.first(), featuredInstruments.first().find('img'));
            featuredInstruments.first().find('img').css('margin-left', '-25%');
        }


        featuredInstruments.mouseenter(function () {
            clearTimeout(featuredTimer);

            if (!($(this).hasClass('on'))) {
                featuredInstrumentShow(this);
            }
        });
        featuredInstruments.mouseleave(function () {
            featuredTimer = setTimeout(featuredInstrumentTick, 4000);
        });

        featuredTimer = setTimeout(featuredInstrumentTick, 8000);
    }

    //Homepage stuff: Wire up PDF links for analytics
    $('#catalog1').click(function () {
        if (typeof _gaq != 'undefined') {
            _gaq.push(['_trackPageview', '/catalog/part1']);
        }
    });
    $('#catalog2').click(function () {
        if (typeof _gaq != 'undefined') {
            _gaq.push(['_trackPageview', '/catalog/part2']);
        }
    });

});

function featuredInstrumentTick() {
    var currentFeaturedInstrument = $('#featuredInstruments ul li.on');
    var nextInstrument = currentFeaturedInstrument.next();
    if (nextInstrument.length == 0) {
        nextInstrument = $('#featuredInstruments ul li:first');
    }

    featuredInstrumentShow(nextInstrument);

    featuredTimer = setTimeout(featuredInstrumentTick, 8000);
}

function featuredInstrumentShow(liObject) {
    var currentFeaturedInstrument = $('#featuredInstruments ul li.on');

    //if this is the first time, figure out a negative margin to center it within the viewport:
    var currentFeaturedImage = currentFeaturedInstrument.find('img');
    var nextFeaturedImage = $(liObject).find('img');

    currentFeaturedImage.animate({ opacity: 0 }, 2000);
    nextFeaturedImage.animate({ opacity: 1 }, 1000);

    currentFeaturedInstrument.removeClass('on');
    $(liObject).addClass('on');

    featuredInstrumentCenter(liObject, nextFeaturedImage);
}

function featuredInstrumentCenter(viewport, image) {
    var viewportWidth = $(viewport).width();
    var nextFeaturedImageWidth = $(image).width();
    var leftMargin = (viewportWidth - nextFeaturedImageWidth) / 2;

    $(image).css('margin-left', leftMargin + 'px');    
}


function NoToPOBoxes(source, arguments) {
    var AddressString = arguments.Value.toUpperCase().replace('.', '').replace(/\s/g,'');
    
    if (
        (AddressString.indexOf('POBOX') > -1) ||
        (AddressString.indexOf('POSTOFFICEBOX') > -1)
    ){
        arguments.IsValid = false;
    } else {
        arguments.IsValid = true;
    }
}

//goToRelatedDropdownValue: sends the user to the selected value in a sibling dropdown element from the sender
function goToRelatedDropdownValue(sender) {
    var destination = $(sender).siblings('select').val();
    if (destination.length > 0) {
        window.location.href = destination;
    }
    return false;
    //window.location.href
}

function copyValueIfTargetBlank(source, target) {
    if ($.trim($(target).val()).length == 0) {
        var sourceValTrimmed = $.trim($(source).val());
        if (sourceValTrimmed.length > 0) {
            $(target).val($(source).val());
        }
    }
}
