﻿
// Register the namespace for the control.
Type.registerNamespace('ItExtenders');

//
// Define the behavior properties.
//
ItExtenders.OnClickWindowOpenBehaviour = function(element) {
    ItExtenders.OnClickWindowOpenBehaviour.initializeBase(this, [element]);

    this._windowName = null;
    this._style = null;
    this._url = null;
    this._popup = null;
}

//
// Create the prototype for the behavior.
//

ItExtenders.OnClickWindowOpenBehaviour.prototype = {

    initialize: function() {
        ItExtenders.OnClickWindowOpenBehaviour.callBaseMethod(this, 'initialize');

        $addHandlers(this.get_element(),
                     { 'click': this._onClick
                     },
                     this);
    },

    dispose: function() {
        $clearHandlers(this.get_element());
        ItExtenders.OnClickWindowOpenBehaviour.callBaseMethod(this, 'dispose');
    },

    //
    // Event delegates
    //
    _onClick: function(e) {
        if (this.get_element() && !this.get_element().disabled) {            
        if(this._url == null || this._url == "" && this.get_element().href)
        {
            this._url = this.get_element().href;
            }
        if(this._url != null && this._url.length > 0){        
        if(this._popup)        {        
            window.open(this._url, this._windowName, this._style, true);            
            }
            else{
            document.location = this._url;
            }
            
            return false;
            }
        }
    },

    //
    // Behavior properties
    //
    get_windowName: function() {
        return this._windowName;
    },

    set_windowName: function(value) {
        if (this._windowName !== value) {
            this._windowName = value;
            this.raisePropertyChanged('windowName');
        }
    },

    get_style: function() {
        return this._style;
    },

    set_style: function(value) {
        if (this._style !== value) {
            this._style = value;
            this.raisePropertyChanged('style');
        }
    },
    
    get_url: function() {
        return this._url;
    },

    set_url: function(value) {
        if (this._url !== value) {
            this._url = value;
            this.raisePropertyChanged('url');
        }
    },
    
    get_popup: function() {
        return this._popup;
    },

    set_popup: function(value) {
        if (this._popup !== value) {
            this._popup = value;
            this.raisePropertyChanged('popup');
        }
    }
}

// Optional descriptor for JSON serialization.
ItExtenders.OnClickWindowOpenBehaviour.descriptor = {
properties: [{ name: 'windowName', type: String },
                    { name: 'style', type: String},
                     { name: 'url', type: String},
                     { name: 'popup', type: Boolean }]
}

// Register the class as a type that inherits from Sys.UI.Control.
ItExtenders.OnClickWindowOpenBehaviour.registerClass('ItExtenders.OnClickWindowOpenBehaviour', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();