/* 
---------------------------------------------------------------
---------------------------------------------------------------
Graphic object
---------------------------------------------------------------
---------------------------------------------------------------
*/
function graphic(id) {
	if (id!=null) {
		this.id = id;
		eval(this.id + 'graphic=this;');
		
		this.element = menuBrowser.getDIVById(id);
		this.element.graphic = this;
		this.slideActive = false;
		this.glideActive = false;
		this.growActive = false;
	}
}
// Basic Methods
graphic.prototype.moveTo = graphicMoveTo
graphic.prototype.moveBy = graphicMoveBy
graphic.prototype.sizeTo = graphicSizeTo
graphic.prototype.sizeBy = graphicSizeBy
graphic.prototype.show = graphicShow
graphic.prototype.hide = graphicHide
graphic.prototype.setZIndex = graphicSetZIndex
graphic.prototype.setBackgroundColor = graphicSetBackgroundColor

// Slide Methods
graphic.prototype.slideTo = graphicSlideTo
graphic.prototype.slideBy = graphicSlideBy
graphic.prototype.slideStart = graphicSlideStart
graphic.prototype.slide = graphicSlide
graphic.prototype.onSlide = new Function()
graphic.prototype.onSlideStart = new Function()
graphic.prototype.onSlideEnd = new Function()
// Glide Methods
graphic.prototype.glideTo = graphicGlideTo
graphic.prototype.glideBy = graphicGlideBy
graphic.prototype.glideStart = graphicGlideStart
graphic.prototype.glide = graphicGlide
graphic.prototype.onGlide = new Function()
graphic.prototype.onGlideStart = new Function()
graphic.prototype.onGlideEnd = new Function()
//Grow Methods
graphic.prototype.growTo = graphicGrowTo
graphic.prototype.growBy = graphicGrowBy
graphic.prototype.growStart = graphicGrowStart
graphic.prototype.grow = graphicGrow
graphic.prototype.onGrow = new Function()
graphic.prototype.onGrowStart = new Function()
graphic.prototype.onGrowEnd = new Function()
graphic.prototype.destroy = graphicDestroy;


function graphicDestroy() {
//	alert("Removing graphic:" + this.id);

	if (this.element.graphic != null) {
		this.element.graphic = null;
	}


	if (this.element != null) {
		this.element = null;
	}

	eval( '' + this.id + 'graphic = null;');
	eval( 'delete ' + this.id + 'graphic;');


}



// Basic Methods
function graphicMoveTo(x,y) {
	if (x!=null) {
		this.x = x
		this.css.posLeft = this.x
	}
	if (y!=null) {
		this.y = y
		this.css.posTop = this.y
	}
}
function graphicMoveBy(x,y) {
	//var onlz;
	//oldz = this.css.zIndex;
	//this.css.zIndex = 99;
	this.moveTo(this.x+x,this.y+y);
	//this.css.zIndex = oldz;

}
function graphicShow() {
if ( navigator.appVersion.indexOf('MSIE 5.0')>0 ) {
	this.css.zIndex = 1;
	}
	this.css.visibility = "visible"
	//this.css.display = "inline"
}
function graphicHide() {
	//this.css.zIndex = 0;
	this.css.visibility = "hidden"
	//this.css.display = "none"
}

function graphicSizeTo(h,w) {
	//this.h = h;
	this.css.posHeight = h;
	//this.w = w;
	this.css.posWidth = w;
	
	this.h = this.element.offsetHeight;
	this.w = this.element.offsetWidth;
	
}
function graphicSizeBy(dh,dw) {
	this.sizeTo(this.h+dh,this.w+dw);
}
function graphicSetZIndex(zIndex) {
//debugger;
	this.element.zIndex = zIndex;
}
function graphicSetBackgroundColor(backgroundColor) {
	this.css.backgroundColor = backgroundColor;
}


