(function($)
{
    $.fn.PromotionScroller = function(options)
    {
		var o = $.extend(
		{
			 adsToDisplay: 3
			,ViewTrackingType: 51
			,ClickTrackingType: 52
			,adRotation: 1
			,rotationSpeed: 600
			,promotionImages: ''
			,borderColor: '#000000'
			,AdRotations: {}
			,EnableViewTracking: 1
			,EnableClickTracking: 0
		}, options);

		//ID,Project,Account,Title,Description,URL,Filename
		this.html('<ul id="ScrollBox"> \
		<li><img class="LeftScrollButton" src="./images/lft-scroll_off.png"></li> \
		<li> \
			<div id="Scroller" style="border: 1px solid ' + o.borderColor + '"> \
				<div id="CurrentPromotions"> </div> \
			</div> \
		</li> \
		<li><img class="RightScrollButton" src="./images/rt-scroll_off.png"></li> \
		</ul>');

		var _totalRotations		= o.AdRotations.length - 1;
		var $CurrentPromotions 	= this.find("#CurrentPromotions");
		var $LeftScrollButton	= this.find(".LeftScrollButton");
		var $RightScrollButton	= this.find(".RightScrollButton");
		var promotionContainerWidth = 320;
		var FirstVisibleID = 0;
		var LastVisibleID;
		var ScrollCounter = 0;
		
		$CurrentPromotions.data('Promotions', {});

		function CreateTrackingEntry(Account, TrackingType)
		{
			$.ajax
			({
				url: "/ClickTracker/click_tracker.php",
				data: 
				({
					c: Account,
					type: TrackingType,
					cl: ''
				}),
				success: function(Response)
				{

				}
			});
		}

		function AddPromotion(_promotion, i)
		{
			var PromotionHTML = '<div style="position: absolute; border-left: 1px solid ' + o.borderColor + '" class="CurrentPromotionContainer"> \
					<table cellspacing="0"> \
					<tr> \
						<td rowspan="2" class="CurrentPromotionTextCell"> \
							<table cellspacing="0"> \
								<tr><td><a href="' + _promotion.URL + '" title=""  class="link hilite bold med" target=_blank>' + _promotion.Title + '</td></tr> \
								<tr><td class="text">' + _promotion.Description + '</td></tr> \
							</table> \
						</td> \
						<td><a href="' + _promotion.URL + '" title="" target=_blank><img class="CurrentPromotionLogo" src="' + o.promotionImages + _promotion.Filename + '" width="80" height="40" alt="' + _promotion.Title + '"/></a></td> \
					</tr> \
					<tr><td class="CurrentPromotionReadMore"><a href="' + _promotion.URL + '" title="" target=_blank><img src="images/Read-More_off.png" alt="Read More"  class="CurrentPromotionReadMore" /></a></td></tr> \
					</table></div>';

			var $Promotion = $(PromotionHTML).appendTo($CurrentPromotions);
			$Promotion.data('ID', _promotion.ID);
			$Promotion.data('Project', _promotion.Project);
			$Promotion.data('Account', _promotion.Account);
			$Promotion.data('Title', _promotion.Title);
			$Promotion.data('Description', _promotion.Description);
			$Promotion.data('URL', _promotion.URL);
			$Promotion.data('Filename', _promotion.Filename);

			$Promotion.bind('click', function(e)
			{
				var target = e.target, // e.target grabs the node that triggered the event.
					$target = $(target);  // wraps the node in a jQuery object

				if ($target.attr("tagName").toLowerCase() == "a" && $target.attr("href") !== undefined)
				{
					CreateTrackingEntry($Promotion.data('Account'), o.ClickTrackingType);
					window.open($target.attr("href"));
				}
				else if ($target.parent().attr("tagName").toLowerCase() == "a" && $target.parent().attr("href") !== undefined)
				{
					CreateTrackingEntry($Promotion.data('Account'), o.ClickTrackingType);
					window.open($target.parent().attr("href"));			
				}

				return false;
			});
			$Promotion.hide();
			return $Promotion;
		}

		// Create elements for each promotion
		for (var i= -1, n = _totalRotations; ++i <= n;)
		{
			$CurrentPromotions.data('Promotions')[i] = new AddPromotion(o.AdRotations[i], i);
		};

		// Only display initial promotions
		for (var i= -1, n = o.adsToDisplay; ++i < n;)
		{
			$CurrentPromotions.data('Promotions')[i].css( { "left": (i * promotionContainerWidth) + "px", "top": "0px" } );
			$CurrentPromotions.data('Promotions')[i].show();
			LastVisibleID = i;
			ScrollCounter++;

			if(o.EnableViewTracking == 1)
			{
				CreateTrackingEntry(o.AdRotations[i].Account, o.ViewTrackingType);
			}
		};
		
		$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseover', function() {
			$(this).attr("src","images/Read-More_overclick.png");
		});

		$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseout', function() {
			$(this).attr("src","images/Read-More_off.png");
		});

		$LeftScrollButton.hover(
		  function() {
			$(this).attr("src","images/lft-scroll_on.png");
		  }, 
		  function() {
			$(this).attr("src","images/lft-scroll_off.png");
		  }
		);

		$RightScrollButton.hover(
		  function () {
			$(this).attr("src","images/rt-scroll_on.png");
		  }, 
		  function () {
			$(this).attr("src","images/rt-scroll_off.png");
		  }
		);

		$CurrentPromotions.css("left", -1);

		$('.RightScrollButton').click(function() 
		{
			if (o.AdRotations.length <= o.adsToDisplay){return false;}
			
			if( !$CurrentPromotions.is(":animated") ) 
			{
				var NextPromotionID;

				if (LastVisibleID < _totalRotations)
				{
					NextPromotionID	= LastVisibleID + 1;
				}
				else
				{
					NextPromotionID	= 0;
				}

				$CurrentPromotions.data('Promotions')[NextPromotionID].css( { "left": (ScrollCounter * promotionContainerWidth) + "px", "top": "0px" } );
 				$CurrentPromotions.data('Promotions')[NextPromotionID].show();
				ScrollCounter++;
				LastVisibleID = NextPromotionID;



				$CurrentPromotions.animate(
				{
					 left:'-=' + promotionContainerWidth
				},o.rotationSpeed
				,function()
				{
						$CurrentPromotions.data('Promotions')[FirstVisibleID].hide();

						if (FirstVisibleID < _totalRotations)
						{
							FirstVisibleID++;
						}
						else
						{
							FirstVisibleID = 0;
						}

					if(o.EnableViewTracking == 1)
					{
						CreateTrackingEntry(o.AdRotations[NextPromotionID].Account, o.ViewTrackingType);
					}
				});

				$CurrentPromotions.find(".CurrentPromotionReadMore").unbind('mouseover');
				$CurrentPromotions.find(".CurrentPromotionReadMore").unbind('mouseout');

				$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseover', function() {
					$(this).attr("src","images/Read-More_overclick.png");
				});

				$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseout', function() {
					$(this).attr("src","images/Read-More_off.png");
				});

			}
			return false;
		});

		$('.LeftScrollButton').click(function() 
		{
			if (o.AdRotations.length <= o.adsToDisplay){return false;}
			
			if( !$CurrentPromotions.is(":animated") ) 
			{
				var NextPromotionID;

				if (FirstVisibleID > 0)
				{
					NextPromotionID	= FirstVisibleID - 1;
				}
				else
				{
					NextPromotionID	= _totalRotations;
				}

				$CurrentPromotions.data('Promotions')[NextPromotionID].css( { "left": ((ScrollCounter - (o.adsToDisplay + 1)) * promotionContainerWidth) + "px", "top": "0px" } );
 				$CurrentPromotions.data('Promotions')[NextPromotionID].show();
				ScrollCounter--;
				FirstVisibleID = NextPromotionID;
				$CurrentPromotions.animate(
				{
					 left:'+=' + promotionContainerWidth
				},o.rotationSpeed
				,function()
				{
						$CurrentPromotions.data('Promotions')[LastVisibleID].hide();

						if (LastVisibleID > 0)
						{
							LastVisibleID--;
						}
						else
						{
							LastVisibleID = _totalRotations;
						}
				
					if(o.EnableViewTracking == 1)
					{
						CreateTrackingEntry(o.AdRotations[NextPromotionID].Account, o.ViewTrackingType);
					}
				});

				$CurrentPromotions.find(".CurrentPromotionReadMore").unbind('mouseover');
				$CurrentPromotions.find(".CurrentPromotionReadMore").unbind('mouseout');

				$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseover', function() {
					$(this).attr("src","images/Read-More_overclick.png");
				});

				$CurrentPromotions.find(".CurrentPromotionReadMore").bind('mouseout', function() {
					$(this).attr("src","images/Read-More_off.png");
				});

			}
			return false;
		});
	}
})(jQuery);
