/*!
 * jQuery Logo Switch Plugin
 * Plugin URL http://logoswitch.ge.ekology.co.nz
 * Version: 0.3 
 
 
 * Change Log 0.3
 *Added loader div to page as IE was not caching the preload array
 
 * Change Log 0.2
 * - fixed preloader to work corectly
 * - added $.inArray in place of indexOf and removed iesucksballs function
 * - added usedIms array to make sure all images get used before being used twice
 * - changed all instances of $ to jQuery
 * - added imheight and imwidth
 */
 
(function(jQuery){

 	jQuery.fn.extend({ 
 		
		//pass the options variable to the function
 		logorotation: function(options) {


			//Set the default values, use comma to separate the settings, example:
			var defaults = {
				delay : 1000,
				imdir : 'ls/',
				ims : Array(),
				numitems : 4,
				imwidth:'',
				imheight:''
			}
				
			var options =  jQuery.extend(defaults, options);

    		return this.each(function() {
				var o = options;
				var	inuse = Array();
				var ims = o.ims;
				var usedIms = Array();
				var wrapper=jQuery(this);
				var counter =1;
				initialise();
			
			function initialise(){
				preloadims();
				
							
				//setup the initial list of images and display onscreen.
				jQuery(wrapper).append('<ul class="logoswitcher"></ul>');
			
				for(i=0; i < o.numitems; i++){
					//get a random image from the ims array
					var index = Math.floor(Math.random()*ims.length);
					var im = ims[index];
					
					//take image out of ims array
					var cim = ims.splice(index,1);
					//add image to inuse array
					inuse.push(cim[0]);
					
					jQuery('ul.logoswitcher').append('<li class="logoswitch-' + i + '"><img src="' + o.imdir + im + '" alt="" width="' + o.imwidth + '" height="' + o.imheight + '" /></li>');
				}
				
				//start the rotator	
				setTimeout(function(){ changeImage(null);},o.delay);	
				
			}

			
			function changeImage(lastim){
				//dowhile repeats if the current image space is the same as the one that has just changed.						
				do{
					//get the old image to change
					var currentIms= jQuery('ul.logoswitcher img');
					var indexToChange = Math.floor(Math.random()*currentIms.length);
					var imToChange = currentIms[indexToChange];
					
				//	console.log('last: ' + lastim + ' new: ' + indexToChange)
					
				}while(lastim == indexToChange);
				

				
				//get a new image to replace the old one
				if(ims.length > 0){
					//use an image from the ims array if it still contains them
					var newindex = Math.floor(Math.random()*ims.length);
					var newim = ims[newindex];
				}else{
					//if all images have been used, reset ims with the usedIms array
					ims = usedIms;
					usedIms = [];
					var newindex = Math.floor(Math.random()*ims.length);
					var newim = ims[newindex];
				}
								
				
				//take the new image out of the ims array
				var cim = ims.splice(newindex,1)
				//put the new image in the inuse array
				inuse.push(cim[0]);
				
				//push oldim back into ims array
				var	oldim = jQuery(imToChange).attr('src');
						place = oldim.lastIndexOf('/');
						oldim = oldim.substring(place + 1);
						usedIms.push(oldim);
				
				//take oldim out of inuse array
					k = jQuery.inArray(oldim,inuse);
					d = inuse.splice(k,1)

					
				//fadeout old image, change to new and fadeback in. Call function changeimage.
				jQuery(imToChange).fadeOut(600)
						.queue(function(n){
								jQuery(this).attr('src', o.imdir + newim);
								n();
							}).fadeIn(1000, function(){
									setTimeout(function(){ changeImage(indexToChange);},o.delay);	
							});

			}//ends change image
			

			
			function preloadims(){
				//had to add loader div coz IE is to spaz to cache from array.
				jQuery('body').append('<div style="width:1px;height:1px;position:absolute;left:-1000px;overflow:hidden;" id="logoswitcher-preload">')
				preloadarea = jQuery('#logoswitcher-preload');
				
				var cache = [];
				for(var i = 0; i < ims.length; i++){	
					var loadIm = document.createElement('img');
					loadIm.src=o.imdir+ims[i];
					cache.push(loadIm);
					preloadarea.append(loadIm);
				
				}
			}
						
    		});
    	}
	});
	
})(jQuery);

