﻿/*          Settings & limits   */
var fwAqtion_Settings = function() {
    this.SideBarW;
    this.SideBarO;
    this.StateSideBar;
}
fwAqtion_Settings.prototype = {
    Set : function() {
        this.SideBarW = getSessionCookie('wSB', 270);
        this.SideBarO = getSessionCookie('oSB', 1);
        this.SideBarO = (this.SideBarO==1);
        this.StateSideBar = getSessionCookie('sSB', 'full');
    },
    ReSet : function() {
        this.Set();
    },
    Store : function() {
        setSessionCookie('wSB', this.SideBarW);
        if (this.SideBarO)
            setSessionCookie('oSB', 1);
        else
            setSessionCookie('oSB', 0);
        setSessionCookie('sSB', this.StateSideBar);
    },
    Defaults : function() {
        setSessionCookie('wSB', 270);
        setSessionCookie('oSB', 1);
        setSessionCookie('sSB', 'full');
    }
}
var fwAqtion_Limits = function() {
    this.maxH = 0;
    this.maxW = 0;
    this.headerH = 0;
    this.contentW = 0;
    this.sideBarH = 0;
    this.minSX = 30;
    this.defSX = 270;
    this.maxSX = 600;
    this.sbMaxIconState = 120;
}
fwAqtion_Limits.prototype = {
    Set : function() {
        this.maxH = xHeight(document);
        this.maxW = xWidth(document);
        if (jsAqtion.hasHeader) 
            this.headerH = xHeight(jsAqtion.Elements['header']);
        
                
        jsAqtion.Settings.SideBarW = xWidth(jsAqtion.Elements['sideBar']);
        
        this.contentW = this.maxW - jsAqtion.Settings.SideBarW + 1;
        this.contentH = this.maxH - this.headerH;
        
        this.sideBarH = this.contentH;
        if (document.getElementById('fw_TopBar'))
            this.contentH -= xHeight(document.getElementById('fw_TopBar'));
    },
    ReSet : function() {
        this.Set();
    }
}
/*          FRAMEWORK           */
var fwAqtion = function() {
    this.Settings = new fwAqtion_Settings();
    this.Limits = new fwAqtion_Limits();
    this.SideBar;
    this.Slider;
    
    this.Elements = [];
    
    this.CMS = [];
    this.popups = [];
    
    this.hasSideBar = false;
    this.hasSlider = false;
    this.hasHeader = false;
    
    this.hasTopBar = false;
    
    this.hasLivePopup = false;
    this.livePopup;
    
    this.Params = [];
}

