﻿// PageManager.js

var __cpm;

Type.registerNamespace('CustomPage');

CustomPage.PageManager = function CustomPage$PageManager() {
    CustomPage.PageManager.initializeBase(this);

//    this._views;
    this._allowPageDesign = true;
    this._mainContainer;
    this._customPageInfo;
    this._tabContainer;
    this._pageMenuId;
    this._pageMenu;
    this._skinLinks = new Array();
    this._initialized = false;
    
    this._onLoadProfileCompleted$delegate = Function.createDelegate(this, this._loadProfileCompleted);
    this._onTabPanelChanged$delegate = Function.createDelegate(this, this._set_tabPanelChangeHandler);
    this._onSkinSelected$delegate = Function.createDelegate(this, this._skinSelectionHandler);
    this._onColNumberSelected$delegate = Function.createDelegate(this, this._colNumberSelectionHandler);
}

CustomPage.PageManager.prototype = {
    
    get_pageMenuId : function() {
        return this._pageMenuId;
    },
    
    set_pageMenuId : function(value) {
       this._pageMenuId = value;
    },
    
    get_mainContainer : function() {
        return this._mainContainer;
    },
    
    set_mainContainer : function(value) {
       this._mainContainer = value;
    },
    
    get_allowPageDesign : function() {
        return this._allowPageDesign;
    },
    
    set_allowPageDesign : function(value) {
       this._allowPageDesign = value;
    },
    
    add_colNumberChanged : function(handler) {
        this.get_events().addHandler("colNumberChanged", handler);
    },
    
    remove_colNumberChanged : function(handler) {
        this.get_events().removeHandler("colNumberChanged", handler);
    },
    initialize : function() {
        CustomPage.PageManager.callBaseMethod(this, 'initialize');
        this._pageMenu = $create(CustomPage.PageMenu, null, {"skinEntryChanged": this._onSkinSelected$delegate, "columnEntryChanged": this._onColNumberSelected$delegate}, null, $get(this._pageMenuId));
        for (var i = 1; i < 6; i++) {
            Array.add(this._skinLinks, $get('ctl00_skin' + i));
        }
        Sys.Services.ProfileService.load(null, this._onLoadProfileCompleted$delegate, this._loadProfileFailed, null);
    },
     
    updatePositions: function() {
       this._tabContainer.updatePositions();
    },
    
    _loadProfileCompleted : function(result, context, methodName)
    {
        this._customPageInfo = Sys.Services.ProfileService.properties.UserCustomPageInfo;
        if (this._customPageInfo.TabPanels.length == 0) {
            this._setInitialDefault();
        }
        this._buildPage();
    },
    
    _buildPage : function() {
        var div = document.createElement('div');
        div.id='TabContainer1';
        div.className="achilles__tab_xp";
        div.style.width = "100%";
        this._mainContainer.appendChild(div);
        this._tabContainer = $create(CustomPage.TabContainer, {"pageManager" : this, "tabPanelsInfo": this._customPageInfo.TabPanels}, {"activeTabChanged":this._onTabPanelChanged$delegate}, null, div);
        this._tabContainer.set_activeTabIndex(this._customPageInfo.ActiveTabPanelIndex);
        this._pageMenu.set_columnEntryIndex(this._customPageInfo.TabPanels[this._customPageInfo.ActiveTabPanelIndex].Columns.length);
        this._pageMenu.set_skinEntryIndex(this._customPageInfo.SelectedSkin);
        
        $get('onwaiting').style.display = 'none';
        this._initialized = true;
    },
    
    _setInitialDefault : function() {
        this._customPageInfo.SelectedSkin = 3;
        var tp = new Object();
        tp.ID = "newpage";
        tp.HeaderText = 'Prima Pagina';
        tp.Columns = new Array();
        Array.add(tp.Columns, new Object());
        Array.add(tp.Columns, new Object());
        Array.add(tp.Columns, new Object());
        tp.Columns[0].Items = new Array(this._createInfoSample(), this._createAdItemInfo());
        tp.Columns[1].Items = new Array(this._createSiteInfo(), this._createCSSample());
        tp.Columns[2].Items = new Array(this._createFainfoSample(), this._createMoreInfoSample());
        Array.add(this._customPageInfo.TabPanels, tp);
        var tp2 = new Object();
        tp2.ID = "newpage2";
        tp2.HeaderText = 'Nuova Pagina';
        Array.add(this._customPageInfo.TabPanels, tp2);
    },
    
    resetPage : function() {
        if (confirm("Tutte le personalizzazioni saranno rimosse. Vuoi continuare?")) {
            this._customPageInfo = new Object();
            this._customPageInfo.TabPanels = new Array();
            this._setInitialDefault();
            this.saveProfile(Function.createDelegate(this, this._reloadOnReset));
        }
    },
    
    _reloadOnReset : function() {
        document.location = document.location;
    },
    
    dispose : function() {
        if (this._tabContainer) {
            this._tabContainer.remove_activeTabChanged(this._onTabPanelChanged$delegate);
        }
        
        if (this._pageMenu) {
            this._pageMenu.remove_skinEntryChanged(this._onSkinSelected$delegate);
            this._pageMenu.remove_columnEntryChanged(this._onColNumberSelected$delegate);
        }
    },
    
    _skinSelectionHandler: function(sender, skinNumber) {
       for (var i = 0; i < 5; i++) {
            if ((i + 1) == skinNumber)
                this._skinLinks[i].disabled = false;
            else
                this._skinLinks[i].disabled = true;
        }
        if (this._initialized) {
            this._customPageInfo.SelectedSkin = skinNumber;
            this.saveProfile();
        }
    },
    
    _colNumberSelectionHandler : function(sender, numOfCols) {
       var currNumOfCols = this._customPageInfo.TabPanels[this._customPageInfo.ActiveTabPanelIndex].Columns.length;
       if (numOfCols > currNumOfCols) {
            for (var i = 0; i < (numOfCols - currNumOfCols); i++)
                this._tabContainer.get_activeTab().addColumn();
       }
       else if (numOfCols < currNumOfCols) {
            for (var i = 0; i < (currNumOfCols - numOfCols); i++)
                this._tabContainer.get_activeTab().removeColumn();
       }
       else
            return;
       this.saveProfile();
       if(Sys.Browser.agent === Sys.Browser.Opera) {
        document.location = document.location;
//        document.forms[0].submit();
       }
    },
    
    _set_tabPanelChangeHandler : function(sender, activeTabIndex) {
        this._customPageInfo.ActiveTabPanelIndex = activeTabIndex;
        this._pageMenu.set_columnEntryIndex(this._customPageInfo.TabPanels[activeTabIndex].Columns.length);
        if (this._initialized) {
            this.saveProfile();
        }
    },
    
    _loadProfileFailed : function(result, context, methodName)
    {
        alert(result.get_message());
    },
    
    saveProfile : function(onCompleted) {
        var arrayVar = new Array('UserCustomPageInfo');
        Sys.Services.ProfileService.properties.UserCustomPageInfo = this._customPageInfo;
        Sys.Services.ProfileService.save(arrayVar, onCompleted, null, null);
    },
    
    showHelp : function this$ShowHelp(helpUrl, helpMode) {
//        if ((typeof(this.menu) != "undefined") && (this.menu != null)) {
//            this.menu.Hide();
//        }

        if (helpMode == 0 || helpMode == 1) {
            if (helpMode == 0 && window.showModalDialog) {
                var dialogInfo = "edge: Sunken; center: yes; help: no; resizable: yes; status: no";
                window.showModalDialog(helpUrl, null, dialogInfo);
            }
            else {
                window.open(helpUrl, null, "scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no");
            }
        }
        else if (helpMode == 2) {
            window.location = helpUrl;
        }
    },
    
    _createAdItemInfo : function() {
        var ai = new Object();
        ai.Id = "pi" + new Date().getTime();
        ai.PopulateServicePath = "";
        ai.PopulateServiceMethod = "GetAd";
        ai.PopulateContext = "";
        ai.Title = "";
        ai.TitleUrl = "";
        ai.TitleImage = "";
        ai.TitleImageUrl = "";
        ai.TitleImageAlt = "";
        ai.Collapsed = false;
        ai.PageItemType = "Single";
        ai.MaximumRows = 0;
        ai.ContentType = "Dynamic";
        ai.RefreshInterval = 0;
        ai.ClientClass = "CustomPage.AdItem";
        return ai;
    },
    
    _createSiteInfo : function() {
        var ai = new Object();
        ai.Id = "si" + new Date().getTime();
        ai.PopulateServicePath = "";
        ai.PopulateServiceMethod = "GetSiteInfo";
        ai.PopulateContext = "";
        ai.Title = "Mia Informazione";
        ai.TitleUrl = "";
        ai.TitleImage = "img/mia_icon.gif";
        ai.TitleImageUrl = "/help.aspx";
        ai.TitleImageAlt = "";
        ai.Collapsed = false;
        ai.PageItemType = "Single";
        ai.MaximumRows = 0;
        ai.ContentType = "Static";
        ai.RefreshInterval = 0;
        ai.ClientClass = "CustomPage.PageItem";
        return ai;
    },
    
    _createFainfoSample : function() {       
        var pi = new Object();
        pi.Id = "fi" + new Date().getTime();
        pi.PopulateServicePath = "";
        pi.PopulateServiceMethod = "FainfoGetPostsBySection";
        pi.PopulateContext = "{\"SectionId\":\"2fd8b385-96ab-4553-ae42-c60910d81f90\",\"SectionName\":\"Cronaca\"}";
        pi.Title = "Fai Informazione - Cronaca";
        pi.TitleUrl = "";
        pi.TitleImage = "img/fai_insert_sm.jpg";
        pi.TitleImageUrl = "http://fai.informazione.it/list.aspx?s=2fd8b385-96ab-4553-ae42-c60910d81f90";
        pi.TitleImageAlt = "Fai Informazione - Cronaca";
        pi.Collapsed = false;
        pi.PageItemType = "List";
        pi.MaximumRows = 5;
        pi.ContentType = "Dynamic";
        pi.RefreshInterval = 0;
        pi.ClientClass = "CustomPage.PageItem";
        return pi;
    },
    
    _createInfoSample : function() {
        var pi = new Object();
        pi.Id = "in" + new Date().getTime();
        pi.PopulateServicePath = "";
        pi.PopulateServiceMethod = "InfoGetSectionNewsItems";
        pi.PopulateContext = '{SectionId: "2cc96883-c65d-4c82-b4ff-87362a8e6497", SectionName: "DALL\u0027INTERNO"}';
        pi.Title = "Informazione.it - DALL'INTERNO";
        pi.TitleUrl = "";
        pi.TitleImage = "img/news_icon.gif";
        pi.TitleImageUrl = "http://www.informazione.it/newssection.aspx?sectionid=2cc96883-c65d-4c82-b4ff-87362a8e6497";
        pi.TitleImageAlt = "Informazione.it - DALL'INTERNO";
        pi.Collapsed = false;
        pi.PageItemType = "List";
        pi.MaximumRows = 5;
        pi.ContentType = "Dynamic";
        pi.RefreshInterval = 0;
        pi.ClientClass = "CustomPage.PageItem";
        return pi;
    },
    
    _createMoreInfoSample : function() {
        var pi = new Object();
        pi.Id = "inm" + new Date().getTime();
        pi.PopulateServicePath = "";
        pi.PopulateServiceMethod = "InfoGetSectionNewsItems";
        pi.PopulateContext = "{\"SectionId\":\"86fbfbb8-18d5-4773-8e3b-d108087ae12b\",\"SectionName\":\"SPORT\"}";
        pi.Title = "Informazione.it - SPORT";
        pi.TitleUrl = "";
        pi.TitleImage = "img/news_icon.gif";
        pi.TitleImageUrl = "http://www.informazione.it/newssection.aspx?sectionid=86fbfbb8-18d5-4773-8e3b-d108087ae12b";
        pi.TitleImageAlt = "Informazione.it - SPORT";
        pi.Collapsed = false;
        pi.PageItemType = "List";
        pi.MaximumRows = 5;
        pi.ContentType = "Dynamic";
        pi.RefreshInterval = 0;
        pi.ClientClass = "CustomPage.PageItem";
        return pi;
    },
    
    _createCSSample : function() {
        var pi = new Object();
        pi.Id = "cs" + new Date().getTime();
        pi.PopulateServicePath = "";
        pi.PopulateServiceMethod = "GetPressReleasesBySection";
        pi.PopulateContext = "{\"SectionId\":\"647a9fa3-f641-4254-aa95-7ac111e9a6e3\",\"SectionName\":\"Information Technology\"}";
        pi.Title = "Comunicati Stampa - Information Technology";
        pi.TitleUrl = "";
        pi.TitleImage = "img/cs_icon.gif";
        pi.TitleImageUrl = "http://www.informazione.it/prlist.aspx?inid=647a9fa3-f641-4254-aa95-7ac111e9a6e3";
        pi.TitleImageAlt = "Comunicati Stampa - Information Technology";
        pi.Collapsed = false;
        pi.PageItemType = "List";
        pi.MaximumRows = 5;
        pi.ContentType = "Dynamic";
        pi.RefreshInterval = 0;
        pi.ClientClass = "CustomPage.PageItem";
        return pi;
    },
    
    displayInfo : function() {
        var a = $get('monitor2');
        for (var i = 0; i < this._tabContainer.get_tabs().length; i++) {
            for (var j = 0; j < this._tabContainer.get_tabs()[i].get_columns().length; j ++) {
                for (var x = 0; x < this._tabContainer.get_tabs()[i].get_columns()[j].get_items().length; x++) {
                    a.value = a.value + i + '/' + j + '/'+ x + '/' + this._tabContainer.get_tabs()[i].get_columns()[j].get_items()[x].get_middleY() + '\n';
                }
            }
        }
        a.value = a.value + '\n\r' + '\n\r';
    }
}


CustomPage.PageManager.registerClass("CustomPage.PageManager", Sys.Component);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
