var host = 'http://www.tsubara.tld/';

/*########## IMAGE PERSO ##########*/
function Preview_Text() {
    Elmt_Img = $('img_preview');

    url_src = 'avatars/_perso/font.php?txt=';
    url_src += $('txt').value;
    url_src += '&ttf=';
    url_src += $('ttf').value;
    url_src += '&taille=';
    url_src += $('taille').value;
    url_src += '&color=';
    url_src += $('color').value;

    Elmt_Img.src = host+url_src;
}

function Gen_Ava_Perso(id_ava, ext) {
    Elmt_Img = $('img_preview');

    url_src = 'avatars/perso/avaperso-';
    url_src += '1_';
    url_src += $('ttf').value;
    url_src += '_2_';
    url_src += $('txt').value.replace('_', '---');
    url_src += '_3_';
    url_src += $('taille').value;
    url_src += '_4_';
    url_src += $('color').value;
    url_src += '_5_';
    url_src += id_ava;
    url_src += '_6_';

    for (i=1; i<=9; i++) {
        if ($('pos'+i).checked) {
            url_src += i;
        }
    }

    url_src += '.' + ext;

    ava_perso.src = host+url_src;

    $('tr_lien_ava').style.display = '';
    $('td_lien_ava').innerHTML = host + url_src;
}
/*########## IMAGE PERSO ##########*/


/*########## RATINGS ##########*/
function SendVote(vote, id) {
    var theUL = $('ratingbar'); // the UL

    // switch UL with a loading div
    theUL.innerHTML = '<div class="loading"></div>';

    new Ajax.Request(host+'_ajax/rpc.php', {
        method: 'get',
        parameters: { n: vote ,id: id },
        onSuccess: function(transport){
            theUL.innerHTML = transport.responseText;
        }
    });
}
/*########## RATINGS ##########*/

/*########## FANFIC ##########*/
var Font_Index = 0;
var Tab_Font = new Array('Arial', 'Comic Sans MS', 'Helvetica', 'Times New Roman', 'Verdana', 'Tahoma');

function Change_Font() {
    Font_Index++;
    if (Font_Index == Tab_Font.length) Font_Index = 0;
    $('Div_Txt').style.fontFamily = Tab_Font[Font_Index];
}

function Txt_Moins() {
    elmt = $('Div_Txt').style.fontSize;
    $('Div_Txt').style.fontSize = (parseInt(elmt.replace('px', ''))-2)+'px';
    $('Div_Txt').style.lineHeight = $('Div_Txt').style.fontSize;
}

function Txt_Plus() {
    elmt = $('Div_Txt').style.fontSize;
    $('Div_Txt').style.fontSize = (parseInt(elmt.replace('px', ''))+2)+'px';
    $('Div_Txt').style.lineHeight = $('Div_Txt').style.fontSize;
}
/*########## FANFIC ##########*/

function Affich_Descript() {
    Effect.toggle('view_descript', 'slide');
}


function $RF(reid) {
    var Checked = $$('input[type=radio][name='+reid+']').find(function(el) { return el.checked });

    if (Checked) {
        return Checked.value;
    } else {
        return null;
    }
}
function $RT(reid) {
    return $$('input[type=radio][name='+reid+']').find(function(el) { return el.checked }).alt;
}

function CheckRadio(elmt, id) {
    $(elmt+'_'+id).checked = true;
    $(elmt+'_select').value = id;
    $(elmt+'_select_txt').value = $(elmt+'_'+id).alt;
}

function init_search() {
    if ($('search_txt').value == 'Rechercher') {
        $('search_txt').value = '';
    }
}

function init_join() {
    if ($('pseudo_txt').value == 'Votre pseudo' || $('pseudo_txt').value == 'Your pseudo') {
        $('pseudo_txt').value = '';
    }                                            
}

