﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="../dnn.js" />
/// <reference path="jQuery.js" />
/// <reference path="RangeLog.js" />
/// <reference path="RangeLog.Client.RadAjaxManager.js" />

RangeLog.Client.RadWindowManager = function(clientID, radAjaxManagerID)
{
    /// <summary>
    /// Javascript class used to easily integrate with the RadWindowManager control client-side functionality.
    /// </summary>

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.defaults =
	{
	    allowMaximize: false,
	    allowMinimize: false,
	    allowMove: false,
	    allowReload: true,
	    allowResize: false,
		center: true,
		height: 500,
		title: null,
		width: 650
	};
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // INSTANCE VARIABLES
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this._ajaxManagerID = radAjaxManagerID;
    this._id = clientID;
    this._windowManager = null;
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

RangeLog.Client.RadWindowManager.prototype =
{
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // GETTERS / SETTERS
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    get_ajaxManagerId: function() {
        /// <summary>
        /// Gets the ajax manager id for this instance.
        /// </summary>
        return this._ajaxManagerID;
    },

    get_id: function() {
        /// <summary>
        /// Gets the client id for this instance.
        /// </summary>
        return this._id;
    },

    set_ajaxManagerId: function(value) {
        /// <summary>
        /// Sets the ajax manager id for this instance.
        /// </summary>    
        this._ajaxManagerId = value;
    },

    set_id: function(value) {
        /// <summary>
        /// Sets the client id for this instance.
        /// </summary>
        this._id = value;
    },
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // METHODS
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    close: function(arguments) {
        var manager = this.get();
        
        if (manager != null) {
            var window = manager.getActiveWindow();

            if (window != null) {
                window.argument = arguments;
                window.close();
            }
        }
    },

    get: function() {
        /// <summary>
        /// Gets the RadWindowManager for this instance.
        /// </summary>

        if (!this._windowManager) {
            this._windowManager = $find(this.get_id());
        }

        return this._windowManager;
    },

    onClientClose: function(window) {
        var manager = this.get();
        var radAjaxManager = new RangeLog.Client.RadAjax.Manager(this.get_ajaxManagerId());

        if (radAjaxManager && (window.argument != "" && window.argument != undefined)) {
            // call to initiate an AjaxRequest on this page's RadAjaxManager
            radAjaxManager.initiateAsyncRequest(window.argument);
        }
    },

    onClientResize: function(sender, args) {
        /* If "ResizeWindowContent" function exists IN THE RADWINDOW aspx (or a UserControl within a RadWindow aspx), call it. */
        if (typeof sender.get_contentFrame().contentWindow.ResizeWindowContent == "function")
            sender.get_contentFrame().contentWindow.ResizeWindowContent();
    },

    showRadWindow: function(url, options) {
        var window = this.get().open(url, "dialog_1");
        var behaviors = Telerik.Web.UI.WindowBehaviors.Close;

        options = jQuery.extend({}, this.defaults, options);

        if (options.title && options.title.length > 0) window.set_title(options.title);
        window.setSize(options.width, options.height);

        // build window behaviors
        if (options.allowMaximize) behaviors += Telerik.Web.UI.WindowBehaviors.Maximize;
        if (options.allowMinimize) behaviors += Telerik.Web.UI.WindowBehaviors.Minimize;
        if (options.allowMove) behaviors += Telerik.Web.UI.WindowBehaviors.Move;
        if (options.allowReload) behaviors += Telerik.Web.UI.WindowBehaviors.Reload;
        if (options.allowResize) behaviors += Telerik.Web.UI.WindowBehaviors.Resize;

        window.set_behaviors(behaviors);

        if (options.center) window.center();

        return false;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
