function changeView( sel ) {
	var form = document.getElementById( 'cms-left-panel-form' );
 	form.action = 'http://www.airfacts.com/component,8/action,change_view/';
	
	form.submit();
}

function postForm( id ) {
	var form = document.getElementById( id );
	
	if ((typeof(manager) != 'undefined') && $('module_model')) {
	   manager.save('module_model');
	}
		
	form.submit();
}

function setType( to ) {
	document.getElementById( 'type' ).value = to;
}

function reorder( id, direction ) {
  var url = 'http://www.airfacts.com/component,8/action,reorder/';
  var form = document.getElementById( 'cms-left-panel-form' );
  var option = document.getElementById( 'option' );
  var option2 = document.getElementById( 'option2' );

  form.action = url;
  option.value = id;
  option2.value = direction;
  form.submit();
}

/*
 * below from http://dojotoolkit.org/pipermail/dojo-interest/2006-September/017064.html
 * modified
 */
function addTinyMCE( id ) {
    tinyMCE.execCommand( 'mceAddControl', true, id );
}

/* toolbar */

/**
 * A generic tool bar that is fairly easy to manipulate in terms of items and rendering.
 * 
 * @title Generic Tool Bar
 */

/**
 * ToolBarItem constructor
 * 
 * @class ToolBarItem
 * @constructor
 */
function ToolBarItem(o) {
    this.label = (o && o.label) ? o.label : '';
    this.onclick = (o && o.onclick) ? o.onclick : '';
    this.scope = (o && o.scope) ? o.scope : null;
}

ToolBarItem.prototype.label = '';
ToolBarItem.prototype.onclick = '';
ToolBarItem.prototype.scope = null; 

/**
 * ToolBar constructor
 * 
 * @class ToolBar
 * @constructor
 */
function ToolBar(t) {
    this._el = document.createElement('div');
    this._el.className = 'toolbar';
    
    this.target = t;
    this.states = [];
    this._isAdded = false;
    this.currentState = 'default';
}

ToolBar.prototype._el = null;
ToolBar.prototype._isAdded = false;

ToolBar.prototype.states = [];
ToolBar.prototype.currentState = 'default';

ToolBar.prototype.target = null;

ToolBar.prototype.getEl = function() {
    return this._el;
};

ToolBar.prototype.add = function(item, state) {
    if (!state) {
        state = 'default';
    }
    
    if (!this.states[state]) {
        this.states[state] = [];
    }
    
    this.states[state][this.states[state].length] = item;
};

ToolBar.prototype.remove = function(item, state) {
    if (!state) {
        state = 'default';
    }
    
    if (!this.states[state]) {
        this.states[state] = [];
    }
    
    var items = this.states[state];
    var newItems = [];
    
    for (var i = 0; i < items.length; i++) {
        if (item != items[i]) {
            newItems[newItems.length] = item;
        }       
    }   
    
    this.states[state] = newItems;
};

ToolBar.prototype.setState = function(s) {
    this.currentState = s;
    this.render(this.currentState);
};

ToolBar.prototype.render = function(state) {
    if (!state) {
        state = this.currentState;
    }
    else {
        this.currentState = state;
    }
    
    if (!this.target) {
        throw "the target property is not set!";
    }
    
    /*
     * clear the toolbar for re-rendering
     */
    this._el.innerHTML = '';
        
    var items = this.states[state];   
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        
        var dom = document.createElement('a');
        YAHOO.util.Event.on(dom, 'click', item.onclick, item.scope, true);
        
        dom.href = '#';
        dom.appendChild(document.createTextNode(item.label));
        
        this._el.appendChild(dom);
    }
    
    if (this._isAdded) {
        return;
    }

    this.target.appendChild(this._el);    
    this._isAdded = true;
};

/* modules */

function CMSModuleManager() {
    this.categories = [];
}

CMSModuleManager.pool = [];
CMSModuleManager.cats = [];

