$(document).ready(function(){
    $('#tabs').bind('tabsshow', function(event, uia, ui) {
        $(ui.panel).find("iframe").each(setIFRAMEHeight);
        return true;
    });
    $("iframe").load(setIFRAMEHeight);
    $("#autosavearticles").click(function(){
        $("#addarticle").attr("disabled", $(this).attr("checked"));
    });
    $("#quality").find("a").click(function(){
        $("#quality_dialog").dialog("open");
        return false;
    });
});

function setIFRAMEHeight() {
    var iframedocument, bottomDIV,
        bodyHeight   = 0, 
        bottomTop    = 0, 
        newheight    = 0,
        scrollHeight = 0;
    if (this 
        && this.contentWindow 
        && (iframedocument = this.contentWindow.document)
        && iframedocument
    ) {
        //3 methods for calculating height
        bottomDIV = iframedocument.getElementById('xsearchservices_bottom');
        if (bottomDIV) bottomTop = bottomDIV.offsetTop;
        if (iframedocument.body) {
            scrollHeight = iframedocument.body.scrollHeight;
            bodyHeight   = iframedocument.body.offsetHeight;
            bodyHeight   = (bodyHeight > scrollHeight ? bodyHeight : scrollHeight);
        }
        newheight = (bottomTop > bodyHeight ? bottomTop : bodyHeight);
        //but only set if the result is non zero and not the CSS IFRAME height setting
        if (newheight > 300) this.style.height = newheight + 64 + 'px';
    }
    return newheight;
}

function addQuote(json) {
    var quoteid = json.root.quote.id;
    var text    = json.root.quote.text;
    $("#quotes").children("ul").append('<li class="quote">' +
        '<form class="ajax ajax_f_removeAncestorLI" action="/article/doremovequote" method="post">' +
            '<input type="hidden" name="quoteid" value="' + quoteid + '"/>' +
            '<input class="close" type="submit" value="x"/>' +
        '</form>' +
        ' &#147;' + text + '&#148;' +
      '</li>');
    $("#confirmquote").fadeIn("slow", function(){$(this).fadeOut("slow");}); //message user
    $("form.ajax").submit(ajaxform);                                         //apply the ajax to the new form
}

function selectedText(text) {
    $.post('/api/article/doaddquote.json', {id:articleid, t:text.toString()}, addQuote, "json");
}

