﻿// PageItem.js

Type.registerNamespace('CustomPage');
CustomPage.PageItem = function CustomPage$PageItem(associatedElement) {
    CustomPage.PageItem.initializeBase(this, [associatedElement]);
    
    this._titleElement;
    this._titleCell;
    this._column;
    this._columnIndex;
    this._allowColumnChange = true;
    this._column = null;
    this._titleRow;
    this._bodyRow;
    this._displayTogglingIcon;
    this._editMenu;
    this._closeIcon;
    
    this._mouseDown$delegate = null;
    
    this._middleX;
    this._middleY;
    
    this._populateServicePath;
    this._populateServiceMethod;
    this._populateContext;
    this._title;
    this._titleUrl;

    this._pageItemInfo;
    this._parms = new Object();
    this._editingBox;
    this._settingsDialog = null;
    
    this._toggleBodyVisibility$delegate = Function.createDelegate(this, this._toggleBodyVisibility);
    this._closeItem$delegate = Function.createDelegate(this, this._raiseClose);
    this._titleTextEditedHandler$delegate;
    this._settingsChangedHandler$delegate;
    
    this._fake;
    
    this._autoRefresh = null;
    
    this._initializing = false;
}

CustomPage.PageItem.prototype = {
    
    add_close : function(handler) {
        this.get_events().addHandler("close", handler);
    },
    remove_close : function(handler) {
        this.get_events().removeHandler("close", handler);
    },
    
    get_pageItemInfo : function this$get_pageItemInfo() {
        return this._pageItemInfo;
    },

    set_pageItemInfo : function this$set_pageItemInfo(value) {
        this._pageItemInfo = value;
    },
    
    get_column : function this$get_column() {
        return this._column;
    },

    set_column : function this$set_column(value) {
        this._column = value;
    },

    get_allowColumnChange : function this$get_allowColumnChange() {
        return this._allowColumnChange;
    },

    set_allowColumnChange : function this$set_allowColumnChange(value) {
        this._allowColumnChange = value;
    },

    get_titleElement : function this$get_titleElement() {
        return this._titleElement;
    },

    set_titleElement: function this$set_titleElement(value) {
        this._titleElement = value;
    },

    get_column : function this$get_column() {
        return this._column;
    },

    set_column : function this$set_column(value) {
        this._column = value;
    },

    get_columnIndex : function this$get_columnIndex() {
        return this._columnIndex;
    },

    set_columnIndex : function this$set_columnIndex(value) {
        this._columnIndex = value;
    },

    get_middleX : function this$get_middleX() {
        return this._middleX;
    },

    set_middleX : function this$set_middleX(value) {
        this._middleX = value;
    },
    
    get_middleY : function this$get_middleX() {
        return this._middleY;
    },

    set_middleY : function this$set_middleX(value) {
        this._middleY = value;
    },
    
    
    initialize : function this$initialize() {
        this._initializing = true;
        CustomPage.PageItem.callBaseMethod(this, "initialize");
        var elt = this.get_element(); //td
        
        var table = document.createElement('table');
        table.id = 'pi' + this._pageItemInfo.Id;
        table.width = '100%';
        table.cellSpacing = '0'; table.cellPadding = '2'; table.border = '0';
        table.className = 'partChrome webpart_content';
        elt.appendChild(table);
        
        if (this._pageItemInfo.PopulateContext != "")
            this._parms = Sys.Serialization.JavaScriptSerializer.deserialize(this._pageItemInfo.PopulateContext);
        
        this._titleRow = this._addTitle(table);
        this._formatTitle(this._column.get_element().style.width);
        this._bodyRow =  this._addBody(table);
        
        this._bodyRow.style.display = this._pageItemInfo.Collapsed ? "none" : "";
        
        if (this._displayTogglingIcon != null) { // derived classes may not use it
            this._displayTogglingIcon.src = this._pageItemInfo.Collapsed ? "img/expand.gif" : "img/collapse.gif";
            this._displayTogglingIcon.title =  this._pageItemInfo.Collapsed ? "Espandi" : "Riduci";
        }
        
        var pm = this._column.get_view().get_pageManager();
        if (this._titleElement &&  pm.get_allowPageDesign() && this._column.get_allowLayoutChange()) {                                  
           this._mouseDown$delegate = Function.createDelegate(this, this.mouseDownHandler)
           Sys.UI.DomEvent.addHandler(this._titleCell, "mousedown", this._mouseDown$delegate);
           this._titleCell.style.cursor = "move";
        }
        
        this.setupAutoRefresh(this._pageItemInfo.RefreshInterval);
        
//        var dropShadow = $create(AjaxControlToolkit.DropShadowBehavior,{'id': 'DropShadow_' + this._pageItemInfo.Id , 'TrackPosition': true, 'Width': 5, 'Opacity': .8 }, null, null, elt);
//        dropShadow.setShadow();
        
        this._initializing = false;
    },
    
    dispose : function this$dispose() {
        CustomPage.PageItem.callBaseMethod(this, "dispose");
        if (this._mouseDown$delegate) {
            Sys.UI.DomEvent.removeHandler(this._titleCell, "mousedown", this._mouseDown$delegate);
            this._mouseDown$delegate = null;
        }
        if (this._displayTogglingIcon != null)
            $removeHandler(this._displayTogglingIcon, 'click', this._toggleBodyVisibility$delegate);
        if (this._closeIcon != null)
            $removeHandler(this._closeIcon, 'click', this._closeItem$delegate);
        if (this._autoRefresh != null) {
           window.clearInterval(this._autoRefresh);
           this._autoRefresh = null;
        }
    },
    
    _addTitle : function(table) {
        var tbl = document.createElement('table'); 
        tbl.cellPadding = '0'; tbl.cellSpacing = '0'; tbl.border = '0';
        tbl.width = '100%';
        var row = tbl.insertRow(0);
        
        var c0 = row.insertCell(row.cells.length);
        c0.vAlign = 'top';
        this._displayTogglingIcon = document.createElement('img');
        this._displayTogglingIcon.src = "img/collapse.gif";
        this._displayTogglingIcon.align = 'baseline';
        this._displayTogglingIcon.style.cursor = "pointer"; 
        this._displayTogglingIcon.style.cursor = "hand";
        this._displayTogglingIcon.style.margin = "2px 4px 0px 4px";
        this._displayTogglingIcon.title = 'Riduci';
        $addHandler(this._displayTogglingIcon, 'click', this._toggleBodyVisibility$delegate);
        c0.appendChild(this._displayTogglingIcon);
        
        var c0b = row.insertCell(row.cells.length);
        c0b.vAlign = 'top';
        c0b.align = "center";
        var a = document.createElement('a');
        a.href = this._pageItemInfo.TitleImageUrl;
        a.title = this._pageItemInfo.TitleImageAlt;
        a.target = "_blank";
        var img = document.createElement('img');
        img.src = this._pageItemInfo.TitleImage;
        img.style.margin = "0px 8px 0px 4px";
        img.border = "0px";
        a.appendChild(img);
        c0b.appendChild(a);
        
        this._titleCell = row.insertCell(row.cells.length);
        this._titleCell.id = 'WebPartTitle_' + this._pageItemInfo.Id;
        this._titleCell.width = '100%';
        this._titleElement = document.createElement('span');
        this._titleElement.innerHTML = this._pageItemInfo.Title;
        this._titleCell.appendChild(this._titleElement);
        
        var c2 = row.insertCell(row.cells.length);
        var span = document.createElement('span');
        span.className = 'webpart_menu';
        span.innerText = 'modifica';
        span.style.cursor = "pointer"; 
        span.style.cursor = "hand";
        span.style.marginLeft = "5px";
        span.id = 'wpmenu_' + this._pageItemInfo.Id;
        this._editMenu = $create(CustomWebPartFramework.WebPartMenu, {"menuEntries":this._createMenuEntries(), "menuEntryHandlers": this._createMenuEntryHandlers()}, null, null, span);
        c2.style.paddingTop = "1px";
        c2.style.paddingRight = "2px";
        c2.vAlign = "top";
        c2.appendChild(span);

        var c3 = row.insertCell(row.cells.length);
        c3.vAlign = 'top';
        c3.style.whiteSpace = 'nowrap';
        this._closeIcon = document.createElement('img');
        this._closeIcon.src = 'img/close_window.gif';
        this._closeIcon.align = 'top';
        this._closeIcon.style.borderWidth = '0px';
        this._closeIcon.title = 'Chiudi';
        this._closeIcon.style.cursor = "pointer"; 
        this._closeIcon.style.cursor = "hand";
        this._closeIcon.style.margin = "2px 4px 5px 2px";
        $addHandler(this._closeIcon, 'click', this._closeItem$delegate);
        c3.appendChild(this._closeIcon);
        
        this._fake = row.insertCell(row.cells.length);
        this._fake.id = 'wpmx_' + this._pageItemInfo.Id;
        this._fake.style.width = "1px";
         
        row = table.insertRow(table.rows.length)
        var cell = row.insertCell(0);
        cell.className = 'webpart_content_title';
        cell.appendChild(tbl);
        return row;
    },
    
    _addBody : function(table) {
        var row = table.insertRow(table.rows.length);
        var cell = row.insertCell(0);
        if (this._pageItemInfo.PageItemType == "List")
            Sys.UI.DomElement.addCssClass(cell, "listitembody"); 
        Sys.UI.DomElement.addCssClass(cell, "webpart_body"); 
        var d = document.createElement('div');
        d.style.width = '100%';
        cell.appendChild(d);
        this.populate(d);
        return row;
    },
    
    populate : function(d) {
        if (d == null || typeof(d) == "number")
            d = this._bodyRow.cells[0].firstChild;
        d.innerHTML = "<center><img src=\"img/indicator_medium.gif\" border=\"0\" align=\"center\"/></center>";
        var c = $create(AjaxControlToolkit.DynamicPopulateBehavior, {ClearContentsDuringUpdate: false, ServicePath: document.location.pathname.length > 5 ? document.location.pathname : "/main.aspx", ServiceMethod: "Dispatcher"}, null, null, d);
        c.add_populated(Function.createDelegate(this, this._populatedHandler));
        this._parms.Method = this._pageItemInfo.PopulateServiceMethod;
        if (this._pageItemInfo.PageItemType == "List") {
            this._parms.MaximumRows = this._pageItemInfo.MaximumRows;
        }
        var context = Sys.Serialization.JavaScriptSerializer.serialize(this._parms);
        c.populate(context);
    },
    
    _populatedHandler : function(sender, e) {
        var elt = sender.get_element();
        var ic = elt.getElementsByTagName('input');
        if (ic.length > 0)
            eval(ic[0].value);
    },
    
    onColumnWidthChange : function(colWidth) {
        this._formatTitle(colWidth);
    },
    
    _formatTitle: function(colWidth) {
        var maxlen;
        switch (colWidth) {
            case "100%":
                maxlen = 150;
                break;
            case "50%":
                maxlen = 70;
                break;
            case "33%":
                 maxlen = 40;
                break;
            case "25%":
                 maxlen = 30;
                break;
        }
        var t = this._pageItemInfo.Title;
        if (t.length > maxlen)
            this._titleElement.innerHTML = t.substr(0, maxlen) + "...";
        else
            this._titleElement.innerHTML = t;
    },
    
    _toggleBodyVisibility : function() {
        var oldCursor = document.cursor;
        document.cursor = "wait";
        this._pageItemInfo.Collapsed = !this._pageItemInfo.Collapsed;
        this._bodyRow.style.display = this._pageItemInfo.Collapsed ? "none" : "";
        this._displayTogglingIcon.src = this._pageItemInfo.Collapsed ? "img/expand.gif" : "img/collapse.gif";
        this._displayTogglingIcon.title =  this._pageItemInfo.Collapsed ? "Espandi" : "Riduci";

        this._column.get_view().get_pageManager().saveProfile();
        document.cursor = oldCursor;
    },
    
    
    _raiseClose : function() {
        var eh = this.get_events().getHandler("close");
        if (eh) {
            eh(this, Sys.EventArgs.Empty);
        }
    },
    
    updatePosition : function this$updatePosition() {
        var pageItemElement = this.get_element();
        var location = __cpTranslateOffset(0, 0, pageItemElement, null, false);
        this._middleX = location.x + pageItemElement.offsetWidth / 2;
        this._middleY = location.y + pageItemElement.offsetHeight / 2;
    },
    
    mouseDownHandler : function(domEvent) {         
        window._event = domEvent; this._column.startDragDrop(this);
        domEvent.preventDefault();
    },
    
    _createMenuEntries : function() {
        var entries = new Array('Rinomina', 'Aggiorna');
//        if (this._pageItemInfo.ContentType == "Dynamic")
//            Array.add(entries, 'Aggiorna');
        if (this._pageItemInfo.PageItemType == "List")
            Array.add(entries, 'Opzioni');
        return entries;
    },
    
    _createMenuEntryHandlers : function() {
        var handlers = new Array();
        Array.add(handlers, Function.createDelegate(this, this._renameMenuEntryHandler));
//        if (this._pageItemInfo.ContentType == "Dynamic")
        Array.add(handlers, Function.createDelegate(this, this._refreshMenuEntryHandler));
        if (this._pageItemInfo.PageItemType == "List" || this._pageItemInfo.ContentType == "Dynamic" )
            Array.add(handlers, Function.createDelegate(this, this._settingsMenuEntryHandler));
        return handlers;
    },
    
    _renameMenuEntryHandler : function() {
        this._editMenu.hidePopup();
        Sys.UI.DomEvent.removeHandler(this._titleCell, "mousedown", this._mouseDown$delegate);
        this._titleTextEditedHandler$delegate = Function.createDelegate(this, this._titleEditedHandler);
        this._editingBox = $create(CustomPage.EditingTextBox, { 'textToEdit': this._pageItemInfo.Title}, {'textEdited':this._titleTextEditedHandler$delegate}, null, this._titleCell);
    },
    
    _refreshMenuEntryHandler : function() {
        this._editMenu.hidePopup();
        this.populate(this._bodyRow.cells[0].firstChild);
    },
    
    _settingsMenuEntryHandler : function() {
        this._editMenu.hidePopup();
        this._settingsChangedHandler$delegate = Function.createDelegate(this, this._settingsChangedHandler); 
        var elt = this.get_element();
        if (this._settingsDialog == null) 
            this._settingsDialog = $create(CustomPage.Tools.SettingsDialog, {ownerItem: this}, null, null, this._fake);
        this._settingsDialog.show();
    },
    
   _titleEditedHandler : function(sender, e) {
        this._pageItemInfo.Title = sender.get_textInserted();
        this._titleElement.innerHTML = this._pageItemInfo.Title;
        this._column.get_view().get_pageManager().saveProfile();  
        this._formatTitle(this._column.get_element().style.width);  
        if (this._editingBox) {
            this._editingBox.remove_textEdited(this._titleTextEditedHandler$delegate);
            this._editingBox.dispose();
            this._editingBox = null;
        }
        Sys.UI.DomEvent.addHandler(this._titleCell, "mousedown", this._mouseDown$delegate);
    },
    
    _settingsChangedHandler : function(sender, e) {
        this._pageItemInfo.MaximumRows = Math.min(50, e.MaxRows);
        this.populate();
        this._column.get_view().get_pageManager().saveProfile();
    },
    
    saveProfile : function() {
        this._column.get_view().get_pageManager().saveProfile();
    },
    
    setupAutoRefresh : function(interval) {
        if (this._autoRefresh != null) {
           window.clearInterval(this._autoRefresh);
           this._autoRefresh = null;
        }
        if (interval > 0) {
            this._autoRefresh = window.setInterval(Function.createDelegate(this, this.populate), interval * 60 * 1000)
        }
    }
}

