var features = new Array(2);
features[0] = new Array(); // feature li's
features[1] = new Array(); // li heights
var index = -1;
var time_allowed = 200;
var min_height = 43;

function init() {
	// put all feature li's into an array
	var all = document.getElementsByTagName('li');
	for (var i=0; i<all.length; i++)
		if(all[i].parentNode.className == 'big_icon_list') {
			features[0][features[0].length] = all[i];
			features[1][features[1].length] = all[i].clientHeight;
		}
	// shrink them to 2 lines
	for (var i=0; i<features[0].length; i++) {
		features[0][i].style.height = min_height;
		features[0][i].style.background = "url(http://www.iggsoftware.com/images/ellipsis.gif) bottom right no-repeat";
	}
}

function showFeature(feature) {
	// hide last feature
	if (index != -1) {
		var now = new Date().getTime();
		smoothAnimate(index, features[0][index].clientHeight, min_height, now, (now+time_allowed));
		features[0][index].style.background = "url(http://www.iggsoftware.com/images/ellipsis.gif) bottom right no-repeat";
	}
	
	// show new feature
	var i = 0;
	while (i < features[0].length) {
		if (features[0][i] == feature) {
			features[0][i].style.background = 'none';
			var now = new Date().getTime();
			smoothAnimate(i, features[0][i].clientHeight, features[1][i], now, (now+time_allowed));
			index = i;
			break;
		}
		i++;
	}
}

function smoothAnimate(obj_index,start_height,end_height,start_time,end_time) {
	var current_time = new Date().getTime();
	if (start_height < end_height) { // grow
		if ((features[0][obj_index].clientHeight < end_height) && (current_time < end_time)) {
			/*var change_time = current_time - start_time;
			var percent_time = change_time/time_allowed;
			var difference_height = end_height - start_height;
			var change_height = percent_time * difference_height;
			var new_height = start_height + change_height;*/
			
			var new_height = start_height + (((current_time - start_time)/time_allowed) * (end_height - start_height));
			features[0][obj_index].style.height = new_height;
			setTimeout("smoothAnimate("+obj_index+","+start_height+","+end_height+","+start_time+","+end_time+")",0);
		} else features[0][obj_index].style.height = end_height;
	} else { // shrink
		if ((features[0][obj_index].clientHeight > end_height) && (current_time < end_time)) {
			var new_height = start_height - (((current_time - start_time)/time_allowed) * (start_height - end_height));
			features[0][obj_index].style.height = new_height;
			setTimeout("smoothAnimate("+obj_index+","+start_height+","+end_height+","+start_time+","+end_time+")",0);
		} else features[0][obj_index].style.height = end_height;
	}
}

// specify which retailers to show here
var images = new Array("apple_stores.gif","amazon.gif","office_depot.gif","frys.gif","officemax.gif","j_and_r.gif","micro_center.gif","tekserve.gif","future_shop.gif");
var index = 0;
var fading = 1;
var opacity = 0;

function rotateRetailers() {
	var obj = document.getElementById("ibank_3_retailers");
	if (obj) {
		if (fading==0){			// FADE IN
			if(opacity<100) {	// If image is not opaque yet
				opacity+=2;		// increase opacity
				setOpacity(obj, opacity);
				setTimeout("rotateRetailers()",15);
			}
			else{				// once image is opaque
				opacity=100;	// reset the opacity to 100%
				fading=1;		// fading out next
				if ((typeof images == "undefined") || (images.length <= 1)) delete rotateRetailers; // if there's only one image, just leave it there
				else setTimeout("rotateRetailers()",3000);  // otherwise hold the image onscreen for a bit, then rotate
			}
		}
		else if (fading==1){	// FADE OUT
			if(opacity>0) {		// If image is not invisible yet
				opacity-=2;		// decrease opacity
				setOpacity(obj, opacity);
				setTimeout("rotateRetailers()",15);
			}
			else{				// once image is invisible
				opacity=0;		// reset the opacity to 0%
				fading=0;		// fading in next
				
				// then swap the image
				if ((typeof images != "undefined") && (typeof images[0] != "undefined")) obj.style.backgroundImage = "url(http://www.iggsoftware.com/images/ibank/retailers/"+images[index]+")";
				else obj.style.backgroundImage = "url(http://www.iggsoftware.com/images/ibank/retailers/apple_stores.gif)";
				// increment the image index
				index++;
				if ((typeof images != "undefined") && (index >= images.length)) index = 0;
				// preload the next image
				if ((typeof images != "undefined") && (typeof images[0] != "undefined")) {
					var pic = new Image(193,46);
					pic.src ="http://www.iggsoftware.com/images/ibank/retailers/"+images[index];
				}
				// call the rotate function again
				rotateRetailers();
			}
		}
		else fading=0; // reset to "fading in" in case of an invalid value
	}
}

/*** 	This function was borrowed from Richard Rutter
		at http://clagnut.com/sandbox/imagefades/		***/
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;
}
