﻿/// <reference name="MicrosoftAjax.js"/>
// Copyright (c) iucon GmbH. All rights reserved.
// For more information about our work, visit http://www.iucon.com

Type.registerNamespace("iucon.sip.Controls.Shop");

iucon.sip.Controls.Shop.DetailsNotepadButton = function(element) {
    iucon.sip.Controls.Shop.DetailsNotepadButton.initializeBase(this, [element]);

    this._productID = null;
    this._servicePath = null;
    this._webRequest = null;
    this._isLoggedIn = false;
    this._isOnNotepad = false;
    this._handlerAttached = false;
    this._link = null;
    this._image = null;

    this._clickHandler = null;
}

iucon.sip.Controls.Shop.DetailsNotepadButton.prototype = {

    initialize: function() {
        iucon.sip.Controls.Shop.DetailsNotepadButton.callBaseMethod(this, 'initialize');

        this._clickHandler = Function.createDelegate(this, this._onClick);

        var element = this.get_element();
        this._link = $get(element.id + "_Link");
        this._image = $get(element.id + "_NotepadImage");

        if (!this._isLoggedIn) {
            this._link.className += " col_E";
        }

        this._attachHandler();
    },

    dispose: function() {
        this._detatchHandler();
        this._clickHandler = null;

        iucon.sip.Controls.Shop.DetailsNotepadButton.callBaseMethod(this, 'dispose');
    },

    _onClick: function(e) {
        if (!this._isLoggedIn) {
            alert(Messages.Login);
        }
        else if (this._isOnNotepad) {
            alert(Messages.IsOnNotepad);
        }
        else {
            //this._detatchHandler();

            if (this._webRequest) {
                this._webRequest.get_executor().abort();
                this._webRequest = null;
            }

            this._image.src = this._image.src.replace(/dotButton_options_notepad\.gif/, "refreshing.gif");

            var params = { ProductID: this._productID };
            this._webRequest = Sys.Net.WebServiceProxy.invoke(this.get_ServicePath(), "AddProductToNotepad", false, params, Function.createDelegate(this, this._onMethodComplete), Function.createDelegate(this, this._onMethodFailed));
        }

        e.stopPropagation();
        e.preventDefault();

        return false;
    },

    _onMethodComplete: function(result, context) {
        this._webRequest = null;

        this._image.src = this._image.src.replace(/refreshing\.gif/, "dotButton_options_notepad.gif");
        this._link.innerHTML = Messages.IsOnNotepad;

        this._isOnNotepad = true;

        if (refreshNotepadTotalisator) refreshNotepadTotalisator();

        //this._attachHandler();
    },

    _onMethodFailed: function(result, context) {
        this._image.src = this._image.src.replace(/refreshing\.gif/, "dotButton_options_notepad.gif");

        //this._attachHandler();
    },

    _attachHandler: function() {
        $addHandler(this._link, "click", this._clickHandler);
        this._link.style.cursor = "pointer";

        this._handlerAttached = true;
    },

    _detatchHandler: function() {
        if (this._clickHandler != null &&
            this._handlerAttached) $removeHandler(this._link, "click", this._clickHandler);
        this._link.style.cursor = "default";

        this._handlerAttached = false;
    },

    /* ProductID */
    get_ProductID: function() {
        return this._productID;
    },
    set_ProductID: function(value) {
        this._productID = value;
    },

    /* ServicePath */
    get_ServicePath: function() {
        return this._servicePath;
    },
    set_ServicePath: function(value) {
        this._servicePath = value;
    },

    /* IsLoggedIn */
    get_IsLoggedIn: function() {
        return this._isLoggedIn;
    },
    set_IsLoggedIn: function(value) {
        this._isLoggedIn = value;
    },

    /* IsOnNotepad */
    get_IsOnNotepad: function() {
        return this._isOnNotepad;
    },
    set_IsOnNotepad: function(value) {
        this._isOnNotepad = value;
    }
}

iucon.sip.Controls.Shop.DetailsNotepadButton.registerClass('iucon.sip.Controls.Shop.DetailsNotepadButton', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

Messages={"Login":"To use your notepad please log in","IsOnNotepad":"Is on notepad","PutOnNotepad":"Put on notepad"};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();