//Created by Elliott Hall 28/2/2006

function Frameset(frames,name,id,moreParams){
    this.frames=frames;
    this.name=name;
    this.id=id;
    this.moreParams=moreParams;
    this.lockRows=false;
    this.lockCols=false;
   
}



Frameset.prototype.write=function(){
    document.write(this.toHTML());
}
Frameset.prototype.toggleFrame=function(frameName){
    var frame=this.findFrame(frameName);
    if (frame){
     (frame.visible==true)?frame.setInvisible(): frame.setVisible();
     this.refreshFrameset();
    }
}

Frameset.prototype.showFrame=function(frameName){
    var frame=this.findFrame(frameName);
    if (frame){
     if (frame.visible==false){
         frame.setVisible();
         this.refreshFrameset();
     }
    }
}

Frameset.prototype.hideFrame=function(frameName){
     var frame=this.findFrame(frameName);
    if (frame){
     if (frame.visible==true){
         frame.setInvisible();         
         this.refreshFrameset();         
     }
    }
}

Frameset.prototype.findFrame=function(frameName){
    for (var x=0;x<this.frames.length;x++){
        if (this.frames[x]){
            if (this.frames[x].frames){
                //Nested Frameset
                var nested=this.frames[x].findFrame(frameName);
                if (nested){
                    return nested;
                }    
            }
            if (this.frames[x].name==frameName){
                return this.frames[x];
            }
        }
    } 
    return "";
}

Frameset.prototype.refreshFrameset=function(){
    document.getElementById(this.id).setAttribute('cols', this.buildCols());
    document.getElementById(this.id).setAttribute('rows', this.buildRows());
    this.onChange();
}

Frameset.prototype.onChange=function(){}
    
Frameset.prototype.getHeight=function(){
    return '*';
}



Frameset.prototype.buildRows=function(){
    if (this.lockRows){
        return '*';
    }
    var rows=''
    for (var x=0;x<this.frames.length;x++){
            //Add the frames;
            if (this.frames[x]){
                rows+= (x==0) ? this.frames[x].getHeight() : ','+this.frames[x].getHeight();                
            }
        }
     return rows;
}

Frameset.prototype.buildCols=function(){
    if (this.lockCols){
        return '*';
    }
    var cols=''
    for (var x=0;x<this.frames.length;x++){
            //Add the frames;
            if (this.frames[x]){
                cols+= (x==0) ? this.frames[x].getWidth() : ','+this.frames[x].getWidth();          
            }
        }
     return cols;
}

Frameset.prototype.getWidth=function(){
    return '*';
}

Frameset.prototype.toHTML=function(){
    var rows=(this.lockRows)?'*':'';
    var cols=(this.lockCols)?'*':'';
    var frameCode = '';
        for (var x=0;x<this.frames.length;x++){
            //Add the frames;
            if (this.frames[x]){
                if (!this.lockRows){
                    rows+= (x==0) ? this.frames[x].getHeight() : ','+this.frames[x].getHeight();
                }
                if (!this.lockCols){
                    cols+= (x==0) ? this.frames[x].getWidth() : ','+this.frames[x].getWidth();
                }
                frameCode +=this.frames[x].toHTML();
            }
        }
    frameCode='<frameset rows="'+rows+'" cols="'+cols+'" name="'+this.name+'" id="'+this.id+'" '+this.moreParams+' >'+frameCode;
    frameCode += '</frameset>';
    return frameCode;
}

function Frame(name,id,rows,cols,src,params,visible){
    this.name=name;
    this.id=id;
    this.rows=rows;
    this.cols=cols;
    this.src=src;
    this.params=params;
    this.visible=visible;
}

Frame.prototype.setGreedy=function(g){
    this.greedy=g;
}

//Abstract classes for triggering events based on frame state changes.
Frame.prototype.onVisible=function(){
    this.onChange();
}

Frame.prototype.onInvisible=function(){
    this.onChange();
}

Frame.prototype.onResize=function(){
    this.onChange();
}

Frame.prototype.onChange=function(){
}

Frame.prototype.setVisible=function(){
    this.visible=true;    
    this.onVisible();
}

Frame.prototype.setInvisible=function(){
    this.visible=false;
    this.onInvisible();
}


Frame.prototype.getHeight=function(){
    var row=(this.rows<0)?'*':this.rows;
    return (this.visible) ? row : 0;
}

Frame.prototype.getWidth=function(){
    var col=((this.cols<0)||(this.greedy))?'*':this.cols;

    return (this.visible) ? col : 0;
}

Frame.prototype.toHTML=function(){
    return '<frame name="'+this.name+'" src="'+this.src+'" id="'+this.id+'" '+this.params+'>';
}



