﻿// for intellisense
/// <reference path="jquery-1.3.2.js" />

var yuunik = yuunik || {};

yuunik.analytics = {
    track: function(url) {
        // check if the disable_analytics meta tag is not specified
        if ($("meta[name=disable_analytics]").length == 0) {
            _gaq.push(['_trackPageview', url]);
        };
    }
};

yuunik.shopping_cart_overview_dlg = {
    dlg_inited: false,
    refresh_shopping_cart_summary: true,

    initShowShoppingCart: function() {
        // bind the click event on both shopping carts
        $(".shopping_cart").bind("click",
            function() {
                yuunik.shopping_cart_overview_dlg.show();
                yuunik.analytics.track("shopping_cart_show_on_click");
            }
        );
    },

    init: function() {
        if (!yuunik.shopping_cart_overview_dlg.dlg_inited) {

            // initialize the dialog
            var $shoppingCartContentDlg = $("#shoppingCartContentDlg");

            $shoppingCartContentDlg.dialog({ resizable: false, draggable: false, bgiframe: true, modal: true,
                width: 700, heigth: 350, autoOpen: false,
                // on close refresh the shopping cart summary
                close: function(event, ui) { yuunik.shopping_cart_overview_dlg.refreshShoppingCartSummary(); }
            });

            // setup the click for the delte item button
            $("#shoppingCartContent .deleteItem", $shoppingCartContentDlg).live("click", yuunik.shopping_cart_overview_dlg.onDeleteItemClick);

            // setup the change of the count combo box event
            // NOTE: this does not work in IE - bug will not be fixed http://dev.jquery.com/ticket/3876
            // so the event will be setup in the show function
            // $("#shoppingCartContent select", $shoppingCartContentDlg).live("change", yuunik.shopping_cart_overview_dlg.onChangeItemCountClick);

            yuunik.shopping_cart_overview_dlg.dlg_inited = true;
        }
    },

    checkOut: function() {
        // close the dialog
        yuunik.shopping_cart_overview_dlg.close(false);

        // go to checkout
        window.location = "/CheckOut/ShipTo";

        // disable checkout
        // window.location = "/CheckOut/Confirm";
    },

    close: function(refresh_shopping_cart_summary) {
        yuunik.shopping_cart_overview_dlg.refresh_shopping_cart_summary = refresh_shopping_cart_summary;

        // unbind the events
        $("#shoppingCartContent select.update").unbind("change");

        $("#shoppingCartContentDlg").dialog("close");
    },

    refreshShoppingCartSummary: function() {
        // refresh the summary for the shopping cart only when needed
        // for check out the refresh is not done to avoid that the window.location redirect
        // will be parallel with an ajax call - which on Chrome and Safari displays an empty dialog box
        if (yuunik.shopping_cart_overview_dlg.refresh_shopping_cart_summary) {
            // delay the ajax call to avoid empty dialog box on FF
            setTimeout('yuunik.ajax.call("/ShoppingCart/Summary", {}, ["#shoppingCartSummary", "#shoppingCartSummaryVert"]);', 50);
        }
    },

    bindItemCountChange: function() {
        $("#shoppingCartContent select.update").bind("change", yuunik.shopping_cart_overview_dlg.onItemCountChange);
    },

    show: function() {
        // load the content of the shopping cart
        yuunik.ajax.load("/ShoppingCart/Show", {v: 1}, "#shoppingCartContent", yuunik.shopping_cart_overview_dlg.onAfterRefresh);

        yuunik.shopping_cart_overview_dlg.init();

        // show the dialog
        $("#shoppingCartContentDlg").dialog("open");

        return false;
    },

    onAfterRefresh: function (responseText, textStatus, XMLHttpRequest) {
        var $shoppingCartDlg = $("#shoppingCartContentDlg");

        // setup the change of the count combo box event
        yuunik.shopping_cart_overview_dlg.bindItemCountChange();
        $shoppingCartDlg.dialog("option", "position", "center");

        // check if the shopping cart is empty
        var buttons = null;
        font_size = "11px"
        if ($("#empty_shopping_cart").length != 0) {
            buttons = {
                "Weiter Einkaufen": function() { yuunik.shopping_cart_overview_dlg.close(true); }
            }

            font_size = "12px"
        }
        else {
            buttons = {
                "Zur Kasse": function() { yuunik.shopping_cart_overview_dlg.checkOut(); },
                "Weiter Einkaufen": function() { yuunik.shopping_cart_overview_dlg.close(true); }
            }
        }

        // set the buttons
        $shoppingCartDlg.dialog("option", "buttons", buttons);

        // hack to change the "Weiter Einkaufen" button style
        $(".ui-dialog-buttonpane :button").each(
            function() {
                var $this = $(this);
                if ($this.text() == "Weiter Einkaufen") {
                    $this.css({
                        "background": "none",
                        "color": "#7d9e21",
                        "font-size": font_size,
                        "padding-top": "6px",
                        "margin-right": "50px"
                    });

                    // break the each loop
                    return false;
                }
            }
        );
    },

    onItemCountChange: function(event) {
        // finds out which element generated the event
        var target = (event.target) ? event.target : event.srcElement;

        var orderItemId = yuunik.shopping_cart_overview_dlg.getOrderItemId(target);

        var count = $(target).val();

        yuunik.shopping_cart.updateOrderItem(orderItemId, count,
        // setup the change of the count combo box event
            yuunik.shopping_cart_overview_dlg.bindItemCountChange);

        return false;
    },

    onDeleteItemClick: function(event) {
        // finds out which element generated the event
        var target = (event.target) ? event.target : event.srcElement;

        var orderItemId = yuunik.shopping_cart_overview_dlg.getOrderItemId(target);

        yuunik.shopping_cart.deleteOrderItem(orderItemId, yuunik.shopping_cart_overview_dlg.onAfterRefresh);

        return false;
    },

    getOrderItemId: function(elem) {
        // finds out the first tr parent element
        var trElem = $(elem).parents("tr:first");

        return trElem.attr("orderItemId");
    }
};

yuunik.shopping_cart = {
    addOrderItem: function(prodProdId, configurations, count, callback) {
        yuunik.ajax.call("/ShoppingCart/AddItem",
            { prodProdId: prodProdId, orderItemConfigurationData: configurations, itemCount: count },
            ["#shoppingCartSummary", "#shoppingCartSummaryVert"], callback);

        yuunik.analytics.track("/ShoppingCart/AddItem/" + prodProdId);
    },

    updateOrderItem: function(orderItemId, count, callback) {
        // call the server to have this item updated
        yuunik.ajax.call("/ShoppingCart/ChangeItemCount",
            { itemId: orderItemId, itemCount: count },
            ["#shoppingCartContent"], callback);
    },

    deleteOrderItem: function(orderItemId, callback) {
        yuunik.ajax.call("/ShoppingCart/RemoveItem",
            { itemId: orderItemId },
            ["#shoppingCartContent"], callback);

        yuunik.analytics.track("/ShoppingCart/RemoveItem/");
    },

    show_order_item_added_info: function() {
        // show info
        $("#info_in_shopping_cart").show();

        // hides the info in 5 seconds
        setTimeout("$('#info_in_shopping_cart').hide();", 3000);
    }
};