// Slide Methods
function graphicSlideTo(endx,endy,jump,wait) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	this.slideStart(endx,endy,distx,disty,jump,wait)
}
function graphicSlideBy(distx,disty,jump,wait) {
	var endx = this.x + distx
	var endy = this.y + disty
	this.slideStart(endx,endy,distx,disty,jump,wait)
}
function graphicSlideStart(endx,endy,distx,disty,jump,wait) {
	if (this.slideActive) return
	if (!jump) jump = 10
	if (!wait) wait = 20
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/jump
	if (num==0) return
	var dx = distx/num
	var dy = disty/num
	this.slideActive = true
	this.onSlideStart(this)
	this.slide(dx,dy,endx,endy,num,1,wait)
}
function graphicSlide(dx,dy,endx,endy,num,i,wait) {
	if (!this.slideActive) return
	if (i++ < num) {
		this.moveBy(dx,dy)
		this.onSlide(this)
		if (this.slideActive) setTimeout(this.id + 'graphic.slide('+dx+','+dy+','+endx+','+endy+','+num+','+i+','+wait+')',wait)
		else this.onSlideEnd(this)
	}
	else {
		this.slideActive = false
		this.moveTo(endx,endy)
		this.onSlide(this)
		this.onSlideEnd(this)
	}
}


// Glide Methods
function graphicGlideTo(endx,endy,angleinc,wait) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	this.glideStart(endx,endy,distx,disty,angleinc,wait)
}
function graphicGlideBy(distx,disty,angleinc,wait) {
	var endx = this.x + distx
	var endy = this.y + disty
	this.glideStart(endx,endy,distx,disty,angleinc,wait)
}
function graphicGlideStart(endx,endy,distx,disty,angleinc,wait) {
	if (this.glideActive) return
	if (endx==this.x) var slantangle = 90
	else if (endy==this.y) var slantangle = 0
	else var slantangle = Math.abs(Math.atan(disty/distx)*180/Math.PI)
	if (endx>=this.x) {
		if (endy>this.y) slantangle = 360-slantangle
	}
	else {
		if (endy>this.y) slantangle = 180+slantangle
		else slantangle = 180-slantangle
	}
	slantangle *= Math.PI/180
	var amplitude = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))
	this.glideActive = true
	this.onGlideStart(this)
	this.glide(amplitude/2,-90,90,this.x+distx/2,this.y+disty/2,slantangle,endx,endy,distx,disty,angleinc,wait);
}
function graphicGlide(amplitude,angle,endangle,centerX,centerY,slantangle,endx,endy,distx,disty,angleinc,wait) {
	if (angle < endangle && this.glideActive) {
		angle += angleinc
		var u = amplitude*Math.sin(angle*Math.PI/180)
		var x = centerX + u*Math.cos(slantangle)
		var y = centerY - u*Math.sin(slantangle)
		this.moveTo(x,y)
		this.onGlide(this)
		if (this.glideActive) {
			setTimeout(this.id + 'graphic.glide('+amplitude+','+angle+','+endangle+','+centerX+','+centerY+','+slantangle+','+endx+','+endy+','+distx+','+disty+','+angleinc+','+wait+')',wait);
		}
		else {
			this.onGlideEnd(this);
		}
	}
	else {
		this.glideActive = false
		this.moveTo(endx,endy)
		this.onGlide(this)
		this.onGlideEnd(this)
	}
}



