﻿jQuery(function() {
    jQuery('div[x-modweb-cartbar]').each(function() {
        var showLoadingMessage = jQuery.jqModweb.toBool(jQuery(this).attr('x-showLoadingMessage'));
        var viewCartHandler = jQuery(this).attr('x-viewcarthandler');
        var hasViewCartHandler = typeof viewCartHandler !== 'undefined';


        // render static parts of cartbar.
        var html = '';
        html += '<div style="float:left;width:24px;height:24px">';
        html += '<img src="/_framework/resources/Designer.CartBar/icon.gif" />';
        html += '</div>';
        jQuery(this).html(html);


        // render dynamic parts.
        var updateContainer = jQuery('<span x-updatecontainer="yes"></span>');
        jQuery(this).append(updateContainer);
        if (showLoadingMessage == true) {
            modweb_designer_cartbar_showLoader(updateContainer);
        }



        // make it a hyperlink.
        if (hasViewCartHandler) {
            jQuery(this).css({ 'cursor': 'pointer' }).click(function() {
                window.location = '/' + viewCartHandler;
            });
        }
        else {
            jQuery.jqModweb.debug.warning('No url handler of type ViewCart was found. CartBar will not be clickable.');
        }
    });

    modweb_designer_cartbar_updateAll({ 'showLoader': false });
});

var modweb_designer_cartbar_showLoader = function(updateContainer) {
    var html = '';
    html += '<div style="float:left;  margin-left:8px; margin-top:5px; font-size:11px; font-weight:bold; " x-loader="yes">';
    html += "Loading..."
    html += '</div>';

    html = jQuery(html);
    updateContainer.html(html);
    html.modwebFadeInOut('normal');
};
var modweb_designer_cartbar_render = function(updateContainer, hasCart, itemCount, totalPrice) {

    // stop loader.
    var loaderContainer = updateContainer.find('[x-loader]');
    //debugger;
    //if (loaderContainer.length > 0) {
    loaderContainer.modwebFadeInOut('stop').hide();
    //}


    if (hasCart === true) {
        var itemLabel = itemCount === '1' ? 'item' : 'items';

        var html = '';
        /*
        html += '<span class="icon">';
        html += '<img src="/_framework/resources/Designer.CartBar/icon.gif">';
        html += '</span>';
        */
        html += '<div style="float:left; margin:5px 0 0 8px; font-weight:bold; font-size:11px;">';
        html += 'Shopping Cart';
        html += '</div>';





        html += '<div style="float:left;margin:5px 0 0 4px; font-size:11px;color:#F5A117;">';
        html += itemCount;
        html += '</div>';

        html += '<div style="float:left;margin:5px 0 0 4px; font-size:11px;">';
        html += itemLabel;
        html += '</div>';

        html += '<div style="float:left;margin:5px 0 0 4px; color:#F5A117;font-size:11px;">';
        html += totalPrice;
        html += '</div>';



        updateContainer.html(html);
    }
    else {
        // no cart for this user.
        var html = '<div style="float:left;margin:5px 0 0 8px; font-size:11px;color:#F5A117;">';
        html += 'No items in cart'
        html += '</div>';
        updateContainer.html(html);
    }
};

var modweb_designer_cartbar_updateAll = function(o) {
    jQuery('div[x-modweb-cartbar]').each(function() {
        var updateContainer = jQuery(this).children('[x-updatecontainer]');

        if (o.showLoader === true) {
            modweb_designer_cartbar_showLoader(updateContainer);
        }


        jQuery.callDotNet('/_framework/resources/Designer.CartBar/CartBar.asmx/GetCurrentCartDetails',
        {},
        { async: true },
        function(data) {
            eval('data=' + data);
            modweb_designer_cartbar_render(updateContainer, data.hasCart, data.itemCount, data.totalPrice);
        },
        function() {
            jQuery.jqModweb.debug.warning('Failed to load CartBar data.');
        }
        );
    });
};
