$(document).ready(function() {
    debug = true;
    $('#insert_tweet_form').ajaxForm({
    	dataType: 'xml',
    	beforeSubmit: prepareTweet,
    	success: handleResponseTweet
    });
});


function prepareTweet() {}


function handleResponseTweet(responseXml) {
	var status = $('status', responseXml).text();
	if (status=='success') {
		var message = $('message', responseXml).text();
		$('#app-server-response').hide('slow');
		$('#app-server-response').html('Tu mensaje ha sido enviado correctamente');
		$('#app-server-response').show('slow');
		$('#last_tweet').html(message);
	}
	else {
		var message = $('error', responseXml).text();
		$('#app-server-response').hide('slow');
		$('#app-server-response').html('Ha ocurrido un error...');
		$('#app-server-response').show('slow');
	}
}


function createFriendship(url) {
	$.post(url, '', function(data) {
		$('#app-server-response').hide('slow');
		$('#app-server-response').html(data);
		$('#app-server-response').show('slow');
	}, 'html');
};

function destroyFriendship(url, id) {
	$.post(url, '', function(data) {
		$('#app-server-response').hide('slow');
		$('#app-server-response').html(data);
		$('#app-server-response').show('slow');
		$(id).hide('slow');
	}, 'html');
};

function showTweets(url, link, text) {
	$.post(url, '', function(data) {
		$('#tweets_list').html(data);
		$('#tweets_list').show('slow');
		$('#'+link).html(text);
	}, 'html');
};

function showUsers(url) {
	$.post(url, '', function(data) {
		$('#users_list').html(data);
		$('#users_list').show('slow');
	}, 'html');
};

function dynamicLoad(url, pos, link, text) {
	$.post(url, '', function(data) {
		$('#'+pos).html(data);
		$('#'+pos).show('slow');
		$('#'+link).html(text);
	}, 'html');
};

function destroyStatus(url, id) {
	$.post(url, '', function(data) {
		var message = $('message', data).text();
		$('#app-server-response').html(message);
		$('#app-server-response').show('slow');
		$(id).hide('slow');
	}, 'html');
};

function replyStatus(id_status, screen_name) {
	$('#id_in_reply_to_status_id').attr('value',id_status);
	$('#insert_tweet').val('@'+screen_name+' ');
	return false;
}

function retweet(url) {
	$.post(url, '', function(data) {
		$('#app-server-response').html(data);
		$('#app-server-response').show('slow');
	}, 'html');
}

