$(document).ready(function(){

	$(window).load(function(){
		//var tweet_up1 = $("#tweet_update").val();
                var tweet_up1 = "";
                //tweet_up1 = escape(tweet_up1);
                //alert(tweet_up1);
                //$("#flag_twitter").val("1");
		$.ajax({
			type : "POST",
			url : "twitterengine.php",
			async : false,
			data : "tweet_update="+tweet_up1,
			success : function(html){
				$("#friend_timeline").html(html);
				Go();
				function Go(){
					setTimeout(function(){
					//alert(1);
					$("#friend_timeline").html("");
					
					$(".loading1").show();
					
					$.ajax({
					type : "POST",
					url : "twitterengine.php",
					async : false,
					data : "tweet_update="+tweet_up1,
					success : function(html){
						$("#friend_timeline").html(html);
						$(".loading1").hide();
						Go();
					}
					});
				},300000);
					
				}
			}
			 });
                 
	});

    $("#tweet").click(function(){
        //alert("Ready for tweet");  
            var tweet_update = $("#tweet_update").val();
            //alert(tweet_update);
            tweet_update = tweet_update.toString();
            tweet_update = encodeURI(tweet_update);
            tweet_update = tweet_update.replace(/\*/g,"%2A").replace(/\@/g,"%40").replace(/\+/g,"%2B").replace(/\//g,"%2F");
            tweet_update = tweet_update.replace(/\!/g,"%21").replace(/\#/g,"%23").replace(/\$/g,"%24").replace(/\&/g,"%26");
            tweet_update = tweet_update.replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\+/g,"%2B").replace(/\=/g,"%3D");
            tweet_update = tweet_update.replace(/\:/g,"%3A").replace(/\;/g,"%3B").replace(/\'/g,"%27").replace(/\?/g,"%3F");
            
            //alert(tweet_update);
            
            $.ajax({
               type : "POST",
               url : "twitterengine.php",
               data : "tweet_update="+tweet_update,
               success : function(html){
                    //alert(html);
                    $("#friend_timeline").html(html);
                    //$("#tweet_update").empty();
                    $("#tweet_update").val("");
                    $("#count_char_tweet").html("0");
               }
            });
            
     });
    
    $("#tweet_update").keyup(function(e){
	var tweet_up
        var count_char_tweet
        /*
        if (e.keyCode == 8)
        {
            //alert($("#tweet_update").val().length);
            if ($("#count_char_tweet").text() != "0")
            {
                count_char_tweet = ($("#tweet_update").val().length);
                $("#count_char_tweet").html(count_char_tweet);
            }   
        }
        else{
            count_char_tweet = ($("#tweet_update").val().length);
            $("#count_char_tweet").html(count_char_tweet);
        }*/
            count_char_tweet = ($("#tweet_update").val().length);
            if (count_char_tweet >= 141)
            {
                $("#count_char_tweet").removeClass("count_char_tweet_class");
                $("#count_char_tweet").addClass("count_char_tweet_class140");
                $("#tweet").attr("disabled","disabled");
            }
            else
            {
                $("#count_char_tweet").removeClass("count_char_tweet_class140");
                $("#count_char_tweet").addClass("count_char_tweet_class");
                $("#tweet").removeAttr("disabled");
            }
            $("#count_char_tweet").html(count_char_tweet);    

    
        });
    $("#tweet_update").keydown(function(e){
        var tweet_up;
        if (e.keyCode == 32)
        {
            tweet_up = $("#tweet_update").val();
    
            var tweet_arr = tweet_up.split(" ");
            var cnt_tweet_arr = tweet_arr.length;
            //alert(cnt_tweet_arr);
            //alert(tweet_arr[cnt_tweet_arr - 1]);
            
            if (tweet_arr[cnt_tweet_arr - 1].search("http") != "-1")
            {
                var url = tweet_arr[cnt_tweet_arr - 1];
                        url = encodeURI(url);
							url  = url.replace(/\*/g,"%2A").replace(/\@/g,"%40").replace(/\+/g,"%2B").replace(/\//g,"%2F");
							url  = url.replace(/\!/g,"%21").replace(/\#/g,"%23").replace(/\$/g,"%24").replace(/\&/g,"%26");
							url  = url.replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\+/g,"%2B").replace(/\=/g,"%3D");
							url  = url.replace(/\:/g,"%3A").replace(/\;/g,"%3B").replace(/\'/g,"%27").replace(/\?/g,"%3F");
							//alert(url);
                $.ajax({
                   type : "POST",
                   url : "get_shorturl.php",
                   async : false,
                   data : "url="+url,
                   success : function(html){
                    //alert(html);
                        $("#new_tweet1").html(html);  
                        var short_url = $("#new_tweet1").text();
                        short_url_arr = short_url.split("|");
                   }
                });
                //alert(tweet_arr[cnt_tweet_arr]);
                var new_tweet = tweet_up.replace(tweet_arr[cnt_tweet_arr -1],short_url_arr[1]);
                //var new_tweet = tweet_up.replace("http","xml");
                //alert(new_tweet);
                //var new_tweet_len = new_tweet.length;
                //new_tweet = new_tweet.substr(0,new_tweet_len);
                //document.write(new_tweet+"xxxx");
                $("#tweet_update").val("");
                //alert(typeof new_tweet);
                //new_tweet = new_tweet.toString()+"aa";
                new_tweet = jQuery.trim(new_tweet);
                $("#tweet_update").val(new_tweet);
            }
            //$("#tweet_update").val("xxxx http://www.hotfev.com/l/asdf");
        }
    });
    $("#hidden_update").keyup(function(e){
       alert(e.keyCode);
    });


    /*
    function refresh_tweet(){
        $("#friend_timeline").empty();
        alert("1");
        $.ajax({
        type : "POST",
        url : "twitterengine.php",
        data : "tweet_update="+tweet_up,
        success : function(html){
            $("#friend_timeline").html(html);
        }
        });
        var tt = setTimeout("refresh_tweet()",100);
        //var jj = setInterval(refresh_tweet(),5000);
        
    */    

    
    //var t = setTimeout("refresh_tweet()",5000);
    //setTimeout("alert('test');",5000);
    


});
