var elbackup={},toUsername="";
function allert(text){
    $("#dialog").remove();
    $("body").append('<div title="Alert Box" style="display:none" id="dialog"></div>');
    $("#dialog").html(text);
    $("#dialog").dialog({
        autoOpen:false,
        bgiframe:true,
        modal:true,
        buttons:{
            Ok:function(){
                $(this).dialog("close")
            }
        }
    });
    $("#dialog").dialog("open")
}
function conffirm(text,func){
    bool=false;
    $("#dialog").remove();
    $("body").append('<div title="Confirm action" style="display:none" id="dialog"></div>');
    $("#dialog").html(text);
    $("#dialog").dialog({
        autoOpen:false,
        bgiframe:true,
        modal:true,
        buttons:{
            No:function(){
                bool=false;
                $(this).dialog("close")
            },
            Yes:function(){
                bool=true;
                $(this).dialog("close")
            }
        },
        beforeclose:function(){
            if(bool){
                func()
            }
        }
    });
    $("#dialog").dialog("open")
}
function delPost(id){
    pid=parseInt(id);
    $.post(AJAXURL,{
        doo:"true",
        act:"delPost",
        id:pid
    },function(data){
        if(data.substr(0,2)=="->"){
            data=data.replace(/->/ig,'<img src="http://'+HOST+'/images/none.gif" class="alert16" /> ');
            data=data.replace(/\n/ig,"<br />");
            allert(data)
        }else{
            $("#cBox"+pid).fadeOut(400,function(){
                $(this).remove()
            });
            if($(".cBox").length<1){
                $("#freakingQuiet").fadeIn(100)
            }
        }
    })
}
function delReply(id,rid){
    pid=parseInt(id);
    rid=parseInt(rid);
    $.post(AJAXURL,{
        doo:"true",
        act:"delReply",
        id:pid,
        rid:rid
    },function(data){
        if(data.substr(0,2)=="->"){
            data=data.replace(/->/ig,'<img src="http://'+HOST+'/images/none.gif" class="alert16" /> ');
            data=data.replace(/\n/ig,"<br />");
            allert(data)
        }else{
            $("#rBox"+rid).fadeOut(500,function(){
                $(this).remove()
            })
        }
    })
}
$(document).ready(function(){
    var initialclasses="";
    $("#qLogIn").toggle(function(){
        initialclasses=$("#qLogIn").attr("className");
        $("#qLogIn").removeClass("sel").addClass("sel").css({
            "border-bottom":"none"
        });
        $("#quickLogIn").fadeIn(200);
        return false
    },function(){
        $("#qLogIn").removeClass().addClass(initialclasses).css({
            "border-bottom":""
        });
        $("#quickLogIn").fadeOut(200);
        return false
    });
    $(".uiBox .icoSendDM").live("click",function(){
        if($(".uiBox img.dmImg").length>0){
            hideDMBoxes()
        }
        $(this).prev("img").addClass("dmImg rounded bottomNotRounded");
        toUsername=$(this).prev("img").attr("alt");
        var floatLeft=$(this).prev("img").parent(".uiBox").css("float")=="right"?true:false;
        $(this).after('<div class="dmBox rounded '+(floatLeft?"ldmBox":"rdmBox")+'"><textarea id="DMpost" tabindex="250"></textarea>                        <div id="DMbuttons" style="position:relative;width:445px;text-align:right;padding-right:5px">                        <div class="rounded charCounter">...</div>                        <span class="smltext" style="margin-right:50px">Send direct message to '+toUsername+'</span>                        <input id="cancelDM" value="Cancel" name="s" class="cancelB smallerB" type="button">                        <input id="sendDM" value="Send" name="s" class="saveB smallerB" type="button">                        </div>                        </div>');
        $(".dmBox textarea").keyup(function(){
            var delta=parseInt($(this).attr("tabindex"))-$(this).val().length;
            if(delta<=0){
                delta=0;
                $(this).val($(this).val().substr(0,parseInt($(this).attr("tabindex"))))
            }
            $(".dmBox .charCounter").html(delta)
        })
    });
    $("#cancelDM").live("click",function(){
        hideDMBoxes()
    });
    $("#sendDM").live("click",function(){
        if($("#DMpost").val().length<=0){
            allert('<img src="http://'+HOST+'/images/none.gif" class="alert16" />Your direct message should not be empty.')
        }else{
            if($("#DMpost").val().length>250){
                allert('<img src="http://'+HOST+'/images/none.gif" class="alert16" />Your direct message should not be longer than 250 characters.')
            }else{
                elbackup["#DMbuttons"]=$("#DMbuttons").html();
                $("#DMbuttons").html('<img style="vertical-align:middle" src="http://'+HOST+'/images/loader2.gif" /> <span class="smltext">Please wait...</span>');
                $.post(AJAXURL,{
                    doo:"true",
                    act:"sendDM",
                    to:toUsername,
                    post:$("#DMpost").val()
                },function(data){
                    $("#DMbuttons").html(elbackup["#DMbuttons"]);
                    if(data.substr(0,2)=="->"){
                        data=data.replace(/->/ig,'<img src="http://'+HOST+'/images/none.gif" class="alert16" />').replace(/\n/ig,"<br />");
                        allert(data)
                    }else{
                        $("#DMbuttons").html('<span class="smltext">Message has been successfully sent!</span>');
                        setTimeout(function(){
                            hideDMBoxes()
                        },3000)
                    }
                })
            }
        }
    });
    $(".commentForm .trickyTextarea").livequery("click",function(){
        var parr=$(this).parent("div");
        parr.css({
            height:"auto"
        });
        $(".uiBox2",parr).show();
        offWidth=$(this).attr("offsetWidth");
        $(this).hide();
        $(".middleBred",parr).show();
        $("textarea",parr).elastic();
        $("textarea",parr).show().focus().css({
            width:offWidth-44-2*parseInt($("textarea",parr).css("padding-left").replace(/px/ig,""))+"px",
            "margin-left":"40px"
        })
    });
    $(".commentForm textarea").livequery("blur",function(){
        if($(this).val()==""){
            var parr=$(this).parent("div");
            parr.css({
                height:"20px"
            });
            $(this).hide();
            $(".uiBox2",parr).hide();
            $(".trickyTextarea",parr).show();
            $(".middleBred",parr).hide()
        }
    });
    $(".commentForm .middleBred").livequery("click",function(){
        var parr=$(this).parent("div");
        if($("textarea",parr).attr("disabled")===true){
            return false
        }
        var kingparr=$(this).parents(".cBox"),toid=parr.attr("tabindex"),post=$("textarea",parr).val();
        $("textarea",parr).attr("disabled",true);
        $.post(AJAXURL,{
            doo:"true",
            act:"saveReply",
            toid:toid,
            post:post
        },function(data){
            $("textarea",parr).attr("disabled",false);
            if(data.substr(0,2)=="->"){
                data=data.replace(/->/ig,'<img src="http://'+HOST+'/images/none.gif" class="alert16" /> ');
                data=data.replace(/\n/ig,"<br />");
                allert(data)
            }else{
                $("textarea",parr).val("").blur();
                $(".commentForm:last",kingparr).before(data)
            }
        })
    });
    $(".postDelete").livequery("click",function(){
        if($(this).attr("id").match("delPost")){
            pid=$(this).attr("id").replace(/delPost/ig,"");
            conffirm("Are you sure you want to delete this post permanently? All replies associated with this post will be deleted as well.",function(){
                delPost(pid)
            })
        }else{
            if($(this).attr("id").match("delReply")){
                rid=$(this).attr("id").replace(/delReply/ig,"");
                pid=$(this).parents(".cBox").attr("id").replace(/cBox/ig,"");
                conffirm("Are you sure you want to delete this reply permanently?",function(){
                    delReply(pid,rid)
                })
            }
        }
    });
    $('.readAllComments').livequery('click',function(){
        var parr=$(this).parents(".cBox"),toid=parseInt(parr.attr("id").replace(/cBox/ig,''));
        $('span',this).hide();
        $('.readAllComments img',parr).fadeIn(100);
        $.post(AJAXURL,{
            doo:"true",
            act:"getAllReplies",
            toid:toid
        },function(data){
            $('span',this).show();
            $('.readAllComments img',parr).hide();
            if(data.substr(0,2)=="->"){
                data=data.replace(/->/ig,'<img src="http://'+HOST+'/images/none.gif" class="alert16" /> ');
                data=data.replace(/\n/ig,"<br />");
                allert(data)
            }else{
                $('.readAllComments',parr).hide();
                $('.rBox',parr).remove();
                $('.replySeparator',parr).after(data);
            }
        });
    });
    $(".cBox").livequery(function(){
        $(this).hover(function(){
            $(".quickReply","#"+$(this).attr("id")).fadeIn(200);
            $(".postDelete","#"+$(this).attr("id")).fadeIn(200)
        },function(){
            $(".quickReply","#"+$(this).attr("id")).fadeOut(100);
            $(".postDelete","#"+$(this).attr("id")).fadeOut(200)
        })
    });
    $(".ucBox > img,#opts a").tipsy();
    $(".ucontr").tipsy({
        gravity:"e"
    });
    $("#switch_poster img").tipsy({
        gravity:"n"
    })
});
function hideDMBoxes(){
    $(".uiBox img.dmImg").each(function(){
        $(this).removeClass("dmImg rounded bottomNotRounded");
        $(this).nextAll(".dmBox").remove()
    })
}