function undef(param) { return param }
function strip(w) { return w.replace(/^\s+/,'').replace(/\s+$/, '') }
function a(action, input_data, callback)
{
    $.post('/game/a/'+action+'/', input_data, function(data)
    {
        data = eval('data = '+data);
        if (data.result == "error") 
            alert(data.exception + '\n\n' + data.trace.join('\n'));
        else
            if (callback) callback(data);
    });
}
function setCaretPosition(elem, caretPos) 
{
    if (elem == null) return;
    if(elem.createTextRange) 
    {
        var range = elem.createTextRange();
        range.move('character', caretPos);
        range.select();
    }
    else 
    {
        elem.focus();
        elem.setSelectionRange(caretPos, caretPos);
    }
}
/////////////////////////////
InputArea = function(jq, func){
    var maxLen = 55;
    if (!$(jq).attr("id"))
        $(jq).attr("id",InputArea.id.get());
    var ta_id = $(jq).attr("id")+"-textarea";
    $(jq).keypress(function(e){ if (this.value.length>maxLen) setTimeout(function(){
                $(jq).blur().hide();
                $('#'+ta_id).val($(jq).val()).show()
                setCaretPosition($('#'+ta_id).get(0), $(jq).val().length);
                $(jq).val('');
            },1)
        }).after(
            $("<textarea>").attr("rows",6).attr("id",ta_id).addClass("inputarea").css({ display:"none", width:$(jq).css("width")}).keypress(
                function(e){ if (e.which==13) { func(this.value); return false;} })
        ).addClass("inputarea");
}
InputArea.id = {
    _id : 0,
    get : function()
    {
        InputArea.id._id++;
        return "inputarea"+InputArea.id._id;  
    }
}
InputArea.reset = function(jq){
    $(jq).show();
    $('#'+$(jq).attr("id")+"-textarea").hide();
}
InputArea.val = function(jq)
{
    if ($(jq).val() != "" && $(jq).val() != InputArea.undef) return $(jq).val();
    if ($('#'+$(jq).attr("id")+"-textarea").val() != InputArea.undef)
        return $('#'+$(jq).attr("id")+"-textarea").val();
    return "";
}
//////////////////////////////////

var highlight_pk=false;
function highlight(card_pk)
{
    $('.id-card-'+card_pk).addClass("b-card-highlight").filter('.b-card-title').map(function(){
        // TODO refactor to "unfold" routine
        this.className='b-card-normal b-card-highlight';
        $(this).parents().filter('.b-card-children-title').slice(0,1).removeClass('b-card-children-title');
    });
    window.setTimeout(function(){ $('.id-card-'+card_pk).removeClass("b-card-highlight"); }, 5000);
    return false;
}
function highlight_prepare(card_pk)
{
    highlight_pk = card_pk;
}
function highlight_release()
{
    if (highlight_pk) highlight(highlight_pk);
    highlight_pk = false;
}

function reply(reply_to_pk, reply_type, reload_children)
{
    if (!reload_children) reload_children="false"
    jQuery.facebox(function(){
        a('reply', {reply:reply_to_pk, metatype:reply_type, reload_children:reload_children}, function(data)
        {
            if (data.ok) jQuery.facebox(data.html);
        })
    });
    return false;
}
function comment_reply(pk, form)
{
    form.hide();
    var text = form.find('.area').val();
    if (text == '') text = form.find('.input').val();
    a('store/comment', {reply:pk, comment:text}, function(data){
        if (data.ok) 
        {
            $('.b-comments #card-comment-'+pk).replaceWith(data.html.replace(/^[^\!]+[^>]+>/,'').replace(/<\![^\!]+$/,''));
        }
    });
    return false;
}
function more_children( parent_pk, skip, context )
{
    a("more/children", {pk:parent_pk,skip:skip}, function(data){ if (data.ok) {
        context.replaceWith(data.html);
    }});
    return false;
}
function more_replies( parent_pk, skip, metatype, context, titles )
{
    a("more/replies", {pk:parent_pk,skip:skip,metatype:metatype,titles:titles}, function(data){ if (data.ok) {
        context.replaceWith(data.html);
    }});
    return false;
}
function more_neighbors( parent_pk, skip, metatype, context )
{
    a("more/neighbors", {pk:parent_pk,skip:skip,metatype:metatype}, function(data){ if (data.ok) {
        context.replaceWith(data.html);
    }});
    return false;
}

function deck(pk, is_remove)
{
    a('deck', {pk:pk, is_remove:is_remove});
}
var _current = false;
var _setfocus = false;
function setfocus(path)
{
    if (_setfocus) { window.clearInterval(_setfocus); _setfocus=false; }
    if (location.pathname != "/game/")
        return location.href = "/game/#"+path; 
    location.hash=path;
    _current = path;
    a('field', {path:path}, function(data){
        $('.b-field .progress').hide();
        if (data.ok) 
        {
            if (data.meta_html != "")
            {
                $('.b-meta-contents .contents').html(data.meta_html);
                $('.b-meta-contents').slideDown();
            }
            $('.b-field .contents').html(data.html);
            $('.b-field .comments-contents').html(data.comments_html);
        }
    });
    if (!_setfocus) _setfocus = window.setInterval(function() { if (location.hash != '#'+_current) return setfocus(location.hash.replace(/^\#/,'')); }, 300);
}

function collapse_comments()
{
    $('.l-field .r').hide();$('.l-field .l').css('width','100%');
    $('.b-field .collapsed').show();
    return false;
}
function show_comments()
{
    $('.l-field .r').show();$('.l-field .l').css('width','70%');
    $('.b-field .collapsed').hide();    
    return false;
}