//Opactity class
function Opacie() {
	//methods
	this.AddObject = mtdAddObject;
	this.FadeUp = mtdFadeUp;
	this.FadeDown = mtdFadeDown;
	this.FadeOutAll = mtdFadeOutAll;
	this.FadeUpAll = mtdFadeUpAll;
	this.JumpUp = mtdJumpUp;
	this.Start = mtdStart;
	this.Stop = mtdStop;
	this.CheckUpdate = mtdCheckUpdate;

	//properties
	this.Objects = new Array;
	this.MaxJumpOpacity = 100;
	this.MaxFadeOpacity = 80;
	this.MinFadeOpacity = 0;
	this.FadeSpeed = 50;
	this.FadeStep = 10;
	
	//private
	var blnRunning = false;
	var intJumpedObject;
	
	//Methods
	
	//Adds an object to the stored list
	// id corresponds to an object id in the DOm
	function mtdAddObject(id) {
		var obj;
		obj = document.getElementById(id);
		if (obj) {
			this.Objects[this.Objects.length] = obj;
			setOpacity(obj, this.MinFadeOpacity)
		}
	}
	
	//Fades up an object
	// id corresponds to an object id in the DOm that's already been added
	function mtdFadeUp(id) {
		var obj;
		for (var i=0; i<this.Objects.length; i++) {
			obj = this.Objects[i];
			if (obj.id == id || i == intJumpedObject) {
				obj.opaciUp = true;
			} else {
				obj.opaciUp = false;
			}
		}
		this.CheckUpdate();
	}
	
	//Fades down an object
	// id corresponds to an object id in the DOm that's already been added
	function mtdFadeDown(id) {
		var obj;
		for (var i=0; i<this.Objects.length; i++) {
			obj = this.Objects[i];
			if (obj.id == id || i == intJumpedObject) {
				obj.opaciUp = false;
			} else {
				obj.opaciUp = true;
			}
		}
		this.CheckUpdate();
	}
	
	//Fades out all objects appart from the jumped one
	function mtdFadeOutAll() {
		this.FadeUp(null);	//we reuse Fadeup here, just easier
	}
	
	//Fades up all objects appart from the jumped one
	function mtdFadeUpAll() {
		this.FadeDown(null);	//we reuse FadeDown here, just easier
	}
	
	//Jumps the fading all the way up
	function mtdJumpUp(id) {
		var obj;
		for (var i=0; i<this.Objects.length; i++) {
			obj = this.Objects[i];
			if (obj.id == id) {
				intJumpedObject = i;
				setOpacity(obj, this.MaxJumpOpacity);
				obj.blur;	//stops line around image maps etc.
			} else {
				obj.opaciUp = false;
			}
		}
		this.CheckUpdate();
	}
	
	//Initializes everything
	function mtdStart() {
		blnRunning = true;
		pauseObjectMethod(this, this.CheckUpdate, this.FadeSpeed);
	}
	
	//Stops all fading etc
	function mtdStop() {
		blnRunning = false;
	}
	
	//Continues to update the fades etc
	//Runs through the map and adjust opacity in the correct direction based on the objImg.opaciUp value
	function mtdCheckUpdate() {
		var blnChanged = false;
		if (blnRunning) {
			var o;
			for (var i=0; i<this.Objects.length; i++) {
				obj = this.Objects[i];
				if (obj.opaciUp) {			//Going Up
					o = getOpacity(obj);
					if (o<this.MaxFadeOpacity) {
						setOpacity(obj, o+this.FadeStep);
						blnChanged = true;
					} else {
						setOpacity(obj, this.MaxFadeOpacity);
					}
				} else {			//Going Down
					o = getOpacity(obj);
					if (o>this.MinFadeOpacity) {
						setOpacity(obj, o-this.FadeStep);
						blnChanged = true;
					} else {
						setOpacity(obj, this.MinFadeOpacity);
					}
				}
			}
			if (blnChanged) {
				pauseObjectMethod(this, this.CheckUpdate, this.FadeSpeed);	//keep calling to continue checking
			}
		}
	}
	
	//Private functions
		
	//Sets opacity of an object to the percentage value
	function setOpacity(obj, percent) {
		try {
			if (obj.filters) {
				if (obj.filters.length == 0) {	//need to add filter first if it's not there
					obj.style.filter = 'alpha(opacity=0)';
				}
				obj.filters.alpha.opacity = percent;
			}
			obj.style.opacity = percent / 100;
		} catch (e) {
			//ignore
		}
		//also cahnge display to be totally sure
		if (percent == 0) {
			obj.style.visibility = 'hidden';
		} else {
			obj.style.visibility = 'visible';
		}
	}
	
	//Returns the opacity of an object as a percentage
	function getOpacity(obj) {
		try {
			if (obj.filters) {
				return parseInt(obj.filters.alpha.opacity);
			} else {
				return parseInt(obj.style.opacity * 100);
			}
		} catch (e) {
			return 0;
		}
	}
		

}
