/* ****************************************************

	@file         global.js
	@description  Comportements globaux
	vim: set noet ts=4 fdm=marker fenc=utf-8:

***************************************************** */

(function($) {

	$(function() {

/* @section Récupération des tweets en AJAX {{{
******************************************************************************/

		var ixTweets = {

			/** Initialisation {{{ */
			init : function() {
				var self = this;

				self.is_mobile = $('body').is('.template-mobile');

				self.liste = $('#tweets').append('<ul></ul>').find('ul').bind('resize_tweets', function() {
					$(this).height( $(window).height() );
					$('.informations-inner').each(function() {
						if ( $(window).height() > $(this).height() ) {
							var t = parseInt( ( $(window).height() - $(this).height() - 60 ) / 2 );
							$(this).css('padding-top', t+'px' );
						}
					});
				})

				if ( !self.is_mobile ) {
					self.liste.trigger('resize_tweets').overscroll();
					$(window).resize(function() {
						self.liste.trigger('resize_tweets');
					});
					$(document).bind("fontresize", function () {
						self.liste.trigger('resize_tweets');
					});
				}

				if ( self.liste.length ) {
					self.first_load = true;
					self.tweets = {};

					setInterval(function() {
						self.refresh( );
					}, 30 * 1000);
					self.refresh( );
				}

			},
			/*}}}*/

			/** Rafraichissement de la liste {{{ */
			refresh : function() {
				var self = this;
				$.ajax({
					type : 'GET',
					url : '/get_tweets/',
					dataType : 'json',
					success : function( data ) {
						data.results.reverse(); // twitter nous renvoit les résultats à l’envers…
						self.add( data );
					}
				});
			},
			/*}}}*/

			/** Ajout des nouveaux tweets {{{
			 *
			 * @param Object data Les données reçues
			 */
			add : function( data ) {
				var self = this;
				self.liste.find('.clicknscroll').remove();
				var html = '';
				$.each( data.results, function() {
					if ( !self.tweets[this.id] ) {
						var text = this.text.twitterize();
						html = '<li class="tweet"><div class="'+(!self.first_load?'tweet-new ':'')+'tweet-inner groupe"><div class="tweet-avatar"><img width="48" height="48" src="'+this.profile_image_url+'" alt="" ></div><div class="tweet-content"><div class="tweet-text"><strong>'+('@'+this.from_user).twitterize().replace(/@/,'')+'</strong> '+text+'</div><div class="tweet-date" title="'+this.created_at+'"></div></div></div></li>' + html;
						self.tweets[this.id] = true;
					}
				});
				if ( !self.is_mobile ) {
					html = '<li class="clicknscroll"></li>' + html;
				}
				if ( self.first_load ) {
					self.first_load = false;
					if ( !self.is_mobile ) {
						html = html + '<li class="stop"></li>';
					}
				}
				self.liste.prepend( html );
				self.update_timestamps();
				//self.fade();
			},
			/*}}}*/

			/** Mise à jour des timestamps {{{ */
			update_timestamps : function() {
				var self = this;
				self.liste.find('.tweet-date').each(function() {
					$(this).html( $.relatizeDate.timeAgoInWords( new Date( $(this).attr('title') ) ) );
				});
		   },
			/*}}}*/

			/** Highlight/fade des nouveaux tweets {{{ */
			fade : function() {
				var self = this;
				self.liste.find('.tweet-new').animate({
					backgroundColor: '#EBEBEB'
				},{
					duration: 1000
				});
		   }
			/*}}}*/

		};

		ixTweets.init();

/*}}}*/

	});

	$(window).load(function() {
		if ( !$('body').is('.template-mobile') ) {
			$('#tweets ul').trigger('resize_tweets');
		}
	});

})(jQuery);
