(function ($)
{
	$.fn.extend(
	{
		mktimeline: function (d)
		{
			d = $.extend(
			{
				canvas_width: 203,
				bar_width: 29,
				table_width: 250,
				offset: 0,
				delay:100,
				repeat:100
			}, d);

			return this.each(function ()
			{
				var a = d,
					c = $(this);
				var delayId, repeatId;
				a.canvas_width =c.width()- 2*$('.goBack').width()-10;
				//a.canvas_width = $(".tlMid", c).width();
				$(".tlMid", c).width(a.canvas_width);
				a.table_width = $("table", c).width();
				a.bar_width = a.table_width / $("table tr:first td", c).size();

				a.offset = a.canvas_width - a.bar_width - $(".tcurrent", c).position().left;
				if (a.offset <= a.canvas_width - a.table_width) a.offset = a.canvas_width - a.table_width;
				if (a.offset > 0) a.offset = 0;

				$("table", c).animate({left: a.offset}, 300);

				$(".timebar", c).hover(
					function () {$(this).addClass("thover")},
					function (){$(this).removeClass("thover")}
				);
				$(".goBack, .goFwd", c).hover(
					function (){$(this).addClass("gohover")},
					function (){$(this).removeClass("gohover")}
				);
				$(".goFwd", c)
				.mousedown(function (){startArFwd()})
				.mouseout(function (){stopAutorepeat()})
				.mouseup(function (){stopAutorepeat()});
				
				$(".goBack", c)
				.mousedown(function (){startArBack()})
				.mouseup(function (){stopAutorepeat()})
				.mouseout(function (){stopAutorepeat()});

				

				function actFwd()
				{
					a.offset -= a.bar_width;
					if (a.offset <= a.canvas_width - a.table_width) a.offset = a.canvas_width - a.table_width;
					$("table", c).animate({left: a.offset}, 50);
				}

				function actBack()
				{
					a.offset += a.bar_width;
					if (a.offset > 0) a.offset = 0;
					$("table", c).animate({left: a.offset}, 50);
				}
				
				function startArFwd(){
					stopAutorepeat();
					delayId = window.setTimeout(function(){
						actFwd();
						repeatId = window.setInterval(function(){
							actFwd();
						},a.repeat);
					},a.delay);
				};

				function startArBack(){
					stopAutorepeat();
					delayId = window.setTimeout(function(){
						actBack();
						repeatId = window.setInterval(function(){
							actBack();
						},a.repeat);
					},a.delay);
				};

				function stopAutorepeat(){
					window.clearTimeout(delayId);
					window.clearInterval(repeatId);
				};

			})
		}
	})
})(jQuery);
