function toggleDock(){
    var toggle = $("dockToggle");
    if(toggle.className == "collapse"){
        toggle.className = "expand";
        toggle.title = "Collapse"
        $("main").show();
    }else {
        toggle.className = "collapse";
        toggle.title = "Expand";
        $("main").hide();
    }
    handleWindowResize();
}

function getViewport(){
    // http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
    var viewportwidth;
    var viewportheight;
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0)
        {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    }
    // older versions of IE
    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return [viewportwidth, viewportheight]
}

function handleWindowResize(){
    var main = $("main");
    var ifra = $("pIframe");
    var title = $("metaSection");
    var toggle = $("dockToggle");
    var ifraHeight = getViewport()[1]-title.getHeight()-toggle.getHeight()-8;
    if(main.visible()){
        ifraHeight -= main.getHeight();
    }
    ifra.setStyle({
        height: ifraHeight+"px",
        width: "100%"
    });
    $("leftColumnContent").setStyle({height: $("main").getHeight()-40+"px"});
    
}

function showFrame(){
    $("pIframe").src = _GLOBAL_VAR["link"]["preview_url"];
    $("pIframe").show();
    $("noPreview").remove();
}

function setFrame(){
    var loadingEl = new Element("div", {
        "id":"loading"
    });
    var left = $("pIframe").getWidth()/2-16;
    var top = $("pIframe").getHeight()/2+$("metaSection").getHeight()-16;
    loadingEl.setStyle({
        'left': left+'px',
        'top': top+'px'
        });
    document.body.appendChild(loadingEl);
    Event.observe($("pIframe"), 'load', function(){
        $("loading").remove();
    });
}

function showOrHidePreview(){
    var elem = $("iframeOutter");
    if(elem.visible()){
        elem.hide();
        $("toolbar_preview").innerHTML = "Open Preview";
    }else{
        elem.show();
        $("toolbar_preview").innerHTML = "Close Preview";
    }
   
}

function load_data(type,page_num){
    var url = "/annotation_mana/load_data";
    showCentralNoticer(DIS_10);
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        parameters:{
            type:type,
            page_num:page_num,
            lid:_GLOBAL_VAR["link"].link_id
        },
        onComplete:function(request){
            if(request.status=='200') {
                var res = request.responseText.evalJSON();
                if(res.status == 1){
                    $(type).innerHTML = res.partial;
                }
            }
            hideCentralNoticer();
        }
    }
    );
}

function changeTab(tab){
    var tabs = ["people","comments"];
    tabs.each(function(tab){
        $(tab).hide();
    });
    $(tab).show();
    switch(tab){
        case "people" :{
            $("tabPeople").addClassName("current");
            $("tabComments").removeClassName("current");
        }break;
        case "comments" :{
            $("tabPeople").removeClassName("current");
            $("tabComments").addClassName("current");
        }break;
    }
}

//-------------------------------------------------------------
function showOrHideAddComment(){
    var elem = $("commentForm");
    if(elem.visible()){
        elem.hide();
    }else{
        elem.show();
    }
}

function postComment(type){
    var url = '/toolbar_wrap/add_comment';
    var cContent = null;
    if(type == "group"){

    }else if(type=="common"){
        var f = $("commentForm");
        cContent = f.cContent;
        if(cContent.value.trim()=="") {
            cContent.focus();
            flashNotice2(ALE_24, "commentNoticer");
            return false;
        }
    }
    var content = cContent.value;
    var params = {
        user_id:_GLOBAL_VAR["user"].user_id,
        link_id:_GLOBAL_VAR["link"].link_id,
        content:content
    };
    showCentralNoticer(DIS_10);
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        parameters:params,
        onComplete:function(request){
            if(request.status=='200') {
                var res = request.responseText.evalJSON();                

                var b = "<li class=\"aItem\" id='comment_"+res["id"]+"'><a class=\"avatarLink\" title=\""+res["real_name"]+"\" href=\"/profile/"+res["uname"]+"\">"
                +"<img src=\""+RESOURCE_HOST+"/images/avatar/user/"+res["uname"]+"_48.jpg"+"\" alt=\""+res["real_name"]+"\"/></a>"
                +"<div class=\"cInfo\"><a class='removeLink' href='javascript:void(0)' onclick=\"removeComment('common',"+res["id"]+")\">Delete</a><a title=\""+res["real_name"]+"'s profile\" href=\"/profile/"+res["uname"]+"\" username=\""+res["uname"]+"\">"
                +res["real_name"]+"</a>"+" <span class=\"cTime\">"+res["created_at"]+"</span></div><div class=\"cContent\">"+res["content"]
                +"</div><div class='clear'></div></li>";
                
                new Insertion.Bottom($("commentsUL"),b);
                cContent.value = "";
                hideCentralNoticer();
            }
        }
    }
    );
    return false;
}

function removeComment(type,cId){
    var url = '/toolbar_wrap/remove_comment';
    var params = {
        c_id:cId,
        user_id:_GLOBAL_VAR["user"].user_id,
        link_id:_GLOBAL_VAR["link"].link_id
    }
    if(type == "group"){

    }
    showCentralNoticer(DIS_10);
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        parameters:params,
        onComplete:function(request){
            if(request.status=='200') {
                result = request.responseText.evalJSON();
                if(result.code==1) {
                    $("comment_"+result.c_id).remove();
                }
            }
            hideCentralNoticer();
        }
    });
    return false;
}


//bury link
function confirmBuryLink(index,linkID){
    var pw = new PopWindow({
        optEnabled: true,
        left:-200,
        top:10,
        title:"Bury this link?",
        root:"actions"
    });
    pw.show({});
    $('wContent').innerHTML = '<p>Want to bury this link? Please explain briefly why (this will really help our community manager) </p><p><textarea style="width:90%" name="pop_msg" id="mark_comment"></textarea></p>';

    pw.submit = function(){
        pw.hide();buryLink(index,linkID)
    };
    $("wSubmit").value = "Ok";
}

function buryLink(index,linkID) {
    var url = "/spam_mana/bury_link";
    showCentralNoticer();
    new Ajax.Request(url,
    {
        asynchronous:true,
        evalScripts:false,
        parameters:{
            link_id:linkID,
            comment:$("mark_comment").value
        },
        onComplete:function(request){
            if(request.status=="200") {
                var flag = $("toolbar_bury");
                flag.innerHTML = "Buried";
                flag.onclick = null;
                hideCentralNoticer();
            }else {
        //alert(request.status);
        }
        }
    });
    return false;
}