
function Collection() {
    this.els = new Array();
    this.length = 0;
}


Collection.prototype.contains = function(obj) {
    for (var i=0; i<this.els.length; i++){
        if (this.els[i] == obj) {
            return true;
        }
    }
    return false;
}

Collection.prototype.add = function(obj) {
    if (!this.contains(obj)) this.els.push(obj);
    this.length = this.els.length;
}

Collection.prototype.remove = function(obj) {
    for (var i=0; i<this.els.length; i++){
        if (this.els[i]== obj) {
            this.els.splice(i,1);
            break;
        }
    }
    this.length = this.els.length;
}

Collection.prototype.applyMethod = function(methodName,params) {
    for (var i=0; i<this.els.length; i++) {
        ((this.els[i])[methodName]).apply(this.els[i],params);
    }
}

Collection.prototype.applyFunc = function(func,params) {
    for (var i=0; i<this.els.length; i++) {
        func.apply(this.els[i],params);
    }
}

Collection.prototype.push = function(obj) {
    //this is just an alias for the add function
    this.add(obj);
}

Collection.prototype.obj = function(index) {
    return this.els[index];
}

Collection.prototype.clone = function() {
    var ret = new Collection();
    for (var i=0; i<this.els.length; i++) {
        ret.els[i] = this.els[i];
    }
    ret.length = this.length;
    return ret;
}

/////////////////////////////////////////////

function jEventHandler(obj,meth) {
    this.obj = obj;
    this.meth = meth;
}

jEventHandler.prototype.exec = function(ps) {
    if (typeof(ps)=="undefined") {
        var p = [];
    } else {
        var p = ps;
    }
    this.meth.apply(this.obj,p);
}

/////////////////////////////////////////////

function jEvent() {
    this.handlers = new Collection();
}

jEvent.prototype.addHandler = function(eventHandler) {
    this.handlers.add(eventHandler);
}

jEvent.prototype.removeHandler = function(eventHandler) {
    this.handlers.remove(eventHandler);
}

jEvent.prototype.fire = function(params) {
    if (this.handlers.length>0) {
        for (var i=0; i<this.handlers.length; i++) {
            this.handlers.obj(i).exec(params);
        }
    }
}

//////////////////////////////////////////////

function DynamicLayer(id) {
	if (arguments.length>0) {
    if (typeof(id)=="string") {
        this.id = id;
        this.el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
    } else {
        this.el = id;
        this.id = this.el.id;
    }
    if (this.el) this.el.css = this.el.style? this.el.style: this.el;
    this.layerNum = DynamicLayer.layers.length;
    DynamicLayer.layers[this.id] = this;
    this.x =  parseInt(this.el.css.left+"0");
    this.y =  parseInt(this.el.css.top+"0");
    this.onFinishAnim = new jEvent();
    this.onDrag = new jEvent();
    }
}

DynamicLayer.layers = new Array();

DynamicLayer.prototype.write = function(str) {
    if (typeof this.el.innerHTML!="undefined") {
        this.el.innerHTML = str;
    } else if (document.layers) {
	    this.el.document.write(str);
	    this.el.document.close();
    }
}

DynamicLayer.prototype.clear = function() {this.write("");}

DynamicLayer.prototype.setCol = function(c) {this.el.css.backgroundColor=c}

DynamicLayer.prototype.setBG = function(surl) {this.el.css.backgroundimage=surl}

DynamicLayer.prototype.moveTo = function(x,y) {
    this.x = x;
    this.y = y;
    var px = (document.layers || window.opera)? 0: "px";
    if (x != null) this.el.css.left = x + px;
    if (y != null) this.el.css.top = y + px;
}

DynamicLayer.prototype.setWidth = function(w) {
    var px = (document.layers || window.opera)? 0: "px";
    if (w != null) this.el.css.width = w + px;
}

DynamicLayer.prototype.setHeight = function(h) {
    var px = (document.layers || window.opera)? 0: "px";
    if (h != null && h>=0) this.el.css.height = h + px;
}

DynamicLayer.prototype.setVisible = function(bool) {
  if (bool) {
    this.el.css.display = "block";
  } else {
    this.el.css.display = "none";
  }
}

