/*
 * This plugin depends on serialScroll
 *
 */

NEWS = {}

NEWS.buildHTML = function(title, content, imgUrl, href, staticText) {
  return '<div class="serial" onclick="window.location=' + "'" + href + "';" + '">' +
    '<a href="'+ href +'" class="lireSuite">' + staticText + '</a>' + // this should be positioned 'absolute' to the parent div.serial (see style in css file)
    (imgUrl ? '<img alt="" class="newsImg" src="' + imgUrl + '"/>' :'') +
    '<h4>' + title + '</h4>' +
    '<p class="content">' + content + '</p>' +
    '</div>';
};

(function($) {
   jQuery.fn.showNews = function(jsonUrl, staticText, options) {
     var settings = $.extend(
       {
	 interval: 15000, // it cycles every 15 sec
	 ajaxTimeout:150000, //it waits 5 min to reload
	 animationSpeed: 1800 // it fades in for about 2 sec
       }, options);
     var $this = $(this);
     var load = function() {
       $.getJSON(jsonUrl, function (data) {
		   $this.empty();
		   for(var i=0; i < data.length; i += 1) {
		     var one_news = data[i];
		     $this.append(
		       NEWS.buildHTML(one_news.title, one_news.summary, one_news.img, one_news.url, staticText));
		   }

		   $this.trigger('goto', [0]);
		   $this.find('.serial:first').show();
		 });
     }

     $this.serialScroll({
			  axis: 'y',
			  items: 'div.serial',
			  start: 1,
			  interval: settings.interval,
			  lazy: true,
			  step: 1, //more backwards, one on one (can be another number)
			  cycle:true,
			  onBefore:function( e, elem, pane, $items, pos ){
			    $(elem).fadeIn(settings.animationSpeed);
			  }
			});

     load();
     if(settings.ajaxTimeout) {
       setInterval(load, settings.ajaxTimeout);
     }

     return $this;
   };
 })(jQuery);

