if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			promotionScroller: function(options) {
				var settings = $.extend({}, $.fn.promotionScroller.defaults, options);
			
				return this.each(
					function() {
						if($.fn.jquery < '1.2.6') {return;}
						var $t = $(this);
						var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
						
						var isPlaying = false;
						var isAnimating = false;
						var isDirect = false;
						var curItem = 1;
						var nextItem = 2;
						var totalItems = 0;
						var highestZindex = 0;

						$t.find('ul').wrap('<div class="promoHolder"></div>');

						var $h = $('.promoHolder');
						var $b = $h.find('li');
						var maxItems = ($b.length <= o['maxItems']) ? $b.length : o['maxItems'];
						
						if(($b == null) || (maxItems <= 1))
							return;
							
						$t.prepend("<div class=\"promoControls\"></div>");

						var $l = $t.find('.promoControls');
						
						$l.prepend("<div class=\"promoCtrls\"></div>");
						$l.prepend("<div class=\"promoTitle\"></div>");
						
						var $c = $l.find('.promoCtrls');
						var $m = $l.find('.promoTitle');
						$c.append("<div class=\"indicator\">&nbsp;</div>");
						$c.append("<ul></ul>");

						var count = 1;
						$b.each(function(){
							if(count <= maxItems){
								$c.find("ul").append("<li id=\"pc" + count + "\"><a href=\"#" + count + "\"" + ((count==1) ? " class=\"on\"" : "") + ">" + count + "</a></li>");
								$(this).css({position:"absolute", top:0, left:0, display:((count==1)?"block":"none"), zIndex:count++});
								totalItems++;
								
								if(totalItems == 1)
								{
									$c.find(".indicator")
										//.css({width: $("#pc1").outerWidth(), height: $("#pc1").outerHeight()});
										.css({left:$("#pc1").position().left});

									//Find any text and update the ticker
									var altTxt = $(this).find("img").attr("alt");
									$m.text(altTxt);
									$m.jTypeWriter({duration: (o['textTimer'] / 1000)});									
								}
							} else {
								$(this).remove();
							}
						});
						
						$c.append("<div class=\"pausePlay Pause\">Pause</div>");
						
						if(o['paused']){
							$c.find(".pausePlay").removeClass("paused").addClass("play").text(o["playText"]);
						} else {
							isPlaying = true;
						}
						
						//Wire up the play/pause button
						$c.find(".pausePlay").click(function(){
							var whatAction = $(this).text();
							if(isPlaying){
								isPlaying = false;
								$t.stopTime();
								$(this).removeClass("paused").addClass("play");
								$(this).text(o["playText"]);
							} else {
								isPlaying = true;
								animate(0);
								$t.everyTime(o['pauseTimer'], animate, 0);
								$(this).removeClass("play").addClass("paused");
								$(this).text(o["pauseText"]);
							}
							return false;
						});

						$c.find("li a").click(function(){
							var goWhere = $(this).text();
							$t.find(".pausePlay").removeClass("paused").addClass("play");
							$t.find(".pausePlay").text(o["playText"]);
							isPlaying = false;
							$t.stopTime();
							nextItem = $(this).text();
							isDirect = true;
							animate();
							$(this).blur();
							return false;
						});
						
						$c.css('width', $c.outerWidth() + 50);

						//Wire up the play function
						$t.everyTime(o['pauseTimer'], animate, 0);
						
						function animate(i) {
							if(!isDirect){
								if(isAnimating || !isPlaying)
									return;
							}

							isAnimating = true;
							$c.find("li a").removeClass("on");
							$h.find("li").each(function(){
								highestZindex = ($(this).css("zIndex") > highestZindex) ? $(this).css("zIndex") : highestZindex;
							});
							
							var cnt = 1;
							$h.find("li").each(function(){
								if(cnt++ == nextItem){
									$l.css({zIndex:++highestZindex + 1});
									$c.css({zIndex:highestZindex + 2});
									$m.css({zIndex:highestZindex + 3});
									
									$m.text('');

									$(this).css({
										zIndex:highestZindex
									}).fadeIn(o['animateTimer'],function(){
										$h.find("li").each(function(){
											if($(this).css("zIndex") != highestZindex){
												$(this).css({display:"none"});
											}
										});						
										$c.find("li a").each(function(){
											if($(this).text() == nextItem){
												$(this).addClass("on");
											}
										});
										nextItem = ((nextItem + 1) > maxItems || (nextItem + 1) > totalItems) ? 1 : ++nextItem;
										isAnimating = false;
									});
									
									$c.find(".indicator")
										//.css({width: $("#pc" + (cnt-1)).outerWidth(), height: $("#pc" + (cnt-1)).outerHeight()})
										.animate({left:$("#pc" + (cnt-1)).position().left},o['animateTimer']);
									
									//Find any text and update the ticker
									var altTxt = $(this).find("img").attr("alt");
									$m.text(altTxt);
									$m.jTypeWriter({duration: (o['textTimer'] / 1000)});									
								}
							});
						}
					}
				);
			}
		});
		
		/*
		* Plugin Defaults
		*/
		$.fn.promotionScroller.defaults = {
				maxItems : 5,
				pauseTimer : 4000,
				animateTimer : 1000,
				textTimer : 1000,
				pause : false,
				pauseText : "Pause",
				playText : "Play"
			};
	});
}

$(document).ready(function(){
	$('#scroller-main').promotionScroller();
});