String.prototype.getControlNameById = function()
{
    return this.replace(/\_/g, "$");
}

function view(id, btn, btnSet, btnReset)
{
    this.id = id;
    this.name = this.id.getControlNameById();
    this.data = null;
    this.ctrl = null;
    this.btn = btn;
    this.btnSet = btnSet;
    this.btnReset = btnReset;
    this.ctrlObject = null;
    this.Init();
}

view.prototype.Init = function()
{
    this.ctrl = document.getElementById(this.id);
    this.data = cmsControlManager.FindFormObject(selectorName); // document.forms[0].item(selectorName);
}

view.prototype.OnSubmit = function()
{
}

view.prototype.OnLoad = function()
{
    this.Init();    
    
    var p = null;
    var temp = this.data.value.split('$');
    if(temp.length > 0)
        p = temp;
    else
        p = new Array(this.data.value);
        
    for(var c = 0; c < p.length; c++)
        if(p[c] == this.id)
        {
            this.ctrl.style.display = "block";
            if(this.btnSet != null && typeof(this.btnSet) != "undefined")
                this.btnSet(this.btn);
            return;
        }
    
    this.ctrl.style.display = "none";
    if(this.btnReset != null && typeof(this.btnReset) != "undefined")
                this.btnReset(this.btn);
}

view.prototype.Hide = function()
{
    this.ctrl.style.display = "none";
    if(this.btnReset != null && typeof(this.btnReset) != "undefined")
        this.btnReset(this.btn);
}

view.prototype.Show = function(panels)
{   
    var p = null;
    var temp = panels.split('$');
    if(temp.length > 0)
        p = temp;
    else    
        p = new Array(panels);
       
    var hide = true;
    for(var c = 0; c < controls.length; c++)
    {
//        for(var i = 0; i < p.length; i++)
//        {
//            if(controls[c].id == p[i])
//                hide = false;
//        }
        
        if(typeof(controls[c].Show) != "undefined" && hide == true)
        {
            controls[c].Hide();
            //controls[c].ctrl.style.display = "none";
        }
    }
    
    var selection = "";
    for(var x = 0; x < p.length; x++)
    {
        var obj = cmsControlManager.FindControlByServerId(p[x]);
        if(obj != null)
        {
            obj.ctrl.style.display = "block";
            selection += obj.id + "$";
            
             if(obj.btnSet != null && typeof(obj.btnSet) != "undefined")
                obj.btnSet(obj.btn);
        }
    }
    selection = selection.substring(0, selection.lastIndexOf('$'));
    this.data.value = selection;
}
//

function scroller(id)
{
    this.id = id;
    this.name = this.id.getControlNameById();
    this.data = null;
    this.ctrl = null;
    this.ctrlObject = null;
    this.Init();
}

scroller.prototype.Init = function()
{
    this.ctrl = document.getElementById(this.id);
    this.ctrlObject = document.getElementById(this.id + "_content");
    this.data = this.data = cmsControlManager.FindFormObject(this.name); //document.forms[0].item(this.name);
}

scroller.prototype.OnSubmit = function()
{
}

scroller.prototype.OnLoad = function()
{
    this.Init();
    this.ctrlObject.scrollLeft = this.data.value;
}

scroller.prototype.Scroll = function(dir, width)
{
    if(dir == "left")
    {
        this.ctrlObject.scrollLeft -= width;
    }
    else
    {
        this.ctrlObject.scrollLeft += width;
    }
    this.data.value = this.ctrlObject.scrollLeft;
}

function flashPlayer(id)
{
    this.id = id;
    this.name = this.id.getControlNameById();
    this.data = null;
    this.ctrl = null;
    this.ctrlObject = null;
    this.Init();
}

flashPlayer.prototype.Init = function()
{
    this.ctrl = document.getElementById(this.id);
    this.ctrlObject = document.getElementById(this.id + "_ctrl");
    this.data = this.data = cmsControlManager.FindFormObject(this.name);
    if(this.data == null)
        this.editMode = false;
    else
        this.editMode = true;
}

flashPlayer.prototype.OnSubmit = function()
{
}

flashPlayer.prototype.ExecCommand = new function(command)
{
}


function image(id)
{
    this.id = id;
    this.name = this.id.getControlNameById();
    this.data = null;
    this.ctrl = null;
    this.ctrlObject = null;
    this.Init();
}

image.prototype.Init = function()
{
    this.ctrl = document.getElementById(this.id);
    this.ctrlObject = document.getElementById(this.id + "_ctrl"); //this.ctrl.children[1];
    this.data = this.data = cmsControlManager.FindFormObject(this.name); //document.forms[0].item(this.name);

    if(this.data == null)
        this.editMode = false;
    else
        this.editMode = true;
}

