jQuery(document).ready(function(){

	cargatweets();


	if(jQuery('.ie_error').length<1)
	{
		if ( (jQuery.browser.msie) && (parseInt(jQuery.browser.version)<7) ) {
			jQuery('body').prepend('<div class="ie_error">Este sitio ha sido creado para ser visualizado en navegadores modernos. Por favor descargue <a href="http://www.mozilla.com/es-ES/firefox/">uno</a>.</div>');
		}
	}
	
	
	jQuery('.newsletter_form').submit(function () {
		jQuery.post('http://villamorra.com.py/test/wp-admin/admin-ajax.php',{
				'action'						:	'alo_em_pubblic_form_check',
				'alo_em_error_email_activated'	:	'Warning: this email address has already been subscribed',
				'alo_em_error_email_added'		:	'Warning: this email address has already been subscribed, but not activated. We are now sending another activation email',
				'alo_em_error_email_incorrect'	:	'The e-email address is not correct',
				'alo_em_error_name_empty'		:	'The name field is empty',
				'alo_em_error_on_sending'		:	'Error during sending: please try again',
				'alo_em_form_lists'				: 	'',
				'alo_em_lang_code'				:	'es',
				'alo_em_opt_email'				:	jQuery('#opt_email').val(),
				'alo_em_opt_name'				:	'Nombre',
				'alo_em_txt_ok'					:	'Subscription successful. You will receive an e-mail with a link. You have to click on the link to activate your subscription.',
				'alo_em_txt_subscribe'			:	'Subscribe',
				'rndval'						:	'1295016557054'
		}, function(data){
			//alert(data);
			jQuery('#newsletter_feedback').html('Le hemos enviado un mensaje para confirmar su suscripcion. Verifique su cuenta de email en unos momentos.');
			jQuery('#newsletter_feedback').fadeIn('fast');
		});
		return false;
	});
});

var tweets=[0];
	var twitteros=[0];
	var x=0;
	var stid2=0;

		function cargatweets(){
			var url = "https://search.twitter.com/search.json?q=+from:Villamorra&callback=?";
			jQuery.getJSON(url,function(data){
				jQuery.each(data.results, function(x, item) {
					twitteros[x]="@"+item.from_user;
					tweets[x]=item.text;
					tweets[x]=tweets[x].parseURL().parseUsername().parseHashtag();
				});
				clearTimeout(stid2);
				muestratweets();
				stid2=window.setTimeout('cargatweets()' ,500000);
			});
		}
				String.prototype.parseURL = function() {
					return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(url) {
						return url.link(''+url+'');
					});
				};

				String.prototype.parseUsername = function() {
					return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
						var username = u.replace("@","")
						return u.link('http://twitter.com/'+username+'');
					});
				};
			
				String.prototype.parseHashtag = function() {
					return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
						var tag = t.replace("#","%23")
						return t.link('http://search.twitter.com/search?q='+tag);
					});
				};


		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 = 'a minute ago';
			  } else if(delta < 120) {
				r = 'couple of minutes ago';
			  } else if(delta < (45*60)) {
				r = (parseInt(delta / 60)).toString() + ' minutes ago';
			  } else if(delta < (90*60)) {
				r = 'an hour ago';
			  } else if(delta < (24*60*60)) {
				r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
			  } else if(delta < (48*60*60)) {
				r = '1 day ago';
			  } else {
				r = (parseInt(delta / 86400)).toString() + ' days ago';
			  }
			  
			  return r;
		}

		function muestratweets(){
			if(x>=tweets.length){x=0;}
			jQuery("#tweet").html(tweets[x]);
			x++;
			stid2=window.setTimeout('muestratweets()' ,10000);
		}

