﻿$(function() {

    //Determine which blue background banner to show for the 3 different heights
    if ($('#smallBG').length > 0) {
        $('body').css({ 'background': '#343434 url(/images/global/bg_blue_small.gif) repeat-x 0 110px' });
        if ($('#shoppingCart').length > 0) {
            $('#cartImage').show();
        }
        else if ($('#checkout').length > 0) {
            $('#checkoutImage').show();
        }
        else if ($('#checkoutConfirm').length > 0) {
            $('#confirmImage').show();
        }
        else if ($('#divEnewsWrapper').length > 0) {
            $('#registrationImage').hide();
            $('#enewsImage').show();
        }
    }
    else if ($('#medBG').length > 0) {
        $('body').css({ 'background': '#343434 url(/images/global/bg_blue_med.gif) repeat-x 0 110px' });
    }
    else if ($('#largeBG').length > 0) {
        $('body').css({ 'background': '#343434 url(/images/global/bg_blue_large.gif) repeat-x 0 110px' });
    }

    //Hide Buy Now confirmation div
    $('#popConfirm').hide();

    //Show enews popup from fake textbox in top right on main.master and revolution pages
    /*$('#revSignup, #news').click(function() {
    $find('enewsPopupBehavior').show();
    });*/

    //By default, hide GuestCheckout option in Login popup (in MainSiteHeader.asxc)
    $('#guestCheckout').hide();

    //If click "Login", hide GuestCheckout.
    $('a[id*=lbLogin]').click(function() {
        $('#guestCheckout').hide();
    });

    //If cllick "Proceed To Checkout", show GuestCheckout.
    $('a[id*=ProceedToCheckoutLinkButton]').click(function() {
        $('#guestCheckout').show();
    });

    //Change opacity of images on hover
    $('#imageScrollWrapper img').hover(function() {
        $(this).stop().animate({ opacity: 1 }, 200)
    }, function() {
        $(this).stop().animate({ opacity: 0.7 }, 800)
    });

    //Used to change opacity of all images globally
    $('#teamRightColInnerRight .teamGalleryThumb img, img.bannerHover, #richRightCol .teamGalleryThumb img').hover(function() {
        $(this).stop().animate({ opacity: 1 }, 200)
    }, function() {
        $(this).stop().animate({ opacity: 0.7 }, 800)
    });

    //Show play div on hover for TV image links
    $('.playNowHover').hover(function() {
        $('.playNowDivSmall, .playNowDiv, .playNowDivSmMed, .playNowDivMed', this).stop().fadeTo(300, 0.6);
    }, function() {
        $('.playNowDivSmall, .playNowDiv, .playNowDivSmMed, .playNowDivMed', this).stop().fadeTo(300, 0);
    });

    // Homepage - News Scroller - Image Hover
//    $('#homeRightCol h5 a').hover(function() {
//        $(this).parent().parent().find('img').stop().fadeTo(500, 1);
//    }, function() {
//        $(this).parent().parent().find('img').stop().fadeTo(500, 0).css({ 'display': 'none' });
//    });

});


