﻿var DisplayController = function(ctrltoupdate, showtext, hidetext) {
    var _this = this;
    var _controls = new Array();
    var _visible = false;
    var _ctltoupdate = ctrltoupdate;
    var _showtext = showtext;
    var _hidetext = hidetext;
    
    this.Visible = function(){
        return _visible;
    };    
    
    this.AddControl = function(id, displaystyle){
        displaystyle = (typeof displaystyle == 'undefined')? 'block' : displaystyle;
        var ctl = new ControlInfo(id,displaystyle);
        _controls[_controls.length] = ctl;    
    };
    
    this.SetInitialView = function(){
        try {
            if (readCookie('dispctrl') == '1') {
                _this.Show();
            }
            else {
                _this.Hide();
            }
        }
        catch(ex){}
    };
    
    this.Toggle = function(){
        if (_visible == true){
            _this.Hide();
        }
        else {
            _this.Show();
        }
        return false;
    };
    
    this.Show = function(){
        for(var i = 0; i<_controls.length; i++){
            var ctlinfo = new ControlInfo();
            ctlinfo = _controls[i];
            var ctl = document.getElementById(ctlinfo.Id());
            if (ctl) {
                ctl.style.display = ctlinfo.DisplayStyle();
            }
        }
        
        _visible = true;
        
        var ct = document.getElementById(_ctltoupdate);
        if (ct) {
            ct.innerHTML = _hidetext;
        }
        
        createCookie('dispctrl','1',30);
    };
    
    this.Hide = function() {
        for(var i = 0; i<_controls.length; i++){
            var ctlinfo = new ControlInfo();
            ctlinfo = _controls[i];
            var ctl = document.getElementById(ctlinfo.Id());
            if (ctl) {
                ctl.style.display = 'none';
            }
        }
        
        _visible = false;
        
        var ct = document.getElementById(_ctltoupdate);
        if (ct) {
            ct.innerHTML = _showtext;
        }
        
        eraseCookie('dispctrl');
    };

};

var ControlInfo = function(id, displaystyle) {
    var _this = this;
    var _id = id;
    var _displaystyle = (typeof displaystyle == 'undefined')? 'block' : displaystyle;
    
    this.Id = function(arg){
        if (typeof arg == 'undefined'){
            return _id;
        }
        else {
            _id = arg;
        }
    };
    
    this.DisplayStyle = function(arg){
        if (typeof arg == 'undefined'){
            return _displaystyle;
        }
        else {
            _displaystyle = arg;
        }
    };
};