fwAqtion.prototype = {
    Destruct : function() {
        if (this.hasSideBar)
        {
            this.SideBar.Destruct();
            this.SideBar = null;
        }
        for (var el in this.Elements)
            this.Elements[el] = null;
        for (var el in this.CMS)
        {
            try {this.CMS[el].Destruct(); } catch(ex) {}
            this.CMS[el] = null;
        }
        for (var el in this.popups)
        {
            try {this.popups[el].Destruct(); } catch(ex) {}
            this.popups[el] = null;
        }
        for (var el in this.Params)
            this.Params[el] = null;
        this.Settings = null;
        
        this.Slider = null;
        this.livePopup = null;
    },
    Init : function() {
        this.Elements['popups'] = document.getElementById('fw_Popup_Container');
        this.Elements['content'] = document.getElementById('fw_Content_Container');
        this.Elements['header'] = document.getElementById('fw_Header_Container');
        this.Elements['sideBar'] = document.getElementById('fw_MenuBar_Container');
        this.Elements['sideBarItems'] = document.getElementById('fw_MenuBar');
        this.Elements['editframe'] = document.getElementById('fw_EditPageFrame');
        this.Elements['sideBarSlider'] = document.getElementById('fw_MenuBarSlider');
        
        
        this.Elements['TopBar'] = document.getElementById('fw_TopBar');
        
        this.hasSideBar = (this.Elements['sideBar'] != undefined);
        this.hasSlider = (this.Elements['sideBarSlider'] != undefined);
        this.hasHeader = (this.Elements['header'] != undefined);
        this.hasTopBar = (this.Elements['TopBar'] != undefined);

        this.Settings.Set();
        this.Limits.Set();
        
        if (this.hasSideBar) {
            this.SideBar = new fwAqtion_SideBar();
        }
        if (this.hasSideBar && this.hasSlider) {
            this.Slider = new fwAqtion_Slider();
            this.Slider.Init();
        }
        if (!this.hasHeader) {
            this.Elements['content'].style.top = '0px';
            this.Elements['sideBarSlider'].style.top = '0px';
            this.Elements['sideBar'].style.top = '0px';
            this.Elements['content'].style.borderTop = 'none';
            this.Elements['sideBarItems'].style.borderTop = 'none';
        } else if (this.hasTopBar) {
            this.Elements['content'].style.top = '79px';
        }
    },
    fixDrawing : function() {
        this.Limits.ReSet();
        if (this.hasSideBar) {
            if (this.Settings.SideBarO && !this.SideBar.isOpen)
               this.SideBar.Open();
            this.SideBar.ReDraw();
        }
        xHeight(this.Elements['content'],         jsAqtion.Limits.contentH );
        xHeight(this.Elements['sideBar'],         jsAqtion.Limits.sideBarH);
        xHeight(this.Elements['editframe'],        jsAqtion.Limits.contentH - 1);
        xHeight(this.Elements['sideBarSlider'],         jsAqtion.Limits.contentH);
        
        if (this.hasSlider) {
            //var topSlider = (jsAqtion.Limits.contentH / 2 ) - 5;
            //this.Elements['sideBarSlider'].style.top = topSlider + 'px';
        }
    },
    registerIFramePage : function( iFrame ) {
        var iframe = frames[iFrame.name];
        if (iframe && iframe.cmsSettings) {
            var settings = new iframe.cmsSettings();
            
            switch(settings.Name) {
                case 'cmsEditPage': // edit page
                    if (jsAqtion.CMS[settings.Name]) {
                        /* 
                            already a page in edit mode
                            
                            Refresh complete framework, so it has the correct pageid
                            need to change this so it only updates: Sidebars
                        */
                        setSessionCookie('page', settings.PageID);
                        
                        //var url = iframe.location.href;
                        //url = url.substr(url.indexOf('?'))
                        //window.location.href = 'default.aspx' + url; 
                        jsAqtion.CMS[settings.Name] = new fwAqtion_EditPage(iframe, settings.PageID);
                        
                        /*var col = xGetElementsByClassName('sbUpdate', document.getElementById('fw_TopBar'), 'input');
                        for(var c=0; c<col.length; c++) 
                        {
                            col[c].click();
                        }*/
                        col = xGetElementsByClassName('sbUpdate', document.getElementById('fw_MenuBar'), 'input');
                        if (col.length > 0) {
                            col[0].click();
                        }
                        //document.getElementById('fw_TopBar').style.display = 'none';
                    } else { 
                        jsAqtion.CMS[settings.Name] = new fwAqtion_EditPage(iframe, settings.PageID);
                    }
                    break;
                case 'cmsMlibExplorer':
                    break;
                case 'cmsMlibInsertImage':
                    //alert('insert image');
                    break;
                case 'cmsMlibInsertFile':
                    //alert('insert file');
                    break;
            }
            if (jsAqtion.CMS[settings.Name])
                jsAqtion.CMS[settings.Name].Init();
        }
    },
    catchPageSwitch : function ( iFrame ) {
        // hier zou je een pageswitch binnen het cms kunnen afvangen.
        // krijg alleen de destination url niet te pakken.
        //
        //alert('unload! ' + iFrame);
    },
    openPopup2: function(name, title, url, width, height, params) {
        jscss('add', document.body, 'lightBoxed');
        
        this.popups[name] = new fwAqtion_popup(name, title, url);
        if (!this.Params[name]) this.Params[name] = [];
        this.Params[this.livePopup] = params;
        this.popups[name].Open(width, height);
    },
    closePopup2: function(name, params) {
        jscss('remove', document.body, 'lightBoxed');
        if (this.popups[name]) {
            this.popups[name].Close();
            this.popups[name] = null;
        }
    },
    openPopup: function(name, title, url, width, height, params) {
        jscss('add', document.body, 'lightBoxed');
        
        if (this.hasLivePopup) {
            this.CMS[this.livePopup].Close();
        } 
        
        if( !this.CMS[name] ) {
            this.CMS[name] = null;
        } 
        this.CMS[name] = new fwAqtion_popup(name, title, url);
        this.livePopup = name;
        this.hasLivePopup = true;
        
        if (!this.Params[name]) this.Params[name] = [];
        
        if (typeof(params) == 'string') {
            var t = this.Params[name];
            t['wysiwyg'] = params;
            this.Params[name] = t;
        } else {
            this.Params[name] = params;
        }
        this.CMS[name].Open(width, height);
    },
    savePopup: function(name, params) {
        jscss('remove', document.body, 'lightBoxed');
        if (!name) name = this.livePopup;
        if (this.hasLivePopup) {
            this.hasLivePopup = false;
            if (name != 'create_campagne' && name != 'edit_campagne' && name != 'create_facebook' && name != 'edit_facebook') {
                this.CMS[name].Close();
                this.CMS[name] = null;
            }
            //this.Params[name] = params;
            //alert(this.livePopup)
            if (params && params.callback) {
                //added 2008-06-20 11:15 might break it all. good luck fred!! :D
                params.callback(params);
            } else {
                switch(this.livePopup) {
                    case 'edit_table':
                        this.CMS['cmsEditPage'].Window.WYSIWYG.updateTable( params, this.Params[this.livePopup]['wysiwyg'] );
                        break;
                    case 'create_table':
                        this.CMS['cmsEditPage'].Window.WYSIWYG.insertTable( params, this.Params[this.livePopup]['wysiwyg'] );
                        break;
                    case 'edit_image':
                    case 'cmsEditPage':
                        //alert(params.type + ' : ' + this.Params[name])
                        if (params.type == 'file') {
                            //alert(params.object);
                            this.CMS['cmsEditPage'].Window.WYSIWYG.insertObject( params.object, this.Params[this.livePopup]['wysiwyg'] );
                        } else if (params.type == 'img') {
                            this.CMS['cmsEditPage'].Window.WYSIWYG.insertImage( params.url, params.width, params.height, params.align, params.border, params.alt, params.hspace, params.vspace, this.Params[name]['wysiwyg'], params);
                        }
                        break; 
                    case 'create_section':
                        var s = getSessionCookie('__folder');
                        tree_ToggleChildNode('000_' + s, s, 2);
                        holdUser('treeCursur');
                        Aqtion.Core.Section.renderSection('000_' + s, s, cb_tree_ToggleChildNode)
                        break;
                           
                }
            }
            if (name != 'create_campagne') {
                this.livePopup = null;
            }
            //frames['fw_EditPageFrame'].style.display = 'none';
            
        }
    },
    closePopup: function(name, params) {
        jscss('remove', document.body, 'lightBoxed');
        if (this.hasLivePopup) {
            //alert(this.livePopup);
            this.hasLivePopup = false;
            this.CMS[this.livePopup].Close();
            this.CMS[this.livePopup] = null;
            this.livePopup = null;
            //alert(params.url + ' : ' + params.alt);
            //this.Params[name] = params;
            //frames['fw_EditPageFrame'].style.display = 'none';
            try {refreshFolder();} catch(c) {}
        } else if (this.CMS[name]) {
            this.CMS[name].Close();
            this.CMS[name] = null;
        } else if (this.popups[name]) {
            this.popups[name].Close();
            this.popups[name] = null;
        }
    }
}



