(function($){
	
	$.fn.mfdisplay = function(options){
		
		var defaults = {
			totalWidth:720,
			totalHeight:344,
			panelWidth: 636,
			duration: 400,
			pause:5,
			image_path:'images',
			fadeSpeed:400
		}
		
		var options = $.extend(defaults, options);
		
		return this.each(function(){
			var themodule = $(this);
			var o = options;
			var intervalId;
			var currentItem;
			o.pause = o.pause * 1000; // convert pause to seconds
			var curPos = 0;
			var indicator = '<div class="mf_current"></div>';
			var oldItem;
			
			//wrap with a DIV class, CSS
			//$(this).wrap('<div class="mf_carousel" />');
			
			//Structure
			if (o.mydata)
			{
				themodule.append('<ul class="mf_image_carousel"></ul><div class="mf_arrows_left"></div><div class="mf_scroller"><ul></ul></div><div class="mf_arrows_right"></div>');
			
				//create the panels and buttons
				var i = 0;
				$.each(o.mydata, function(key, item){
					i++;
					themodule.find('.mf_image_carousel').append('<li  id="item'+i+'"><div class="mf_img"><img src="'+item.panel_img+'" width="696" height="258" alt="" /></div><div class="mf_text">'+item.panel_text+'</div><div class="mf_link"><a class="learn_more_button" href="'+item.learn_link+'">Learn More</a></div></li>');
				
					themodule.find('.mf_scroller ul').append('<li rel="item'+i+'"><h2>'+item.my_title+'</h2><p>'+item.my_text+'</p></li>');

				});
			}
			
			var myItems = themodule.find('.mf_scroller ul').children();
			var myPanels = themodule.children('.mf_image_carousel').children();
			var leftButton = themodule.find('.mf_arrows_left');
			var rightButton = themodule.find('.mf_arrows_right');
			
			var scrollerUL = themodule.find('.mf_scroller ul');
		
			var scrollWidth = myItems.first().outerWidth();
			var scrollTotalWidth = myItems.length * 213;
			
			scrollerUL.css({width:scrollTotalWidth+'px'});
			
			

			// Init
			myItems.prepend(indicator);
			myPanels.hide();
			currentItem = myItems.first();
			select_item(currentItem, false);
			
			
			myItems.click(function(){
				select_item($(this), false);
				
			});
			
			if (myItems.length < 4)
			{
				leftButton.css({opacity:'.0'});
				rightButton.css({opacity:'.0'});
				leftButton.hide();
				rightButton.hide();
			}
				
			leftButton.click(function(){
				var newPos = curPos + scrollWidth;
				if (newPos <= 0)
				{
					curPos = newPos;
					move_scroller(newPos);
					clearInterval(intervalId);
					start_countdown();
				}
			});
			
			rightButton.hover(
				function(){ //rollover
					var newPos = curPos - scrollWidth;
					if (newPos > o.panelWidth- scrollTotalWidth)
					{
						$(this).css({backgroundPosition:'-29px 0px'});
						if(!$.support.cssFloat) {
							$(this).css({backgroundPositionX: '-29px',
							                backgroundPositionY: '0px'});
						}
					}
						
				},
				function(){ //rollout
				$(this).css({backgroundPosition:'0px 0px'});
			});
			
			leftButton.hover(
					function(){ //rollover
						var newPos = curPos + scrollWidth;
						if (newPos <= 0)
						{
							$(this).css({backgroundPosition:'-29px -63px'});
							if(!$.support.cssFloat) {
								$(this).css({backgroundPositionX: '-29px',
								                backgroundPositionY: '-63px'});
							}
						}
							
					},
					function(){ //rollout
							$(this).css({backgroundPosition:'0px -63px'});
					
					});
					
			rightButton.click(function(){
				var newPos = curPos - scrollWidth;
				if (newPos > o.panelWidth- scrollTotalWidth)
				{
					curPos = newPos;
					move_scroller(newPos);
					clearInterval(intervalId);
					start_countdown();
				}
					
			});
			
			function move_scroller(pos)
			{
				scrollerUL.animate({marginLeft: pos+"px"}, { queue:false, duration:o.duration, easing: 'easeOutExpo'}); 
			}
			
			function select_item(item, automatic)
			{
				if (item.length ==0)
					item = myItems.first();

				var oldItemName = currentItem.attr('rel');
				$('#'+oldItemName).hide();

				currentItem = item;

				var itemName = item.attr('rel');
				$('#'+itemName).show().css({opacity:'0'});
				$('#'+itemName).animate({'opacity' : 1}, o.fadeSpeed, function() {	
									
					});
					
				// 
				
				$('.mf_current').hide();
				item.find('.mf_current').show();
				myItems.removeClass('is_current');
				item.addClass('is_current');
				
				// alert(item.position().left);
				if (item.position().left < 0)
				{
					curPos = 0;
					move_scroller(curPos);
				}
				
				if (item.position().left > 426)
				{
					curPos = curPos - scrollWidth;
					move_scroller(curPos);
				}

				if (!automatic)
				{
					clearInterval(intervalId);
					start_countdown();
				}
					
			}
			
			
			function start_countdown()
			{
				intervalId = setInterval(function() {
					select_item(currentItem.next(), true);
								}, o.pause);
			}
			
			
			}); // end each		

		} // End plugin.
	
})(jQuery);


