/* ImageMorph function by Greg Brown, www.ibdg.co.nz */
/* coordinate functions from quirksmode.org */

function ImageMorph(mn,p){
	this.ip = p ? p : '';
	this.mn = mn;
	this.inc = 0.04;
	this.int = 50;
	this.il = new Array();
	this.imageList = this.il;
	this.ni = 0;
	this.hih = null;
};

ImageMorph.prototype.init = function(imgId,capId){
	this.img = document.getElementById(imgId);
	this.cap = document.getElementById(capId);
};

ImageMorph.prototype.setHorridIeHack = function(x,y){
	this.hih = new Object();
	this.hih.x = x;
	this.hih.y = y;
};

ImageMorph.prototype.setHorridHack = function(x,y){
	this.hgh = new Object();
	this.hgh.x = x;
	this.hgh.y = y;
};

ImageMorph.prototype.setAnimSpeed = function(inc,int){
	if (inc) this.inc = inc;
	if (int) this.int = int;
};


ImageMorph.prototype.switchImage = function(id){
	if (id == 'previous') this.ni--;
	else if (!id || id == 'next')	this.ni++;
	else this.ni = id;
	if (this.ni >= this.il.length) this.ni = 0;
	if (this.ni < 0) this.ni = this.il.length - 1;

	//preload before calling fade script:
	this.temp = new Image();
	this.temp.onload = new Function(this.mn+'.morph();');
	img = this.isArray(this.il[this.ni]) ? this.il[this.ni][0] : this.il[this.ni];
	//alert(this.isArray(this.il[this.ni]));
	this.temp.src = this.ip + img;
};


ImageMorph.prototype.morph = function(){
	//alert(img.parentNode);
	if (!this.ol){
		this.makeOverlay();
		ghost_addEvent(window,'resize',new Function(this.mn+'.posOverlay()'));
	}
	this.ol.style.opacity = 1;
	this.ol.style.filter = 'alpha(opacity=100)';
	this.ol.src = this.img.src;
	setTimeout(new Function(this.mn+'.startFade();'),500);	
};

ImageMorph.prototype.makeOverlay = function(){
	if (document.getElementById('ghost_dom_body'))
		this.ol = document.getElementById('ghost_dom_body').appendChild(document.createElement('img'));
	else this.ol = document.body.appendChild(document.createElement('img'));
	
	this.ol.style.visibility = 'hidden';
	this.ol.style.position = 'absolute';
	this.ol.style.zIndex = 100;
	this.ol.style.height = this.img.style.height ? this.img.style.height + 'px' : (this.img.height ? this.img.height + 'px' : 'auto');
	this.ol.style.width = this.img.style.width ? this.img.style.width + 'px' : (this.img.width ? this.img.width + 'px' : 'auto');
	this.posOverlay();	
	this.ol.style.visibility = 'visible';
};
ImageMorph.prototype.posOverlay = function(){
	if (this.ol){
		this.ol.style.top = this.fpy(this.img) + 'px';	
		this.ol.style.left = this.fpx(this.img) + 'px';
	}
};

ImageMorph.prototype.startFade = function(newSrc){
	// alert(this.img);

	// alert(this.img.src);
	this.img.style.opacity = 0;
	this.img.style.filter = 'alpha(opacity=0)';
	//this.img.src = this.ip + this.il[this.ni];
	//alert(this.ni);
	// alert(this.img.src);
	//alert(this.ol.newOp);
	//this.ol.newOp = -1;
	//this.fadeObj();
	setTimeout(new Function(this.mn + '.img.src = "' + this.temp.src + '";' + this.mn + '.fadeObj();'),1);	

};

ImageMorph.prototype.fadeObj = function(){
	this.ol.newOp = (this.ol.newOp && this.ol.newOp != -1) ? (this.ol.newOp > this.inc ? this.ol.newOp - this.inc : 0) : 1;
	this.ol.style.opacity = this.ol.newOp;
	this.ol.style.filter = 'alpha(opacity=' + (this.ol.newOp * 100) + ')';
	if (this.cap){
		this.cap.style.opacity = this.ol.newOp;
		this.cap.style.filter = 'alpha(opacity=' + (this.ol.newOp * 100) + ')';
	}
	this.img.style.opacity = 1 - this.ol.newOp;
	this.img.style.filter = 'alpha(opacity=' + (100 - this.ol.newOp * 100) + ')';
	if (this.ol.newOp > 0) setTimeout(new Function(this.mn+'.fadeObj()'),this.int);
	else {
		this.ol.newOp = -1;
		// insert new caption if found
		if (this.cap){
			if (this.isArray(this.il[this.ni]) && this.il[this.ni][1]){
				this.cap.innerHTML = this.il[this.ni][1];
				this.cap.style.opacity = 1;
				this.cap.style.filter = 'alpha(opacity=100)';
			}
			else this.cap.innerHTML = '';
		}
	}
};


/* coordinate functions from quirksmode.org */
ImageMorph.prototype.fpx = function(o)
{
	var cl = document.all && this.hih ? this.hih.x : (this.hgh ? this.hgh.x : 0);
	if (o.offsetParent)
	{
		while (o.offsetParent)
		{
			cl += o.offsetLeft;
			o = o.offsetParent;
		}
	}
	else if (o.x)
		cl += o.x;
	return cl;
};

ImageMorph.prototype.fpy = function(o)
{
	var ct = document.all && this.hih ? this.hih.y : (this.hgh ? this.hgh.y : 0);
	if (o.offsetParent)
	{
		while (o.offsetParent)
		{
			ct += o.offsetTop;
			o = o.offsetParent;
		}
	}
	else if (o.y)
		ct += o.y;
	return ct;
};

ImageMorph.prototype.isArray = function(a){
	return ((a && typeof a == 'object') || typeof a == 'function') && a.constructor == Array;
};



if (typeof ghost_addEvent != 'function')
{
 var ghost_addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') ghost_addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = ghost_addEvent._i++;
  o._evts[t][f._i] = f;
 };
 ghost_addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}