/**
 * Image rotation class
 *
 * Usage:
 * <div id="rotatorMainDiv" style="background-position: left top; background-repeat: no-repeat; width: 980px; height: 288px">
 * <div id="rotatorLoadingDiv">
 * <img src="" border="0" />
 * </div>
 * </div>
 * 
 * <script type="text/javascript">
 * rotator = new ImageRotator('rotator');
 * rotator.setInterval(5000)
 * 		 .setFadeInInterval(1000)
 * 		 .setRandom(true)
 * 		 .addImage('UI/Images/Header/Home/homebaner01_new.jpg')
 * 		 .addImage('UI/Images/Header/Home/homebaner02_new.jpg')
 * 		 .addImage('UI/Images/Header/Home/homebaner03_new.jpg')
 * 		 .addImage('UI/Images/Header/Home/homebaner04_new.jpg')
 * 		 .addImage('UI/Images/Header/Home/homebaner05_new.jpg');
 * rotator.init(rotator);
 * </script>
 *
 */
function ImageRotator(name) {

	this.name = name;					// name of control
	this.timer = null;				// timer
	this.interval = 5000; 			// swap interval
	this.fadeInInterval = 1000;	// fade in transition 
	this.images = new Array();		// image sources
	this.loadingDiv = null;			// preloader div id
	this.mainDiv = null;				// main div id
	this.currentIndex = -1;			// current img index
	this.useRandom = false;			// use random or display in row


	/**
	 * Init rotation
	 */
	this.init = function(obj) {
		this.mainDiv = this.name + 'MainDiv';
		this.loadingDiv = this.name + 'LoadingDiv';
		jQuery('#'+this.loadingDiv+' img').load( function() {
			jQuery('#'+obj.loadingDiv).css('opacity', 0).animate({opacity: 1}, obj.fadeInInterval);
			obj.timer = setTimeout(obj.name+'.intermedSwap()', obj.interval/2);
		});
		this.swap();
	}

	this.intermedSwap = function() {
		jQuery('#'+this.mainDiv).css('background-image', "url('"+this.images[this.currentIndex]+"')");
		this.timer = setTimeout(this.name+'.swap()', this.interval/2);
	}

	/**
	 * Set interval
	 * @param int
	 */
	this.setInterval = function(interval) {
		this.interval = interval;
		return this;
	}

	/**
	 * Set fade in interval
	 * @param int
	 */
	this.setFadeInInterval = function(interval) {
		this.fadeInInterval = interval;
		return this;
	}

	/**
	 * Add image by path (src)
	 * @param string
	 */
	this.addImage = function(src) {
		this.images.push(src);
		return this;
	}

	/**
	 * Set random
	 * @param bool
	 */
	this.setRandom = function(rnd) {
		this.useRandom = rnd;
		return this;
	}

	/**
	 * Fill in main div with loaded img and load next
	 */
	this.swap = function() {
		if (this.useRandom)
			this.getNextRandom();
		else
			this.getNext();
		jQuery('#'+this.loadingDiv).css('opacity', 0);
		jQuery('#'+this.loadingDiv+' img').attr('src', this.images[this.currentIndex]);
	}

	/**
	 * Gets next img
	 */
	this.getNext = function() {
		if (++this.currentIndex >= this.images.length)
			this.currentIndex = 0;
	}

	/**
	 * Gets next img - random
	 */
	this.getNextRandom = function() {
		if (this.images.length < 2)
			return this.currentIndex = 0; // prevent deadlock
		else {
			do 
				index = Math.floor(Math.random()*this.images.length);
			while (index == this.currentIndex);   // prevent repeating
			this.currentIndex = index;
		}
	}

}