/**
* Insert text at position
*/
function insert_text(text, spaces)
{
    var textarea;
    
    textarea = $('message');
    
    if (spaces) 
    {
        text = ' ' + text + ' ';
    }
    
    if (!isNaN(textarea.selectionStart))
    {
        var sel_start = textarea.selectionStart;
        var sel_end = textarea.selectionEnd;

        mozWrap(textarea, text, '')
        textarea.selectionStart = sel_start + text.length;
        textarea.selectionEnd = sel_end + text.length;
    }
    else if (textarea.createTextRange && textarea.caretPos)
    {
        if (baseHeight != textarea.caretPos.boundingHeight) 
        {
            textarea.focus();
            storeCaret(textarea);
        }

        var caret_pos = textarea.caretPos;
        caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
    }
    else
    {
        textarea.value = textarea.value + text;
    }
    
    textarea.focus();                
}
/**
* From http://www.massless.org/mozedit/
*/
function mozWrap(txtarea, open, close)
{
    var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var scrollTop = txtarea.scrollTop;

    if (selEnd == 1 || selEnd == 2) 
    {
        selEnd = selLength;
    }

    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);

    txtarea.value = s1 + open + s2 + close + s3;
    txtarea.selectionStart = selEnd + open.length + close.length;
    txtarea.selectionEnd = txtarea.selectionStart;
    txtarea.focus();
    txtarea.scrollTop = scrollTop;

    return;
}

function ButtonLoginOut() {
    $('buttonlogin').style.backgroundImage = 'url(/commun/img/buttonbar.gif)';
    $('buttonlogin').src = '/commun/img/buttonbar.gif';
}

function ButtonLoginOver() {
    $('buttonlogin').style.backgroundImage = 'url(/commun/img/buttonbarhover.gif)';
    $('buttonlogin').src = '/commun/img/buttonbarhover.gif';
}


function View_Score(num) 
{   
    switch(num) {
        case 1:
            if ($('score_txt').style.display != '') 
            {
                $('link_1').className = 'select';
                $('link_2').className = '';
                $('link_3').className = '';

                $('score_img').hide();
                $('score_zic').hide();
                $('score_txt').show();
                //Effect.toggle('score_txt', 'slide');
            }
            break;   
        case 2:
            if ($('score_img').style.display != '') 
            {
                $('link_2').className = 'select';
                $('link_1').className = '';
                $('link_3').className = '';

                $('score_txt').hide();
                $('score_zic').hide();
                $('score_img').show();
                //Effect.toggle('score_img', 'slide');
            }
            break;   
        case 3:
            if ($('score_zic').style.display != '') 
            {
                $('link_3').className = 'select';
                $('link_2').className = '';
                $('link_1').className = '';

                $('score_img').hide();
                $('score_txt').hide();
                $('score_zic').show();
                //Effect.toggle('score_zic', 'slide');
            }
            break;
    }
}

function Signal_Abus(id)
{
    new Ajax.Request('/_ajax/signal_abus.php', {
        method: 'get',
        parameters: {id: id },
        onSuccess: function(transport)
        {
            alert('Votre abus à bien été signalé !');
        }
    });
}

function Signal_Abus_User(id) 
{
    new Ajax.Request(host+'_ajax/signal_abus.php', {
        method: 'get',
        parameters: {id: id, mod: 2 },
        onSuccess: function(transport){
            alert(transport.responseText);
            alert('Votre abus à bien été signalé !');
        }
    });
}

function Send_Error()
{
    new Ajax.Request('/_ajax/send_error.php', {
        method: 'post',
        parameters: {info: $('error_info').value, msg: $('error_msg').value },
        onSuccess: function(transport){
            $('error_form').hide();
            $('error_send').show();
        }
    });
}

function Hide_Graph_Day()
{
    if ($('graph_day')) $('graph_day').innerHTML = '&nbsp;';
}

function View_Graph_Day(dt)
{
    if ($('graph_day')) $('graph_day').innerHTML = dt;
}

function Select_Graph()
{
    new Ajax.Request('/_ajax/view_graph.php', {
        method: 'get',
        parameters: {type: $('stat_type').value, order: $('stat_order').value, user_id: $('user_id').value },
        onSuccess: function(transport){
            $('div_graph').innerHTML = transport.responseText;
            $$('.TipsBox').each( function(link) {
                new Tooltip(link);
            });
        }
    });
}