CMSModuleManager.prototype.categories = [];

CMSModuleManager.prototype.save = function(to) {
    for (var i = 0; i < this.categories.length; i++) {
        this.categories[i].save();
    }

    $(to).value = this.categories.toJSON();
};

CMSModuleManager.prototype.render = function(t) {
    var target = $(t);   
    
    var domCategories = $$('#module-manager ul li.first');  
    for (var i = 0; i < domCategories.length; i++) {           
        var category = new CMSModuleCategory(domCategories[i]);
        this.categories[this.categories.length] = category; 
    }
};

function CMSModuleCategory(el) {
    this.modules = [];    
    
    this.id = el.id.replace(/^.+[_]/, '');
    this.el = el;    
        
    var toolbar = new ToolBar(this.el);
    toolbar.add(new ToolBarItem({'label' : 'add module', 'onclick' : this.onAdd, 'scope' : this}));        
    toolbar.add(new ToolBarItem({'label' : 'unlink module', 'onclick' : this.onDelete, 'scope' : this}));        
    toolbar.add(new ToolBarItem({'label' : 'move up', 'onclick' : this.onMoveUp, 'scope' : this}));
    toolbar.add(new ToolBarItem({'label' : 'move down', 'onclick' : this.onMoveDown, 'scope' : this}));    
    toolbar.render(); 
    
    var domModules = YAHOO.util.Selector.query('div.replace', this.el);
    for (var i = 0; i < domModules.length; i++) {
        var domModule = domModules[i];
                  
        var module = this.onAdd();
        module.cmsModuleId = domModule.id.replace(/^.+[_]/, '');
        module.cmsModuleCategoryId = this.id;
	    module.select();
	    
	    this.el.removeChild(domModule);
    }
}

CMSModuleCategory.prototype.id = '';
CMSModuleCategory.prototype.context = null;
CMSModuleCategory.prototype.el = null;
CMSModuleCategory.prototype.modules = [];

CMSModuleCategory.prototype.sortModules = function() {
    this.modules.sort(function(a, b) {
        if (a.order < b.order) {
            return -1;
        }
        
        if (a.order > b.order) {
            return 1;
        }        
        
        return 0;
    });
};

CMSModuleCategory.prototype.onMoveUp = function() {
    var selection = this.getSelection();
    if (!selection) {
        alert('Please choose a module to move up');
        return;
    }       
        
    this.sortModules();
    for (var i = 0; i < this.modules.length; i++) {
        var module = this.modules[i];               
    
        if (selection == module) {
            if (i == 0) {
                return;
            }
            
            var target = this.modules[i - 1];
            this.el.removeChild(module.el);
            this.el.insertBefore(module.el, target.el);
            
            module.order--;
            target.order++;
        }
    }    
};

CMSModuleCategory.prototype.onMoveDown = function() {
    var selection = this.getSelection();
    if (!selection) {
        alert('Please choose a module to move down');
        return;
    }          
        
    this.sortModules();
    for (var i = 0; i < this.modules.length; i++) {
        var module = this.modules[i];               
    
        if (selection == module) {
            if ((i + 1) == this.modules.length) {
                return;
            }
            
            var target = this.modules[i + 1];
            this.el.removeChild(target.el);
            this.el.insertBefore(target.el, module.el);
            
            module.order++;
            target.order--;
        }
    }      
};

CMSModuleCategory.prototype.onAdd = function() {
    var module = new CMSModule({cmsModuleCategoryId:this.id});
    module.order = this.getNextOrder();
    module.el = document.createElement('div');
    module.el.innerHTML = module.getHTML();
    
    this.el.appendChild(module.el);
    
    return this.modules[this.modules.length] = module;    
};

CMSModuleCategory.prototype.onDelete = function() {
    var selection = this.getSelection();
    if (!selection) {
        alert('Please choose a module to unlink');
        return;
    }   
    
    var modules = [];
    for (var i = 0; i < this.modules.length; i++) {
        var module = this.modules[i];               
    
        if (selection == module) {
            this.el.removeChild(module.el);
        }
        else {
            modules[modules.length] = module;
        }
    }
    
    this.modules = modules;
};

