//
// jquery.showcase.js
//

(function($){
	// Common Valiables
	var _self, timer;
	var settings = {};
	var current = 0, next = 1, len = 0;
	var imagePool, menuPool, attrPool = [];
	var BASE_ZINDEX = 9;
	var screenWidth = 0;
	
	// Settings
	var defaults = {
		imagesClassName: "images",
		menusClassName: "menus",
		activeMenuClassName: "active",
		autoslide: true,
		duration: 5000
	};
	
	//
	// Functions
	//
	
	// Start Slide Timer
	var startTimer = function(){
		//console.log("Start timer");
		timer = setTimeout(function(){
			try{
				update();
			}catch(e){
				//console.log(e);
				stopTimer();
			}
		}, settings.duration);
	};
	// Stop Slide Timer
	var stopTimer = function(){
		//console.log("Stop timer");
		clearTimeout(timer);
	};
	// Regular Interval Update
	var update = function(){
		//console.log("update "+current+" -> "+next);
		swapImages();
		moveImage();
	};
	// Use Autoslide
	var startAutoslide = function(){
		//console.log("start autoslide");
		settings.autoslide = true;
		startTimer();
	};
	// Stop Autoslide
	var stopAutoslide = function(){
		//console.log("stop autoslide");
		settings.autoslide = false;
		stopTimer();
	};
	// Swap z-index of Images
	var swapImages = function(){
		
		// All Images Down to Bottom
		imagePool.each(function(){
			$(this).css("zIndex", BASE_ZINDEX-3)
		});
		
		// Current Image Raise to Top
		var currentImage = imagePool.get(current);
		$(currentImage).css("zIndex", BASE_ZINDEX);
		
		// Next Image Raise to Second
		var nextImage = imagePool.get(next);
		$(nextImage).css("zIndex", BASE_ZINDEX-1)
	};
	// Slide Animation Execute
	var moveImage = function(){
		$(imagePool.get(current)).animate({ 
			"opacity": 0
		}, 1000, "linear", function(){
			onAnimationFinish();
		});
		$(imagePool.get(next)).animate({
			"opacity": 1
		}, 990, "linear");
		/*$(imagePool.get(current)).fadeOut(1000, function(){
			onAnimationFinish();
		});
		$(imagePool.get(next)).fadeIn(990);*/
		// Custom Event Launch
		//console.log(imagePool.eq(next).find("img").attr("src").indexOf("_b.")>0);
		var colorFlag = imagePool.eq(next).find("img").attr("src").indexOf("_b.")>0;
		_self.trigger("onAnimationStart", colorFlag);
	};
	// Slide Animation Callback
	var onAnimationFinish = function(){
		var colorFlag = imagePool.eq(next).find("img").attr("src").indexOf("_b.")>0;
		_self.trigger("onAnimationFinish", colorFlag);
		// Count Up
		current = next++;
		len = imagePool.size();
		// Counter Reset
		if(next>=len) { next=0; }
		
		// Update Recurse
		if(settings.autoslide) { startTimer(); }
	};
	//
	// Public Methods
	//
	var methods = {
		init : function(options){
			
			//console.log("Initializing");
			settings = $.extend(defaults, options||{});
			
			// Find Images and Menus
			imagePool = _self.find("."+settings.imagesClassName+" li");
			
			swapImages();
			//startTimer();
			
			// Loading effects
			_self.find("."+settings.imagesClassName).css('opacity', 0);
			_self.append(
				$('<img/>').attr("src", "img/com/loading.gif")
					.addClass("loading")
					.css("position", "absolute")
					.css("left", "50%")
					.css("top", "195px")
					.css("margin-left", "-16px")
					.css("margin-top", "-16px")
			);
			
			$(window).load(function(){
				_self.find('.loading').fadeOut(500, function(){
					$(this).remove();	
				});
				//_self.find("."+settings.imagesClassName).fadeIn(500, function(){
				//	startTimer();
				//});
				_self.find("."+settings.imagesClassName).animate({ 
					"opacity": 1
				}, 1000, "linear", function(){
					startTimer();
				});
				//screenWidth = document.body.clientWidth||document.documentElement.clientWidth;
				//console.log(screenWidth);
			});
			
		},
		startAutoslide: function(){
			startAutoslide();
		},
		stopAutoslide: function(){
			stopAutoslide();
		}
	};

	//
	// Constructor
	//
	$.fn.showcase = function( method ) {
		_self = $(this);
			
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.showcase' );
		}
		return this;
	};
})(jQuery);
