﻿/// <summary>
/// PageManager class.
/// </summary>
/// <remarks>
/// Constructor is called serverside in the PageManager class.
/// </remarks>
function _PageManager(_pageid)
{
    var _PageID = _pageid;
//    var _DragDropBehaviors = new Array();
//    var _FloatingBehaviors = new Array();
    var _WindowBehaviors = new Array();
//    var _ZonePadding = 2;
//    var _ModuleMargin = 2;
    var _KeyHide = 17;
    var _opacityTransitionSettings = { Duration: 0.0, Min: 0.9, Max: 1.0 };
    var _plScripts = new Array();
    var _requestInitScripts = new Array();
    var _endRequestScripts = new Array();

    /// <summary>
    /// Enable multyple async postback queueing.
    /// </summary>
    var _prm = null;
    // Uncomment this to enable ajax request queueing
    //var _pbQueue = new Array();
    //var _argsQueue = new Array();
    this.Init = function() {
        if (Sys && Sys.WebForms && Sys.WebForms.PageRequestManager) {
            _prm = Sys.WebForms.PageRequestManager.getInstance();
            _prm.add_initializeRequest(PageManager.InitializeRequestHandler);
            _prm.add_endRequest(PageManager.EndRequestHandler);
            _prm.add_pageLoading(PageManager.PageLoadingHandler)
            _prm.add_pageLoaded(PageManager.PageLoadedHandler)
        }
    };
    this.AppInitHandler = function() {
    };
    this.InitializeRequestHandler = function(sender, args) {
        var cancel = false;
        try {
            for (var i = 0; i < _requestInitScripts.length; i++) {
                var func = new Function(_requestInitScripts[i]);
                cancel = !func();
            }
        } catch (e) {  };

        if (cancel) {
            args.set_cancel(true);
        }

        if (_prm.get_isInAsyncPostBack()) {
            args.set_cancel(true);
            // Uncomment this to enable ajax request queueing
            // _pbQueue.push(args.get_postBackElement().id);
            // _argsQueue.push(document.forms[0].__EVENTARGUMENT.value);
        }
    };
    this.EndRequestHandler = function(sender, args) {
        // Scripts that are executed when a request has finsihed
        for (var i = 0; i < _endRequestScripts.length; i++) {
            if (typeof (eval(_endRequestScripts[i])) == 'function') {
                eval(_endRequestScripts[i]);
            }
        }
        // Uncomment this to enable ajax request queueing
        // if (_pbQueue.length > 0) {
        //     __doPostBack(_pbQueue.shift(), _argsQueue.shift());
        // }
    };
    this.PageLoadingHandler = function(sender, args) {
        Sys.Application.add_init(PageManager.AppInitHandler);
    };
    this.PageLoadedHandler = function(sender, args) {
        // Call dynamic async scripts
        try {
            for (var i = 0; i < _plScripts.length; i++) {
                eval(_plScripts[i]);
            }
            for (var i = 0; i < 10; i++) {
                if (eval("typeof AsyncScript" + i + " == 'function'")) {
                    eval("AsyncScript" + i + "()");
                }
            }
        } catch (e) { };
    };
    this.AddRequestEndScript = function(script) {
        // Check if script already exists
        for (var i = 0; i < _endRequestScripts.length; i++) {
            if (_endRequestScripts[i] == script) return;
        }
        _endRequestScripts.push(script);
    },
    this.AddRequestInitScript = function(script) {
        _requestInitScripts.push(script);
    };
    this.AddScript = function(script) {
        // Check if script already exists
        for (var i = 0; i < _plScripts.length; i++) {
            if (_plScripts[i] == script) return;
        }
        _plScripts.push(script);
    };

    /// <summary>
    /// Get opacity transition settings.
    /// </summary>
    this.get_opacityTransition = function() { return _opacityTransitionSettings; };
//    /// <summary>
//    /// Get or set DragDropBehaviors.
//    /// </summary>
//    this.get_DragDropBehaviors = function() { return _DragDropBehaviors; };
//    this.set_DragDropBehaviors = function(value) { _DragDropBehaviors = value; };
//    /// <summary>
//    /// Get or set FloatingBehaviors.
//    /// </summary>
//    this.get_FloatingBehaviors = function() { return _FloatingBehaviors; };
//    this.set_FloatingBehaviors = function(value) { _FloatingBehaviors = value; };
//    /// <summary>
//    /// Get or set DragDropBehaviors.
//    /// </summary>
//    this.get_WindowBehaviors = function() { return _WindowBehaviors; };
//    this.set_WindowBehaviors = function(value) { _WindowBehaviors = value; };
//    /// <summary>
//    /// Add or remove DragDropBehavior.
//    /// </summary>
//    this.add_DragDropBehavior = function(behavior) 
//    { 
//        // Make sure we don't ever register the same behavior twice!
//        for (var i = 0; i < _DragDropBehaviors.length; i++)
//        {
//            if (_DragDropBehaviors[i].get_element().id == behavior.get_element().id)
//            {
//                return;
//            }
//        }
//        
//        _DragDropBehaviors.push(behavior); 
//    };
//    this.remove_DragDropBehavior = function(behavior)
//    {
//        for (var i = 0; i < _DragDropBehaviors.length; i++)
//            if (_DragDropBehaviors[i] == behavior) _DragDropBehaviors.splice(i, 1);
//    };
//    /// <summary>
//    /// Adds or remove FloatingBehaviors.
//    /// </summary>
//    this.add_FloatingBehavior = function(behavior) 
//    { 
//        // Make sure we don't ever register the same behavior twice!
//        for (var i = 0; i < _FloatingBehaviors.length; i++)
//        {
//            if (_FloatingBehaviors[i].get_element().id == behavior.get_element().id)
//            {
//                return;
//            }
//        }
//               
//        _FloatingBehaviors.push(behavior); 
//    };
//    this.remove_FloatingBehavior = function(behavior)
//    {  
//        for (var i = 0; i < _FloatingBehaviors.length; i++)
//            if (_FloatingBehaviors[i] == behavior) _FloatingBehaviors.splice(i, 1);
//    };
    /// <summary>
    ///  Add or remove DragDropBehavior.
    /// </summary>
    this.get_Window = function(id)
    { 
        alert(_WindowBehaviors.length);
        for (var i = 0; i < _WindowBehaviors.length; i++)
        {
            if (_WindowBehaviors[i].get_element().id == id)
            {
                return _WindowBehaviors[i];
            }
        }
    };
    this.add_WindowBehavior = function(behavior)
    { 
        // Make sure we don't ever register the same behavior twice!
        for (var i = 0; i < _WindowBehaviors.length; i++)
        {
            if (_WindowBehaviors[i].get_element().id == behavior.get_element().id)
            {
                return;
            }
        }
        
        _WindowBehaviors.push(behavior); 
    };
    this.remove_WindowBehavior = function(behavior)
    {
        for (var i = 0; i < _WindowBehaviors.length; i++)
            if (_WindowBehaviors[i] == behavior) _WindowBehaviors.splice(i, 1);
    };
    /// <summary>
    /// Gets the hide Module while dragging key.
    /// </summary>
    this.get_KeyHide = function() { return _KeyHide; };

//    /// <summary>
//    /// Get FloatingBehavior.
//    /// </summary>
//    this.get_FloatingBehavior = function(item)
//    {
//        for (var i = 0; i < _FloatingBehaviors.length; i++)
//            if (_FloatingBehaviors[i].get_element() == item)
//                return _FloatingBehaviors[i];
//    };
//    /// <summary>
//    /// Get DragDropBehavior.
//    /// </summary>
//    this.get_DragDropBehavior = function(item)
//    {
//        for (var i = 0; i < _DragDropBehaviors.length; i++)
//            if (_DragDropBehaviors[i].get_element() == item)
//                return _DragDropBehaviors[i];
//    };
//    /// <summary>
//    /// Called when a module has been dropped.
//    /// </summary>
//    this.module_Drop = function(data, dragdopBehavior) {
//        // Server side module position updating
//        // Create new postback
//        Utils.doPostback(dragdopBehavior.get_ContainerID(), "UpdateModulePosition?ModuleID=" +
//        data.FloatingBehavior.get_ModuleID() + "&Index=" + data.FloatingBehavior.get_Index());
//        //Utils.doPostback(dragdopBehavior.get_element().id, "UpdateModulePosition?ModulePositionID=" +
//        //data.FloatingBehavior.get_PositionID() + "&Index=" + data.FloatingBehavior.get_Index());

//        /*// The following code is for PURE client updating!!!
//        // Raise client onDrop event
//        dragdopBehavior._raiseEvent("onDrop", new Framework.Controls.DropEventArgs
//        (dragdopBehavior.get_PageID(), data.FloatingBehavior.get_ModuleID(),
//        data.FloatingBehavior.get_PositionID(), data.item,
//        dragdopBehavior.get_element()));

//        // Create new update collection
//        var _updateCollection = new Array();

//        // Update source zone
//        if (data.FloatingBehavior.get_Container() != dragdopBehavior.get_element()) {
//        this.update_Zone(data.FloatingBehavior.get_Container(),
//        this.get_DragDropBehavior(data.FloatingBehavior.get_Container()),
//        _updateCollection);
//        }

//        // Update target zone
//        this.update_Zone(dragdopBehavior.get_element(), dragdopBehavior, _updateCollection);*/

//        // <-- DEBUG
//        /*for (var i = 0; i < _updateCollection.length; i++)
//        {
//        var _update = _updateCollection[i];
//        //alert("Title: " + _update.Title + "\nPositionID: " + _update.PositionID + "\n" + _update.SourceContainer + "\n" + _update.TargetContainer
//        // + "\n" + _update.TargetIndex);
//        alert("PositionID: " + _update.PositionID + "\n" + _update.ContainerID + "\n" + _update.Index);
//        }
//        return;*/
//        // DEBUG -->

//        /*// Update positions in DB
//        if (_updateCollection.length > 0) {
//        Services.Module.UpdatePositions(_updateCollection, dragdopBehavior.get_element().id,
//        data.FloatingBehavior.get_element().id, this.module_DropComplete);
//        }*/
//    };
//    this.module_DropComplete = function(result) {
//        // Get DragDropBehavior
//        var _dragdopBehavior = PageManager.get_DragDropBehavior($get(result.ModuleZone));

//        // Raise client onDrop event
//        _dragdopBehavior._raiseEvent("onDropComplete", new Framework.Controls.DropCompleteEventArgs
//                                                      ($get(result.ModuleZone), $get(result.Module),
//                                                      result.Positions));

//        // Update new user position FloatingBehaviors
//        for (var i = 0; i < result.Positions.length; i++) {
//            var _position = result.Positions[i];

//            // Get FloatingBehavior
//            var _floatingBehavior = PageManager.get_FloatingBehavior($get(_position.ModuleContainerID));

//            // Update positions id
//            _floatingBehavior.set_PositionID(_position.PositionID);
//        }
//    };
    /// <summary>
    /// Deletes module.
    /// </summary>
    /// <param name="control">Module.</param>
    /// <param name="buttons">State buttons collection.</param>
    /// <param name="posid">Module position id.</param>
    this.module_Delete = function(control, posid, modid, zone) {
        //Module.onDelete(control, posid, modid);

        // Create new postback
        Utils.doPostback(zone.id, "DeleteModule?ModuleID=" + modid + "&ModulePositionID=" + posid);

        // Prevent button from creating a postback
        return false;
    };
//    /// <summary>
//    /// Add padding to all Behaviors.
//    /// </summary>
//    this.add_moduleZonesVisual = function() {
//        // Process DragDropBehaviors
//        for (var i = 0; i < _DragDropBehaviors.length; i++) {
//            var _behavior = _DragDropBehaviors[i];

//            if (!_behavior.get_isCatalog()) {
//                var el = _behavior.get_element();
//                el.oldPadding = el.style.padding;
//                el.style.padding = _ZonePadding + "px";
//                el.style.paddingTop = el.style.paddingBottom = (_ZonePadding) + "px";
//            }
//        }

//        // Process floating behaviors
//        for (var i = 0; i < _FloatingBehaviors.length; i++) {
//            var _behavior = _FloatingBehaviors[i];

//            if (!_behavior.get_isCatalog()) {
//                var el = _behavior.get_element();
//                el.oldMargin = el.style.marginBottom;
//                el.style.marginBottom = (_ZonePadding) + "px";
//            }
//        }
//    };
//    /// <summary>
//    /// Removes padding from all Behaviors.
//    /// </summary>
//    this.remove_moduleZonesVisual = function() {
//        // Process DragDropBehaviors
//        for (var i = 0; i < _DragDropBehaviors.length; i++) {
//            var _behavior = _DragDropBehaviors[i];

//            var el = _behavior.get_element();
//            if (el.oldPadding) {
//                el.style.padding = el.oldPadding;
//                el.style.paddingTop = el.style.paddingBottom = el.oldPadding;
//            } else {
//            el.style.padding = "0";
//            }
//        }

//        // Process floating behaviors
//        for (var i = 0; i < _FloatingBehaviors.length; i++) {
//            var _behavior = _FloatingBehaviors[i];

//            var el = _behavior.get_element();
//            if (el.oldMargin) {
//                el.style.marginBottom = el.oldMargin;
//            }
//            else {
//                el.style.marginBottom = "0";
//            }
//        }
//    };
//    /// <summary>
//    /// Add the DragDropContainerCssClass to all ModuleZones.
//    /// </summary>
//    this.add_zoneDropContainerClass = function() {
//        // Process DragDropBehaviors
//        for (var i = 0; i < _DragDropBehaviors.length; i++) {
//            var _behavior = _DragDropBehaviors[i];

//            if (!_behavior.get_isCatalog()) {
//                var el = _behavior.get_element();
//                el.className = _behavior.get_DragDropContainerCssClass();
//            }
//        }
//    };
//    /// <summary>
//    /// Removes the DragDropContainerCssClass from all ModuleZones.
//    /// </summary>
//    this.remove_zoneDropContainerClass = function()
//    {
//        // Process DragDropBehaviors
//        for (var i = 0; i < _DragDropBehaviors.length; i++)
//        {
//            var _behavior = _DragDropBehaviors[i];
//            
//            var el = _behavior.get_element();
//                el.className = "";
//        }
//    };
//    /// <summary>
//    /// Refreshes given FloatingBehavior.
//    /// </summary>
//    /// <remarks>
//    /// Used for async postbacks to associate the handle with the control.
//    /// </remarks>
//    this.Refresh_FloatingBehavior = function(behviorid)
//    {
//        for (var i = 0; i < _FloatingBehaviors.length; i++)
//        {
//            if (_FloatingBehaviors[i].get_element().id == behviorid)
//            {
//                _FloatingBehaviors[i].Refresh();
//            }
//        }
//    };
//    /// <summary>
//    /// Refreshes given DragDropBehavior.
//    /// </summary>
//    /// <remarks>
//    /// Used for async postbacks to associate the handle with the control.
//    /// </remarks>
//    this.Refresh_DragDropBehavior = function(behviorid) {
//        for (var i = 0; i < _DragDropBehaviors.length; i++) {
//            if (_DragDropBehaviors[i].get_element().id == behviorid) {
//                _DragDropBehaviors[i].Refresh();
//            }
//        }
//    };
//    /// <summary>
//    /// Start D&D behavoir.
//    /// </summary>
//    this.startDragAndDrop = function(controlid) {
//        for (var i = 0; i < _FloatingBehaviors.length; i++) {
//            if (_FloatingBehaviors[i].get_ControlID() == controlid) {
//                _FloatingBehaviors[i].delayedDragAndDrop();
//            }
//        }
//    };
    /// <summary>
    /// Disables the display.
    /// </summary>
    this.DisableDisplay = function(_index, _blockerCssClass, targetEl) {
        Utils.DisableDisplay(_index, _blockerCssClass, targetEl);
    };
    /// <summary>
    /// Enables the display.
    /// </summary>
    this.EnableDisplay = function(_remove) {
        Utils.EnableDisplay(_remove);
    };
    /// <summary>
    /// Creates a animated page scroll to the given position.
    /// </summary>
    /// <remarks>
    /// Support only vertical scrolling.
    /// </remarks>
    this.PageScroll = function(x, speed) {
        $('html, body').animate({ scrollTop: x }, speed);
    };
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();