// JavaScript Document

jQuery(document).ready(function($) { count_update(); });

function follow(o,u,a){	$.post('/timeline/set_follow',{u:u,a:a},function(data){ if(data != 0){ $(o).replaceWith(data); } }); }
function spam(u){ if(confirm('Are you sure you want to report this user as spam?')) $.post('/account/report_spam',{u:u},function(data){ if(data != 0){ alert('Thank You. An alert has been sent.') } }); }
function block(o,u,a){	if(confirm('Are you sure you want to '+(a=='u'?'unblock':'block')+' this user from following you?'))$.post('/timeline/block_user',{u:u,a:a},function(data){ if(data != 0){ $(o).parent().replaceWith(data); } }); }

function elaborate(){ 
	jQuery.facebox($('#elaborate').html());
	$('textarea.status-text').val($('#status').val()); 
	count_update();	
	var status = $('#status').val(); 
	if( status.length == 0 ){ 
		$('.btn-update').attr('disabled','disabled').addClass('btn-disabled');  
		}
	}


function init_timeline(){
	
	$('a[rel=facebox]').facebox();
	
	$('#timeline .comment-box').each(function(){ 
		var parent_id = this.id;
		
		$('#'+this.id+' .status-comment-box').focus(function(){
  			  var top = this.parentNode.parentNode.parentNode;
			  $(top).addClass("infocus");
			  if(this.value == this.defaultValue){ this.value = ''; }
			  if(this.value != this.defaultValue){ this.select(); }
			
								 }).blur(function(){
			  
			  if(this.value == ''){
				  
				var top = this.parentNode.parentNode.parentNode;
				setTimeout(function(){ $(top).removeClass("infocus"); },200);
				this.value = this.defaultValue;
				
				
				}		
								 
					 });
		
		});
	}

function init_more(){
	$('#more').click(function(){ 
		var w = $(this).attr('href');
		$('.more').addClass('loading').html("");
		$.post('/timeline/extend',{w:w},function(data){ $("#pagination").replaceWith(data); init_more(); init_timeline(); }); 
		return false; 
		});
	}


function show_more(o,i){
	
	$(o).addClass('loading'); 
	
	  $.post('/timeline/load_more',
			 {id:i},
			 function(data){ 
				if(data && data != 0){ 
					$(o).remove();
					$('#status-text-'+i).html(data);
					}}); 
	return false; 
	
	}


function invite_user(o,i){ $(o).replaceWith('<img src="/image/common/invite-disabled.gif" />'); $.post('/account/invite_contact',{i:i},function(data){ }); }


function delete_message(i){ $('#message_'+i+' .delete').addClass('loading'); $.post('/user/delete_message',{i:i},function(data){ $('#message_'+i).hide('fast'); });  }


function toggle_share(n){
		if($('#status-update .post_'+n).val()== '1'){
				$('.post_'+n).val('0')
				$('.img_post_'+n).attr('src','/css/img/account/network/'+n+'_24_off.png').tipsy({gravity: 's'});
				$('.tipsy-inner').html('Sharing Disabled');
			} else {
				$('.post_'+n).val('1');
				$('.img_post_'+n).attr('src','/css/img/account/network/'+n+'_24.png').tipsy({gravity: 's'});
				$('.tipsy-inner').html('Sharing Enabled');
			}
			
	}

function count_update(){
	
	$('.status-text').keyup(function(){ 
	var remaining = (140 - this.value.length);
	
	if(remaining == 140){ 
		$('.btn-update').attr('disabled','disabled').addClass('btn-disabled');
	} else if(remaining < 10 && remaining >= 0){ 
		color = '#5C0002'; 
		$('.btn-update').removeAttr('disabled').removeClass('btn-disabled');
	} else if(remaining < 0){ 
		color = '#D40D12'; 
		$('.btn-update').attr('disabled','disabled').addClass('btn-disabled');
	} else { color = '#999999';
		$('.btn-update').removeAttr('disabled').removeClass('btn-disabled');
	}
	
	$('.status-count').text(remaining).css('color',color);
	
	});
	
	}


function load_comments(o,i){ 
	$(o).addClass('loading'); $.post('/timeline/load_comments',
									 {id:i},
									 function(data){ 
									 	if(data != 0){ 
											$(o).parent().replaceWith(data); 
											}}); 
	return false; 
	}
	
	
function show_comment(id){ 
	$('#comment-item-'+id).removeClass("hidden"); 
	$('#comment-item-'+id+' textarea').focus(); 
	}


