// JavaScript Document
var t = null;

function Rotator(c,m,s,l) {
	this.images = new Array();
	this.maxImages = m;		//maximum number of images
	this.speed = s;			//transition time in seconds
	this.showMenu = l;		//render clickable nav links
	this.container = document.getElementById(c);

	this.imgTop = document.createElement('div');
	this.imgBtm = document.createElement('div');
	this.imgTop.className = "rTop";
	this.imgBtm.className = "rBtm";
	
	this.container.appendChild(this.imgTop);
	this.container.appendChild(this.imgBtm);
	
	if(this.showMenu){
		this.itemMenu = document.createElement('div');
	    this.itemMenu.className = "itemMenu";
	    this.container.appendChild(this.itemMenu);
	}
}

//Start Rotation
Rotator.prototype.Start = function() {
	var i = arguments[0];
	var r = this;
	if(typeof(i) == 'undefined'){ i=0; }
	if(typeof(i) == 'number' && r.images.length > 0) {
		
		r.LoadContent(i);
		if(r.showMenu){ r.SwitchClass(i); }
		
		if(i < r.images.length-1){ i++; }
		else { i = 0; }
		
		t = setTimeout(function(){ r.Start(i) },r.speed*1000);
	} 
	else { alert("Runtime Error: Unknown Index"); }
}

//Pause rotation and set item for 15 seconds
Rotator.prototype.Pause = function() {
	clearTimeout(t);
	var i = arguments[0];
	var r = this;
	if(typeof(i) == 'undefined'){ i=0; }
	if(typeof(i) == 'number' && r.images.length > 0) {
		
		r.LoadContentNoFade(i);
		r.SwitchClass(i);
		
		t = setTimeout(function(){ 
							if(i < r.images.length-1){ i++; }
							else { i = 0; }
							r.Start(i); },((15 - r.speed) * 1000));
	} 
	else { alert("Runtime Error: Unknown Index"); }
}

//load hidden item content no fade in
Rotator.prototype.LoadContentNoFade = function(id) {
	var r = this;
	r.imgTop.style.background = "url(" + r.images[id][0].src + ") no-repeat";
	var tmp = id;
	if(r.images[tmp][1].length > 0){
		r.imgTop.onclick = function(){ window.location.href = r.images[tmp][1]; }
	} else {
		r.imgTop.onclick = '';
	}
	
	if(id == 0) { id = r.images.length-1; }
	else { id = id - 1; }
	r.imgBtm.style.background = "url(" + r.images[id][0].src + ") no-repeat";
}


//load hidden item content
Rotator.prototype.LoadContent = function(id) {
	var r = this;
	r.imgTop.style.background = "url(" + r.images[id][0].src + ") no-repeat";
	var tmp = id;
	if(r.images[tmp][1].length > 0){
		r.imgTop.onclick = function(){ window.location.href = r.images[tmp][1]; }
	} else {
		r.imgTop.onclick = '';
	}
	
	if(id == 0) { id = r.images.length-1; }
	else { id = id - 1; }
	r.imgBtm.style.background = "url(" + r.images[id][0].src + ") no-repeat";
	r.FadeIn(0);
}


Rotator.prototype.FadeIn = function(op) {
	var r = this;
	if(op < 100){ 
		if(navigator.appName.indexOf('Netscape') != -1){
			r.imgTop.style.opacity = (op/100);
			r.imgTop.style.MozOpacity = (op/100);
		}else {
			r.imgTop.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + op + ")";
		}
		setTimeout(function(){ r.FadeIn(op + 10); }, 75); 
	} else { 
		if(navigator.appName.indexOf('Netscape') != -1){
			r.imgTop.style.opacity = 1;
			r.imgTop.style.MozOpacity = 1;
		} else {
			r.imgTop.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
		}
		r.imgBtm.style.display = "block";
	}
}

Rotator.prototype.SwitchClass = function (id) {
    var obj = null;
	for(i=0; i < this.images.length; i++){
		obj = document.getElementById('item'+(i+1));
		if(i == id) { obj.className = 'itemCurrent'; }
		else { obj.className = 'item'; }
	}
}

//Appends image information into image array
Rotator.prototype.AddImage = function (path, desc) {
    if (this.images.length < this.maxImages) {
        var r = this;
        var img = new Image();
        img.src = path;
        r.images.push(new Array(img, desc));

        if (this.showMenu) {
            var itm = document.createElement('div');
            var cnt = r.images.length;
            itm.className = 'item';
            itm.id = 'item' + cnt;
            itm.innerHTML = cnt;
            itm.onclick = function () { r.Pause(cnt - 1); }
            if(cnt == 1){ r.itemMenu.appendChild(itm); }
	    else { r.itemMenu.insertBefore(itm, r.itemMenu.firstChild); }
        }
    }
}
