//Wipe function
function ajax_search(){
    //$("#drroper_info").html('<img src="moreajax.gif" />');
    $.ajax({
        type: "GET",
        url: "find.php",
        data: "",
        dataType: "json",
        cache: false,
        success: function(data){
        
    	
            if (data.total_drrop_count >= 1) {
            	var word_length =data.drrop_body.length;
                //文字数に応じてのフォントサイズ変更      
                if (word_length < 30) {
                    $("#wipe_box").html(data.drrop_body).css("font-size", "60px").animate({left: "0px"},{queue: false}).fadeTo(300, 1);
                }
                else if (word_length < 45) {    
                        $("#wipe_box").html(data.drrop_body).css("font-size", "40px").animate({left: "0px"}, {queue: false }).fadeTo(300, 1);
                    }
                    else if (word_length < 160) {
                            $("#wipe_box").html(data.drrop_body).css("font-size", "24px").animate({left: "0px"}, {queue: false}).fadeTo(300, 1);
                        }
                        else {
                        	$("#wipe_box").html(data.drrop_body).css("font-size", "16px").animate({left: "0px"}, {queue: false}).fadeTo(300, 1);
                        }
                
                $("#drroper_confirm").text("true");
                $("#drroper_info").show();
                $("#drroper_name").html(data.username + " ");
                $("#drroper_post_time").text(data.post_time);
                
                $.loadImages("profile/2_" + data.photo, function(){
                    $("#drroper_photo img:first").attr('src', "profile/2_" + data.photo).fadeIn();
                });     
                
                $("#drroper_city").text("(" + data.city);
                $("#drroper_country").text(", " + data.country + ")");
                $("#comment_count").text(data.comment_count + " ");
                
                if (data.view_count != "0") {
                    $("#view_count").text(data.view_count + " times to see!");
                }
                else {
                    $("#view_count").text("");
                }
                if (data.buddy_count != "0") {
                    $("#buddy_count").html('<img src="image/count/' + data.buddy_count + '.png" />');
                }
                else {
                    $("#buddy_count").html("");
                }
                
                //drropへのreply
                $("#drrop_id_hidden").val(data.drrop_id);
                
                $("#block_image").show();
                $("#spam_image").show();

            }
            else {
            
                $("#wipe_box").html("It's quite sunny out there, let's wait for the rain.").css("font-size", "30px").animate({
                    left: "0px"
                }, {
                    queue: false
                }).fadeTo(300, 1);
                $("#drroper_name").html("");
                $("#drroper_post_time").text("");
                $("#drroper_city").text("");
                $("#drroper_country").text("");
                $("#view_count").text("");
                $("#comment_count").text(data.comment_count + " ");
                $("#drroper_confirm").text("true");
                $.loadImages("profile/2_default_man.png", function(){
                	$("#drroper_photo img:first").attr('src', "profile/2_default_man.png").show();
                });
                //$("#drroper_info").show();
                $("#drrop_id_hidden").val(data.drrop_id);
                //alert("no drrop!");
                
                $("#block_image").hide();
                $("#spam_image").hide();
                
            }
            
        }
    });
}

function wipe(){
    if ($("#drroper_confirm").text() == "true") {
        $("#drroper_confirm").text("false");
        
        $("#wipe_box").animate({
            left: "150px",
            opacity: "0"
        }, "fast", function(){
        
            $(this).css({
                left: "-100px"
            });
            $("#drroper_photo img").hide();
            ajax_search();
        });
    }
}

function DoAll(){

    //Watermark
    $('.watermarked').each(function(){
        $(this).watermark('watermark', $(this).attr('title'));
    });
    
    //Textarea Expand
    $(".drrop_reply_post_second_textarea").autoGrow();
    $("#drrop_post_textarea").autoGrow();
    $("#drrop_reply_post_textarea").autoGrow();
}


