 /*******************************************
 * Ajax CMS Systeem v0.1 (c) 2008 T-Design
 * 
 * Deze class maakt het mogelijk om dmv een
 * toets de admin mode in te gaan en de pagina
 * 'live' aan te passen en door te sturen naar
 * de Database
 *
 * Opties:
 * new Ajax.CMS();
 ********************************************/

if((typeof Prototype == 'undefined') ||
    (typeof Ajax.Modal != 'function')) {
    throw('Ajax.CMS vereist Prototype & Ajax.Modal!');
}

var aModal; 

Ajax.CMS = Class.create();
Ajax.CMS.prototype = {
    initialize: function() {
        this.options = Object.extend({
            ajaxFile: 'backbase/php/page.php',
            authFile: 'session',
            enableBut: null,
            modalKey: 119 
            /* keyCode van JS (te vinden op: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx)
               nu in gesteld op F8 (das 119) wil je een andere change het dan hier! */
        }, arguments[0] || {} );
        
        this.bindKey();
    },
    
    bindKey: function() {
        Event.observe(document, 'keydown', this.bindKeyListener.bind(this), false);
    },
    
    bindKeyListener: function(e) {
        if (e == null) { // IE
            this.keycode = event.keyCode;
        } else { // Mozilla
            this.keycode = e.keyCode;
        }
        
        this.key = String.fromCharCode(this.keycode).toLowerCase();
        
        if((this.key == 'f8') || (this.keycode == this.options.modalKey)) {
            this.initCheckAuth();
        }
        
    },
    
    initCheckAuth: function() {
        if(!$('mcEditButImg_0')) {
            this.aOptions = {
                asynchronous: false,
                method: 'get',
                parameters: '?pa=' + this.options.authFile,
                onSuccess: this.parseCheckAuth.bind(this)
            }
            
            this.options.enableBut = null;
            
            new Ajax.Request(this.options.ajaxFile, this.aOptions);
        }
    },
    
    parseCheckAuth: function(t) {
        this.response = t.responseText;
        
        if(this.response == 0) {
            aModal = new Ajax.Modal('270', '115');
        } else {
            this.enableButtons();
        }
    },
    
    checkAuth: function(fName, url) {
        this.url = url;
        this.formName = fName;
        this.formSerial = $(this.formName).serialize(); 
        
        this.options = {
            asynchronous: false,
            method: 'post',
            parameters: this.formSerial,
            onSuccess: this.validateAuth.bind(this)
        }
        
        new Ajax.Request(this.url, this.options);
    },
    
    validateAuth: function(t) {
        this.rText = t.responseText;
        this.mMiddle = $('modalMiddle');
        
        if(this.rText == 0 || this.rText == '') {
            this.formElems = $(this.formName).getElementsByTagName('input');
            
            for(var i = 0; i < this.formElems.length; i++) {
                if(this.formElems[i].type == 'text' || this.formElems[i].type == 'password') {
                    this.formElems[i].style.cssText = 'background-color: red; border-color: #ABADB3; border-width: 1px';
                }
            }
        } else if(this.rText == 1) {
            this.enableButtons();
        } else {
            // niets doen
        }
    },
    
    enableButtons: function() {        
        if(this.options.enableBut != 1) {
            if($('modalWrapper')) {
                new Effect.Fade($('mCloseBut').parentNode.parentNode.id, { duration: 0.5 });
                new Effect.Fade('modalOverlay', { queue: 'end', duration: 0.3, afterFinish: function() { 
                    Element.remove('modalOverlay');
                    Element.remove('modalWrapper'); } 
                });
            }
            
            this.newBut = $('mcNewBut');
            this.editBut = (($_GET[0] == 'portfolio') ? document.getElementsByClassName('mcEditButP') : document.getElementsByClassName('mcEditBut'));
            this.editButLink = document.getElementsByTagName('small');
            
            if(this.newBut) {
                this.newButton = document.createElement('img');
                this.newButton.setAttribute('id', 'mcNewButImg');
                this.newButton.setAttribute('src', 'backbase/images/layout/button_new.png');
                this.newButton.setAttribute('cat', $_GET[0]);
                this.newButton.style.cssText = 'display:none; cursor:pointer';
            
                this.newBut.appendChild(this.newButton);
            }
            
            for(var i = 0; i < this.editBut.length; i++) {
                this.editButton = document.createElement('img');
                this.editButton.setAttribute('id', 'mcEditButImg_' + i);
                this.editButton.setAttribute('src', 'backbase/images/layout/button_edit.png');
                this.editButton.setAttribute('muid', (($_GET[0] == 'portfolio') ? $_GET[2] : this.editBut[i].parentNode.parentNode.id.replace('mcWrapper_', '')));
                this.editButton.style.cssText = 'display:none; cursor:pointer';
                
                $(this.editBut[i]).appendChild(this.editButton);
                new Effect.Appear(this.editButton, { duration: 0.3 });
            }
            
            new Effect.Appear(this.newButton, { duration: 0.3 });
            this.options.enableBut = 1;
            
            this.observeButtons();
        }
    },
    
    observeButtons: function() {
        this.editBut = (($_GET[0] == 'portfolio') ? document.getElementsByClassName('mcEditButP') : document.getElementsByClassName('mcEditBut'));
        
        Event.observe(this.newButton, 'click', function() { 
            new Ajax.Modal('400', '270', 'Nieuw bericht :: ' + $_GET[0].ucFirst(), 'backbase/php/admin.php', '?pa=new_entry&cat=' + $_GET[0], true);
        }, false);
        
        // Portfolio...
        if($_GET[0] == 'portfolio') {
            for(var i = 0; i < this.editBut.length; i++) {
                Event.observe(this.editBut[i], 'click', function(evt) {
                    this.editID = $_GET[2];
                    
                    this.editFT = Event.element(evt).id; this.editNum = $(this.editFT).readAttribute('muid'); this.editST = $('mcPortfolioEntryTop').firstChild.innerHTML;
                    
                    if(this.editST.match('3D :: ')) {
                        this.editTitle = this.editST.replace('3D :: ', '');
                    } else if(this.editST.match('Beeld :: ')) {
                        this.editTitle = this.editST.replace('Beeld :: ', '');
                    } else if(this.editST.match('Website :: ')) {
                        this.editTitle = this.editST.replace('Website :: ', '');
                    }
                 
                    new Ajax.Modal('400', '360', 'Bewerken :: ' + this.editTitle, 'backbase/php/admin.php', '?pa=edit_entry&cat=' + $_GET[0] + '&id=' + this.editID , true);
                }, false);
            }
        } else {
            for(var i = 0; i < this.editBut.length; i++) {
                Event.observe(this.editBut[i], 'click', function(evt) {
                    this.editID = $(Event.element(evt).id).readAttribute('muid');
                    
                    this.editFT = Event.element(evt).id; this.editNum = $(this.editFT).readAttribute('muid'); this.editST = $('mcTop_' + this.editNum).firstChild.innerHTML;
                    this.editTitle = ((this.editST.match('Nieuws :: ') ? this.editST.replace('Nieuws :: ', '') : this.editST.replace('Tutorials :: ', '')));
                 
                    new Ajax.Modal('400', '350', 'Bewerken :: ' + this.editTitle, 'backbase/php/admin.php', '?pa=edit_entry&cat=' + $_GET[0] + '&id=' + this.editID , true);
                }, false);
            }
        }
    }
}