var ticker = { 	//buttonHTML: '<img src="/images/show_all.png" style="width:70px; height:17px; cursor:pointer" />',
		buttonHTML: '<a href="javascript:void();" id="tickerexpand" >Show All</a>',
		buttonOffset: [910, -28],
		dsetting: {snippetLength:30, manual:false, timers: {rotatePauseDuration:3000, fxDuration:300}},
		effectFunctions: ['fadeIn', 'slideDown'],

	fetchAjaxContent:function($, s)
	{
		window.status+='x';
		clearTimeout(s.playTimer);
		clearTimeout(s.pauseTimer);
		clearTimeout(s.refreshAjaxTimer);
		if (s.$button)
		{
			s.$button.remove()
			s.$menu.remove()
		}
		s.$ticker.html("Fetching ticker contents...");
		$.ajax({
			url: s.remoteContent[0],
			error:function(ajaxRequest)
			{
				s.$ticker.html('Error fetching ticker content.<br/><b>Server Response:</b> ' + ajaxRequest.responseText)
			},
			success:function(content)
			{
				s.$ticker.html(content)
				ticker.setup($, s)
				if (s.remoteContent[1] > 5000)
					s.refreshAjaxTimer = setTimeout(function(){ticker.fetchAjaxContent($, s)}, s.remoteContent[1]);
				else if (s.remoteContent[1] > 0)
					alert("A value larger than 5 seconds is needed between AJAX updates");
			}
		});
	},

	getMessageTitles:function(s)
	{
		var titles = [];
		for (var i=0; i < s.msgLength; i++)
		{
			var title = s.$contents.eq(i).attr('title') || s.$contents.eq(i).text().substring(0, s.snippetLength) + ' ...';
			titles.push(title);
		}
		return titles;
	},

	addDropMenu:function($, s)
	{
		var titles = this.getMessageTitles(s);
		var $lis   = $([]);
		var $menu  = $('<ul class="dropdownlist"></ul>');
		for (var i = 0; i < s.msgLength; i++)
		{
			//$lis=$lis.add($('<li/>').html((i+1)+". "+titles[i]).wrapInner('<a href="#message'+(i+1)+'" data-pos="'+i+'"></a>'));
			$lis=$lis.add($('<li/>').html(titles[i]).wrapInner('<a href="#message'+(i+1)+'" data-pos="'+i+'"></a>'));
		}
		
		$menu.append($lis).hide().unbind('click').click(function(e)
		{
			if (e.target.tagName=="A")
			{
				clearTimeout(s.playTimer)
				s.curMsgIdx = parseInt(e.target.getAttribute('data-pos'))
				ticker.selectMessage($, s, s.curMsgIdx)
				if (!s.manual)
					s.playTimer = setTimeout(function(){ ticker.rotateMsg($, s)}, s.timers.rotatePauseDuration)
				e.preventDefault()
			}
		})
		$menu.appendTo(document.body)
		$menu.data('state', 'closed')
		s.$menu    = $menu
		s.$menulis = $lis
	},

	positionButton:function($, s)
	{
		var tickerOffset = s.$ticker.offset();
		var buttonPos    = [tickerOffset.left + this.buttonOffset[0], tickerOffset.top + s.$ticker.outerHeight() + this.buttonOffset[1]];
		s.$button.css({left:buttonPos[0], top:buttonPos[1]});
	},

	addExpandButton:function($, s)
	{
		s.$button = $(this.buttonHTML).css({position:'absolute'}).appendTo(document.body);
		this.positionButton($, s);
		this.addDropMenu($, s);
		s.$button.unbind('click').bind('click', function(e)
		{
			//s.$menu.css({left:s.$button.css('left'), top:parseInt(s.$button.css('top')) - ticker.buttonOffset[1]}).show();
			s.$menu.css({zIndex:1100, left:s.$button.css('left'), top:parseInt(s.$button.css('top')) - s.$menu.outerHeight()}).show();
			s.$menulis.removeClass('selected').eq(s.curMsgIdx).addClass('selected');
			s.$menu.data('state', 'open');
			e.stopPropagation();
		});
	},

	selectMessage:function($, s, selected)
	{
		s.$contents.stop(true,true).hide().eq(selected)[s.fxFunction](s.timers.fxDuration, function()
		{
			if (this.style && this.style.removeAttribute) this.style.removeAttribute('filter');
		})
		s.curMsgIdx = selected;
		if (s.$menu.data('state')=="open")
			s.$menulis.removeClass('selected').eq(selected).addClass('selected');
	},

	rotateMsg:function($, s)
	{
		if (s.$ticker.data('state')=="over")
		{
			clearTimeout(s.pauseTimer)
			s.pauseTimer = setTimeout(function(){ticker.rotateMsg($, s)}, 100);
			return;
		}
		s.nextMsgIdx = (s.curMsgIdx < s.msgLength - 1) ? s.curMsgIdx + 1 : 0;
		this.selectMessage($, s, s.nextMsgIdx);
		s.playTimer = setTimeout(function(){ticker.rotateMsg($, s)}, s.timers.rotatePauseDuration);
	},

	setup:function($, s)
	{
		s.$contents = s.$ticker.find('.tickercontent').hide();
		s.msgLength = s.$contents.length;
		s.curMsgIdx = 0;
		ticker.addExpandButton($, s);
		ticker.selectMessage($, s, s.curMsgIdx);
		if (!s.manual)
		{
			s.$ticker.unbind('mouseenter').bind('mouseenter', function(){$(this).data('state', 'over')});
			s.$ticker.unbind('mouseleave').bind('mouseleave', function(){$(this).data('state', 'out')});
			s.playTimer = setTimeout(function(){ticker.rotateMsg($, s)}, s.timers.rotatePauseDuration);
		}
	},

	init:function(settings)
	{
		// Fired when DOM of HTML is ready
		jQuery(document).ready(function($)
		{ 
			var s = settings
			s = jQuery.extend({}, ticker.dsetting, s);
			s.timers.rotatePauseDuration += s.timers.fxDuration;
			s.$ticker = $('#'+s.id);
			if (s.$ticker.length==0)
				return;
			s.fxFunction = (s.fx == "fade") ? ticker.effectFunctions[0] : ticker.effectFunctions[1];
			if (s.remoteContent && s.remoteContent[0].length > 0)
			{
				s.remoteContent[1] = s.remoteContent[1] * 1000;
				ticker.fetchAjaxContent($, s)
			}
			else
			{
				ticker.setup($, s);
			}
			
			$(window).bind('load resize', function(e)
			{
				if (s.$button) setTimeout(function(){ticker.positionButton($, s)}, ( e.type == "load") ? 200 : 0);
			})
			
			$(document).click(function()
			{
				if (s.$menu)
				{
					s.$menu.hide();
					s.$menu.data('state', 'closed');
				}
			});
			
		});
	}
}
