(function($){
	
	
	////////////////////////////////////////////////////////////
	//	PRIMARY NAVIGATION
	////////////////////////////////////////////////////////////
	$.fn.geoPrimaryNavigation = function( options ) {
		
		// Multiple Elements ? Regsiter all elements
		if (this.length > 1) {
			$(this).each( function() { $(this).geoPrimaryNavigation( options ); } );
			return this;
		}
		
		// Private Variables
		var defaults = {
			offsetTop:6,
			offsetLeftColl: {}
		};
		var options = $.extend( defaults, options );
		var scope = this;
		var listObjs = $("li", this);
		var menuDelay = null;
		var currentMenu;
		var isAllowedToShow = false;
		
		// Private Methods
		var init = function() {
			$.each(listObjs, function() {
				var id = $(this).attr("id");
				var m = $("div#"+id+"-menu");
				var l = $(this).position().left-options.offsetLeftColl[id];
				m.css({
					"position":"absolute",
					"top":$(this).position().top-options.offsetTop,
					"left":l
				});
				
				//alignPanel( m );
				
				if (id != "") {
					$(this).hover( onListItemOver, onListItemOut );
					m.mouseleave( onListMenuOut );
					
					$("#gallery-background").mouseenter( function() {
						// Force hide all other menus.
						$.each(listObjs, function(index) {
							id = $(this).attr("id");
							m = $("div#"+id+"-menu");
							m.trigger("mouseleave");
						});
					});
				}
			});
		};
		
		var onListItemOver = function() {
			if (currentMenu)
				currentMenu.trigger("mouseleave");
			
			var cid = $(this).attr("id");
			currentMenu = $("#"+cid+"-menu");
			
			isAllowedToShow = true;
			menuDelay = window.setTimeout(showMenuNow, 600);
		}
		
		var onListItemOut = function() {
			window.clearTimeout( menuDelay );
			isAllowedToShow = false;
			
			var cid = $(this).attr("id");
			var id;
			
			// Force hide all other menus.
			$.each(listObjs, function(index) {
				id = $(this).attr("id");
				m = $("div#"+id+"-menu");
				if (cid !== id) {
					m.trigger("mouseleave");
				}
			});
		}
		
		var showMenuNow = function() {
			window.clearTimeout( menuDelay );
			if (isAllowedToShow) {
				currentMenu.show();
			}
		}
		
		var onListMenuOut = function() {
			isAllowedToShow = false;
			window.clearTimeout(menuDelay);
			
			var m = $("div#"+$(this).attr("id"));
			m.hide();
		}

		var alignPanel = function( menu ) {
			if (menu.hasClass("menu-align-right")) {
				var tab = $(menu).find(".primary-nav-menu-tab");
				var contents = $(menu).find(".primary-nav-menu-contents");
				tab.css({
					"position":"absolute",
					"right":"0"
				});
				contents.css({
					"position":"absolute",
					"left" : -(menu.outerWidth()/2)
				});
			}
		}
		
		// Public Methods
		scope.initialize = function() {
			init();
			return scope;
		};
		
		// Support Chaining
		return scope.initialize();
	}
	
	
	////////////////////////////////////////////////////////////
	//	Gallery Controller : 3 B'S
	////////////////////////////////////////////////////////////
	$.fn.geoGalleryController = function( options ) {
		
		// Multiple Elements ? Regsiter all elements
		if (this.length > 1) {
			$(this).each( function() { $(this).geoGalleryController( 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();
	}
})(jQuery);

