/*
 * Plugin name:	jQuery marquee plugin
 * Authored by:	The Atom Group
 * 
 */

// Plugin wrapper - named for debugging
(function marquee_Plugin($, window, undefined) {
	
	// Marquee plugin
	// #Context: Marquee wrapper div
	// @_left: The left control - any jQuery selector
	// @_right: The right control - any jQuery selector
	// @_display: The display control - any jQuery selector
	// @_state: The progress control - any jQuery selector
	// @_start: Starting position for display images
    // @_speed: Speed at which the marquee should run
	$.fn.marquee = function(_left, _right, _state, _display, _start, _speed) {
		// Create empty UL - added for validation purposes
		jQuery("<ul/>").insertAfter(this.find("div.scroll-left"));
		
	    // Cache state and display jQuery objects
	    var $state = $(_state), $display = $(_display),
			// Interval variable
			interval = null;
	    
	    // Build state navigation based off of number of display content items
	    for(var i=0; i<$display.length; i++) {
	        $state.append('<li class=""><a class="option" href="#link"></a></li>');
	    }
	    
	    $state.find("li").last().addClass("last");
	    
	    // sets width of nav
	    var $scroll_wrap = $state.parents(".scroll-wrap").width();
	    $state.parents(".scroll-wrap").css({
	        "position": "static",
	        "width": $scroll_wrap
	    });
	    
	    
	    // Set up the initial state and display controls
	    $state.children().eq(_start).addClass("active");
	    $display.eq(_start).show();
		
		// Auto start code
		interval = window.setInterval(function() {
			change.call(jQuery(_right));
		}, _speed);
		
		// Change image in marquee
		function change() {
			// Cache control, and active state jQuery objects
			var $control = $(this), $active_state = $state.children(".active"),
				// Test for left control, default to right
				direction = ( $control.is(_left) ) ? "left" : "right",
				// Obtain current active position, or use the start index, also set to prev_position
				prev_position = position = $active_state.index(),	
				// Increment or decrement the position based on direction
				display_count = $display.length;
			
			// Test if animation is in progress via psuedo class, if not change position
			if(!$display.eq(prev_position).is(":animated")) {
			    // Normalize the position using modulus
			    position = ( direction === "left" ) ? (position-1)%display_count : (position+1)%display_count;
    			
			    // Set the new active class
			    $active_state.removeClass("active");
			    $state.children().eq(position).addClass("active");
    			
			    // Fade out the old, fade in the new - cross fade
			    $display.eq(prev_position).fadeOut("slow");
			    $display.eq(position).fadeIn("slow");
			}
		}
		
		// Li clicks
		$state.delegate("li", "mousedown", function() {
			// Cancel interval
			window.clearInterval(interval);
			
			// Remove all active classes
			$state.find(".active").removeClass("active");
			var $this = jQuery(this);
			
			// Add the new active class
			$this.addClass("active");
			
			// Cross fade in the correct div
			$display.filter(":visible").fadeOut("slow");
			$display.eq($this.index()).fadeIn("slow");
		});
		
		// Preserve chain
		return this.delegate(_left + ", " + _right, "mousedown", function() {
			// Cancel interval
			window.clearInterval(interval);
			
			// Execute change image
			change.call(this);
		});
			
	}

// Pass references to window and undefined to localize
})(jQuery, window, undefined);