DynamicLayer.prototype.setClip = function(top, right, bottom, left) {
	if (document.getElementById || document.all)
	{
		var clipstring = 'rect('+top+'px,'+right+'px,'+bottom+'px, '+left+'px)';
		//clipstring = "rect(" + top + "px " + right + "px " + bottom + "px " + left + "px)";
		this.el.style.clip = clipstring;
	}
	else if (document.layers)
	{
		this.el.css.clip.top = top;
		this.el.css.clip.bottom = bottom;
        this.el.css.clip.right = right;
        this.el.css.clip.left = left;
	}
}

DynamicLayer.prototype.captureMouse = function() {
    if (is.ns4) this.el.captureEvents(Event.MOUSEDOWN);
    this.el.onmousedown = DynamicLayer.mouseDown;
    
    this.onMouseDown = new jEvent();
}

DynamicLayer.mouseDown = function() { 
    //this static function is called by the actual HTML element (DOM object)
    if (DynamicLayer.layers[this.id]) {
        var dl = DynamicLayer.layers[this.id];
        dl.mouseDown.apply(dl,[]);
        return false;
    }
    return true;
}

DynamicLayer.prototype.mouseDown = function() { //fires the onMouseDown event
    this.onMouseDown.fire([this]);
}

DynamicLayer.prototype.startDrag = function(l,r,t,b) {
	//creates a care-taker object called dragger, which handles the mouse event capturing and automatically shifts the layer's position.
	//when the drag stops, the dragger object is deleted, so all these extra variables don't hang around being useless, like dole bludgers, or hippies.
	this.stopDrag();
    this.dragger = {};
    if (typeof(l)!="undefined") this.dragger.l = l; else this.dragger.l=-1000000;
    if (typeof(r)!="undefined") this.dragger.r = r; else this.dragger.r=1000000;
    if (typeof(t)!="undefined") this.dragger.t = t; else this.dragger.t=-1000000;
	if (typeof(b)!="undefined") this.dragger.b = b; else this.dragger.b=1000000;
    this.dragger.mox = DynamicLayer.mouse.x;
    this.dragger.moy = DynamicLayer.mouse.y;
    this.dragger.ox = this.x;
    this.dragger.oy = this.y;
    this.dragger.dl = this;
    this.dragger.mouseMove = function(x,y) {
        dx = x-this.mox;
        dy = y-this.moy;
        if (dy<this.t) dy = this.t;
        if (dy>this.b) dy = this.b;
        if (dx<this.l) dx = this.l;
        if (dx>this.r) dx = this.r;
       //console.echo("("+dx+","+dy+")");
        this.dl.moveTo(this.ox+dx,this.oy+dy);
        this.dl.onDrag.fire([this.dl.x,this.dl.y]);
    }
    this.dragger.mouseHandler = new jEventHandler(this.dragger,this.dragger.mouseMove);
    DynamicLayer.onMouseMove.addHandler(this.dragger.mouseHandler);
    DynamicLayer.stopHighlight = true;
}

DynamicLayer.prototype.stopDrag = function() {
	if (this.dragger) {
    DynamicLayer.onMouseMove.removeHandler(this.dragger.mouseHandler);
    delete this.dragger;
    DynamicLayer.stopHighlight = false;
    }
}

// helper functions
function BrowserCheck() {
	var b = navigator.appName;
	if (b=="Netscape") this.b = "ns";
	else if (b=="Microsoft Internet Explorer") this.b = "ie";
	else this.b = b;
	this.version = navigator.appVersion;
	this.v = parseInt(this.version);
	this.ns = (this.b=="ns" && this.v>=4);
	this.ns4 = (this.b=="ns" && this.v==4);
	this.ns5 = (this.b=="ns" && this.v==5);
	this.ie = (this.b=="ie" && this.v>=4);
	this.ie4 = (this.version.indexOf('MSIE 4')>0);
	this.ie5 = (this.version.indexOf('MSIE 5')>0);
	this.min = (this.ns||this.ie);
	this.opera = (navigator.userAgent.indexOf('Opera') != -1);
}
is = new BrowserCheck();

screenDimensions = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  doResize: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
    this.onResize.fire([this.width,this.height]);
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  },
  
  doScroll: function() {
    this.getScrollX();  this.getScrollY();
    this.onScroll.fire([this.scrollX,this.scrollY]);
  },
  
  onResize: new jEvent(),
  onScroll: new jEvent()
}

//alert("hi");

window.onresize = function() { screenDimensions.doResize(); }
window.onscroll = function() { screenDimensions.doScroll(); }
