/* Ce fichier est volontairement non-packÃ© */

// On est prÃªts !
$(document).ready(function() {
	/* ----------------------------------------- */
	/* Compte                                     */
	/* ----------------------------------------- */
		
	var Affiche = document.getElementById("block_compteur_p");
                	function Rebour() {
                    	var date1 = new Date();
						var date2 = new Date ("03 11 19:30:00 2012");
                    	var sec = (date2 - date1) / 1000;
                    	var n = 24 * 3600;
                    	if (sec > 0)
                    	{
                        	j = Math.floor (sec / n);
                        	h = Math.floor ((sec - (j * n)) / 3600);
                        	mn = Math.floor ((sec - ((j * n + h * 3600))) / 60);
                        	sec = Math.floor (sec - ((j * n + h * 3600 + mn * 60)));
						
							var texte = new String("<span>La soirée dans  </span>");
                        
                        	if (j == 1) {
                           	 Affiche.innerHTML = texte + j +" jours "+ h +" heures "+ mn +" minutes "+ sec + " secondes ";
                        	} 
							else if (j == 0) {
                            	if (h == 0) {
                                	if (mn == 0) {
                                    	Affiche.innerHTML = texte + sec + " secondes ";
                              	  }
                               	 else {
                                    Affiche.innerHTML = texte + mn +" minutes "+ sec + " secondes ";
                               	 }
                          	  }
                            else {
                                Affiche.innerHTML = texte + h +" heures "+ mn +" minutes "+ sec + " secondes ";
                            }
                        }
                        else {
                            Affiche.innerHTML = texte + j +" jours "+ h +" heures "+ mn +" minutes "+ sec + " secondes ";
                        }
                        window.status = texte + j +" jours "+ h +" heures "+ mn +" minutes "+ sec + " secondes ";
						setTimeout (Rebour, 1000);
                    }
                    else {
                        Affiche.innerHTML = "Le concours est terminé.";
                    }
                }
                Rebour();	
		
		
	/* ----------------------------------------- */
	/* Menu                                      */
	/* ----------------------------------------- */
	sfHover = function() {
        var sfEls = document.getElementById("menu").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
                sfEls[i].onmouseover=function() {
                        this.className+=" sfhover";
                }
                sfEls[i].onmouseout=function() {
                        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
                }
        }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
	
	
	/* ----------------------------------------- */
	/* BackTop                                   */
	/* ----------------------------------------- */
	
	$(window).scroll(function() {

        	if($(window).scrollTop() > 350){	
        			$("#up").show('slow');}
        			
        	else{
        		$("#up").hide('fast');
        		}
			});
		$('#up a').live('click', function(event){
 			event.preventDefault();
  			$('html,body').animate({scrollTop: 0}, 'slow');
			});
			
		$(document).ready(function(){
        		$("#up").hide();});
	
	
	/* ----------------------------------------- */
	/* Tweets                                    */
	/* ----------------------------------------- */
	
	// Declare variables to hold twitter API url and user name
	var twitter_api_url = 'http://search.twitter.com/search.json';
	var twitter_user    = '48HSM2012';
	var count = 3;
	
	// Enable caching
	$.ajaxSetup({ cache: true });
	
	// Send JSON request
	// The returned JSON object will have a property called "results" where we find
	// a list of the tweets matching our request query
	$.getJSON(twitter_api_url + '?callback=?&rpp='+count+'&q=from:' + twitter_user,	function(data) {
		$.each(data.results, function(i, tweet) {
			// Before we continue we check that we got data
			if(tweet.text !== undefined) {
			// Calculate how many hours ago was the tweet posted
			var date_tweet = new Date(tweet.created_at);
			var date_now   = new Date();
			var date_diff  = date_now - date_tweet;
			var hours      = Math.round(date_diff/(1000*60*60));
			
			// Build the html string for the current tweet
			var tweet_html = '<li>'+tweet.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/gi, '<a href="$1">$1</a>').replace(/[\@]+([A-Za-z0-9-_]+)/gi, '<a href="http://twitter.com/$1">@$1</a>')+'<span class="tweettime" ><a href="http://twitter.com/'+twitter_user+'/status/'+tweet.id_str+'">il y a '+hours+' h</a></span>'+'</li>';
			/*
			var tweet_html = '<div class="tweet_text">';
          tweet_html    += '<a href="http://www.twitter.com/';
          tweet_html    += twitter_user + '/status/' + tweet.id + '">';
          tweet_html    += tweet.text + '<\/a><\/div>';
          tweet_html    += '<div class="tweet_hours">' + hours;
          tweet_html    += ' hours ago<\/div>';
			*/
			// Append html string to tweet_container div
			$('#coming').empty();
			$('#tweets').append(tweet_html);
			}
		});
	});
	
	
	
});
