window.addEvent('domready', function() {

	$('nuevo_tweet_usuario').addEvent('keyup', function() {
		max_chars = 140;
		current_value	= $('nuevo_tweet_usuario').value;
		current_length	= current_value.length;
		remaining_chars = max_chars-current_length;
		$('counter_number').innerHTML = remaining_chars;
		
		var tecla, int_value, out_value;
		
		if(remaining_chars<=5){
			$('counter_number').setStyle('color', '#dd0000');
		} else {
			$('counter_number').setStyle('color', '#dddddd');
		}
	});
	
	if ($('boton_update_tweet')) {
		$('boton_update_tweet').addEvent('click', function(){
					current_value	= $('nuevo_tweet_usuario').value;
					current_length	= current_value.length;
					if (current_length > 0 && current_length <= 140)
						$('formu_nuevo_tweet').submit();
					
		});
	}
	
	if ($$('.retweetear')) {
		var retweet = $$('.retweetear');
		retweet.each(function(item) {
				item.addEvent('click', function(){
					$('nuevo_tweet_usuario').set("html", item.title);
					$('counter_number').innerHTML = 140-$('nuevo_tweet_usuario').value.length;
				});

			});
	}
	
	if ($$('.wrap_tweet')) {
		var retweet = $$('.wrap_tweet');
		retweet.each(function(item) {
				item.addEvent('mouseover', function(){
					item.setStyle('background-color', '#f7f7f7');
				});
			});
		
		retweet.each(function(item) {
			item.addEvent('mouseout', function(){
				item.setStyle('background-color', '#ffffff');
			});
		});
		
	}
	
	
	if ($$('.reply_tweet_icon')) {
		var retweet = $$('.reply_tweet_icon');
		retweet.each(function(item) {
				item.addEvent('click', function(){
					$('nuevo_tweet_usuario').set("html", (item.get('class')).replace("reply_tweet_icon ","") );
					$('counter_number').innerHTML = 140-$('nuevo_tweet_usuario').value.length;
				});

			});
	}
	
	if ($$('.directo_tweet_icon')) {
		var retweet = $$('.directo_tweet_icon');
		retweet.each(function(item) {
				item.addEvent('click', function(){
					$('nuevo_tweet_usuario').set("html", (item.get('class')).replace("directo_tweet_icon ","") );
					$('counter_number').innerHTML = 140-$('nuevo_tweet_usuario').value.length;
				});

			});
	}
	
	if ($$('.favorito_tweet_icon')) {
		var retweet = $$('.favorito_tweet_icon');
		retweet.each(function(item) {
				item.addEvent('click', function(){
					var id_favorito = ((item.get('class')).replace("favorito_tweet_icon favorito",""));
					$('id_favorito').value = id_favorito;
					$('formu_favorito_tweet').submit();
				});

			});
	}


	if ($$('.nofavorito_tweet_icon')) {
		var retweet = $$('.nofavorito_tweet_icon');
		retweet.each(function(item) {
				item.addEvent('click', function(){
					var id_favorito = ((item.get('class')).replace("nofavorito_tweet_icon favorito",""));
					$('id_favorito').value = id_favorito;
					$('formu_favorito_tweet').submit();
				});

			});
	}
	
	
	
	function dhtmlLoadScript(url)
	{
	   var e = document.createElement("script");
	   e.src = url;
	   e.type="text/javascript";
	   document.getElementsByTagName("head")[0].appendChild(e);
	}

	
	
	// You can skip the following two lines of code. We need them to make sure demos
	// are runnable on MooTools demos web page.
	/*if (!window.demo_path) window.demo_path = '';
	var demo_path = window.demo_path;*/
	// --
		
	//We can use one Request object many times.
	var req = new Request.HTML({url:'ultweet/ajax', 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('wrap_each_tweet').set('text', '');
			//Inject the new DOM elements into the results div.
			$('wrap_each_tweet').adopt(html);
			
			dhtmlLoadScript("http://www.tweetrest.com/js/nuevo_tweet.js");
			
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('wrap_each_tweet').set('text', 'The request failed.');
		}
	});
	
	if ($('makeRequest')) {
		$('makeRequest').setStyle('visibility','visible');
		$('makeRequest').addEvent('click', function() {
			req.send();
		});
	}
	
	//We can use one Request object many times.
	var req10 = new Request.HTML({url:'ultweet/ajax10', 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('wrap_each_tweet').set('text', '');
			//Inject the new DOM elements into the results div.
			$('wrap_each_tweet').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('wrap_each_tweet').set('text', 'Error en la peticion.');
		}
	});
	
	if ($('makeRequest10')) {
		$('makeRequest10').addEvent('click', function() {
			req10.send();
		});
	}
	
	
	
	
	
	//We can use one Request object many times.
	var req_destacados = new Request.HTML({url:'poputweet/ajax_destacados', 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('wrap_each_tweet').set('text', '');
			//Inject the new DOM elements into the results div.
			$('wrap_each_tweet').adopt(html);
			
			dhtmlLoadScript("http://www.tweetrest.com/js/nuevo_tweet.js");
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('wrap_each_tweet').set('text', 'The request failed.');
		}
	});
	
	if ($('makeRequest_destacados')) {
		$('makeRequest_destacados').addEvent('click', function() {
			req_destacados.send();
		});
	}
	
	//We can use one Request object many times.
	var req_destacados10 = new Request.HTML({url:'poputweet/ajax_destacados10', 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('wrap_each_tweet').set('text', '');
			//Inject the new DOM elements into the results div.
			$('wrap_each_tweet').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('wrap_each_tweet').set('text', 'Error en la peticion.');
		}
	});
	
	if ($('makeRequest_destacados10')) {
		$('makeRequest_destacados10').addEvent('click', function() {
			req_destacados10.send();
		});
	}
	
	if ($('boton_short_url')){
		$('boton_short_url').addEvent('click', function() {
			$('formu_nueva_url').submit();
		});
	}




});
