// JavaScript Document

// Source from http://www.geoeye.com 

$.fn.homeSlider = function( options ) {
		
		// Multiple Elements ? Regsiter all elements
		if (this.length > 1) {
			$(this).each( function() { $(this).homeSlider( options ); } );
			return this;
		}
		
		// Private Variables
		var scope = this;
		var defaults = {
			collection0:null,
			collection1:null,
			collection2:null,
			collection0Class:null,
			collection1Class:null,
			collection2Class:null,
			time:8000,
			mouseTarget:document,
			menuTargets:"",
			mouseTargetY:470
		};
		var options = $.extend( defaults, options );
		var listObjs = $("li", scope);
		var isRunning = false;
		var intervalID;
		var currentIndex = 0;
		var nextIndex = -1;
		
		// Private Methods
		var init = function() {};
		var initListeners = function() {
			//$(options.mouseTarget).bind( 'mousemove', onUserMouseMove );
			$(options.mouseTarget).bind( 'mouseover', onUserMouseAction );
			$(options.mouseTarget).bind( 'mouseout', onUserMouseAction );
			
			if (options.menuTargets.length > 0) {
				$(options.menuTargets).bind( 'mouseover', onUserMenuAction );
				$(options.menuTargets).bind( 'mouseout', onUserMenuAction );
			}
			
			$(window).resize( onWindowResize );
			$(window).trigger('resize');
		};
		var onWindowResize = function( e ) {
			var targetW = listObjs.width();
			var winW = $(window).width();
			var w;
			if (targetW > winW) {
				w = -((targetW - winW) / 2);
			}
			else {
				w = ((winW - targetW ) / 2);
			}
			$.each(listObjs, function() {
				$(this).css("left", w+"px");
			});
		}
		
		
		
		var onUserMouseAction = function( e ) {
			if (isRunning) {
				stop();
			}
			else {
				//reset();
			}
		}
		var onUserMenuAction = function( e ) {
			//if (isRunning) {
				stop();
				var c;
				var id = $(this).attr("id");
				$.each(options.menuTargets, function(index) {
					c = "animation-target-"+(index+1);
					if (id == c) {
						nextIndex = (index+1);
					}
				});
				next();
			//} else {
				//reset();
			//}
		}
		
		var onTimedEvent = function( e ) {
			next();
		};
		var next = function() {
			if (nextIndex == currentIndex) {
				return;
			}
			
			// HIDE
			options.collection1.filter(options.collection1Class+currentIndex).hide();
			options.collection0.filter(options.collection0Class+currentIndex).delay(600).fadeOut(400);
			
			var busLine = $("div .business-line-sprites");
			var oldClass = options.collection2[currentIndex];
			if (busLine.hasClass( oldClass ))
				busLine.removeClass( oldClass )
			
			if (nextIndex == -1) {
				currentIndex++;
				if (currentIndex >= listObjs.length) {
					currentIndex = 0;
				}
			} else {
				currentIndex = nextIndex;
				nextIndex = -1;
			}
			
			// SHOW
			options.collection0.filter(options.collection0Class+currentIndex).delay(100).fadeIn(400, function() {
				var e = options.collection1.filter(options.collection1Class+currentIndex);
					e.show();
					var newClass = options.collection2[currentIndex];
					if ( newClass != "null")
						busLine.addClass( newClass );
			});
		};
		var run = function() {
			isRunning = true;
			intervalID = setInterval( onTimedEvent, options.time );
		};
		var reset = function() {
			if (isRunning) {
				stop();
			}
			run();
		};
		var stop = function() {
			isRunning = false;
			clearInterval( intervalID );
		};
		
		
		// Public Methods
		scope.initialize = function() {
			init();
			initListeners();
			run();
			return scope;
		};
		
		// Support Chaining
		return scope.initialize();
	}

