function timeline_update(){
	theDate=new Date();
	new Ajax.Request('content-v3.php',{
		parameters: {q: 'friends_timeline',t: escape(theDate.toLocaleString())},
		method: 'get',
		onFailure: function(){
			//ourDate = new Date();
			//$('debug').innerHTML+='(fail) '+ourDate.toLocaleString();
			//var uname=getCookie('uname');
  			//var userlink = '<a target=\'_blank\' href=\'http://twitter.com/'+uname+'\'>'+uname+'</a>';
  			$('title').innerHTML="BeTwittered";
			$('alerts').show();
  			$('alerts').innerHTML='connection error';
  		},
		onSuccess: function(transport){
			if(transport.responseText.substring(0,6)=="<table"){
				//ourDate = new Date();
				//$('debug').innerHTML+='(succ) '+ourDate.toLocaleString()+'<br/>';
				//$('debug').innerHTML+='(succ-http) '+transport.status+'<br/>';
				$('timeline').innerHTML=transport.responseText;
				$('alerts').hide();
				$('alerts').innerHTML='';
			}else{
				//ourDate = new Date();
				//$('debug').innerHTML+='(else) '+ourDate.toLocaleString()+'<br/>';
				//$('debug').innerHTML+='(else-http) '+transport.status+'<br/>';
				$('alerts').show();
				$('alerts').innerHTML=transport.responseText;
			}
			//var uname=getCookie('uname');
  			//var userlink = '<a target=\'_blank\' href=\'http://twitter.com/'+uname+'\'>'+uname+'</a>';
  			//$('title').innerHTML="BeTwittered";
			$('refresh_button').writeAttribute('src','arrow_refresh.png');
		},
		onLoading: function(){
			$('refresh_button').writeAttribute('src','asterisk_yellow.png');
			//$('title').innerHTML='<font color=\'red\'>refreshing...</font>';
			//ourDate = new Date();
			//$('debug').innerHTML+='(LOAD) '+ourDate.toLocaleString()+'<br/>';
			//$('debug').innerHTML+='(LOAD-http) '+transport.status+'<br/>';
		}
	});
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}


function postProcessUpdateKeypress(box, e) {
    var val = box.value;
    updateStatusTextCharCounter(val);
}


//monitors textarea and updates "characters left" count
function updateStatusTextCharCounter(value) {
	 $('countdown').innerHTML = 140 - value.length;
	 if (value.length > 130) {
	 	$('countdown').setStyle({ color: '#d40d12' });
	 } else if (value.length > 120) {
	 	$('countdown').setStyle({ color: '#5c0002' });
	 } else {
	 	$('countdown').setStyle({ color: '#aaaaaa' });
	 }
}

function postProcessUpdateKeypress_alt(box, e, target) {
    var val = box.value;
    updateStatusTextCharCounter_alt(val,target);
}


//monitors textarea and updates "characters left" count
//MUST ID THE COUNTDOWN BY MESSAGE ID!!!!!!!
function updateStatusTextCharCounter_alt(value,target) {
	 $(target).innerHTML = 140 - value.length;
	 if (value.length > 130) {
	 	$(target).setStyle({ color: '#d40d12' });
	 } else if (value.length > 120) {
	 	$(target).setStyle({ color: '#5c0002' });
	 } else {
	 	$(target).setStyle({ color: '#aaaaaa' });
	 }
}

function userEject(id){
	// this function hides/shows user info element by ID.
	// it will also be expanded to swap the "eject" button
	// between "eject" to "close" image.
	// document.write(id);
	element=$(id);
	tweet_element=$("ext_target_tweet_"+id);
	info_element=$("ext_target_info_"+id);
	if(element.visible()){
		//element.slideUp();
		timeline_refresh=setInterval('timeline_update()', 240000);
		element.hide();
	}else{
		//element.slideDown();
		tweet_element.hide();
		info_element.show();
		clearInterval(timeline_refresh);
		element.show();
	}
}

function show_ext_target_tweet(type,id,tweeter){
	tweet_element="ext_target_tweet_"+id;
	info_element="ext_target_info_"+id;
	//set the button text and prepopulate
	//the textareas appropriately
	if (type == "reply") {
		button_text = "Reply";
		tweet_text = "@" + tweeter;
	}
	else {
		button_text = "Direct";
		tweet_text = "d " + tweeter;
	}
	$(info_element).hide();
	$(tweet_element).show();
	show_tweet_form(tweet_element, tweet_text, button_text);
}

function show_tweet_form(target_element, tweet_text, button_text){
	var ext_target="";
	ext_target+='	<div class="ext_tweet_div">';
	ext_target+='		<form action="gadget-v3.php" method="post" class="ext_tweet_form">';
	ext_target+='			<textarea id="'+target_element+'_textarea" class="ext_tweet_textarea" name="tweet" onkeyup="postProcessUpdateKeypress_alt(this, event, \''+target_element+'_countdown\')" rows="2" cols="40">'+tweet_text+' </textarea>';
	ext_target+='			<span class="ext_tweet_dash">';
	ext_target+='				<input type="submit" value="'+button_text+'" class="ext_tweet_button"/>';
	ext_target+='				<span class="countdown" id="'+target_element+'_countdown"></span>';
	ext_target+='			</span>';
	ext_target+='		</form>';
	ext_target+='	</div>';

	//fill the target with text from above
	$(target_element).innerHTML=ext_target;
	
	//now count the chars in the textarea and set the char counter for the user
	text_value=$(target_element+"_textarea").value;
	countdown_element= target_element+"_countdown";
	updateStatusTextCharCounter_alt(text_value,countdown_element);

	//and set the focus so they can just start typing...
	$(target_element+"_textarea").focus();
}