CustomPage.PageItem.registerClass("CustomPage.PageItem", Sys.UI.Control);




CustomPage.AdItem = function CustomPage$AdItem(associatedElement) {
    CustomPage.AdItem.initializeBase(this, [associatedElement]);
}

function CustomPage$AdItem$initialize() {
    CustomPage.AdItem.callBaseMethod(this, "initialize");
}

function CustomPage$AdItem$dispose() {
    CustomPage.AdItem.callBaseMethod(this, "dispose");
}

function CustomPage$AdItem$_addTitle(table) {
    var tbl = document.createElement('table'); 
    tbl.cellPadding = '0'; tbl.cellSpacing = '0'; tbl.border = '0';
    tbl.width = '100%';
    var row = tbl.insertRow(0);

    this._titleCell = row.insertCell(row.cells.length);
    var d = new Date();
    this._titleCell.id = 'ad_' + d.getTime();
    this._titleCell.width = '100%';
    this._titleElement = document.createElement('span');
    this._titleElement.innerHTML = "Pubblicità";
    this._titleCell.appendChild(this._titleElement);
    this._titleCell.align = "center";
    
    row = table.insertRow(table.rows.length)
    var cell = row.insertCell(0);
    cell.className = 'webpart_content_title';
    cell.appendChild(tbl);
    return row;
}

