﻿jQuery(function () {
    jQuery('div[data-modweb-cartbar]').each(function () {
        var showLoadingMessage = jQuery.jqModweb.toBool(jQuery(this).data('showloadingmessage'));
        var showImage = jQuery.jqModweb.toBool(jQuery(this).data('showimage'));
        var imageUrl = jQuery(this).data('imageurl');
        var navigateUrl = jQuery(this).data('navigateurl');
        var textColor = jQuery(this).data('textcolor');
        var hasNavigateUrl = typeof navigateUrl !== 'undefined';
        if (hasNavigateUrl)
            navigateUrl = decodeURIComponent(navigateUrl);

        // default image url if none set.
        if (typeof (imageUrl) === 'undefined') {
            imageUrl = '/_framework/resources/Designer.CartBar/icon.gif';
        } else {
            imageUrl = decodeURIComponent(imageUrl);
        }

        // render static parts of cartbar.
        var html = '';
        if (showImage) {
            html += '<div style="float:left;width:24px;height:24px">';
            html += '<img src="' + imageUrl + '" />';
            html += '</div>';
        }
        jQuery(this).html(html);

        // render dynamic parts.
        var updateContainer = jQuery('<span x-updatecontainer="yes"></span>');
        jQuery(this).append(updateContainer);
        if (showLoadingMessage == true) {
            var html = '';
            html += '<div style="float:left;  margin-left:8px; margin-top:5px; font-size:11px; font-weight:bold; " x-loader="yes">';
            var loadingText = updateContainer.parent().attr('data-loadingtext');
            loadingText = typeof (loadingText) !== 'undefined' ? loadingText : "Loading...";
            html += loadingText;
            html += '</div>';
            html = jQuery(html);
            updateContainer.html(html);
            html.modwebFadeInOut('normal');
        }


        // make it a hyperlink.
        if (hasNavigateUrl) {
            jQuery(this).css({ 'cursor': 'pointer' }).click(function () {
                //window.location = '/' + viewCartHandler;
                window.location = navigateUrl;
            });
        }
        else {
            jQuery.jqModweb.debug.warning('No url handler of type ViewCart was found. CartBar will not be clickable.');
        }


        jQuery(this).bind('modweb-refresh', function (e, o) {
            var updateContainer = jQuery(this).children('[x-updatecontainer]');
            var mainContainer = jQuery(this);

            // stop loader.
            var loaderContainer = updateContainer.find('[x-loader]');
            loaderContainer.modwebFadeInOut('stop').hide();

            //var textColor = mainContainer.attr('data-textcolor');

            if (textColor !== '')
                mainContainer.css({ 'color': textColor });

            if (o.hasCart === true) {
                var html = '';
                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:' + textColor + ';">';
                html += o.itemCount;
                html += '</div>';

                html += '<div style="float:left;margin:5px 0 0 4px; font-size:11px;">';
                html += o.itemCount === '1' ? 'item' : 'items';
                html += '</div>';

                html += '<div style="float:left;margin:5px 0 0 4px; color:' + textColor + ';font-size:11px;">';
                html += o.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:' + textColor + ';">';
                //html += 'No items in cart'
                var emptyText = updateContainer.parent().attr('data-emptytext');
                //emptyText = !jQuery.jqModweb.isNullOrEmpty(emptyText) ? emptyText : "No items in cart";
                emptyText = typeof (emptyText) !== 'undefined' ? emptyText : "No items in cart";
                html += emptyText;
                html += '</div>';
                updateContainer.html(html);
            }
        });
        jQuery(this).bind('modweb-showloader', function (e, o) {
            var updateContainer = jQuery(this).children('[x-updatecontainer]');
            var html = '';
            html += '<div style="float:left;  margin-left:8px; margin-top:5px; font-size:11px; font-weight:bold; " x-loader="yes">';
            var loadingText = updateContainer.parent().attr('data-loadingtext');
            loadingText = typeof (loadingText) !== 'undefined' ? loadingText : "Loading...";
            html += loadingText;
            html += '</div>';
            html = jQuery(html);
            updateContainer.html(html);
            html.modwebFadeInOut('normal');
        });
        jQuery(this).bind('modweb-reload', function (e, o) {
            var defaults = {
                showLoader: true
            }
            o = jQuery.fn.extend({}, defaults, o);
            var cartBar = jQuery(this);
            var updateContainer = jQuery(this).children('[x-updatecontainer]');

            if (o.showLoader === true) {
                jQuery(this).triggerHandler('modweb-showloader', {});
            }

            jQuery.callDotNet('/_framework/resources/Designer.CartBar/CartBar.asmx/GetCurrentCartDetails',
            {},
            { async: true },
            function (data) {
                eval('data=' + data);
                cartBar.triggerHandler('modweb-refresh', data);
            },
            function () {
                jQuery.jqModweb.debug.warning('Failed to load CartBar data.');
            }
            );
        });

        jQuery(this).triggerHandler('modweb-reload', { showLoader: false });
    });
});