// Grow Methods
function graphicGrowTo(endh,endw,inc,wait) {
	if (endh==null) endh = this.h
	if (endw==null) endw = this.w
	var dh = endh-this.h
	var dw = endw-this.w
	this.growStart(endh,endw,dh,dw,inc,wait)
}
function graphicGrowBy(dh,dw,inc,wait) {
	var endh = this.h + dh
	var endw = this.w + dw
	this.growStart(endh,endw,dh,dw,inc,wait)
}
function graphicGrowStart(endh,endw,dh,dw,inc,wait) {
	if (this.growActive) return
	if (endh==this.h) var slantangle = 90
	else if (endw==this.w) var slantangle = 0
	else var slantangle = Math.abs(Math.atan(dw/dh)*180/Math.PI)
	if (endh>=this.h) {
		if (endw>this.w) slantangle = 360-slantangle
	}
	else {
		if (endw>this.w) slantangle = 180+slantangle
		else slantangle = 180-slantangle
	}
	slantangle *= Math.PI/180
	var amplitude = Math.sqrt(Math.pow(dh,2) + Math.pow(dw,2))
	this.growActive = true
	this.onGrowStart(this)
	this.grow(amplitude/2,-90,90,this.h+dh/2,this.w+dw/2,slantangle,endh,endw,dh,dw,inc,wait);
}
function graphicGrow(amplitude,angle,endangle,centerh,centerw,slantangle,endh,endw,dh,dw,inc,wait) {
	if (angle < endangle && this.growActive) {
		angle += inc
		var u = amplitude*Math.sin(angle*Math.PI/180)
		var h = centerh + u*Math.cos(slantangle)
		var w = centerw - u*Math.sin(slantangle)
		this.sizeTo(h,w)
		this.onGrow(this)
		if (this.growActive) {
			setTimeout(this.id + 'graphic.grow('+amplitude+','+angle+','+endangle+','+centerh+','+centerw+','+slantangle+','+endh+','+endw+','+dh+','+dw+','+inc+','+wait+')',wait);
		}
		else {
			this.onGrowEnd(this);
		}
	}
	else {
		this.growActive = false
		this.sizeTo(endh,endw)
		this.onGrow(this)
		this.onGrowEnd(this)
	}
}

/*
---------------------------------------------------------------
Graphic IE Subclass object
---------------------------------------------------------------
*/ function graphicIE(id) {
	this.base = graphic;
	this.base(id);
	if (id!=null) {
		this.css = this.element.style;
		this.x = this.css.posLeft;
		this.y = this.css.posTop;
		this.w = this.element.offsetWidth;
		this.h = this.element.offsetHeight;
	}
}
graphicIE.prototype = new graphic;


/*
---------------------------------------------------------------
Graphic IE5 Subclass object
---------------------------------------------------------------
*/ function graphicIE5(id) {
	this.base = graphicIE;
	this.base(id);
}
graphicIE5.prototype = new graphicIE;

/*
---------------------------------------------------------------
Graphic NS Subclass object
---------------------------------------------------------------
*/ function graphicNS(id) {
	this.base = graphic;
	this.base(id);
	if (id!=null) {
		this.css = this.element;
		this.x = this.css.left;
		this.y = this.css.top;
		this.w = this.css.clip.width;
		this.h = this.css.clip.height;
		//++ need to pass this in somehow
		//this.element.bgColor = 'lime';
	}
}
graphicNS.prototype = new graphic;

// Basic Methods
graphicNS.prototype.moveTo = graphicNSMoveTo
graphicNS.prototype.moveBy = graphicNSMoveBy
graphicNS.prototype.sizeTo = graphicNSSizeTo
graphicNS.prototype.sizeBy = graphicNSSizeBy
graphicNS.prototype.show = graphicNSShow
graphicNS.prototype.hide = graphicNSHide
graphicNS.prototype.setBackgroundColor = graphicNSSetBackgroundColor

// Basic Methods
function graphicNSMoveTo(x,y) {
	if (x!=null) {
		this.x = x
		this.css.left = this.x
	}
	if (y!=null) {
		this.y = y
		this.css.top = this.y
	}
}
function graphicNSMoveBy(x,y) {
	this.moveTo(this.x+x,this.y+y)
}
function graphicNSShow() {
	this.css.visibility = "visible"
	//this.css.display = "inline"
}
function graphicNSHide() {
	this.css.visibility = "hidden"
	//this.css.display = "none"
}

function graphicNSSizeTo(h,w) {
	this.h = h;
	this.w = w;
	this.css.resizeTo(w, h);
}
function graphicNSSizeBy(dh,dw) {
	this.sizeTo(this.h+dh,this.w+dw);
}
function graphicNSSetBackgroundColor(backgroundColor) {
	this.element.bgColor = backgroundColor;
}


