/*!
 * jQuery JavaScript Library Extension v1.1
 * http://www.steve-grosbois.com
 * @version $Id: jquery-kwiky-extension.js 6 2010-03-10 14:27:25Z steve $
 * Copyright 2010, Steve Grosbois
 */

/**
 * Rend les url du contenu HTML de l'élément cliquable
 * @url http://devkick.com/blog/parsing-strings-with-jquery/
 */
$.fn.clickableUrl = function() {  
    var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;  
    this.each(function() {  
		$(this).html(  
            $(this).html().replace(regexp,'<a href="$1">$1</a>')  
        );  
    });  
    return $(this);  
}  

/**
 * Rend les url du contenu HTML de l'élément cliquable
 * @url http://devkick.com/blog/parsing-strings-with-jquery/
 */
$.fn.clickableTwitterHashTag = function() {  
    var regexp = /(#\w*)/gi;
    this.each(function() {  
    	$(this).html(  
            $(this).html().replace(regexp, '<a href="http://twitter.com/search?q=$1">$1</a>')  
        );  
    });  
    return $(this);  
}  

/**
 * Rend les url du contenu HTML de l'élément cliquable
 * @url http://devkick.com/blog/parsing-strings-with-jquery/
 */
$.fn.clickableTwitterUser = function() {  
    var regexp1 = /(@\w*)/gi;  
    var regexp2 = /http:\/\/twitter.com\/@/gi;  
    this.each(function() {  
    	$(this).html(  
            $(this).html().replace(regexp1,'<a href="http://twitter.com/$1">$1</a>')  
        ); 
    	$(this).html(  
            $(this).html().replace(regexp2,'http://twitter.com/')  
        ); 
    });  
    return $(this);  
} 

/**
 * Escape HTML
 * @url http://devkick.com/blog/parsing-strings-with-jquery/
 */
$.fn.escapeHtml = function() {
	this.each(function() {
		$(this).html(
			$(this).html()
				.replace(/"/g,"&quot;")
				.replace(/</g,"&lt;")
				.replace(/>/g,"&gt;")
				.replace(/&/g,"&amp;")
		);
	});
	return $(this);
}

/**
 * Récupère les derniers status d'un utilisateur Twitter
 * @param twitterUsername Required String : Nom d'utilisateur sur Twitter
 * @param count Optional Integer Default=10 : Nombre de tweet à récupérer
 */
$.fn.twitterStatus = function(twitterUsername, count) {  
	var element = $(this);
	if (typeof count == "undefined") {
		count = 10;
	}	
	// Twitter API retrieve only 8 tweets if you set count to 10 !?!?!
	//count += 2;
	var jsonUrl = 'http://twitter.com/statuses/user_timeline/' + twitterUsername + '.json?count=' + count + '&callback=?';
	var i = 0;
	$.getJSON(jsonUrl, function(data){
	  $.each(data, function(i, item) { 
		var html = '';
		//"Thu Feb 04 12:39:21 +0000 2010"
		var values = item.created_at.split(" ");
		var date = new Date(Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]));
		date.setSeconds(date.getSeconds() + item.user.utc_offset);
		var day = date.getDate() + '';
		if (day.length == 1) {
			day = '0'+ day;
		}
		var month = date.getMonth() + 1 + '';
		if (month.length == 1) {
			month = '0'+ month;
		}
		var date_text = 'Le ' + day + '/' + month + '/' + date.getFullYear() + ' à ' + date.getHours() + ':' + date.getMinutes();
		html += '<p class="tweet" id="tweet-' + i + '">' + item.text + '</p><br/>';
		html += '<span class="meta" id="tweet-meta-' + i + '">';
		html += '<a href="http://twitter.com/' + twitterUsername + '/statuses/' + item.id + '">' + date_text + '</a>';
		html += '&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://twitter.com/' + twitterUsername + '">Suivez moi sur Twitter</a>'
		html += '</span>';
		element.append(html); 
		i++;
	  }); 
	  element.children('p').clickableUrl();
	  element.children('p').clickableTwitterHashTag();
	  element.children('p').clickableTwitterUser();
	});
	
}

/**
 * Récupère les dernières photos d'un utilisateur Flickr
 * @param flickrId Required String : Identifiant d'utilisateur sur Flickr
 * @param count Optional Integer Default=10 : Nombre de photos à récupérer
 */
$.fn.flickrPhotosPublic = function(flickrId, count) {  
	var element = $(this);
	if (typeof count == "undefined") {
		count = 10;
	}	
	$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=' + flickrId + '&lang=fr-fr&format=json&jsoncallback=?', function(data){
		var i = 0;
		$.each(data.items, function(i, item){
			if(i < count) {
				$("<img/>").attr("src", item.media.m).appendTo(element).wrap("<a href='" + item.link + "'></a>");
			}
			i++;
		});
	});
}