﻿// PageMenu.js


Type.registerNamespace('CustomPage');

CustomPage.PageMenu = function(element) {

    CustomPage.PageMenu.initializeBase(this, [element]);
    
    this._headerClickHandler = null;
    this._onMouseOutHandler = null;
    this._bodyClickHandler = null;
    
    this._columnEntryIndex = -1;
    this._skinEntryIndex = -1;
    
    this._ul;
    this._columnLinks;
    this._skinLinks;
    this._sepLine;
    
    this._itemsContainer = null;
}
CustomPage.PageMenu.prototype = {

    set_columnEntryIndex : function(value) {
        if (value != this._columnEntryIndex) {
            this.selectColumnEntry(value - 1);
            this._columnEntryIndex = value;
            if (this._popupVisible) {
                this.hidePopup();
            }
            var eh = this.get_events().getHandler("columnEntryChanged");
            if (eh) {
                eh(this, value);
            }
        }
    },
    
    set_skinEntryIndex : function(value) {
        if (value != this._skinEntryIndex) {
            this.selectSkinEntry(value - 1);
            this._skinEntryIndex = value;
            var eh = this.get_events().getHandler("skinEntryChanged");
            if (eh) {
                eh(this, value);
            }
        }
    },
    add_columnEntryChanged : function(handler) {
        this.get_events().addHandler("columnEntryChanged", handler);
    },
    
    remove_columnEntryChanged : function(handler) {
        this.get_events().removeHandler("columnEntryChanged", handler);
    },

    add_skinEntryChanged : function(handler) {
        this.get_events().addHandler("skinEntryChanged", handler);
    },
    
    remove_skinEntryChanged : function(handler) {
        this.get_events().removeHandler("skinEntryChanged", handler);
    },
    
    initialize : function() {

        CustomPage.PageMenu.callBaseMethod(this, 'initialize');
        
        // menu header
        var elt = this.get_element();
        
        // items container
        this._itemsContainer = document.createElement('div');
        this._itemsContainer.id = this.get_id() + '_menuitems_';
        this._itemsContainer.style.textAlign = 'left';
        
        if(Sys.Browser.agent === Sys.Browser.InternetExplorer) {
            elt.appendChild(this._itemsContainer);  
            this._itemsContainer.style.zIndex = "100";
        }
        else {
            document.documentElement.appendChild(this._itemsContainer);
        }
        
        // menu items
        this._ul = document.createElement('ul');
        Sys.UI.DomElement.addCssClass(this._ul, "pagemenuentries");
        this._itemsContainer.appendChild(this._ul);

        this._columnLinks = new Array(); 
        Array.add(this._columnLinks, this._createLink("1 colonna", "img/blank.gif", "javascript:$find('" + elt.id + "').set_columnEntryIndex(1);", "Cambia layout pagina" ));
        for (var i = 2; i < 5; i++) { // columns
            Array.add(this._columnLinks, this._createLink(i + " colonne", "img/blank.gif", "javascript:$find('" + elt.id + "').set_columnEntryIndex(" + i + ");", "Cambia layout pagina" ));
        }
        for (var i = 0; i < this._columnLinks.length; i ++) {
            this._ul.appendChild(this._createMenuEntry(this._columnLinks[i]));
        }
        
        var d = document.createElement('div');
        this._sepLine = document.createElement('img');
        this._sepLine.src = "img/row1.gif";
        d.appendChild(this._sepLine);
        this._ul.appendChild(d);
        
        this._skinLinks = new Array();
        Array.add(this._skinLinks, this._createLink("Arancio", "img/skin1.gif", "javascript:$find('" + elt.id + "').set_skinEntryIndex(1);", "Cambia tema" ));
        Array.add(this._skinLinks, this._createLink("Celeste", "img/skin2.gif", "javascript:$find('" + elt.id + "').set_skinEntryIndex(2);", "Cambia tema" ));
        Array.add(this._skinLinks, this._createLink("Grigio", "img/skin3.gif", "javascript:$find('" + elt.id + "').set_skinEntryIndex(3);", "Cambia tema" ));
        Array.add(this._skinLinks, this._createLink("Porpora", "img/skin4.gif", "javascript:$find('" + elt.id + "').set_skinEntryIndex(4);", "Cambia tema" ));
        Array.add(this._skinLinks, this._createLink("Verde", "img/skin5.gif", "javascript:$find('" + elt.id + "').set_skinEntryIndex(5);", "Cambia tema" ));
        for (var i = 0; i < this._skinLinks.length; i ++) {
            this._ul.appendChild(this._createMenuEntry(this._skinLinks[i]));
        }
        
        
        this._popupBehavior = $create(AjaxControlToolkit.PopupBehavior, { 'id':elt.id + 'PopupBehavior', 'parentElement':elt }, null, null, this._itemsContainer);
        
        this._headerClickHandler = Function.createDelegate(this, this._onHeaderClick);
        $addHandler(elt, 'click', this._headerClickHandler);
        
        this._onMouseOutHandler = Function.createDelegate(this, this._onMouseOut);
        $addHandler(this._itemsContainer, 'mouseout', this._onMouseOutHandler);
        
        this._bodyClickHandler = Function.createDelegate(this, this._onBodyClick);
        $addHandler(document.body, 'click', this._bodyClickHandler);
        
        this._dropShadow = $create(AjaxControlToolkit.DropShadowBehavior,{'id':this._itemsContainer.id + 'DropShadowBehavior', 'TrackPosition': false, 'Width': 1, 'Opacity': .2 }, null, null, this._ul);

    },
    
    dispose : function() { 
        
        var elt = this.get_element();
        
        if (this._headerClickHandler) {
            $removeHandler(elt, 'click', this._headerClickHandler);
            this._headerClickHandler = null;
        }
        
        if (this._onMouseOutHandler) {
            $removeHandler(this._itemsContainer, 'mouseout', this._onMouseOutHandler);
            this._onMouseOutHandler = null;
        }
        
        if (this._bodyClickHandler) {
            $removeHandler(document.body, 'click', this._bodyClickHandler);
            this._bodyClickHandler = null;
        }
        
        if (this._popupBehavior) {
            this._popupBehavior.dispose();
            this._popupBehavior = null;
        }
        CustomPage.PageMenu.callBaseMethod(this, 'dispose');
    },
    
     _createLink : function(text, imgSrc, href, title) {
        var a = document.createElement('a');
        a.href = href;
        a.innerHTML = String.format('<img src="{0}" title="{1}" style="border-style:none;vertical-align:middle;" />', imgSrc, title) + text;
        return a;
    },
    
    _createMenuEntry : function(link) {
        var li = document.createElement('li');
        Sys.UI.DomElement.addCssClass(li, "pm-item");
        li.appendChild(link);
        return li;
    },
    
    selectColumnEntry : function(numOfCols) {
        for (var i = 0; i < this._columnLinks.length; i++) {
            var img = this._columnLinks[i].getElementsByTagName('img')[0];
            if (i == numOfCols) { 
                img.src = "img/arrow.gif";
            }
            else {
                img.src = "img/blank.gif";
            }
        }
    },
    
    selectSkinEntry : function(skinNumber) {
        for (var i = 5; i < this._ul.childNodes.length; i++) {
            var n = this._ul.childNodes[i];
            if (i == (skinNumber + 5)) { 
                Sys.UI.DomElement.removeCssClass(n, "pm-item");
                Sys.UI.DomElement.addCssClass(n, "pm-selecteditem");
            }
            else {
                Sys.UI.DomElement.removeCssClass(n, "pm-selecteditem");
                Sys.UI.DomElement.addCssClass(n, "pm-item");
            }
        }
        this._sepLine.src = String.format("img/row{0}.gif", skinNumber + 1);
    },
    
    showPopup : function() {
        var old = CustomWebPartFramework.WebPartMenu.__VisiblePopup;
        if (old && old._popupBehavior) {
            old.hidePopup();
        }
            
        this._popupBehavior.set_x(0);
        this._popupBehavior.set_y(15);
        this._popupBehavior.show();
        this._popupVisible = true;
        this._dropShadow.setShadow();
        CustomWebPartFramework.WebPartMenu.__VisiblePopup = this;
    },

    hidePopup : function() {
        this._popupBehavior.hide();
        this._popupVisible = false;
        CustomWebPartFramework.WebPartMenu.__VisiblePopup = null;
    },
    
    _onHeaderClick : function(e) {
        if (!this._popupVisible) {
            this.showPopup();
        }
        else
            this.hidePopup();
            
        if (e) {
            e.stopPropagation();
        }
    },
    
    _onMouseOut : function(e) {
        if(this._checkMouseLeave(this._itemsContainer, e)) {
            this.hidePopup();
        }
    },
    
    _containsDOM : function(container,containee){
        var isParent=false;
        do{
            if((isParent=container==containee))
                break;containee=containee.parentNode;
        }
        while(containee!=null);return isParent;
    },

    _checkMouseLeave : function(element, e){
        e=(e)?e:((window.event)?window.event:"");
        window.status=e;
        if(e.rawEvent.relatedTarget){
            return !this._containsDOM(element,e.rawEvent.relatedTarget);
        }else{
            if(element.contains(e.rawEvent.toElement)){
                return(false);
            }else{
                return(true);
            }
        }
    },
    
    _onBodyClick : function() {
        if (this._popupVisible) {
            this.hidePopup();
        }
    }
    
}
CustomPage.PageMenu.registerClass('CustomPage.PageMenu', Sys.UI.Control);



CustomPage.FilterTypes = function() {
    /// <summary>
    /// Character filter to be applied to a textbox
    /// </summary>
    /// <field name="Custom" type="Number" integer="true">
    /// Custom Characters
    /// </field>
    /// <field name="Numbers" type="Number" integer="true">
    /// Numbers (0123456789)
    /// </field>
    /// <field name="UppercaseLetters" type="Number" integer="true">
    /// Uppercase Letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
    /// </field>
    /// <field name="LowercaseLetters" type="Number" integer="true">
    /// Lowercase Letters (abcdefghijklmnopqrstuvwxyz)
    /// </field>
    throw Error.invalidOperation();
}
CustomPage.FilterTypes.prototype = {
    Custom           :  0x1,
    Numbers          :  0x2,
    UppercaseLetters :  0x4,
    LowercaseLetters :  0x8
}
CustomPage.FilterTypes.registerEnum('CustomPage.FilterTypes', true);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();