var MAX_COMMENT_LENGTH = 4096;

function check_leave_comments(show)
{
    var callback =
    {
        success:  comments_checked,
        failure:  comments_checked,
        argument: []
    };
    YAHOO.util.Connect.asyncRequest('GET', 'checkcomments.php?show=' + show, callback, null, null);
}

function comments_checked(rsp)
{
    var txt = rsp.responseText;
    if (txt.indexOf('fail:') == 0)
        alert(txt.substring(5));
    else
    if (txt == 'ok')
    {
        document.getElementById('copen').style.display = '';
        document.getElementById('cclosed').style.display = 'none';
        document.getElementById('cclosed1').style.display = 'none';
        show_comment_form();
        document.getElementById('subject').focus();
    }
    else
    if (txt == 'authrequired')
    {
        document.getElementById('cauthrequiredexplain').style.display = '';
        document.getElementById('cauthrequired').style.display = '';
        document.getElementById('cclosed').style.display = 'none';
        document.getElementById('cclosed1').style.display = 'none';
        draw_auth_box(document.getElementById('cauthrequired'));
    }
    else
        alert('Sorry, but you are unable to leave comment: Internal error');
}

function clear_comment()
{
    document.getElementById('subject').value = '';
    document.getElementById('text').value = '';
}

function post_comment(show)
{
    var subj = document.getElementById('subject').value;
    var text = document.getElementById('text').value;
    if (text.length == 0)
    {
        alert('Please enter comment text');
        return;
    }
    if (text.length > MAX_COMMENT_LENGTH)
    {
        alert('Comment text should not exceed ' + MAX_COMMENT_LENGTH + " characters");
        return;
    }
    var callback =
    {
        success:  comment_posted,
        failure:  comment_posted,
        argument: [show]
    };
    var postb = document.getElementById('postcomment');
    postb.disabled = true;
    postb.value    = 'Posting..';
    var cancelb = document.getElementById('cancelcomment');
    cancelb.disabled = true;

    var data = 'show=' + show + '&subject=' + encodeURIComponent(subj) + '&text=' + encodeURIComponent(text);
    YAHOO.util.Connect.asyncRequest('POST', 'addcomment.php', callback, data, 'application/x-www-form-urlencoded; charset=UTF-8');
}

function comment_posted(rsp)
{
    var postb = document.getElementById('postcomment');
    postb.disabled = false;
    postb.value    = 'Post';
    var cancelb = document.getElementById('cancelcomment');
    cancelb.disabled = false;

    var txt = rsp.responseText;
    if (txt == 'ok')    
    {
        clear_comment();
        document.getElementById('copen').style.display = 'none';
        document.getElementById('cclosed').style.display = '';
        document.getElementById('cclosed1').style.display = '';
        var callback =
        {
            success:  comment_fetch_ok,
            failure:  comment_fetch_fail,
            argument: []
        };
        YAHOO.util.Connect.asyncRequest('GET', 'comments.php?show=' + rsp.argument[0] + '&r=' + Math.random(), callback, null, null);
    }
    else
    if (txt.indexOf('fail:') == 0)
        alert(txt.substring(5));
    else
        alert('Sorry, we were unable to record your comment: Internal error');
}

function comment_fetch_ok(rsp)
{
    get('comments').innerHTML = rsp.responseText;
    show_comment_tstamps();
}

function comment_fetch_fail(rsp)
{
}

function image_ok(src, id)
{
    var o = get("userpicture" + id);
    if (!o)
        return;
    var scaled = get_scaled_size(src.width, src.height, 75, 75, false);
    var rect = get_rect(scaled, 75, 75);
    var html = '';
    var uname = get('uname' + id);
    if (uname)
        html += '<a target="_new" href="' + homepage_url(uname.value) + '">';
    html += '<img style="margin: 0px 0px 0px 0px; position: absolute; clip: ' + css_rect(rect) + '" src="' + src.src + '" width="' + scaled[0] + '" height="' + scaled[1] + '" title="' + src.title + '" alt="' + src.alt + '"/>';
    if (uname)
        html += '</a>';
    o.style.left = Math.floor(5 + (75 - scaled[0]) / 2) + 'px';
    o.style.top  = Math.floor(4 + (75 - scaled[1]) / 2) + 'px';
    o.innerHTML  = html;
}

function image_error(src, id)
{
    var o = get("userpicture" + id);
    if (!o)
        return;
    var uname = get('uname' + id);
    var html = '';
    if (uname)
        html += '<a target="_new" href="' + homepage_url(uname.value) + '">';
    html += '<img src="' + iurl('no_photo_b.gif') + '" width="67" height="75" alt="' + src.alt + '" title="' + src.title + '"/>';
    if (uname)
        html += '</a>';
    o.style.left = (5 + (75 - 67) / 2) + 'px';
    o.style.top  = 4 + 'px';
    o.innerHTML  = html;
}

function comment(show, page)
{
    var callback =
    {
        success:  comment_fetch_ok,
        failure:  comment_fetch_fail,
        argument: []
    };
    YAHOO.util.Connect.asyncRequest('GET', 'comments.php?show=' + show + '&page=' + page + '&r=' + Math.random(), callback, null, null);
}

function show_comment_form()
{
    get('textcomment').style.display = '';
}

function get_tag(text, name)
{
    var start = '<' + name + '>';
    var spos = text.indexOf(start);
    if (spos < 0)
        return null;
    var end = '</' + name + '>';
    var epos = text.indexOf(end);
    if (epos < 0)
        return null;
    return text.substring(spos + start.length, epos);
}

function local_time(id, date)
{
    var tokens = date.split(/[: \-]/);
    var d = new Date(Date.UTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3], tokens[4]));
    var s = d.getFullYear() + '-' + d2(d.getMonth() + 1) + '-' + d2(d.getDate()) + ' ' + d2(d.getHours()) + ':' + d2(d.getMinutes());
    get('cdate' + id).innerHTML = s;
}

function show_comment_tstamps()
{
    var i = 0;
    var o = get('cdate' + i);
    while (o)
    {
        var date = o.innerHTML;
        var tokens = date.split(/[: \-]/);
        var d = new Date(Date.UTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3], tokens[4]));
        var s = d.getFullYear() + '-' + d2(d.getMonth() + 1) + '-' + d2(d.getDate()) + ' ' + d2(d.getHours()) + ':' + d2(d.getMinutes());
        o.innerHTML = s;
        o.style.visibility = 'visible';
        i++;
        o = get('cdate' + i);
    }
}

function d2(num)
{
    if (num >= 10)
        return num;
    return '0' + num;
}

function login_complete()
{
    get('cauthrequiredexplain').style.display = 'none';
    get('cauthrequired').style.display = 'none';
    check_leave_comments(get('imageid').value);
}

function login_canceled()
{
    get('cauthrequiredexplain').style.display = 'none';
    get('cauthrequired').style.display = 'none';
    rec_canceled();
}

function rec_canceled()
{
    clear_comment();
    document.getElementById('copen').style.display = 'none';
    document.getElementById('cclosed').style.display = '';
    document.getElementById('cclosed1').style.display = '';
}


