if(!window.spi){
	window.spi = new Object();
}

window.spi.ani = new Object();
window.spi.ani.images = new Array();
window.spi.ani.imgbase = '';
window.spi.ani.index = 0; 
window.spi.ani.cid = null; 
window.spi.ani.iid = null; 
window.spi.ani.speed = 1000
window.spi.timerIDs = []

spi.ani.add = function (i){
	var url = spi.ani.imgbase + i; 
	spi.ani.images.push(url)
	simplePreload(url)
}
spi.ani.run_imageblender = function (cid,iid,speed){
	window.spi.ani.cid = cid; 
	window.spi.ani.iid = iid; 
	window.spi.ani.speed = speed
	for(var i=0;i<spi.ani.images.length;i++){
		setTimeout('bi()',5000*i)
	}
}

function bi(){
	blendimage(spi.ani.cid,spi.ani.iid,spi.ani.images[spi.ani.index],spi.ani.speed)
	spi.ani.index += 1
}

// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload(){ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++) {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

/* ------------------------------------ --------------------------------------- */
// blender library from http://brainerror.net/scripts/javascript/blendtrans/

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id)
	if(object){
		var o = object.style
		o.opacity = (opacity / 100);
		o.MozOpacity = (opacity / 100);
		o.KhtmlOpacity = (opacity / 100);
		o.filter = "alpha(opacity=" + opacity + ")";
	}
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it invisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	//make image transparent
	changeOpac(0, imageid);
	//make new image
	document.getElementById(imageid).src = imagefile;
	//fade in image
	for(i = 0; i <= 100; i++) {
		var tid = setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed))
		window.spi.timerIDs.push( tid );
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}
	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
/* ------------------------------------ --------------------------------------- */

function cancelBlend(){
	for(var i=0;i<window.spi.timerIDs.length;i++){
		clearTimeout(window.spi.timerIDs[i])
	}
	window.spi.timerIDs = []
}

