var ajax_url = 'http://www.rodgor.ru/';
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

function show_form(id) {
    if ($('div#com_form'+id).html() != '') {
        $('div#com_form'+id).toggle();
        $('div#post_form > form#inner_form > textarea#body').focus();
    } else {
        pid = id;
        $('div#post_form').parent().hide();
        $('div#close').show();
        $('div#post_form').appendTo('div#com_form'+id);
        $('div#com_form'+id).show();
        $('div#post_form > form#inner_form > textarea#body').focus();
    }
}

function add_com(elem_type, elem_id, region_id, js_cb) {
    $('div#post_form > form#inner_form > *').attr('disabled', 'true');
    $.get(
        ajax_url+"comments/add",
        {body: $('form#inner_form > textarea#body').val(), pid: pid, elem_type: elem_type, elem_id: elem_id, region_id: region_id, js_cb: js_cb},
        function(data) {
            if(data.status == 'ok') {
                $("div#comments").html(data.comments);
                pid=0;
                eval('if(typeof ' + js_cb + ' == \'function\') '+js_cb+'('+data.com_count+')');
            } else if (data.status == 'wrong_type') {
                $('div#post_form > form#inner_form > *').attr('disabled', '');
            } else if (data.status == 'wrong_elem_id') {
                $('div#post_form > form#inner_form > *').attr('disabled', '');
            } else if (data.status == 'wrong_user') {
                $('div#post_form > form#inner_form > *').attr('disabled', '');
            } else if (data.status == 'empty_body') {
                $('div#post_form > form#inner_form > *').attr('disabled', '');
            }
        },
        "jsonp"
    );
}

function del_com(id, js_cb) {
    $.get(
        ajax_url+"comments/del/"+id,
        {js_cb: js_cb},
        function(data) {
            if(data.status == 'ok') {
                $("div#comments").html(data.comments);
                pid=0;
                eval('if(typeof ' + js_cb + ' == \'function\') '+js_cb+'('+data.com_count+')');
            }
        },
        "jsonp"
    );
}

function vote_com(id, vote_type) {
    $.get(
        ajax_url+"comments/vote/"+id,
        {vote_type: vote_type},
        function(data) {
            if(data.status == 'ok') {
                $("div#comments").html(data.comments);
                pid=0;
            }
        },
        "jsonp"
    );
}

function close_com_form() {
    pid = 0;
    $('div#post_form').parent().hide();
    $('div#close').hide();
    $('div#post_form').appendTo('div#com_form0');
    $('div#com_form0').show();
}

function smile_add(smile, obj) {
    var oText = document.forms.inner_form.body;
    getActiveText(oText);
    var AddSmilie = " " + smile + " ";
    AddText(AddSmilie, oText);
}

function setfocus(oText)
{
    oText.focus();
}

function getActiveText(oText)
{
    setfocus(oText);
    if (!is_ie || (is_ie && !document.selection))
    {
        return false;
    }

    var sel = document.selection;
    var rng = sel.createRange();

    if (rng != null && (sel.type == "Text" || sel.type == "None"))
    {
        text = rng.text;
    }
    if (rng != null && oText.createTextRange)
    {
        oText.caretPos = rng.duplicate();
    }
    return true;
}


function AddText(NewCode, oText)
{
    if (typeof(oText.createTextRange) != "undefined" && oText.caretPos)
    {
        var caretPos = oText.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
        caretPos.select();
    }
    else if (oText.selectionStart || oText.selectionStart == '0')
    { // its mozilla and we'll need to re-write entire text
        var start_selection = oText.selectionStart;
        var end_selection = oText.selectionEnd;

        // fetch everything from start of text area to selection start
        var start = (oText.value).substring(0, start_selection);
        // fetch everything from start of selection to end of selection
        var middle = NewCode;
        // fetch everything from end of selection to end of text area
        var end = (oText.value).substring(end_selection, oText.textLength);

        oText.value = start + middle + end;
        setfocus(oText);
        oText.selectionStart = end_selection + middle.length;
        oText.selectionEnd = start_selection + middle.length;
        getActiveText(oText);
        AddTxt = "";
        return;
    }
    else
    {
        oText.value += NewCode;
    }
    setfocus(oText);
    getActiveText(oText);
    AddTxt = "";
}