/*
 * This plugin depends on serialScroll
 *
 */

// PUBLIC FUNCTION
TICKER = {};

// _varName mark an optional parameter
TICKER.buildMessageHTML = function(startDate, location, description, _endDate, _count, _nbOfItems, from_label, to_label) {
	var hasEndDateClass = "" ;

	if (!_endDate) {
		hasEndDateClass = " noEndDate";
	}

	return 	'<div class="message-wrapper"><table class="serial ' + hasEndDateClass + '">' +
					'<tbody><tr><td class="beginDate"><h4>' + startDate + '</h4></td><td><h4>' + location + '</h4></td></tr>' +
					'<tr> <td class="endDate"><div class="imgDate"></div><h4>' + (_endDate ? _endDate :'') +
					'</h4></td><td class="description">' + description + '</td></tr></table>' +
					(_count ? '<div class="counter">' + _count + "/" + _nbOfItems + '</div>' : '') +
			'</div>' ;
};


// You really need to wrap the plugin into a function to make it work in IE
(function($) {
	$.fn.showTicker = function(serverTime, options) {

		var settings = $.extend({
			html: "", // inner HTML inside the ticker. Built the html using TICKER.buildMessageHTML for each msg
			noMsgTitle: "Have a nice day", // translated message in case there is no message
			noMsgDescription: "No message for now", // translated message in case there is no message
			reload: true, // set to false to disable ajax reload
			jsonUrl: "/ticker/json",
			timeout: 120000, // default timeout used to reload the ticker
			from_label: "from",
			to_label: "to"
			}, options);

		var $this = $(this);
		var $prev = $('#tickerArrowTop');
		var $next = $('#tickerArrowBottom');

		var reload = function() {
			$.getJSON(settings.jsonUrl, function (data) {
				$this.empty();
				if (data.server_time) {
					showNoMessage(data.server_time);
				} else {
					for(var i=0; i < data.length; i += 1) {
					var msg = data[i];
					$this.append(
						TICKER.buildMessageHTML(msg.occurence_start_date, msg.location, msg.description, msg.occurence_end_date, i + 1, data.length, settings.from_label, settings.to_label));
					}
					if(data.length == 1) {
						removeArrows();
					} else {
						$this.trigger('goto', [0]);
					}
				}
			});
		}


		var removeArrows = function () {
			$prev.add($next).css('visibility', 'hidden');
		};

		var showNoMessage = function(serverTime) {
			removeArrows();
			$this.append(TICKER.buildMessageHTML(serverTime, settings.noMsgTitle, settings.noMsgDescription));
		};

		$this.serialScroll({
			prev: $prev,
			next: $next,
			axis: 'y',
			items: 'table.serial',
			start: 1,
			lazy: true,
			step: 1, //more backwards, one on one (can be another number)
			cycle:false, //you probably don't want this
			onBefore:function( e, elem, pane, $items, pos ){
				$prev.add($next).css('visibility', 'visible');
				if(pos === 0) {
					$prev.css('visibility',	'hidden');
				}else if (pos === $items.length -1) {
					$next.css('visibility' , 'hidden');
				}
			}
		});

		$this.append(settings.html);

		var msgNb = $this.find('.serial').length;
		if(msgNb === 0) {
			showNoMessage(serverTime);
		} else if (msgNb == 1) {
			removeArrows();
		}

		reload(); // To avoid the caching side effect of the home page, force a fetch to the server
		if (settings.reload) {
			setInterval(reload, settings.timeout);
		}

		return $this;
	}
})(jQuery);


