﻿$(document).ready(function() {
    $('#resultsTarget').overlay();
    $('div.close').hover(
            function() {
                $(this).css({ backgroundPosition: "0 1" });
            }, function() {
                $(this).css({ backgroundPosition: "-29 1" });
            }
    );
    $('.priceslink').click(function() {
        getData($(this).attr('rel'));
        return false;
    });
    $('.detailslink').click(function() {
        getProductData($(this).attr('rel'));
        return false;
    });
    $('.cardOuter').hover(
     function() { $(this).addClass('active').find('.cardSmall').addClass('cardBig').removeClass('cardSmall'); },
     function() { $(this).removeClass('active').find('.cardBig').removeClass('cardBig').addClass('cardSmall'); }
     );
});
function bindNewPricingLink() {
    $('.newPricesLink').click(function() {
        getData($(this).attr('rel'));
        return false;
    });
    $('.newDetailsLink').click(function() {
        getProductData($(this).attr('rel'));
        return false;
    });
    //$('.button').hover(function() { $(this).addClass('btnhover') }, function() { $(this).removeClass('btnhover') });
    initializePictures();
}
function getProductData(productName) {
    $('#loading').show();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Product_Dashboard.asmx/getProductDashboard",
        dataType: 'json',
        data: "{'language':'" + language + "','fileName':'~/content/xml/feeds/" + filename + ".xml','productName':'" + productName + "'}",
        success:
        function(msg) {
            showResults(msg);
            bindNewPricingLink();
        }, error:
        function(XMLHttpRequest, textStatus, errorThrown) {
            $('#loading').hide();
            alert("Oh dear. We seem to have an error. It may be that the product data is unavailable. The server says: " + errorThrown);
            return true;
        }
    })
};
function getData(productName) {
    $('#loading').show();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Pricing.asmx/getPriceData",
        dataType: 'json',
        data: "{'region':'" + region + "','componentType':'" + category + "','componentId':'" + productName + "'}",
        success:
        function(msg) {
            showResults(msg);
            formatPricing();
        }, error:
        function(XMLHttpRequest, textStatus, errorThrown) {
            $('#loading').hide();
            alert("Oh dear. We seem to have an error. It may be that the pricing data is unavailable. The server says: " + errorThrown);
            //return true;
        }
    })
};
function showResults(msg) {
    $('#wrap').html(msg.d);
    var api = $('#resultsTarget').overlay();
    api.load();
    $('#loading').hide();
    return false;
};
function formatPricing() {
    $('table', '.retailers').hide();
    $('table.pprops').hide();
    $('tr.shown table.pprops').show();
    $('tr.shown .retailers').show();
    $('.overlay a').click(function() {
    _hbLink($(this).attr("name"));
    });
    $('.purl', '.retailers').unbind().click(function() {
        $(this).parents('td').find('table').slideToggle('slow').parents('td').prev('td').find('table').slideToggle('slow');
        return false;
    });
}