
$(document).ready(function(){
	
    $.fn.extend({
        linkUrl: function() {
          var returning = [];
          var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
          this.each(function() {
            returning.push(this.replace(regexp,"<a href=\"$1\">$1</a>"))
          });
          return $(returning);
        },
        linkUser: function() {
          var returning = [];
          var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
          this.each(function() {
            returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\">@$1</a>"))
          });
          return $(returning);
        },
        linkHash: function() {
          var returning = [];
          var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
          this.each(function() {
            returning.push(this.replace(regexp, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'))
          });
          return $(returning);
        },
        capAwesome: function() {
          var returning = [];
          this.each(function() {
            returning.push(this.replace(/(a|A)wesome/gi, 'AWESOME'))
          });
          return $(returning);
        },
        capEpic: function() {
          var returning = [];
          this.each(function() {
            returning.push(this.replace(/(e|E)pic/gi, 'EPIC'))
          });
          return $(returning);
        },
        makeHeart: function() {
          var returning = [];
          this.each(function() {
            returning.push(this.replace(/[&lt;]+[3]/gi, "<tt class='heart'>&#x2665;</tt>"))
          });
          return $(returning);
        }
      });	
	
	
	
//*********************************************************************************
//*********************************************************************************
	$('#login_form').validate();

//*********************************************************************************
//*********************************************************************************	
	
	$('.profile_image').hover(function(){
		$(this).addClass("al_img_hover");
	}, function(){
		$(this).removeClass("al_img_hover"); 
	});
	
//*********************************************************************************
//*********************************************************************************	


	$('#login_btn').click(
		function() {
			//$('#logging_in').dialog('open');
			var form = $('#login_form_wrapper').find("form");
            form.submit();
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});

//*********************************************************************************
//*********************************************************************************

	$('#forgot-pass').click(function() {
		show_forgot_pass_dialog();
		}
	)
	.hover(function(){ 
		$(this).addClass("ui-state-hover"); 
	},function(){ 
		 $(this).removeClass("ui-state-hover"); 
	}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	

//*********************************************************************************
//*********************************************************************************

	$('#create-user').click(function() {
		show_dialog_add_account();
		}
	)
	.hover(function(){ 
		$(this).addClass("ui-state-hover"); 
	},function(){ 
		 $(this).removeClass("ui-state-hover"); 
	}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
//*********************************************************************************
//*********************************************************************************

	$('#admin').click(
		function() {
			window.location.replace("/admin.php");
			$('#loading_accounts').dialog('open');	
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
//*********************************************************************************
//*********************************************************************************

	$('#logout').click(
		function() {
			window.location.replace("?logout=true");
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});

//*********************************************************************************
//*********************************************************************************

	$('#add_twitter_account').click(function() {
		show_dialog_add_twitter_account();
		}
	)
	.hover(function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
//*********************************************************************************
//*********************************************************************************
	
	$('#get_tinyurl').click(
		function() {
			show_dialog_get_tiny_url();
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});


//*********************************************************************************
//*********************************************************************************

	$('#home').click(
		function() {
			window.location.replace("/home.php");
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	

//*********************************************************************************
//*********************************************************************************
	
	$('#update_status').click(
		function() {
			//$('#updating_accounts').dialog('open');	
			var form = $('#status_form').find("form");
            form.submit();
		}
	)
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)

	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
		
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	

//********************************************************************************
//********************************************************************************

	$("#admin_list").tabs().find(".ui-tabs-nav").sortable({axis:'x'});

});

//*********************************************************************************
//*********************************************************************************

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}

function addText(thefield){
	if (thefield.value=="")
		thefield.value = "e-mail address"
}

function checkall(thefield,cnt){
	if (document.getElementById("chkall").checked == true){
		for (i=0;i<cnt;i++){
			document.getElementById("chk"+i).checked = true;
		}
	}else{
		for (i=0;i<cnt;i++){
			document.getElementById("chk"+i).checked = false;
		}	
	}
}

//Dynamic Counter
 $(document).ready(function(){  
	 $('#status').each(function(){  
	     var length = $(this).val().length;  
	     $(this).parent().find('.counter').html( 140 );  
	     $(this).keyup(function(){  
	         var new_length = $(this).val().length;  
	         $(this).parent().find('.counter').html( 140 - new_length );  
	     });  
	     $(this).keyup(function(){  
	         var new_length = $(this).val().length;  
	         $(this).parent().find('.counter').html( 140 - new_length );  
	     });  
	     $(this).change(function(){  
	         var new_length = $(this).val().length;  
	         $(this).parent().find('.counter').html( 140 - new_length );  
	     });  	     
	 });  
 }); 


function refresh_rt(email,password){
	$("#loader1").show();
	var url = "/data/proxy.php?ref=rt";
	$.get(url, function(data){
		$("#recent_tweets").html(data);
		$("#recent_tweets").tabs().find(".ui-tabs-nav").sortable({axis:'x'});
		$("#loader1").hide();
	});
}

function refresh_ft(email,password){
	$("#loader2").show();
	var url = "/data/proxy.php?ref=ft";
	$.get(url, function(data){
		$("#friend_tweets").html(data);
		$("#friend_tweets").tabs().find(".ui-tabs-nav").sortable({axis:'x'});
		$("#loader2").hide();
	});
}

function refresh_ft_click(email,password){
	$("#loader2").show();
	var url = "/data/proxy.php?ref=ft";
	$.get(url, function(data){
		//$("#friend_tweets").html(data);
		//$("#friend_tweets").tabs().find(".ui-tabs-nav").sortable({axis:'x'});
		$("#loader2").hide();
	});
	
}

function get_tiny_url(url){
	var text = document.getElementById('status').value;
	var url = "/data/proxy.php?ref=tu&url=" + url;
	$.get(url, function(data){
		$("#status").val(text + data);
		$("#get_tinyurl_form").dialog('close');
	});
	
}

function tweet(member,update){
	$('.counter').html('<img src="/img/ajax-loader2.gif"/>');
	var url = "/data/proxy.php?ref=dt&m=" + member + "&u=" + update + "";
	$.get(url, function(data){
		$("#account_update_results").append("<h1>" + data + "</h1>");
	});
	$('.counter').html("");
}

function show_dialog_add_account(){
	var url = "/data/dialog.php?ref=signup";
	$.get(url, function(data){
		$("#show_dialog_add_account").html(data);
	});
}

function show_forgot_pass_dialog(){
	var url = "/data/dialog.php?ref=forgot_pass";
	$.get(url, function(data){
		$("#show_forgot_pass_dialog").html(data);
	});
}

function show_dialog_add_twitter_account(){
	var url = "/data/dialog.php?ref=add_twitter_account";
	$.get(url, function(data){
		$("#show_dialog_add_twitter_account").html(data);
	});
}

function show_dialog_get_tiny_url(){
	var url = "/data/dialog.php?ref=get_tiny_url";
	$.get(url, function(data){
		$("#show_dialog_get_tiny_url").html(data);
	});
}

function reply(username){
	$("#status").val(username + " ");
	$("#status").focus();
}

function retweet(username,a){
	var tweet = $("#" + a + " .ft_rs_text").text().replace(/<&#91;^>&#93;*>/g, "");
	$("#status").val(username + " " + tweet);
	$("#status").focus();
}

function direct_message(username){
	$("#status").val(username + " ");
	$("#status").focus();

}

function insert_tiny_url(url){
	var text = document.getElementById('status').value;
	$("#status").val(text + url);
	$("#status").focus();
}

function mention(username){
	var text = document.getElementById('status').value;
	$("#status").val(text + username);
}

function clear_status(){
	$("#status").val("");
	$("#status").focus();

}

function update_confirmation(member) {
	var answer = confirm("Update this account?")
	if (answer){
		var un = $("#tabs-" + member + " #t_un").val();
		var pw = $("#tabs-" + member + " #t_pw").val();
		var url = "?update=true&member=" + member + "&un=" + un + "&pw=" + pw;
		$.get(url, function(data){
			window.location = "/admin.php";
		});

	}
}

function delete_confirmation(member) {
	var answer = confirm("Delete this account?")
	if (answer){
		var un = $("#tabs-" + member + " #t_un").val();
		var pw = $("#tabs-" + member + " #t_pw").val();
		var url = "?delete=true&member=" + member + "&un=" + un;
		$.get(url, function(data){
			window.location = "/admin.php";
		});

	}
}