function add_comment(f){
		
		var top = f.parentNode;
			
			if(trim12(f.comment.value) != ''){ 
				$.post('/timeline/post_comment',
					   {comment:f.comment.value,timeline_id:f.timeline_id.value},
					   function(data){ 
					   		if(data){ $(top).before(data); }
					    })
				
			}
		
		//cleanup
		$(top).removeClass("infocus"); 
		f.comment.value = f.comment.defaultValue;
		
		
		}

function delete_status(o,id,r){
	
	if(confirm('Are you sure you want to delete this entry?')){
	$(o).addClass('loading');
	$.post('/timeline/delete_status',
		   {timeline_id:id},
		   function(data){
			   if(data == 1){ 
			   	$('#user-status-'+id).hide('fast');
				if(r) $('#user-status-'+r).hide('fast');
				}
				$(o).removeClass('loading');
				});
	}
	
	}

function delete_comment(o,id){
	
if(confirm('Are you sure you want to delete this comment?')){
	$(o).addClass('loading');
	$.post('/timeline/delete_comment',
		   {comment_id:id},
		   function(data){
			   if(data == 1){ 
			   	$('#user-comment-'+id).remove();
				}
				$(o).removeClass('loading');
				});
	}
	
	}
	
function toggle_likes(o,id){
	var action = o.innerHTML
	$(o).addClass('loading');
	$.post('/timeline/toggle_likes',
		   {timeline_id:id,action:action},
		  	function(data){
		  		if(data){
				 	$('#comment-item-'+id+' .like').replaceWith(data);
		  			$('#comment-item-'+id).removeClass("hidden"); 
					o.innerHTML = action == 'Like' ? 'Unlike' : 'Like';
					$('a[rel=facebox]').facebox();
					}
					$(o).removeClass('loading');
		   
		   })
	
	
	}

function repost_status(o,id){
	$(o).addClass('loading');
		$.post('/timeline/repost_status',
			   {timeline_id:id},
			   function(data){
				 $(o).replaceWith('Reposted');
				   }
			  );
	
	
	}


function trim12 (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

jQuery.fn.maxLength = function(max){
	this.each(function(){
		//Get the type of the matched element
		var type = this.tagName.toLowerCase();
		//If the type property exists, save it in lower case
		var inputType = this.type? this.type.toLowerCase() : null;
		//Check if is a input type=text OR type=password
		if(type == "input" && inputType == "text" || inputType == "password"){
			//Apply the standard maxLength
			this.maxLength = max;
		}
		//Check if the element is a textarea
		else if(type == "textarea"){
			//Add the key press event
			this.onkeypress = function(e){
				//Get the event object (for IE)
				var ob = e || event;
				//Get the code of key pressed
				var keyCode = ob.keyCode;
				//Check if it has a selected text
				var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
				//return false if can't write more
				return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
			};
			//Add the key up event
			this.onkeyup = function(){
				//If the keypress fail and allow write more text that required, this event will remove it
				if(this.value.length > max){
					this.value = this.value.substring(0,max);
				}
			};
		}
	});
};

//alphanumeric -> http://itgroup.com.ph/alphanumeric/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(2($){$.c.f=2(p){p=$.d({g:"!@#$%^&*()+=[]\\\\\\\';,/{}|\\":<>?~`.- ",4:"",9:""},p);7 3.b(2(){5(p.G)p.4+="Q";5(p.w)p.4+="n";s=p.9.z(\'\');x(i=0;i<s.y;i++)5(p.g.h(s[i])!=-1)s[i]="\\\\"+s[i];p.9=s.O(\'|\');6 l=N M(p.9,\'E\');6 a=p.g+p.4;a=a.H(l,\'\');$(3).J(2(e){5(!e.r)k=o.q(e.K);L k=o.q(e.r);5(a.h(k)!=-1)e.j();5(e.u&&k==\'v\')e.j()});$(3).B(\'D\',2(){7 F})})};$.c.I=2(p){6 8="n";8+=8.P();p=$.d({4:8},p);7 3.b(2(){$(3).f(p)})};$.c.t=2(p){6 m="A";p=$.d({4:m},p);7 3.b(2(){$(3).f(p)})}})(C);',53,53,'||function|this|nchars|if|var|return|az|allow|ch|each|fn|extend||alphanumeric|ichars|indexOf||preventDefault||reg|nm|abcdefghijklmnopqrstuvwxyz|String||fromCharCode|charCode||alpha|ctrlKey||allcaps|for|length|split|1234567890|bind|jQuery|contextmenu|gi|false|nocaps|replace|numeric|keypress|which|else|RegExp|new|join|toUpperCase|ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('|'),0,{}));

