/*
 * Image Fade Plugin
 * 
 * Copyright (c) 2009 Tilmann Fruntke <me@till-horizon.com>
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *   
 */


var imageFade = function ( div, options ) {
	var imageFade = this;
	
	
	jQuery(div).css({position: 'relative'});
	
	this.div = jQuery(div);
	
	defaults = {
			delay:           3000, 
			fade:		 	 1000,
			randomize:		 true,
			fadeElement:	'.slide-tab',
			next:			'.next',
			prev:			'.prev'
	    }
	
	
	this.next = 0;
	
	
	
	this.current = this.next;
	this.last = new Array();
	
	this.settings = jQuery.extend({}, defaults, options);
	this.images = [];
	
	this.slideshow = true;
	
	this.nextIndex = function () {
		var nextInd = imageFade.current + 1;
			
		if ( nextInd >= imageFade.images.length ) {
			nextInd = 0;
		}
			
		return nextInd;
	}
	
	this.prevIndex = function () {
		var prevInd = imageFade.current - 1;
		
		if ( prevInd < 0 ) {
			prevInd = imageFade.images.length - 1;
		}
			
		return prevInd;
	}
	
	this.nButton = jQuery(this.settings.next);
	this.pButton = jQuery(this.settings.prev);
	
	if ( jQuery(this.nButton).attr('class') && jQuery(this.pButton).attr('class')  ) {
		
		this.nButton.bind('click', function() {
			imageFade.slideshow = false;
			imageFade.lastOne = imageFade.current;
			imageFade.current = imageFade.nextIndex();
			imageFade.play();
		});
		this.nButton.attr('href','#next');
		
		this.pButton.bind('click', function() {
			imageFade.slideshow = false;
			imageFade.lastOne = imageFade.current;
			imageFade.current = imageFade.prevIndex();
			imageFade.play();
		});
		this.pButton.attr('href','#prev');
		
	}
	
	this.rand = function ( n ) {
		  return ( Math.floor ( Math.random ( ) * n + 1 ) );
   }
	console.log('div_fade');
	
	// load images.
	jQuery(div).children( imageFade.settings.fadeElement ).each(function(i, obj){
		
		jQuery(obj).css({position:'absolute',display:'block',opacity:'1.0'});
		
		imageFade.images.push( jQuery(obj).hide() );
		
	});
	
	if ( this.settings.randomize ) {
		this.current = this.rand ( this.images.length ) - 1
	}
	
	this.isFirst = true;
	
	this.doSlideshow = function () {
		
		if ( !imageFade.slideshow ) {
			imageFade.slideshow = true;
			setTimeout( function() { imageFade.doSlideshow(); } , 3000 );
			return false;
		}
		
		imageFade.lastOne = imageFade.current;
		imageFade.current = imageFade.nextIndex();
		imageFade.play();
	}
	
	this.play = function() {
	
		var picToShow = imageFade.current;

		
		
		
		// keep it clean
		if ( imageFade.last.length >= 5 ) {
			imageFade.div.children(imageFade.settings.fadeElement+':first').remove();
			imageFade.last.shift();
		}
	
		imageFade.last.push( 
				imageFade.images[picToShow].appendTo( imageFade.div )
		);
		
		if ( !imageFade.isFirst ) {
			
			imageFade.images[imageFade.lastOne].fadeOut( imageFade.settings.fade ); 
			setTimeout( function() {  imageFade.div.children(imageFade.settings.fadeElement+':last').hide().fadeIn(imageFade.settings.fade )  } , (imageFade.settings.fade) );
			
			
		} else {
			imageFade.isFirst = false;
			imageFade.div.children(imageFade.settings.fadeElement+':last').show();
		}
		
		if ( imageFade.slideshow ) {
			var fDelay = imageFade.settings.fade + imageFade.settings.delay;
			setTimeout( function() { imageFade.doSlideshow(); } , fDelay );
		}
	}

	
	
	this.play();
}

//Array.push() - Add an element to the end of an array, return the new length
if( typeof Array.prototype.push==='undefined' ) {
 Array.prototype.push = function() {
  for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
   this[b+i] = a[i];
  }
  return this.length;
 };
}


