// JavaScript Document
$(function(){
		   
	/// Animar info
	$('#info_click').hover(function() {  
          
        //Display the caption  
        $('#head_info_pop').stop(true,false).animate({"top": "-19px", "opacity": '1'}, 500);  
    },  
    function() {  
     
        //Hide the caption  
         $('#head_info_pop').stop(true,false).animate({"top": "-39px", "opacity": '0'}, 500);  
    });
	
	
	//$('#coluna_dir').animate({'height': $('#coluna_esq').height(), });
	$('#flickr_item_logo').hover(function() {  
          
        //Display the caption  
        $(this).find('img').stop(false,true).fadeIn(400);  
    },  
    function() {  
     
        //Hide the caption  
        $(this).find('img').stop(false,true).fadeOut(400);  
    });
	
	
	///// Item Página/////////
	
		//Hover
	$('.pg_item').hover(function() {  
          
        //Display the caption  
        $(this).find('.caption').stop(true,false).animate({'top':'0px'}, {duration:200});
    },  
    function() {  
     
        //Hide the caption  
        $(this).find('.caption').stop(true,false).animate({'top':'120px'}, {duration:200});
    });
	
  
  //assign hover actions to each flickr images
$('.flickr_item').live('mouseover', function(){
     $(this).find('img').stop(false,true).animate({'width':'85px', 'height':'85px', 'top':'-5px', 'left':'-5px'}, {duration:200});  
});
$('.flickr_item').live('mouseout', function(){
   $(this).find('img').stop(false,true).animate({'width':$('.flickr_item').width(), 'height':$('.flickr_item').height(), 'top':'0', 'left':'0'}, {duration:100}); 
});

  
  
		   
////FLICKR e TWITTER /////////////

//assign your api key equal to a variable
	
	$('.flickr').each(function(){
							   
	var photoset_id = $(this).attr("photoset_id");
	var este = $(this);
	if(photoset_id != null) {
		//alert(photoset_id);
	var apiKey = '2eb4524473e9b3761f91e487661a0482';

    //the initial json request to flickr
    //to get your latest public photos, use this request: http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=' + apiKey + 				'&user_id=29096781@N02&per_page=15&page=2&format=json&jsoncallback=?
	$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + 	'&photoset_id='+ photoset_id +'&per_page=12&page=1&format=json&jsoncallback=?', function(data){

    	//loop through the results with the following function
    	$.each(data.photoset.photo, function(i,item){

        //build the url of the photo in order to link to it
        var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_s.jpg'
		var photoURL2 = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg'

        //turn the photo id into a variable
        var photoID = item.id;
        
		//create an imgCont string variable which will hold all the link location, title, author link, and author name into a text string
		var imgCont = '<div class="flickr_item"><a href="' + photoURL2 + '" class="highslide" onclick="return hs.expand(this)"><img src="' + photoURL + '" width="75" height="75" /></a></div>';

		//append the 'imgCont' variable to the document
		$(imgCont).appendTo(este);

		});

	}); }; });
	
	if($('#coluna_dir').width() != null) {
	$.getJSON('http://search.twitter.com/search.json?q=from:luiscarli&rpp=6&callback=?', function(data){
	//$.getJSON('http://twitter.com/statuses/user_timeline/luiscarli.json?count=7', function(data){

    	//loop through the results with the following function
    	$.each(data.results, function(i,item){
			//$.each(data, function(i,item){      
        
			//create an imgCont string variable which will hold all the link location, title, author link, and author name into a text string
			var twitCont = '<div class="item"><p class="twitter">' + item.text + '</p></div>';

			//append the 'imgCont' variable to the document
			$(twitCont).appendTo('#coluna_dir').linkify();


		});

	}); };
	
	var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,
      url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g,

	linkifyThis = function () {
        var childNodes = this.childNodes,
            i = childNodes.length;
        while(i--)
        {
          var n = childNodes[i];
          if (n.nodeType == 3) {
            var html = $.trim(n.nodeValue);
            if (html)
            {
              html = html.replace(/&/g, '&amp;')
                         .replace(/</g, '&lt;')
                         .replace(/>/g, '&gt;')
                         .replace(url1, '$1<a href="http://$2" target="_blank">$2</a>$3')
                         .replace(url2, '$1<a href="$2" target="_blank">$2</a>$5');
              $(n).after(html).remove();
            }
          }
          else if (n.nodeType == 1  &&  !/^(a|button|textarea)$/i.test(n.tagName)) {
            linkifyThis.call(n);
          }
        }
      };

  $.fn.linkify = function () {
    return this.each(linkifyThis);
  };

    //$('.item').linkify();

});
	
$(window).load(function () {
	$('#load').fadeOut(400);  
  $('#coluna_dir').animate({'height': $('#coluna_esq').height()+20+'px', });
  
  $('.move').each(function () {
		var W = $(this).parent().width();
		var tempo = ($(this).width()- W)*30;
		//alert(W);
  if($(this).width()>W){
    $(this).animate({'left':-$(this).width()+ W +'px'}, {"duration":tempo, "easing":"linear"});
  $(this).animate({'left':'0px'}, {"duration":tempo, "easing":"linear"});
  $(this).everyTime( tempo*2, function (){
  $(this).animate({'left':-$(this).width()+W+'px'}, {"duration":tempo, "easing":"linear"});
  $(this).animate({'left':'0px'}, {"duration":tempo, "easing":"linear"});
												});};
							});
  
});



///////EXTEND

/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 *
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval < 0)
				return;

			if (typeof times != 'number' || isNaN(times) || times < 0) 
				times = 0;
			
			times = times || 0;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});

