//ROLLOVER RELATED
///////////////////////////
var over = new Object();
var out = new Object();
function addRollovers(idArr) {
	var imgs, a;

	if (!document.images) return;

	for (i = 0; i < idArr.length; i++) {
		imgs = getObj(idArr[i]);
		if (!imgs) continue;
		a = imgs.parentNode;
		if (a && a.tagName == 'A'){
			over[ idArr[i] ] = new Image();
			out[ idArr[i] ] = new Image();
			over[ idArr[i] ].src = getRollSrc(imgs.src,"gif",1);
			out[ idArr[i] ].src = imgs.src;
			a.onmouseover = function() { this.childNodes[0].src = over[this.childNodes[0].id].src; }
			a.onfocus = function() { this.childNodes[0].src = over[this.childNodes[0].id].src; }
			a.onmouseout = function() { this.childNodes[0].src = out[this.childNodes[0].id].src; }
			a.onblur = function() { this.childNodes[0].src = out[this.childNodes[0].id].src; }
		}
	}
}
function getRollSrc(src, ext, hover) {
	return (hover) ? src.replace("a."+ext,"b."+ext) : src.replace("b."+ext,"a."+ext);
}



//****************************
//** FROM: somewhere. Seen these functions elsewhere too many times to properly credit
//***************************
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function addEvent(obj,evType,fn){
	if(obj.addEventListener){
		obj.addEventListener(evType,fn,true);
		return true;
	}
	else if(obj.attachEvent){
		var r=obj.attachEvent("on"+evType,fn);
		return r;
	}
	else{
		eval("obj.on"+evType+ " = " + fn);
		return true;
	}
}


function getObj(name) { //also known as $()
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function ImgRotator (objId,speed)
{
	this.objId = objId; // VARIABLE SHOULD BE NAMED THIS AS WELL
	this.obj = getObj(objId);
	this.speed = speed;
	this.ImgSrc = new Object();
	this.ImgAlt = new Object();
	this.ImgLnk = new Object();
	this.curIndex =  0;
	this.count = 0;
	this.started = false;
	this.preloaded = false;
	this.paused = false;
	if (this.obj) {
		var a = this.obj.parentNode;
		if (a.tagName == 'A') { this.addImage(this.obj.src,this.obj.alt,a.href); this.curIndex++; a.style.display = 'block'; }
		this.obj.onmouseover = function() { eval(this.id).paused = true; }
		this.obj.onmouseout = function() { eval(this.id).paused = false; }
	}
}

var IR = ImgRotator.prototype;

IR.addImage = function(source,alt,lnk) { this.ImgSrc[this.count] = source; this.ImgLnk[this.count] = lnk; this.ImgAlt[this.count++] = alt; }
IR.preloadImages = function() {  this.preloaded = true;for(i=0;i < this.count;i++) { var img= new Image(); img.src = this.ImgSrc[i]; } }
IR.beginSlideShow = function() { if (!this.obj) return;  this.started = true; setInterval(this.showImage.bind(this),this.speed); }
IR.showImage = function () {
	if (this.paused) return;
	var a = this.obj.parentNode;
	a.style.backgroundImage = 'url('+this.obj.src+')';
	if ( a.tagName == 'A'){
		a.href=this.ImgLnk[this.curIndex];
		( a.href.indexOf('http://') >= 0 ) ? a.target = '_blank' : a.target = '';
	}
	setOpacity(this.obj,0);
	this.obj.title = this.ImgAlt[this.curIndex];
	this.obj.alt = this.ImgAlt[this.curIndex];
	this.obj.src = this.ImgSrc[this.curIndex];
	this.curIndex++; this.curIndex %= this.count;
	
	fadeIn(this.objId,0);
	if (!this.preloaded) { // fetch the next image when preloading has
		imgy = new Image();
		imgy.src = this.ImgSrc[this.curIndex];
	}
}


//From Prototype: http://prototype.conio.net/
Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    __method.apply(object, arguments);
  }
}
if (!Function.prototype.apply) {
  // Based on code from http://www.youngpup.net/
  Function.prototype.apply = function(object, parameters) {
    var parameterStrings = new Array();
    if (!object)     object = window;
    if (!parameters) parameters = new Array();
    
    for (var i = 0; i < parameters.length; i++)
      parameterStrings[i] = 'parameters[' + i + ']';
    
    object.__apply__ = this;
    var result = eval('object.__apply__(' + 
      parameterStrings.join(', ') + ')');
    object.__apply__ = null;
    
    return result;
  }
}
