/*
Blue Shift Inc Website Javascript

Takes element and fades in or out from current opacity, at a specified speed.

Original scripts by http://www.brainerror.net/scripts_js_blendtrans.php, altered by Max Johnson 2007
*/

function opacity(id, opacStart, opacEnd, millisec)
{
    var obj = document.getElementById(id);
    
    //speed for each frame
    var speed = 5*Math.round(millisec / 100);
    var timer = 0;
    
    //clear any previous opacity timers - added by Max
    if( obj.opacTimer )
    {
		for(i = 0; i <= 100; i++) {
			clearTimeout(obj.opacTimer[i]);
		}
    }
    
    // set/blank out opacTimer array
	obj.opacTimer = [];
	
    if( window.changeOpac )
    {
	    //determine the direction for the blending, if start and end are the same nothing happens
	    if(opacStart > opacEnd) {
	        for(i = opacStart; i >= opacEnd; i-=5)// used to be i-- but was slowing down system - Max
	        {
	            obj.opacTimer.push( setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)) );
	            timer++;
	        }
	        obj.opacTimer.push( setTimeout("showHideElement( \"" + id + "\", \"hide\" )",((timer+1) * speed)) );// added by Max
	    } else if(opacStart < opacEnd) {
	        showHideElement( id , "show" );// added by Max
	        
	        for(i = opacStart; i <= opacEnd; i+=5)// used to be i++ but was slowing down system - Max
	        {
	            obj.opacTimer.push( setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)) );
	            timer++;
	        }
	    }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = (opacity < 100)? "alpha(opacity=" + opacity + ")": null;// alpha filter messes up .png transparency in IE so at 100% turn it off. - added by Max
}

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++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
    //standard opacity is 100 *** reset by Max to be 0
	var currentOpac = 0;

    //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);
} 