CMSModuleCategory.prototype.getSelection = function() {
    var inputs = this.el.getElementsByTagName('input');
    var val = null;
    for (var i = 0; i < inputs.length; i++) {
        var tag = inputs[i];
                
        if (tag.type != 'radio') {
            continue;
        }
        
        if (!tag.name.match(/^cmsmoduleitem_radio_/)) {
            continue;
        }    
        
        if (tag.checked) {
            val = tag.value;
            break;
        }
    }
    
    if (!val) {
        return null;
    }
    
    for (var i = 0; i < this.modules.length; i++) {
        var module = this.modules[i];
        if (module.uid == val) {
            return module;
        }
    }    

    return null;
};

CMSModuleCategory.prototype.toString = function() {
    return 'CMSModuleCategory[' + this.id + ']';
};

CMSModuleCategory.prototype.save = function() {
    for (var i = 0; i < this.modules.length; i++) {
        this.modules[i].save();
    }
};

CMSModuleCategory.prototype.getNextOrder = function() {
    var highest = 0;
    
    for (var i = 0; i < this.modules.length; i++) {
        var module = this.modules[i]; 
        if (module.order > highest) {
            highest = module.order;
        }
    }
    
    return (highest + 1);
};

function CMSModule(o) {
    if (o) {
        this.id = o.id ? o.id : 0;
        this.name = o.name ? o.name : 0;
        this.description = o.description ? o.description : '{no description}';
        this.cmsModuleId = o.cmsModuleId ? o.cmsModuleId : 0;
        this.cmsModuleCategoryId = o.cmsModuleCategoryId ? o.cmsModuleCategoryId : 0;
        this.order = o.order ? o.order : 0;
    }
    
    this.uid = ++CMSModule.uniqid;    
}

CMSModule.uniqid = 0;

CMSModule.prototype.id = 0;
CMSModule.prototype.uid = 0;
CMSModule.prototype.name = '';
CMSModule.prototype.description = '';
CMSModule.prototype.cmsModuleId = 0;
CMSModule.prototype.cmsModuleCategoryId = 0;

CMSModule.prototype.el = null;

CMSModule.prototype.save = function() {    
    var type = $('cmsmoduleitem_type_' + this.uid);
    if (type) {
        this.cmsModuleId = type.options[type.options.selectedIndex].value;        
    }
};

CMSModule.prototype.select = function() {
    var type = $('cmsmoduleitem_type_' + this.uid);
    if (!type) {
        return;
    }   
    
   for (var i = 0; i < type.options.length; i++) {     
     if (type.options[i].value == this.cmsModuleId) {
       type.options[i].selected = true;
       return;
     }
   }    
};

CMSModule.prototype.getHTML = function() {
    var buf = ['<div class = "cmsmoduleitem">'];

    buf[buf.length] = '<input type = "radio" name = "cmsmoduleitem_radio_' + this.cmsModuleCategoryId + '" id = "cmsmoduleitem_radio_' + this.uid + '" value = "' + this.uid + '" /> ';    
    
    buf[buf.length] = '<select id = "cmsmoduleitem_type_' + this.uid + '">';
    buf[buf.length] = '<option value = "0">--none--</option>';
    
    for (var i = 0; i < CMSModuleManager.pool.length; i++) {
        var pi = CMSModuleManager.pool[i];
        
        buf[buf.length] = '<option value = "' + pi.cmsModuleId + '"'; //>
        
        if (pi.cmsModuleId == this.id) {
            buf[buf.length] = ' selected="selected"';
        }
        
        buf[buf.length] = '>' + pi.name + ' (' + pi.description + ')</option>';
    }    
    
    buf[buf.length] = '</select></div>';
    return buf.join('');
};