/***friend_feed.js
Author: Oscar De Gracia Jr

jAlert(meesage,title,callback(optional));
***/
jQuery(document).ready(function(){
get_status_message('status','dont');
	
function unique_requestid() {
	var timestamp = Number(new Date()).toString();
	var random = Math.random() * (Math.random() * 100000 * Math.random() );
	var unique = new String();
	unique = timestamp + random;
return '?requestid='+unique;
}
function toggle_ajaxloader(param){
	if(param == 'hide'){
		
		jQuery('#ffajaxloader_small').css({'visibility':'hidden'});		
	}else{
		jQuery('#ffajaxloader_small').css({'visibility':'visible'});	
	}
}
function edit_textarea(param){
	if(param == 'hide'){
	
		jQuery('#ff_statmsg').css({'background-image' : 'url("profile/input_box.gif")'}).css({'color' : '#F63CEF'});
		jQuery('#ff_extra_info_div').hide();
		
	}else{
		
		jQuery('#ff_statmsg').css({'background-image' : 'url("profile/input_box_dim.gif")'}).css({'color' : '#ffffff'});
	}

}
jQuery('#friendfeed_displaystat').click(function() {
var vuserId = jQuery('#ff_userid').attr("value");
var userId = jQuery('#ff_loggeduserid').attr("value");
	if (vuserId == userId)
	{
		
		jQuery('#ff_statmsg').attr('value','');
		get_status_message('textedit','decode');
		jQuery('#ff_extra_info_div').show();
		jQuery('#friendfeed_formedit').show();
		jQuery('#friendfeed_displaystat').hide();
		jQuery('#ff_statmsg').focus();
		
		
	}
});

function get_status_message(param, char_decode){
	var vuserId = jQuery('#ff_userid').attr("value");
	jQuery.ajax({
					type:'POST',
					url:'friendfeed/ajax/get_status.php' + unique_requestid(),
					data: 'vuser_id='+ vuserId + '&char_decode='+char_decode,
					success:function(val){
					if(param == 'textedit'){
						jQuery('#ff_statmsg').attr('value',val);
						counter_update();						
					}else{
						var t = setTimeout( function() {jQuery('#statupdatetext').html(val);}, 2000 ); 											
					}
					}
				});
}

function save_status_update(){
	var statmsg = jQuery('#ff_statmsg').attr("value");
	var t = setTimeout( function() {jQuery.ajax({	
													type:'POST',
													url:'friendfeed/ajax/save_status.php' + unique_requestid(),
													data: 'statusmsg='+ statmsg,
													error:function(){},
													success:function(){
														toggle_ajaxloader('hide');
														edit_textarea('hide');
														get_status_update()}
												});
												
												  }, 2000 ); 

}
function get_status_update(){
	var vuserId = jQuery('#ff_userid').attr("value");
	jQuery.ajax({
					type:'POST',
					url:'friendfeed/ajax/get_status.php' + unique_requestid(),
					data: 'vuser_id='+ vuserId,
					success:function(val){
					jQuery('#statupdatetext').html(val);
					jQuery('#friendfeed_formedit').hide();
					jQuery('#friendfeed_displaystat').show();
					}
				});
}
function validate_status_update(getXhrVal){
	var statmsg = jQuery('#ff_statmsg').attr("value");
	jQuery.ajax({	
					type:'POST',
					url:'friendfeed/ajax/get_status_validation.php' + unique_requestid(),
					data: 'statusmsg='+ statmsg,
					error:function(){},
					success:function(val){
						getXhrVal(val);
				
					}
			});
			
											 
}

jQuery('#ff_statbox_button').click(function() {
	
	validate_status_update(function(status) {
		var stat = status;
		var statlength = stat.length;
		//alert (statlength);
		
		
		if(statlength != 0){
		jAlert(status,'ERROR:Status Update',function() {
		jQuery('#ff_statmsg').focus();
		});
		}else{
		toggle_ajaxloader('show');
		edit_textarea('show');
		save_status_update();			
		}
		
	});

});

jQuery('#ffextra_info').click(function() {
	
	jQuery('#ff_statmsg').attr('value','');
	jQuery('#ff_statmsg').keyup();
	jQuery('#ff_statmsg').focus();
});

function counter_update(){
	var statlength = jQuery('#ff_statmsg').val().length;
	var diffcount = 150 - statlength; 
	jQuery('#ffextra_info_counter').html(diffcount);
}

jQuery('#ft_on').click(function() {
	
	var checked_status = this.checked;
	jQuery('.opff').each(function()
	{
		this.checked = checked_status;
	});
});

jQuery('#ft_off').click(function() {
	
	
	jQuery('.opff').each(function()
	{
		this.checked = false;
	});
});

jQuery('.opff').click(function() {
	
	var checkval = this.checked;
	var ft_on =  document.getElementById('ft_on');

	var ft_off =  document.getElementById('ft_off');

	if(checkval)
	{
		if(ft_off.checked){
			ft_off.checked = false;
			ft_on.checked = true;
		}
		
	}else{
	
		var n = jQuery('#fformaccount .opff:checked').length;
		if(n==0){
			ft_off.checked = true;
			ft_on.checked = false;
		}
	}
	
});

});