image.prototype.OnSubmit = function()
{
    if(this.data != null)
    {
        this.data.value = this.ctrlObject.src;
    }
}

image.prototype.ExecCommand = new function(command)
{
}

var textRange = null;

function textArea(id)
{
    //alert(id);
    this.id = id;
    this.name = this.id.getControlNameById();
    this.editArea = null;
    this.data = null;
    this.ctrl = null;
    this.ctrlObject = null;
    this.Init();
}

textArea.prototype.Init = function()
{
    this.ctrl = document.getElementById(this.id);
    this.ctrlObject = this.ctrl;
    this.editArea = document.getElementById(this.id + "_editArea");
    this.data = this.data = cmsControlManager.FindFormObject(this.name); //document.forms[0].item(this.name);
    if(this.data == null)
        this.editMode = false;
    else
    {
        this.editMode = true;
        this.editArea.onselect = this.CacheSelection;
        this.editArea.onkeyup = this.CacheSelection;
        this.editArea.onmouseup = this.CacheSelection;
    }
}

textArea.prototype.OnSubmit = function()
{
    if(this.editArea != null)
    {
        this.data.value = this.editArea.innerHTML;
    }
}

textArea.prototype.ExecCommand = function(command)
{
    if(textRange != null)
    {
        textRange.select();  
        if(command == "CreateLink")
            textRange.execCommand(command, true, "");
        else
            textRange.execCommand(command);      
        textRange.select();
    }
}

textArea.prototype.CacheSelection = function(ev)
{
    textRange = document.selection.createRange();   
}

var controls = new Array();

function cmsControlManager()
{
    window.onload = this.OnLoad;      
    this.virtualPath = "/myCF/"; 
    this.fileSystemBrowser = "DialogFrame.htm?content=NetFSManager.aspx";
}

cmsControlManager.prototype.OnLoad = function()
{
    for(c = 0; c < controls.length; c++)
    {
        if(typeof(controls[c].OnLoad) != "undefined")
            controls[c].OnLoad();
    }
}

cmsControlManager.prototype.OnSubmit = function()
{
    for(c = 0; c < controls.length; c++)
        controls[c].OnSubmit();
}

cmsControlManager.prototype.AddControl = function(ctrl)
{
    if(this.FindControlById(ctrl.id) == null)
    {
        controls.push(ctrl);
    }
}

cmsControlManager.prototype.FindFormObject = function(name)
{
    var el = document.forms[0].elements;
    for(var x = 0; x < el.length; x++)
        if(el[x].name == name)
            return el[x];
    return null;
}

cmsControlManager.prototype.FindControlById = function(id)
{
    for(var c = 0; c < controls.length; c++)
    {
        if(controls[c].id == id)
            return controls[c];
    }
            
    return null;
}

cmsControlManager.prototype.FindControlByServerId = function(id)
{
    for(var c = 0; c < controls.length; c++)
    {
        var p = controls[c].id.split('_');
        if(p.length > 0)
        {            
            if(p[p.length - 1] == id)
            {
                return controls[c];
            }
        }
        else
        {
            if(controls[c].id == id)
                return controls[c];
        }
    }
            
    return null;
}

cmsControlManager.prototype.ToolBarCommand = function(id, command)
{
    var ctrl = this.FindControlById(id);
    if(ctrl != null)
        ctrl.ExecCommand(command);
}

cmsControlManager.prototype.PagePreview = function()
{
    window.open(document.location.href);
}

cmsControlManager.prototype.OpenWindow = function(url, name, width, height)
{
    var prop = "width=" + width + ", height=" + height + ", toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no";
    window.open(url, name, prop);
}

cmsControlManager.prototype.OpenFileSystemBrowser = function(id, propertyName, formData)
{
    var obj = this.FindControlById(id);
    if(obj != null && obj.ctrlObject != null)
        obj = obj.ctrlObject;
 
    if(obj == null)
        obj = document.getElementById(id);
 
    if(obj != null)
    {   
        var dialogArgs = new Object();      
        
        if(formData)
        {
            var data = this.FindFormObject(obj.name);
            dialogArgs.obj = data;
            dialogArgs.propertyName = "value";
        }
        else
        {
            dialogArgs.obj = obj;
            dialogArgs.propertyName = propertyName;        
        }
        dialogArgs.cancel = false;
        
        //window.showModalDialog(this.rootAbsoluteUrl + this.fileSystemBrowser, dialogArgs, "scroll:no; dialogHeight:400px; dialogWidth:600px; dialogLeft:200px;"); 
    
        window.showModalDialog(this.virtualPath + this.fileSystemBrowser, dialogArgs, "scroll:no; dialogHeight:400px; dialogWidth:600px; dialogLeft:200px;"); 
        return dialogArgs.cancel;
    }
    return false;
}

var cmsControlManager = new cmsControlManager();

