var text_less_min = "less than a minute ago";
var text_one_min = "about a minute ago";
var text_n_mins = "%t minutes ago";
var text_one_hour = "about an hour ago";
var text_n_hours = "about %t hours ago";
var text_one_day = "1 day ago";
var text_n_days = "%t days ago";

function parse_date(date_str) {
	return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
}

function relative_time(time_value) {
	var parsed_date = parse_date(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	if(delta < 60) {
		return text_less_min;
	} else if(delta < 120) {
		return text_one_min;
	} else if(delta < (45*60)) {
		return text_n_mins.replace('%t', parseInt(delta / 60).toString());
		//return (parseInt(delta / 60)).toString() + s.text_n_mins;
	} else if(delta < (120*60)) {
		return text_one_hour;
	} else if(delta < (24*60*60)) {
		return text_n_hours.replace('%t', parseInt(delta / 3600).toString());
		//return (parseInt(delta / 3600)).toString() + s.text_n_hours;
	} else if(delta < (48*60*60)) {
		return text_one_day;
	} else {
		return text_n_days.replace('%t', parseInt(delta / 86400).toString());
		//return (parseInt(delta / 86400)).toString() + s.text_n_days;
	}
}

function loadNews() {
	getPosts();
	getTweets();
}

function getTweets() {
	var tweetNameRegexp = /[\@]+([A-Za-z0-9-_]+)/gi;
	var URLRegexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;

	$.jTwitter('jurema', 7, function(data){
	    $.each(data, function(i, post){
		
		textTweet = post.text.replace(URLRegexp,"<a href=\"$1\">$1</a>")
		textTweet = textTweet.replace(tweetNameRegexp,"<a href=\"http://twitter.com/$1\">@$1</a>");
	
		dateTweet = relative_time(post.created_at);
	
		$("#titleTwitt" + (i + 1)).prepend(textTweet);
		$("#dateTwitt" + (i + 1)).html(dateTweet);
	
	
	   });
	
	   $('#loadingTwitter').remove();
	
	});
	
}

function getPosts() {
	$.ajax("http://posterous.com/api/2/sites/754317/posts/public", {	
	    crossDomain:true, 
	    dataType: "jsonp", 
	    success:function(data,text,xhqr){
	        $.each(data, function(i, item) {			
				
				HTMLTitlePost = "<a href='" + item['full_url'] + "'>"+ item['title'] +"</a>"
				datePost = $.format.date(new Date(item['display_date']), "MMMM dd, yyyy");
				
				$("#titlePost" + (i + 1)).append(HTMLTitlePost);
				$("#datePost" + (i + 1)).append(datePost);
				
				if(i >= 2) return false;
				return true;
	        });
			
			$('#loadingPost').remove();
			
	    }
	});
}

/**** WORK DETAIL ********/

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function resetIcoButtons(actualButton, typeButton, templateURL) {
	//typeButton : image or video
	$(".videoIcon").each(function(index) { this.src = templateURL + "/images/internal_project_video_icon.png"; })
	$(".imageIcon").each(function(index) { this.src = templateURL + "/images/internal_project_image_icon.png"; })
	actualButton.src = templateURL + "/images/internal_project_" + typeButton + "_icon_selected.png";
}

function changeMainImage(actualButton, imagePath, templateURL) {
	
	resetIcoButtons(actualButton, 'image', templateURL);
	
	if($('#work_detail_image').html() == null) {
		$("#main_image").html("");
		$("#main_image").html('<img id="work_detail_image" src="" />');
	}
	
	$('#work_detail_image').fadeTo(250, 0, function() {
		this.src="";
		this.src= imagePath;
		$('#work_detail_image').fadeTo(250, 1, null);
	});
	
}

function changeMainVideo(actualButton, site, movieID, templateURL) {
	
	resetIcoButtons(actualButton, 'video', templateURL);
	
	if($('#work_detail_image').html() == null) {
		$("#main_image").html("");
		$("#main_image").html(getVideo(site, movieID));
	} else {
		$('#work_detail_image').fadeTo(250, 0, function() {
			$("#main_image").html(getVideo(site, movieID));				
			$('#work_detail_image').fadeTo(250, 1, null);
		});
	}
	
}

function getVideo(site, movieID) {
	
	movieString = '';
	
	if(site == "vimeo") {
		
		movieString = '<iframe  src="http://player.vimeo.com/video/'+movieID+'?byline=0&amp;portrait=0&amp;color=ffffff" width="624" height="360" frameborder="0"></iframe>'
		
	}

	if(site == "youtube")
	{
		movieString = '<iframe title="YouTube video player" width="600" height="368" src="http://www.youtube.com/embed/' + movieID + '" frameborder="0" allowfullscreen></iframe>';
	}
	
	return movieString;
}
//////////////////////////

