/**
 * MMS Carousel / Slideshow for Dream Republic
 * @author Charles Peter
 * @email vmxbmx@yahoo.com
 * 
 */

;(function($) {
	
	var cache = [];
	
	$.mmsCarouselClass = function() {
		this.construct();
	};
	
	$.extend($.mmsCarouselClass.prototype, {
		construct: function() {
			this.run();
		},
		
		run: function() {
			
			// preload images
			
			var imgs = ['images/dreamcards_preview/01.gif', 'images/dreamcards_preview/02.gif', 'images/dreamcards_preview/03.gif', 'images/dreamcards_preview/04.gif'];
			var timg = '';
			for (var j=0; j < imgs.length; j++) {
				var cacheImage = document.createElement('img');
				cacheImage.src = imgs[j];
				cache.push(cacheImage);
			}
			
			// run!
			
			setInterval(function() {
				var img = $('.tcard img');
				$(img).animate({ opacity: 0 }, 1000, function() {
					
					var currImg = $(img).attr('src').toString().match(/[0-9]+/);
					currImg = parseInt(currImg);
					
					var exists = false;
					var nextImg = 'images/dreamcards_preview/0' + parseInt(currImg + 1) + '.gif';
					for (var i=0; i<imgs.length; i++) {
						if (imgs[i] == nextImg) {
							exists = true;
						}
					}
					
					if (exists) {
						$(img).attr('src', nextImg).animate({opacity: 1}, 1000);
					}
					else {
						nextImg = 'images/dreamcards_preview/01.gif';
						$(img).attr('src', nextImg).animate({opacity: 1}, 1000);
					}
				});
			}, 5000);
		}
	});
	
	$(document).ready(function() {
		if (typeof $.mmsCarousel == 'undefined') {
			$.mmsCarousel = new $.mmsCarouselClass();
		}
	});
	
})(jQuery);