/* tree stuff */
function chCursor( el, cr ) {
    document.getElementById(el).style.cursor = cr;
}
function holdUser( el ) {
    chCursor( el, 'wait' );
}
function freeUser( el ) {
    chCursor( el, 'default' );
}
function tree_ToggleChildNode( uid, sid, totCh ) {
    if (totCh != 0 ) {
        var el = document.getElementById(uid + "_childs");
        var elc = document.getElementById(uid + "_childs_container");
        if (el.className == '' || elc.innerHTML != '') {
            el.className = (el.className == '') ? 'nodeItemChildren' : '';
        } else {
            holdUser('treeCursur');
            Aqtion.Core.Section.renderSection(uid, sid, cb_tree_ToggleChildNode)
        }
    }
}
function cb_tree_ToggleChildNode( response ) {
    var json = Sys.Serialization.JavaScriptSerializer.deserialize(response);
    //alert(json.uid);
    //alert(json.sid);
    //alert(json.html);
    var el = document.getElementById(json.uid + "_childs");
    var elc = document.getElementById(json.uid + "_childs_container");
    
    el.className = (el.className == '') ? 'nodeItemChildren' : '';
    elc.innerHTML = json.html;
    freeUser('treeCursur');
}
function tree_EditNode(id, uid) {
    //var ids = {'sid':id,'uid':uid};
    //setSessionCookie('__treeEditNode', Sys.Serialization.JavaScriptSerializer.serialize(ids));
    
    jsAqtion.openPopup('create_section', 'Nieuwe sectie maken', '/modules/cms/popups/cmspCreateSection.aspx?sectionid=' + id);
    window.returnValue = false;
    window.event.cancelBubble = true;
    return false;
}
function tree_UpdateNode( json ) {
    var ids = Sys.Serialization.JavaScriptSerializer.deserialize(json);
    var elc = document.getElementById(ids.uid + "_childs_container");
    elc.innerHTML = '';
    tree_ToggleChildNode( ids.uid, ids.sid, 1 )
}
function tree_DelNode(id, uid, p_id, p_uid) {
    var b = confirm('Weet u zeker dat u de sectie wilt verwijderen?\nOok alle onderliggende secties en pagina\'s worden verwijderd.');
    if (b) {
        Aqtion.Core.Section.MoveSection(id, 1);
    }
    window.returnValue = false;
    window.event.cancelBubble = true;
    return false;
}
function tree_EditFile(id, uid) {
    jsAqtion.openPopup('create_section', 'Nieuwe sectie maken', '/modules/cms/popups/cmspCreatePage.aspx?page=' + id);
    window.returnValue = false;
    window.event.cancelBubble = true;
    return false;
}
function tree_DelFile(id, uid, p_id, p_uid) {
    var b = confirm('Weet u zeker dat u de pagina wilt verwijderen?');
    if (b) {
        Aqtion.Core.Page.Delete(id);
    }
    window.returnValue = false;
    window.event.cancelBubble = true;
    return false;
}


