/*****************************************************************\

Copyright (c) 2006 Mike Ryan mike@bitdamaged.com

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the 
Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




*****************************************************************/

/**
	There's two settings to adjust the transition speed.
	
	1. The number of steps in a transition. (BIFNumSteps).
	2. The Transition speed. 
	
	Generally if you want to lengthen or shorten a transition you 
	simply want to change the number of steps (usually between
	around 20 to 200;
	
	The transition speed is the "framerate" which can effect the 
	smoothnesss of the transition.  At somepoint around 60 you're going
	to start noticing the jumps between steps. However if you have a
	ton of steps then the "jumps" will be less noticeable.
	
	BIFTimeBetweenImages is how long we sit on an image before starting 
	the next transition.
	

**/

var BIFNumSteps = 40;  //  Adjust here for length of transitions/
var BIFTransitionSpeed = 35; // Framerate
var BIFTimeBetweenImages = 3000; // TimeBetween




var BitImageFader = {
		
		newImageIndex : 0,
		counter : 0,
		dir : true,
		Imgs : [],


		
		
		init : function() {
			// We're going to preload the images. 
			for(var i=0;i< BitImageFader.overImages.length; i++ ) {
				var im = new Image();
				im.src = BitImageFader.overImages[ i ];
				BitImageFader.Imgs[ i ] = im;
			}
			
			// Grab the old image 
			BitImageFader.oldimg = document.getElementById('BitFaderImage');
			
			// Create a new image element 
			BitImageFader.newimg = document.createElement('img');
			BitImageFader.newimg.src = BitImageFader.Imgs[ BitImageFader.newImageIndex = 1 ].src;
			
			// Reuse the existing CSS
			BitImageFader.newimg.className = BitImageFader.oldimg.className;			
			
			// Place it right over the old image.
			BitImageFader.newimg.style.position = 'absolute';
			BitImageFader.newimg.style.left = findPosX( BitImageFader.oldimg ) + 'px';
			BitImageFader.newimg.style.top =  findPosY( BitImageFader.oldimg ) + 'px';
			
			
			// Make sure it's hidden.
			BitImageFader.setOpacity( BitImageFader.newimg, 0 );
			
			// I hate HATE testing for particular browsers but I can't see 
			// a better way to do this. 
			if ( window.opera ) {
					BitImageFader.newimg.style.display = 'none';
			}
			
			// Attach to the document.
			document.body.appendChild( BitImageFader.newimg );
			
			// Initialize 
			BitImageFader.counter++;
			
			// Start after the interval
			BitImageFader.timeout = setTimeout( function() { BitImageFader.fade() }, 
																												BIFTimeBetweenImages );
		},
		
		
		/****************************************************************************
		
		function fade();
		
		Main fade function
		
		Sets the new opacity for the two images and the last time through
		it switches old for new and grabs a new image.
		****************************************************************************/
		fade : function() {
			
			// Next Step!
			var oldo = ( BIFNumSteps - BitImageFader.counter ) / BIFNumSteps ;
			
			// Images are inverse.
			var newo = 1 - oldo;
			
			// Set the opacity and increment the counter.
			BitImageFader.setOpacity( BitImageFader.oldimg, oldo );
			BitImageFader.setOpacity( BitImageFader.newimg, newo );
			
			BitImageFader.counter++ 
			
			if ( BitImageFader.counter <= BIFNumSteps ) {
				// Fade isnt' over so redo.
				setTimeout( function() { BitImageFader.fade() } , BIFTransitionSpeed );
			} else {
				
				// Fade is over make sure we're at the absolute ends.
				BitImageFader.setOpacity( BitImageFader.oldimg, 0 );
				BitImageFader.setOpacity( BitImageFader.newimg, 100 );	
				
				
				// Opera Hack.
				if ( window.opera ) {
						BitImageFader.oldimg.style.display = 'none';
						BitImageFader.newimg.style.display = 'block';
				}
				
				// Switch the two image elements. What's old is new and vice versa.
				var tmp = BitImageFader.oldimg;
				BitImageFader.oldimg = BitImageFader.newimg;
				BitImageFader.newimg = tmp;
				// Clean up
				tmp = null;
				
				// Get the Next Image in the set.
				BitImageFader.getNewImage();	
				
				// reset.
				BitImageFader.counter = 0;
				// Start Again later.
				setTimeout( function() { BitImageFader.fade() } , BIFTimeBetweenImages );
			}
			
		},
		
		// Get a new Image
		getNewImage : function() {
			BitImageFader.newImageIndex++;
			BitImageFader.newImageIndex =  BitImageFader.newImageIndex == BitImageFader.overImages.length ? 0 : BitImageFader.newImageIndex;
			BitImageFader.newimg.src = BitImageFader.Imgs[ BitImageFader.newImageIndex ].src;
		},
		
		// Set the opacity.
		setOpacity : function( obj,  opa ) {
				obj.style.KhtmlOpacity = opa;
				obj.style.opacity = opa;
				obj.style.MozOpacity = opa;
				obj.style.filter = "alpha(opacity=" + ( opa * 100) + ")";
		}
}

/************************************************
	This bit was stolen from quirksmode.com
	http://www.quirksmode.org/js/findpos.html
	
	Well done :) 
*************************************************/

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while ( obj.offsetParent ) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}