
(function($) {
    $.fn.basket = function(options) {
        return this.each(function() {
            var $this = $(this);

            var opts = $.extend({}, $.fn.basket.defaults, options);

            if (($.cookie(opts.cookieId) == null || $.cookie(opts.cookieId) == undefined) && opts.sessionServiceUrl != "") {
                $.getJSON(opts.sessionServiceUrl, $.noop);
            }

            if (($.cookie(opts.cookieId) != null || $.cookie(opts.cookieId) != undefined) && opts.basketServiceUrl != "") {
				
				$.getJSON(opts.basketServiceUrl, function(response) {

      				$item = $($.fn.basket.formatBasket(opts.title, opts.css, response.AmountProducts, response.TotalPrice, opts.homeCaption, opts.homeUrl, opts.orderUrl, opts.itemsName, opts.showAmount));

            		$item.data('basket.priority', opts.priority);

					// weird stuff, the template already contains a basket... when the basket with priority 1 returns
					// replace the template one with the new generated basket...
					if (opts.priority == 1) {
					   $("#h-basket").replaceWith($item);
					   return;
					}

                    $last = $this.children().last();

                    var cnt = true;

                    lastPriority = $last.data('basket.priority');
                    currentPriority = $item.data('basket.priority');

                    if (currentPriority > lastPriority) {
                        $last.after($item);
                    } else {
                        while ($last.prev().length != 0 && cnt) {
                            lastPriority = $last.data('basket.priority');

                            if (currentPriority == lastPriority - 1) {
                                cnt = false;
                            } else {
                                $last = $last.prev();
                            }
                        }
                    }

                    if (currentPriority > lastPriority) {
                        $last.after($item);
                    } else {
                        $last.before($item);
                    }

                });

            }
        })
    };

    function isCurrentShop(shop) {
        return shop == $('meta[name=hema.shop]').attr("content");

    }

    function log($text) {
        if (window.console && window.console.log) {
            window.console.log($text);
        }
    };

    $.fn.basket.defaults = {
        priority: 0,
        title: "",
        cookieId: "HemaSession",
        basketServiceUrl: "",
        sessionServiceUrl: "http://services.hema.nl/SessionProvider/HemaSession?callback=?",
        homeCaption: "",
        homeUrl: "",
        orderUrl: "",
        css: "",
        itemsName: "artikel(en)",
        showAmount: true
    };

    $.fn.basket.formatBasket = function(title, css, amount, total, homeCaption, homeUrl, orderUrl, itemsName, showAmount) {
		var TemplatingOrderButton = $(".TemplatingOrderButton");
		var OrderButtonStyle="background-image: url(&quot;http://images.hema.local/ui/2/nl-NL/lente/button/forward__bestellen.png&quot;); width: 121px;";
		if (TemplatingOrderButton.length!=0)
		{		
			 OrderButtonStyle = $(TemplatingOrderButton).attr("style");
		}
		
        html = "<div class='" + css + "'>";
        html += "<div class='h-basketheader'>";
        html += "<h3>" + title + "</h3>";
        html += "<p>";
        html += "<strong>" + total.toFixed(2) + "</strong>";
        html += "<span>" + $.fn.basket.itemsDisplay(showAmount, amount, itemsName) + "</span>";
        html += "</p>";
        html += "<a href='" + homeUrl + "'>" + homeCaption + "</a>";
        html += "</div>";
        html += "<div class='h-basketcontent'>";
        html += "<b class='h-basketlist-bottom'></b>";
        html += "<form action='" + orderUrl + "'>";
        html += "<input value='' class='h-button' style='" + OrderButtonStyle +"' type='submit'>";
        html += "</form>";
        html += "</div>";
        html += "</div>";
        
        return html;
    };

    $.fn.basket.itemsDisplay = function(showAmount, amount, itemsName) {
        if (showAmount) {
            return amount + " " + itemsName;
        }
        else {
            return itemsName;
        }

        return html;
    };

})(jQuery);