function tree_SelectFile(id, uid) {
    setSessionCookie('page', id);
    var iframe = document.getElementById('fw_EditPageFrame');
    iframe.src = 'edit_page.aspx?page=' + id;
    
    /* publicatie buttons bijwerken
    var pubbtn = document.getElementById('updPubBtns');
    if (pubbtn)
        pubbtn.click();
        */
    //ToggleSideBars('info');
    if (document.getElementById('fw_TopBar'))
    {
        var col = xGetElementsByClassName('sbUpdate', document.getElementById('fw_TopBar'), 'input');
        for(var c=0; c<col.length; c++) 
        {
            col[c].click();
        }
    }
}
function fixTopBar()
{
    try {document.getElementById('fw_TopBar').style.left = jsAqtion.Settings.SideBarW + 'px';} catch(c) {}
    xWidth(document.getElementById('fw_TopBar'), jsAqtion.Limits.contentW );
}
function toggleHighLight(oid, iid) {
    var obj = document.getElementById(oid);
    var img = document.getElementById(iid);
    
    if (!obj || !img)
        return;
    
    if (obj.value == '') {
        obj.value = 'true';
        //img.style.border = 'inset 1px;';
        img.src = '/assets/modules/img/btnHighlightpage.gif';
    } else {
        obj.value = '';
        //img.style.border = 'outset 1px;';
        img.src = '/assets/modules/img/btnDehighlighPage.gif';
    }
}