//When it Load
$(function(){
	//console.log(document.getElementsByTagName('*').length);

    //First reload finished
    ajax_search();
    
    //Drrop post
    $("#drrop_post_form").submit(function(){
        var drrop_body = $("#drrop_post_textarea").val();
        if (drrop_body != "" && drrop_body != "Post a New Drrop") 
            var dataString = 'drrop_body=' + drrop_body;
        $.ajax({
            type: "POST",
            url: "drrop_post.php",
            data: dataString,
            success: function(html){

//                var drrop_drrop = '<div id="drrop_success">Drroped Successfully!</div>';
//                $(drrop_drrop).hide().appendTo("#header_menu").show();
//                setTimeout(function(){
//                    $("#drrop_success").animate({
//                        opacity: "0"
//                    }, "slow", function(){
//                        $(this).remove()
//                    });
//                }, 2000);
        	
        	showDialog2("Drroped Successfully");
                
                $(html).css("opacity", "0").prependTo('.drrop_line_area').fadeTo(1000, 1)
                $("#drrop_post_textarea").val("").attr("rows", "2");
                DoAll();
                ;
            }
        });
        
        return false;
    });
    
    //Drrop button
    $("#drrop_post_textarea").focus(function(){
        $("#drrop_post_submit").show();
        $(this).css("min-height", "35px");
    }).blur(function(){
        if ($(this).val() == "") {
            $("#drrop_post_submit").hide();
        }
    });
    
   
    
    $("#drrop_reply_post_form").submit(function(){
        wipe();
        var reply_body = $('#drrop_reply_post_textarea').val();
        var drrop_id = $("#drrop_id_hidden").val();
        if (reply_body != "" && reply_body != "Reply and Connect") {
        
            var dataString = 'reply_type=1&reply_body=' + reply_body + '&drrop_id=' + drrop_id;
            $.ajax({
                type: "POST",
                url: "drrop_reply_post.php",
                data: dataString,
                success: function(html){
                
                    $("#drrop_reply_post_textarea").val("").css("min-height", "18px");
                    //var drrop_drrop = '<div id="drrop_success">Drroped Successfully!</div>';
                    
                    var item = $(html).css("opacity", "0");
                    $('.drrop_line_area').prepend(item);
                    $('.drrop_line_area li:first').fadeTo(1000, 1);
  
                    var current = $('body').data('current_url');
                    if(current =="gleaning"){
                    	$("#drrop_reply_post_textarea").val("").attr("rows", "2").css("min-height","40px");
                    }else{
                    	$("#drrop_reply_post_textarea").val("").attr("rows", "1").css("min-height","18px");
                    }
                    
                    showDialog2("Replied Successfully");
                    DoAll();
                }
            });
        }
        
        return false;
    });
    
    //Reply button
    $("#drrop_reply_post_textarea").focus(function(){
        $("#drrop_reply_post_button_box").show();
        $(this).css("min-height", "40px");
    }).blur(function(){
        if ($(this).val() == "") {
            $("#drrop_reply_post_button_box").hide();
            
//            if(current =="gleaning"){
//            	$(this).val("").attr("rows", "2").css("min-height","40px");
//            }else{
//            	$(this).val("").attr("rows", "1").css("min-height","18px");
//            }

        }
    });
    
    
    //Second Reply button
    $(".drrop_reply_post_second_textarea").live("focus", function(){
        $(this).css("min-height", "30px");
        var hidden_id = $(this).attr("id");
        var bbb = "#r_" + hidden_id;
        $(bbb).show();
    }).live("blur", function(){
        if ($(this).val() == "") {
            $(this).css("min-height", "18px");
            var hidden_id = $(this).attr("id");
            var bbb = "#r_" + hidden_id;
            $(bbb).hide();
        }
    });
    
    //Second Reply Form
    $(".drrop_reply_post_second_form").live("submit", function(e){
        e.preventDefault(e);
        // return false;
        var reply_body = $('.drrop_reply_post_second_textarea', this).val();
        var drrop_id = $("input[type='hidden']", this).val();
        var save_check_hidden = $('.save_hidden', this);
        var save_check = $('.save_hidden', this).text();
        var $hello = $(this);
        
        if (reply_body != "" && reply_body != "Reply to type text here") {
        
            var dataString = 'reply_type=2&reply_body=' + reply_body + '&drrop_id=' + drrop_id + '&save_check=' + save_check;
            $.ajax({
                type: "POST",
                url: "drrop_reply_post.php",
                data: dataString,
                success: function(html){
                    $(save_check_hidden).text("");
                    
                    var item = $(html).css("opacity", "0");
                    $hello.parent(".drrop_reply_second_box").before(item);
                    $(item).parent().children().fadeTo(1000, 1);
                    
                    
                    
                    $(item).parent().siblings().children(".drrop_line_left_area_save").css("background-image", "url('./image/drrop_mark.png')");
                    $hello.parent(".drrop_reply_second_box").remove();
                    
                    showDialog2("Replied Successfully");
                    DoAll();
                }
            });
        }
        
        return false;
    });
    
    
    
    //Save drrop
    $("#save_button").click(function(){
        wipe();
        var drrop_id = $("#drrop_id_hidden").val();
        var dataString = 'drrop_id=' + drrop_id;
        $.ajax({
            type: "POST",
            url: "save_drrop.php",
            data: dataString,
            success: function(html){
        	 //showDialog2("Saved Successfully");
                //$('.drrop_line_area').prepend(html);
                $(html).css("opacity", "0").prependTo('.drrop_line_area').fadeTo(1000, 1);
                DoAll();
            }
        });
    });
    
    //Delete drrop
    $(".delbutton").live("click", function(e){
        var element = $(this);
        var del_id = element.attr("id");
        var info = 'drrop_id=' + del_id;
        if (confirm("Are you sure you want to delete? There's no undo after deleting!")) {
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: info,
                success: function(data){
                }
            });
            $(this).parents(".drrop_line").animate({
                opacity: "hide"
            }, "slow");
        }
        e.preventDefault();
    });
    
    //Delete Replies
    $(".delbutton_reply").live("click", function(e){
        var element = $(this);
        var del_id = element.attr("id");
        var info = 'reply_id=' + del_id;
        if (confirm("Are you sure you want to delete? There's no undo after deleting!")) {
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: info,
                success: function(html){
       
                }
            });
            $(this).parents(".reply_line").animate({
                opacity: "hide"
            }, "slow");
        }
        e.preventDefault();
        //return false;
    });
    
    //Cancel Save
    $(".delbutton2").live("click", function(e){
        var element = $(this);
        var del_id = element.attr("id");
        var info = 'drrop_id=' + del_id;
        if (confirm("Are you sure you want to cancel? There's no undo after canceling!")) {
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: info,
                success: function(){
                }
            });
            $(this).parents(".drrop_line").animate({
                opacity: "hide"
            }, "slow");
        }
        e.preventDefault();
    });
    
    
    //Watermark
    $('.watermarked').each(function(){
        $(this).watermark('watermark', $(this).attr('title'));
    });
    
    //Auto Rexize
    $("#drrop_post_textarea").autoGrow();
    $("#drrop_reply_post_textarea").autoGrow();
    $(".drrop_reply_post_second_textarea").autoGrow();
    //$("#feedback_post_textarea").autoGrow();
    $("#profile_text").autoGrow();
    $("#feedback_post_textarea").autoGrow();
        
    //See More 
    $('.more').live("click", function(){
        var ID = $(this).attr("id");
        
        var my = /my/; // show my
        var profile = /profile/; // profile 
        if (ID.match(my)) {
            ID = ID.replace(/my/, '');
            var info = "show_type=1&page=" + ID;
        }
        else 
            if (ID.match(profile)) {
                ID = ID.replace(/profile/, '');
                
                var profile_user_id = $('body').data('profile_user_id');
                
                var info = "show_type=2&page=" + ID + "&profile_user_id=" + profile_user_id;
                
            }
            else {
                var info = "page=" + ID;
            }
        
        //alert(info);
        
        if (ID) {
            $("#more" + ID).html('<div id ="load_image" style="text-align:center;height:40px;"><img src="moreajax.gif" /></div>');
            $.ajax({
                type: "POST",
                url: "drrop_list.php",
                data: info,
                cache: false,
                success: function(html){
                    $("#more" + ID).remove();
                    $("ul.drrop_line_area").append(html);
                    //var last_page = parseInt(ID) +5;
                    
                    DoAll();
                }
            });
        }
        else {
            $(".morebox").html('The End');
            $("#more" + ID).remove();
        }
        return false;
    });
    
    //Show my drrop only
    $("#show_my_drrop_buton").live("click", function(e){
        $("#repeat_box").append('<div id ="load_image" style="text-align:center;height:80px;"><img src="moreajax.gif" /></div>');
        $("#drrop_bowl_box_link").html('<a href="#" id="show_all_buton">Show All Drrops</a>');
        $("ul.drrop_line_area").html("");
        $(".morebox").remove();
        //$("#repeat_box").html('<div style="text-align:center;padding-top:10px;"><img src="moreajax.gif" /></div><div style="height:500px"></div>');
        $.ajax({
            type: "POST",
            url: "drrop_list.php",
            data: "show_type=1",
            cache: false,
            success: function(html){
                $("#load_image").remove();
                
                $("ul.drrop_line_area").append(html);
                DoAll();
            }
        });
        
        e.preventDefault();
        return false;
    });
    
    
    $("#show_all_buton").live("click", function(e){
        $("#repeat_box").append('<div id ="load_image" style="text-align:center;height:80px;"><img src="moreajax.gif" /></div>');
        $("#drrop_bowl_box_link").html('<a href="#" id="show_my_drrop_buton">Show My Drrops Only</a>');
        $("ul.drrop_line_area").html("");
        //$("#repeat_box").html('<div style="text-align:center;padding-top:10px;"><img src="moreajax.gif" /></div><div style="height:500px"></div>');
        $.ajax({
            type: "POST",
            url: "drrop_list.php",
            data: "",
            cache: false,
            success: function(html){
                $("#load_image").remove();
                $(".morebox").remove();
                $("ul.drrop_line_area").append(html);
                DoAll();
            }
        });
        
        e.preventDefault();
        return false;
    });
    
    
    
    //Feedback
    $("#feedback_post_button_box").click(function(){
        var user_id = $("#feedback_user_id").text();
        var feedback = $("#feedback_post_textarea").val();
        var dataString = 'feedback=' + feedback + '&user_id=' + user_id;
        
        if (feedback != "" && feedback != "Please help us improving Drrop with your feedback!") {
        
            $.ajax({
                type: "POST",
                url: "feedback.php",
                data: dataString,
                success: function(data){
                    //alert(data);
                    $("#feedback_post_textarea").hide();
                    $("#feedback_post_button_box").hide();
                    $("#feedback_post_form").hide();
                    $("#feedback_complete").show();
            
                }
            });
        }
    });
    
    
    
    //Wipe
    $("#wipe_button").click(function(){
        wipe();
    });
    
    //key操作 press  
    $(this).bind('keydown', 'f2', function(){
        wipe();
    });
    
    
    //Page Slide
    $("#slide-left").pageSlide({
        width: "800px",
        direction: "left",
        modal: true
    });
    
    //Menu slide
    $(".menu_main").click(function(){
        $('ul.menu_1').slideToggle('medium');
        
        $("#wrapper").click(function(){
            $('ul.menu_1').hide();
        });
        
    });
    
    
    
    //Block Spam Box
    $("#drroper_photo").hover(function(){
        $("#block_spam_box").toggle();
    }, function(){
        $("#block_spam_box").toggle();
        
    });
    
    		$("#block_box").live("mouseover",function(){
    			var onSrc = $("img",this).attr('src').replace('.png', '_o.png');
    		    $("img",this).attr('src', onSrc);

    			//$(this).css("padding-left","f4fcff");
    		}).live("mouseout",function(){
    			var onSrc = $("img",this).attr('src').replace('_o.png', '.png');
    		    $("img",this).attr('src', onSrc);
    			
    		});
    		
    		$("#spam_box").live("mouseover",function(){
    			var onSrc = $("img",this).attr('src').replace('.png', '_o.png');
    		    $("img",this).attr('src', onSrc);

    			//$(this).css("padding-left","f4fcff");
    		}).live("mouseout",function(){
    			var onSrc = $("img",this).attr('src').replace('_o.png', '.png');
    		    $("img",this).attr('src', onSrc);
    			
    		});
    
    
    $("#block_box").live("click", function(){
    
        var drrop_id = $("#drrop_id_hidden").val();
        var info = 'drrop_id=' + drrop_id;
        if (confirm("Do you want to add this user to your Umbrella List? \n\n- You won't see any drrops from this user \n- This user won't be able to see your drrops")) {
            $.ajax({
                type: "POST",
                url: "block_post.php",
                data: info,
                success: function(data){
                    if (data == "ok") {
                        //$("#wipe_box").html('<div id="block_success">Blocked Successfully  <span style="color:#999">(You can unblock this user from Umbrella List)</span></div>');
                        //$("#block_success").show();
                        showDialog("Blocked Successfully!");
                    }
                    //					$(this).parents(".drrop_line").animate({ backgroundColor: "#fbc7c7" }, "fast")
                    //					.animate({ opacity: "hide" }, "slow");
                    wipe();
                }
            });
            
        }
        return false;
    });
    
    
    $("#spam_box").live("click", function(){
    
        var drrop_id = $("#drrop_id_hidden").val();
        var info = 'drrop_id=' + drrop_id;
        if (confirm("Do you want to report this drrop as spam?")) {
            $.ajax({
                type: "POST",
                url: "spam_post.php",
                data: info,
                success: function(data){
                    if (data == "ok") {
                        //$("#wipe_box").html('<div id="block_success">Spam Reported</div>');
                        //$("#block_success").show();
                        showDialog("Spam Reported!");
                        wipe();
                    }
                }
            });
            
        }
        return false;
    });
    
    $(".block_small_button").live("click", function(){
        var reply_id = $(this).attr('title');
        var info = 'reply_id=' + reply_id;
        if (confirm("Do you want to add this user to your Umbrella List? \n\n- You won't see any drrops from this user \n- This user won't be able to see your drrops")) {
            $.ajax({
                type: "POST",
                url: "block_post.php",
                data: info,
                success: function(data){
                    if (data == "ok") {
                        //$("#wipe_box").html('<div id="block_success">Blocked Successfully  <span style="color:#999">(You can unblock this user from Umbrella List)</span></div>');
                        //$("#block_success").show();
                        //alert("Success");
                        showDialog("Blocked Successfully!");
                        
                    }
                    //					$(this).parents(".drrop_line").animate({ backgroundColor: "#fbc7c7" }, "fast")
                    //					.animate({ opacity: "hide" }, "slow");
                }
            });
            
        }
        return false;
    });
    
    $(".spam_small_button").live("click", function(){
        var reply_id = $(this).attr('title');
        var info = 'reply_id=' + reply_id;
        if (confirm("Do you want to report this drrop as spam?")) {
            $.ajax({
                type: "POST",
                url: "spam_post.php",
                data: info,
                success: function(data){
                    if (data == "ok") {
                        //$("#wipe_box").html('<div id="block_success">Spam Reported</div>');
                        //$("#block_success").show();
                        showDialog("Spam Reported!");
                    }
                    //					$(this).parents(".drrop_line").animate({ backgroundColor: "#fbc7c7" }, "fast")
                    //					.animate({ opacity: "hide" }, "slow");
                }
            });
            
        }
        return false;
    });
    
    
    //Block Spam Small reply_line
    $(".reply_line").live("mouseover", function(e){
        var $child = $(this).children(".hidden_reply_box");
        var hidden = "#reply_" + $child.val();
        
        //$(this).childcss("background-color", "black");
        $(hidden).css("visibility", "visible");
        //$(this).children(".reply_line_block_area").css("display","block");
    }).live("mouseout", function(e){
        var $child = $(this).children(".hidden_reply_box");
        var hidden = "#reply_" + $child.val();
        //$(".reply_line_block_area").css("display","none");
        $(hidden).css("visibility", "hidden");
        //$(this *).childlen(".reply_line_block_area").css("visibility","hidden");
    
    });
    
   
	
	$(".block_small_button").live("mouseover",function(){	
		var onSrc = $("img",this).attr('src').replace('.png', '_o.png');
	    $("img",this).attr('src', onSrc);

		//$(this).css("padding-left","f4fcff");
	}).live("mouseout",function(){
		var onSrc = $("img",this).attr('src').replace('_o.png', '.png');
	    $("img",this).attr('src', onSrc);
		
	});
	
	$(".spam_small_button").live("mouseover",function(){
		var onSrc = $("img",this).attr('src').replace('.png', '_o.png');
	    $("img",this).attr('src', onSrc);

	}).live("mouseout",function(){
		var onSrc = $("img",this).attr('src').replace('_o.png', '.png');
	    $("img",this).attr('src', onSrc);
		
	});
    
});


//Dialog Window
function showDialog(word){
    //Cancel the link behavior
    
    //Get the A tag
    var id = $("#dialog");
    
    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
    
    //Set the popup window to center
    $(id).css('top', (winH / 2) - ($(id).height()));
    $(id).css('left', winW / 2 - $(id).width() / 2);
    
    $(id).html(word);
    
    $(id).fadeIn("fast");
    setTimeout(function(){
        $(id).fadeOut("hide");
    }, 2000);
    
}

function showDialog2(word){

    var id = $("#dialog2");
    $(id).html(word);
    
    $(id).fadeIn("fast");
    $(id).css("font-weight","bold");
    setTimeout(function(){
        $(id).fadeOut("hide");
    }, 2000);
    
}


