(function($) {
	$.fn.specPicker = function( userOptions ) {
		var defaultOptions = {
			speed: 500,
			filmstripW: 0,
			filmstripH: 0,
			filmstripP: 0.8,
			imageW:1400,
			imageH:900,
			imageM:50,
			backButton:"color_back",
			nextButton:"color_next",
			thumbnailHolder:"color_nav",
			imageHolder:"color_images",
			activeClass:"active",
			panelActiveClass:"active"
		}
		var uO = $.extend( defaultOptions, userOptions );

		var self = this;
	
		return this.each(function() {
//console.log( "Attaching Spec Picker..." );
			var $this = $( this );
			var $panels = $this.find( ".spec_panels" );
			var $nav = $this.find( ".spec_nav" );
			var $viewport = $this.find( ".spec_viewport" );

			var wrapperW = $this.width();
			var wrapperH = $this.height();
			var totalImages = $nav.find( ".spec_viewport li" ).length;
			var activeIndex = 0;
			var imageIndex = 0;
			var currentPanel = 0;
			var activePanel = 0;

			/****************************************
			 *  Functions
			 */

			function getCurrentDuration() {
				var currentDistance = Math.abs( currentPanel - activePanel );
				var currentDuration = ( currentDistance * currentDistance ) + ( 300 * Math.abs( currentDistance ) ) + 200;
//				console.log( currentDuration );
				return currentDuration;
			}
			function initialize() {
				$viewport.find( "li:eq(0)" ).addClass( "active" );
				$viewport.find( "li" ).bind( "click", function() {
					currentPanel = $( this ).index();
					activePanel = $( this ).parent().find( ".active" ).index();
					$( this ).parent().find( "li" ).removeClass( "active" );

//					console.log( "current -> " + currentPanel + ", active -> " + activePanel );
					if( currentPanel != activePanel ) {
						var panelOffset = 0 - uO.imageW * currentPanel;
						$panels.fadeTo( uO.speed, 0.25, function() {
							$panels.animate({left:panelOffset + "px"}, getCurrentDuration(), function() {
								$panels.fadeTo( uO.speed, 1.0, function() {
									$viewport.find( "li:eq(" + currentPanel + ")" ).addClass( "active" );
									fireTag(2207.2,{'<specs_title>':currentPanel});
								});
								
							});
						});
					}

				});
			} // end initialize
			initialize();

		}); // end return this
		
		return this;
	}; // end plugin
	
	$.log = function(message) {
		if(window.console) {
			console.debug(message);
		} else {
			alert(message);
		}
	};
})(jQuery);