function CustomPage$AdItem$_addBody(table) {
    var row = table.insertRow(table.rows.length);
    var cell = row.insertCell(0); 
    Sys.UI.DomElement.addCssClass(cell, "webpart_body"); 
    var d = document.createElement('div');
    d.style.overflow = "hidden";
    d.style.width = '100%';
    d.style.textAlign = "center";
    cell.appendChild(d);
    this.populate(d);
    return row;
}

function CustomPage$AdItem$populate(d) {
    if (this._initializing) return;
    if (d == null) {
        d = this._bodyRow.cells[0].firstChild;
    }

    if (this._sourceDiv != null) {
        d.removeChild(this._currFrame);
        this._sourceDiv.appendChild(this._currFrame);
    }
    this._sourceDiv = $get('contentAd' + this._colWidth.replace(/%/,""));
    this._currFrame = this._sourceDiv.getElementsByTagName('div')[0];
    this._currFrame.style.marginBottom = "25px";
    d.appendChild(this._currFrame);  
}



function CustomPage$AdItem$onColumnWidthChange(colWidth) {
    this._colWidth = colWidth;
    this.populate();
}
    
function CustomPage$AdItem$_formatTitle(colWidth) {

}

CustomPage.AdItem.prototype = {   
    _sourceDiv : null,
    _currFrame : null,
    _colWidth : null,
    
    initialize : CustomPage$AdItem$initialize,
    dispose : CustomPage$AdItem$dispose,
    _addTitle : CustomPage$AdItem$_addTitle,
    _addBody : CustomPage$AdItem$_addBody,
    populate : CustomPage$AdItem$populate,
    _formatTitle : CustomPage$AdItem$_formatTitle,
    onColumnWidthChange : CustomPage$AdItem$onColumnWidthChange
}

CustomPage.AdItem.registerClass("CustomPage.AdItem", CustomPage.PageItem);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();