// Product Detail Page Scripts

$(document).ready(function() {
    
    // Format the Price. If blank, hide it.
    var priceText = $('div.price span').text();
    if (priceText != '') {
        var price = parseFloat(priceText);
        $('div.price span').text('$' + price.toFixed(2));
    } else {
    //    $('.price').hide();
    }
    
    // Hides the List Price if it is zero:
    $('.productListPrice').each(function() {
        if ($(this).find('span').html() == '$00.00') {
            $(this).hide();
        }
    });

    
    // adds a dollar sign ($) in front of the price in product options:
    $('.prodOptionPrice').each(function() {
        if ($(this).text() != '') {
            $(this).text('$' + $(this).text())
        }
    });
    
    // Removes zero dollar price listing from product options dropdowns:
    var str = '';
    $("div.textChoice select option:contains(' - $0')").each(function() {
        str = $(this).html();
        str = str.replace(' - $0','')
        $(this).html(str)
    });
    
    // Tab switcher.
    $('#productTabs dl#nav dt').each(function() {
        $(this).click(function() {
            if ($(this).attr('class') != 'current') {
                $('#productTabs dl#nav dt').removeClass('current');
                $('#productTabs dl#nav dd').hide();
                $(this).addClass('current');
                var currentContent = $(this).find('a').attr('href');
                $(currentContent).fadeIn();
                //$('#productTabContents')
                //    .html($(currentContent).html())
                //    .fadeIn();
            }
            return false;
        });
        
    });
    
    // Onload: make first tab current and display the first tab's content.
    $('#productTabs dl#nav dt:first').addClass('current');
    $('#productTabs dd.tabContent:first').fadeIn();
    //$('#productTabContents').html($('#productTabs dd.tabContent:first').html()).fadeIn();
    
    // Write a Review Popup.
    var getStoreDomain = $('input#varStoreDomain[type=hidden]').val(); // find the value of hidden input 'varStoreDomain'
    var getProductId = $('input#storeProductId[type=hidden]').val(); // find the value of hidden input 'storeProductId'
    
    $('a.writeReviewLink').click(function() {
        window.open($(this).attr('href'),'Images','width=600, height=600, menubar=no, scrollbars=yes, resizable=yes, status=no, toolbar=no');
        return false;
    });
    
    // Email This Page Popup:
    $('a.emailPop').click(function() {
        window.open('' + getStoreDomain + 'EmailFriend.aspx?productID=' + getProductId + '#Write','Images','width=400, height=210, menubar=no, scrollbars=yes, resizable=yes, status=no, toolbar=no');
        return false;
    });
    
    // Add product title to the end of the breadcrumbs:
    var productTitle = $('h1').text();
    $('#breadCrumbs a:last').after('&nbsp;&raquo;&nbsp;' + productTitle);
    
    // Handle the image switching here:
    $('#productThumbnails img').each(function() {
        $(this).click(function() {
            var thisPath = $(this).attr('src');
            $('#largeImage img').attr('src',thisPath);
        });
    });
    
    $('#largeImage img').click(function() {
        var thisImgUrl = $(this).attr('src');
        window.open(thisImgUrl, 'Images', 'width=620,height=620,menubar=no,scrollbars=no,resizable=yes,status=no,toolbar=no');
        return false;
    });
    
    // Sets the large image's src to the firstr thumbnail:
    $('#largeImage img').attr('src', $('#productThumbnails img:first').attr('src'));
    
    // Removes the "Home" menu item's current class:
    $('#Menu li.item').find("a:contains('Home')").removeClass('current');
    
    // Hide the first Breadcrumb element:
    $('div.breadCrumbs:first').hide();
    
    // Check to see if default dropdowns are selected. If so, throw an error:
    var alertText = '';
    $('input#btnAddToCart').click(function() {
        $(".textChoice select option:selected:contains('Select')").each(function() {
            labelText = $(this).parent().parent().parent().parent().find('label').text();
            alertText += '\n - Please select your ' + labelText;
        });
        
        if (alertText != '') {
            window.alert('Could not add product to the cart. ' + alertText)
            alertText = '';
            return false;
        }
    });
    if ( priceJson ) {
        updateProductOptionPrice();
        $('div.price').hide();
    }    
});



if ( priceJson ) {
  $("div.textChoice select").change(function() {
    updateProductOptionPrice();
    $('div.price').show();
  });
}

var guids = [];
function updateProductOptionPrice() {
  if ( !guids.length ) {
        // Get the order of guids that the API built; doesn't necessarily match the order
        // of product option choices being rendered in HTML
        var keyStr;
        $.each(priceJson, function(i, val) {
            keyStr = i;
            return false;
        });
        var guiAry = keyStr.split(',');
        var id;
        $.each(guiAry, function(i, val) {
            id = $("option[value='" + val + "']").parent().attr('name');
            guids.push( id.replace('optionCount', '') );
        });
    }
    // based on the order of guids, build our hash
    var hash = '';
    $.each(guids, function(i, val) {
        if ( hash ) {
          hash += ',';
        }
        hash += $("select[name='optionCount" + val + "']").val();
    });
    
    if ( priceJson[hash] ) {
        //alert(hash + ': ' + priceJson[hash]);
        var priceVar = priceJson[hash][0];
        var price = parseFloat(priceVar);
        $("div.price span").text('$' + price.toFixed(2));
    }
}