﻿/// <reference path="../Plugins/jquery-1.3.2-vsdoc.js" />
//register a name space
Type.registerNamespace("Controls");

/// <summary>
/// V1.0 the MiniCart, note client cache should be disabled so not web browser back/forward will be wrong
/// The plugin should be used on the MiniCartContainer div.
/// The plugin can handle fixed positioning
/// Depends:
/// jquery-1.4.js
///	ui.core.js
/// ExtendJquery.js
/// MicrosoftAjax.js
/// AjaxLink.js
/// </summary>
Controls.MiniCart = function() {
}
//REVIEW add refresh metod
Controls.MiniCart.prototype =
{
    _init: function() {
        //fixed positioning in old browsers!
        if ($(this.element).css("position") == "fixed") {
            if ($.browser.msie && $.browser.version <= 6) {
                $(this.element).css({ position: "absolute" });
                this._placeCart();
                $(window).scroll($.createEventDelegate(this, this._placeCart));
            }
        }
        if (this.options.placeCartOnTop || this.options.placeCartOnBottom) {
            $(window).bind("resize", $.createEventDelegate(this, this._placeCart));
        }
    },
    registerBuyButttons: function(context) {
        $("a.BuyBehavior", context).AjaxLink({ enableHistory: false, ajaxTarget: "#" + this.element.attr("ID"), loadedFunction: Function.createDelegate(this, this._onAjaxLinkLoad) });
        $(":submit.BuyBehavior", context).parents('form').AjaxLink({ enableHistory: false, ajaxTarget: "#" + this.element.attr("ID"), loadedFunction: Function.createDelegate(this, this._onAjaxLinkLoad) });
    },
    _onAjaxLinkLoad: function(isAjaxRequest,ajaxTargetElement,ajaxLinkElement)
    {
        if(isAjaxRequest)
        {
            this._onBuy();
        }
    },
    _onBuy: function() {
        if (this.options.onBuyEffect == null) {
            this.element.fadeOut();
            this.element.fadeIn();
        }
        else {
            this.options.onBuyEffect(this.element);
        }
    },
    _placeCart: function() {
        if (this.options.placeCartOnTop) {
            $(this.element).css({ top: ($(window).scrollTop() + $(window).height() - $(this.element).height()) + "px" });
        }
        if (this.options.placeCartOnBottom) {
            $(this.element).css({ top: $(window).scrollTop() + "px" });
        }
    },
    defaults: {
        onBuyEffect: null, // the function to run after a buy click
        placeCartOnTop: false, //place the cart at the top of window
        placeCartOnBottom: false //place the cart at the bottom of window
    }
}

//register the class , replace null witha class if you want inheritance
Controls.MiniCart.registerClass('Controls.MiniCart', null, Sys.IDisposable);

//Register the class as a jquery widget!
$.registerAsWidget(Controls.MiniCart);




