/**
*
* A animated banner for jquery
 * By Stefan Sedich (stefan.sedich@gmail.com)
 * 
 * To Use:
 *
 */ //trace debug toolfunction trace(aMsg) {	setTimeout(function() { throw new Error("[trace] " + aMsg); }, 0);}/*last part of main function call:// collapse imagelink array removing missing elements //for (m=imagetotl-1;m>=3;m--) {	testExists(imagelink[m]); 	alert("test");   //this statement that must be here to make it work! (What gives???) //          	if(nB) {		imagetotl--;			for (n=m;n<=imagetotl+1;n++) {				imagelink[n]=imagelink[n+1];			}	}}// Seed the starting image sequence //document.pic.src=imagelink[0];break;}}}}function testExists(imagepath) {    req = getreq();    req.onreadystatechange = imagexists;    req.open("head", imagepath, true);    req.send(null);     }function imagexists(){    if(req.readyState == 4) {        if(req.status == 200)        {            // image exists            nB=false;        }	   else if(req.status==404)	   {// image doesn't exist	       nB=true;	   }        else        {            // all other error codes will set this            nB=null;        }		return nB;        }}function getreq() { // returns false if exists	if(window.ActiveXObject) { // if IE		try {		return new ActiveXObject("Msxml2.XMLHTTP");	} catch(e) {		try {		return new ActiveXObject("Microsoft.XMLHTTP");		} catch(e) {		return;			}		}	} else if(window.XMLHttpRequest) { // if Mozilla, Safari, etc.		return new XMLHttpRequest();		}}*/
jQuery.FancyBanner = {

	//Banner options
	options : {		
		container	: "",		
		interval	: 2000
	},
	
	//Member variables
	mNumImages		: 0,
	mUpto			: 0,
	mImagesLoaded	: 0,
	mPreloads		: null,
	mTimeout		: null,    
    mCallback        : null,
	
	//Main banner creation functions. Sets up the banner, Preloads the images and Shows the first image of the banner.
	Create			: function(images, options, callback) {		
						
        mCallback = callback;
        
		//Merge options with defaults if they are passed in.
		if(options)
			jQuery.FancyBanner.options = jQuery.extend(jQuery.FancyBanner.options, options);
				
		jQuery.FancyBanner.mNumImages = images.length;
		
		//Preload Images
		jQuery.FancyBanner.PreloadImages(images);
		
		//Clear the container, used 
		$(jQuery.FancyBanner.options.container).empty();
		
		//For each image, append it to the showcase area as an img, and a unique id.
		for(i=0;i<images.length;i=(i+1)) {
		
			//Add each image.
			/**
			* !! CHANGE THIS TO SETUP THE STYLE OF THE ADDED IMAGE !!
			*/
			$(jQuery.FancyBanner.options.container).append("<img style='position: absolute; left: 0px; display: none;' src='"+images[i]+"' id='bannerImage"+i+"' class='bannerImage' />");		 				
			
			//Bind onload event handler for the image. Which calls the ImageLoaded method.
			$("#bannerImage" + i).bind('load', function() { jQuery.FancyBanner.ImageLoaded(); });			
			
		}
		
		//Show the first image.
		document.getElementById("bannerImage0").style.display = "block";			
	
	},
	
	//Preloads our images.
	PreloadImages 	: function(images) {
		
		jQuery.FancyBanner.mPreloads = $.preloadImages(images);
	
	},
	
	//Called as each image is loaded. When it has loaded all images it starts the rotations.
	ImageLoaded		: function() {
	
		//Add to the count of images that are loaded.
		jQuery.FancyBanner.mImagesLoaded = (jQuery.FancyBanner.mImagesLoaded + 1);
		
		if(jQuery.FancyBanner.mImagesLoaded == jQuery.FancyBanner.mNumImages) {
			//Start the animation once all images are fully loaded. 
            if(jQuery.FancyBanner.mNumImages > 1) {
                mTimeout = setTimeout(function () { jQuery.FancyBanner.SwapImage(); }, jQuery.FancyBanner.options.interval); 
                mCallback();
            }
		}			
		
	},
	
	//Performs the image swaps.
	SwapImage		: function() {
	
		//Get the current image.
		var current = "#bannerImage"+jQuery.FancyBanner.mUpto;
		
		//Fade it out.
		$(current).fadeOut(1000);
		
		//Increment the image we are upto.
		jQuery.FancyBanner.mUpto = (jQuery.FancyBanner.mUpto + 1);
			
		if((jQuery.FancyBanner.mUpto + 1) > jQuery.FancyBanner.mNumImages) {
			
			//If we have passed all images, set the current image back to the first.
			jQuery.FancyBanner.mUpto = 0;
			var next = "#bannerImage"+jQuery.FancyBanner.mUpto;
			
			//Fade in the first image.
			$(next).fadeIn(1000);
			
		} else {
		
			//Otherwise fade in the next image.
			var next = "#bannerImage"+jQuery.FancyBanner.mUpto;
			$(next).fadeIn(1000);
			
		}
		
		mTimeout = setTimeout(function () { jQuery.FancyBanner.SwapImage(); }, jQuery.FancyBanner.options.interval); 
	
	},
	
	Stop		: function() {            
        clearTimeout(mTimeout);
        mStopped = true;        
	}
	
};


jQuery.preloadImages = function(inImages)
{
	var imgs = new Array();
	for(var i = 0; i<inImages.length; i++)
	{
		imgs[i] = jQuery("<img>").attr("src", inImages[i]);
	}
	
	return imgs;
}
