var lastfm_lastsong_rss = '';


/**********************************************/
/* Hello, mais tu viens faire quoi par ici ?? */
/* on vient récupérer du code ? */

$(document).ready(function(){
    
    $('#slider_project').jCarouselLite({
		auto: 5000,
		speed: 750,
		visible: 1,
		beforeStart: function(balise) {                                
			var name = $(balise).attr('id');
			$('#txt_'+name).fadeOut();
			
		},
		afterEnd: function(balise) {                       
			var name = $(balise).attr('id');
			$('#txt_'+name).fadeIn();
		}
	});    
	
						
						
	$.ajax({  
        type : 'GET',  
        url : 'http://tumblr.affinity-web.org/lastfm.php',  
        dataType : 'jsonp',  
        error : function() {},  
        success : function(lastfm_lastsong_rss) {

            var appendHtml = "";
			$(lastfm_lastsong_rss).find('item title').each(function(){
					
					var title = $(this).html();
					title = explode('–', title);

					appendHtml += "<li><strong>"+cutString(title[0], 20)+"</strong><br /> <span>"+cutString(title[1], 35)+"</span></li>";
			});

			$("#slider_music ul").append(appendHtml);
			
			$('#slider_music').jCarouselLite({
									auto: 3000,
									speed: 300,
									visible: 1
								});
		  
        }  
    }); 
					
                        
    $('#footer ul a').hover(function() {

        var icone = $(this).find('img').attr('src').replace('_b.png', '.png');         
        $(this).find('img').attr('src', icone);                      

    }, function() {

        var icone = $(this).find('img').attr('src').replace('.png', '_b.png')
        $(this).find('img').attr('src', icone);      

    });
	
	/* twitter */
	$.ajax({  
        type : 'GET',  
        url : 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=emerika&count=20',  
        dataType : 'jsonp',  

        error : function() {},  

        success : function(data) { 		
			
			var nb = 0;
						
            $.each(data, function(index, item){

				if(nb < 5) {
				
					// retweet ou pas
					if(item.retweeted_status != null) {
					    $('#tweet').append('<div class="clearfix"><p>' + '<img class="profile_icon" src="' + item.retweeted_status.user.profile_image_url + '" width="40" height="40" alt="Twitter">' + item.retweeted_status.text.linkify() + '</p></div>' + '<div id="web_intent">' + '<span class="time">' + relative_time(item.created_at) + '</span>' + '<img src="http://tumblr.affinity-web.org/tumblr/image//retweet_mini.png" width="16" height="16" alt="Retweet">' + '<a href="http://twitter.com/intent/retweet?tweet_id=' + item.id_str + '">' + 'Retweet</a>' + '<img src="http://tumblr.affinity-web.org/tumblr/image//favorite_mini.png" width="16" height="16" alt="Favorite">' + '<a href="http://twitter.com/intent/favorite?tweet_id=' + item.id_str + '">' + 'Favorite</a>' + '</div>');
						nb++;
						
					} else {								
						var firstLetter = item.text.substring(0, 1);			
													
						// mention ou pas 
						if(firstLetter != '@')	{							
							$('#tweet').append('<div class="clearfix"><p>' + '<img class="profile_icon" src="' + item.user.profile_image_url + '" width="40" height="40" alt="Twitter">' + item.text.linkify() + '</p></div>' + '<div id="web_intent">' + '<span class="time">' + relative_time(item.created_at) + '</span>' + '<img src="http://tumblr.affinity-web.org/tumblr/image//retweet_mini.png" width="16" height="16" alt="Retweet">' + '<a href="http://twitter.com/intent/retweet?tweet_id=' + item.id_str + '">' + 'Retweet</a>' + '<img src="http://tumblr.affinity-web.org/tumblr/image//favorite_mini.png" width="16" height="16" alt="Favorite">' + '<a href="http://twitter.com/intent/favorite?tweet_id=' + item.id_str + '">' + 'Favorite</a>' + '</div>');
							nb++;
						}
					}
					
				}
            });                   
        }  
    }); 
                        
    
});


// fonction utile
function relative_time(time_value) {  
    var values = time_value.split(" ");  
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];  
    var parsed_date = Date.parse(time_value);  
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();  
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);  
    delta = delta + (relative_to.getTimezoneOffset() * 60);  

    var r = '';  
    if (delta < 60) {  
        r = 'il y a une minute';  
    } else if(delta < 120) {  
        r = 'il y a quelques minutes';  
    } else if(delta < (45*60)) {  
        r = 'il y a '+(parseInt(delta / 60)).toString() + ' minutes';  
    } else if(delta < (90*60)) {  
        r = 'il y a une heure';  
    } else if(delta < (24*60*60)) {  
        r = 'il y a '+(parseInt(delta / 3600)).toString() + ' heures';  
    } else if(delta < (48*60*60)) {  
        r = 'il y a un jour';  
    } else {  
        r = 'il y a '+(parseInt(delta / 86400)).toString() + ' jours';  
    }  

      return r;
}

// fonction utile
// Create Usable Links  
String.prototype.linkify = function() {  
       return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {  
              return m.link(m);  
      });  
}; 

function cutString(str, nb) {
    
    if(str.length > nb) {        
        str = str.substring(0, nb)+' ...';
    }
    
    return str
}



function explode (delimiter, string, limit) {
// Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
// 
// version: 1103.1210
// discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +     improved by: kenneth
// +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +     improved by: d3x
// +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
// *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
// *     example 2: explode('=', 'a=bc=d', 2);
// *     returns 2: ['a', 'bc=d']
var emptyArray = {        0: ''
};

// third argument is not required
if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {        return null;
}

if (delimiter === '' || delimiter === false || delimiter === null) {
    return false;    }

if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
    return emptyArray;
} 
if (delimiter === true) {
    delimiter = '1';
}
 if (!limit) {
    return string.toString().split(delimiter.toString());
} else {
    // support for limit argument
    var splitted = string.toString().split(delimiter.toString());        var partA = splitted.splice(0, limit - 1);
    var partB = splitted.join(delimiter.toString());
    partA.push(partB);
    return partA;
}}


/**
 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.2 or above
 *
 * http://gmarwaha.com/jquery/jcarousellite/
 *
 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0.1
 * Note: Requires jquery 1.2 or above from version 1.0.1
 */
(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);