$(window).bind("load", function() {
    //Products image scroller on products landing page
    var div = $('#productsImageScroll');
    var ul = $('#imageScrollWrapper');
    var ulPadding = 15;
    var li = $('#imageScrollWrapper li');
    var img = $('#imageScrollWrapper img');
    var divWidth = div.width();
    var lastLi = ul.find('li:last-child');
    var wrapperWidth = 0;

    //Set the width for the image scroller container
    li.each(function() {
        wrapperWidth += $(this).outerWidth(true);
        //console.log('wrapperWidth' + wrapperWidth);
    });
    ul.css({ width: wrapperWidth });

    div.css({ overflow: 'hidden' });



    //Center products if they don't fill width 100%
    if (wrapperWidth <= 940) {
        var centerWidth = (940 - wrapperWidth) / 2;
        ul.css({ left: centerWidth });
    }
    //else {
    //div.scrollLeft(150); //((-940 - wrapperWidth) / 2);
    //}

    //Animate the scroller when moving left or right
    div.mousemove(function(e) {
        //console.log('e.page' + e.pageX);
//        var divOffsetLeft = div.offset().left;
//        var ulOffsetLeft = $(lastLi).offset().left - (divOffsetLeft) + lastLi.outerWidth(); //.offsetLeft
//        console.log('lastLiOffsetLeft:' + $(lastLi[0]).offset().left);

//        var right = (ulOffsetLeft - (divWidth + divOffsetLeft));
//        var ulLeftOutsideArea = wrapperWidth - divWidth - right;
//        var widthToScrollRight = (e.pageX - divOffsetLeft) + (right);
//        var widthToScrollLeft = (e.pageX - divOffsetLeft) - (ulLeftOutsideArea / divWidth);
//        console.log('ulOffsetLeft' + ulOffsetLeft);
//        //console.log('divOffsetLeft'+divOffsetLeft);
//        console.log('right' + right);
//        //console.log('right/DivWidth' + (right / divWidth));
//        //console.log('right' + (right/divWidth));
//        //console.log('right' + ulOffsetLeft);
//        //console.log('widthToScrollLeft' + widthToScrollLeft);
//        console.log('widthToScrollRight ' + widthToScrollRight);

//        if (e.pageX > ($(document).width() / 2)) {
//            div.scrollLeft(widthToScrollRight);
//        }
//        else {
//            div.scrollLeft(widthToScrollLeft);
//        }

                var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
                var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
                //console.log('left:' + left);
                div.scrollLeft(left);
    });

});


//Animate Buy Now popup confirmation
function buyNowAddToCart() {
    var quantityTextBoxes = $('input[id*=buyPop_QuantityTextBox]');
    var quantityTextBox = quantityTextBoxes[0];
    var EnteredQty = quantityTextBox.value;

    var outOfStockHiddenFields = $('input[id*=OutOfStockHiddenField]');
    var outOfStockHiddenField = outOfStockHiddenFields[0];
    var outOfStock = outOfStockHiddenField.value;

    var ddlSizes = $('select[id*=buyPop_ddlSize]');
    var ddlSize = ddlSizes[0];
    var EnteredSize = ddlSize == null ? null : ddlSize.value;

    var ddlFlavors = $('select[id*=buyPop_ddlFlavor]');
    var ddlFlavor = ddlFlavors[0];
    var EnteredFlavor = ddlFlavor == null ? null : ddlFlavor.value;

    if (outOfStock=='False' && EnteredQty != '0' && IsNumeric(EnteredQty) && EnteredFlavor != '' && EnteredSize != '') {
        var quantityRequired = $('input[id*=buyPop_QuantityTextBox]')[0];

// I, Bill, tried to get a smoother UI experience, but no success.
//        var position = $('#formWpr').position();
//        
        $('#popConfirm').slideDown('400');
//        $('#formWpr').css({position: 'absolute', top:position.top, left: position.left})
//        .animate(
//        {
//            top: position.top - ($('#formWpr').height() / 2),
//            left: position.left - ($('#formWpr').width() / 2)
//        }, 'normal');


        if (EnteredQty == '1')
            $('#quantitySpan').text(EnteredQty + ' item');
        else
            $('#quantitySpan').text(EnteredQty + ' items');
    }
        
    return false;
}

function IsNumeric(input) {
    return (input - 0) == input && input.length > 0;
}

//Show buy now popup
function showPopup() {
    $('#popConfirm').hide();
    $find('BuyNowPopupBehavior').show();
}

//Close buy now popup
function closePopup() {
    var modalPopupBehavior = $find('BuyNowPopupBehavior');
    modalPopupBehavior.hide();
}

//Show TV popup
function showTvPopup() {
    $find('tvPopupBehavior').show();
}

//Show yellow feedback message
function showFeedback() {
    $('.divFeedback').slideDown('400').delay('3500').slideUp('slow');
}
// show feedback message, and leave it there.
function showFeedbackStay() {
    $('.divFeedback').slideDown('400');
}

//Expand accordion panes on Checkout page
function changeSelected(i) {
    $find('ctl00_ctl00_Content_subContent_Accordion1_AccordionExtender').set_SelectedIndex(i);
}

function hideTvRating() {
    $('.tvRateFeedback').slideDown('400').delay('3500').slideUp('slow');
}

// Check for iOS
function isIOS() {
    return (navigator && navigator.platform && navigator.platform.match(/^(iPad|iPod|iPhone)$/));
}

