$(function() {
	getBlogArticles();
    getTweets();
    
    
    function getBlogArticles() {
    	var articlesCount = 2;        
        var url = 'http://blog.pastelstudios.com/feed/';
        
        $.ajax({
        	  type: "POST",
        	  url: "/" + lang + "/index/blogarticles/",
        	  dataType: 'json',
        	  success: function(data) {
        		  var articleLinkConts = $("a.id_blog");
        		         		  
        		  for(var i=0; i<data.length; i++){
      	      		var title = data[i].title;
      	      		var link = data[i].link;
      	      		var $cont = $(articleLinkConts[i]);
      	      		$cont.attr("href", link);
      	      		$("span.head_text", $cont).html(title);
      	      	}
        	  }
        });
    }
    
    function getTweets() {
    	var user = 'pastelstudios';
        var tweetsCount = 2;
        
        var url = 'http://twitter.com/statuses/user_timeline.json?screen_name=' + user + 
        	'&include_rts=1&callback=?';
        
        $.ajax({
        	  type: "GET",
        	  url: url,
        	  dataType: 'json',
        	  success: function(data) {
        		var tweetConts = $("a.id_tweeter");
        		var counter = 0;
        		
    	      	for(var i=0; i<data.length; i++){    	      		
    	      		if(counter == tweetsCount) {
    	      			break;
    	      		}  	      		
    	      		
    	      		if(!newsExists(data[i].source)){
    	      			var text = 	data[i].text;    	      		
    	      			var tweet = text.replace(/(:? *(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"");
    	      			
    	      			$("span.id_text", tweetConts[counter]).html(tweet);
    	      			counter ++;
    	      		}
    	      	}
        	        
        	  }
        });
    }
    
    function newsExists(tweetSource) {
		var blogHref = "http://blog.pastelstudios.com/";
    	
    	var match = tweetSource.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig)
		if(match != null) {
			var href = match[0];
			if(href == blogHref) {
				return true;
			}
		}
    	
    	return false;
    }
    
});
