
// usage: $(div_id_2).update(AA_Config.loader);
var AA_Config = {
  AA_INSTAL_PATH: '/aaa/',
  SESS_NAME:      '',
  SESS_ID:        '',
  loader:         '<img src="/aaa/images/loader.gif" border="0" width="16" height="16">',
  icon_new:       '<img src="/aaa/images/icon_new.gif" border="0" width="17" height="17">',
  icon_close:     '<img src="/aaa/images/icon_close.gif" border="0" width="17" height="17">'
}


// now AA specific functions
function AA_HtmlToggle(link_id, link_text_1, div_id_1, link_text_2, div_id_2) {
    if ( $(jqid(div_id_1)).is(':visible') ) {
        $(jqid(div_id_1)).hide('fast');
        $(jqid(div_id_2)).show('fast');
        $(jqid(link_id)).html(link_text_2);
        $(jqid(link_id)).addClass("is-on");
    } else {
        $(jqid(div_id_2)).hide('fast');
        $(jqid(div_id_1)).show('fast');
        $(jqid(link_id)).html(link_text_1);
        $(jqid(link_id)).removeClass("is-on");
    }
}


function AA_Ajax(div_id, url, param, onload) {
    AA_AjaxCss(jqid(div_id), url, param, onload);
}

function AA_AjaxCss(selector, url, param, onload) {
    $(selector).html(AA_Config.loader);
    $(selector).load(url, param, onload);
}

/** selector_update is optional and is good for updating table rows, where we want to show/hide tr, but update td */
function AA_HtmlAjaxToggleCss(link_id, link_text_1, link_text_2, selector_hide, url, selector_update) {
    var link = jqid(link_id);
    if ( $(link).hasClass('is-on')) {
        $(selector_hide).hide('fast');
        $(link).html(link_text_1);
        $(link).toggleClass('is-on');
    } else {
        $(selector_hide).show('fast');
        $(link).toggleClass('is-on');
        // not loaded from remote url, yet?
        if ( !$(link).hasClass('aa-loaded')) {
            $(link).addClass('aa-loaded');
            AA_AjaxCss(selector_update ? selector_update : selector_hide, url);
        }
        $(link).html(link_text_2);
    }
}

/** calls AA responder with permissions of current user and displays returned
 *  html code into div_id
 *  Usage:
 *     FrmSelectEasy('from_slice', $slice_array, $from_slice, 'onchange="DisplayAaResponse(\'fieldselection\', \'Get_Fields\', {slice_id:this.options[this.selectedIndex].value})"');
 *     echo '<div id="fieldselection"></div>';
 **/
function DisplayAaResponse(div_id, method, params) {
    var sess = (AA_Config.SESS_NAME != '') ? AA_Config.SESS_NAME + '=' + AA_Config.SESS_ID : 'AA_CP_Session=' + GetCookie('AA_Sess');
    AA_AjaxCss(jqid(div_id), AA_Config.AA_INSTAL_PATH + 'central/responder.php?' + sess + '&command='+ method, {parameters: params});
}

function AA_Response(method, resp_params, ok_func, err_func) {
    var sess  = (AA_Config.SESS_NAME != '') ? AA_Config.SESS_NAME + '=' + AA_Config.SESS_ID : 'AA_CP_Session=' + GetCookie('AA_Sess');
    $.post(AA_Config.AA_INSTAL_PATH + 'central/responder.php?' + sess + '&command='+ method, resp_params, function(data) {
        if ( data.substring(0,5) == 'Error' ) {
            if (typeof err_func != "undefined") {
                err_func(data);
            }
        } else {
            if (typeof ok_func != "undefined") {
                ok_func(data);
            }
         }
    });
}

/** Sends the form and replaces the form with the response
 *  Polls ussage - @see: http://actionapps.org/en/Polls_Module#AJAX_Polls_Design
 */
function AA_AjaxSendForm(form_id, url) {
    var filler_url = url || 'modules/polls/poll.php3';  // by default it is used for Polls
    if (filler_url.charAt(0)!='/') {
        filler_url = AA_Config.AA_INSTAL_PATH + filler_url;   // AA link
    }

    var valdiv = jqid(form_id);
    var code   = $(valdiv + ' *').serialize();
    $(valdiv).append(AA_Config.loader);

    $.post(filler_url, code, function(data) {
        $(valdiv).attr("data-aa-edited", "0");
        $(valdiv).html(data);
    });
}


function displayInput(valdivid, item_id, fid) {
    var valdiv = jqid(valdivid);

    // already editing ?
    switch ($(valdiv).attr('data-aa-edited')) {
       case '1': return;
       case '2': $(valdiv).attr("data-aa-edited", "0");  // the state 2 is needed for Firefox 3.0 - Storno not works
                 return;
    }
    $(valdiv).attr("data-aa-oldval", $(valdiv).html());
    $(valdiv).html(AA_Config.loader);

    AA_Response('Get_Widget', { field_id: fid, item_id: item_id }, function(data) {
            var valdiv = jqid(valdivid);
            $(valdiv).attr('data-aa-edited', '1');
            $(valdiv).html(data);
            var aa_input = $(valdiv).children('select,textarea,input').first();
            $(aa_input).focus();  // select the input field (<select> or <input>)
            $(aa_input).keydown( function(event) {
                switch (event.which) {
                case 13: $(this).nextAll('input.save-button').click();   break; // Enter
                case 27: $(this).nextAll('input.cancel-button').click(); break; // Esc
//                case 9:  $(this).nextAll('input.save-button').click(); $(this).closest('div.ajax_container').nextAll('div.ajax_container').click(); break; // Tab
                case 9:  // Tab
                         // we must grab the next input right now - after save-button click we have no current div
                         var next_input = $('div.ajax_container').eq($('div.ajax_container').index($(this).parents('div.ajax_container'))+1);
                         $(this).nextAll('input.save-button').click();
                         $(next_input).click();
                         break;
                }
            });
        }
    );
}

/** return back old value - CANCEL pressed on AJAX widget */
function DisplayInputBack(input_id) {
    var valdiv   = jqid('ajaxv_'+input_id);
    $(valdiv).html( $(valdiv).attr('data-aa-oldval') );
    $(valdiv).attr('data-aa-edited', '2');
}



function jqescape(s) {
    // escape all special characters (like [])
    return s.replace(/([^a-zA-Z0-9_-])/g, '\\$1')
}

function jqid(s) {
    // escape all special characters (like [])
    return '#' + jqescape(s);
}

/** This function replaces the older one - proposeChange
 *  The main chane is, that now we use standard AA input names:
 *   aa[i<item_id>][<field_id>][]
 */
function AA_SendWidgetAjax(id) {
    var valdiv = jqid('ajaxv_'+id);
    var code   = $(valdiv + ' *').serialize();
    $(valdiv).append(AA_Config.loader);

    var alias_name = $(valdiv).attr('data-aa-alias');

    code += '&inline=1&ret_code_enc='+alias_name;

    $.post(AA_Config.AA_INSTAL_PATH + 'filler.php3', code, function(data) {
        var res;
        // just one iteration, but without the loop we are not able to get the item_id
        for (var item_id in data) {
            res = data[item_id];
            $(valdiv).html(res.length>0 ? res : '--');
            break;
        }
        $(valdiv).attr("data-aa-edited", "0");
    });
}

/** This function replaces the older one - proposeChange
 *  The main chane is, that now we use standard AA input names:
 *   aa[i<item_id>][<field_id>][]
 */
function AA_SendWidgetLive(id) {
    $('*[id ^="'+id+'"]').addClass('updating');
    var valdivid   = jqid('widget-' + id);
    var code = $(valdivid + ' *').serialize();

    code += '&inline=1';  // do not send us whole page as result

    $.post(AA_Config.AA_INSTAL_PATH + 'filler.php3', code, function(data) {
        $('*[id ^="'+id+'"]').removeClass('updating');
    });
}

/* Cookies */

function SetCookie(name, value) {
   var expires = new Date();
   expires.setTime (expires.getTime() + (1000 * 60 * 60 * 24 * 1)); // a day
   document.cookie = name + "=" + escape(value) +
                      "; expires=" + expires.toGMTString() +
                      "; path=/";
    // + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
    // + ((path == null)    ? "" : ("; path=" + path))
    // + ((domain == null)  ? "" : ("; domain=" + domain))
    // + ((secure == true)  ? "; secure" : "");
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
    var arg  = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i    = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function DeleteCookie(name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=/";
}

function ToggleCookie(name,val) {
    if ( GetCookie(name) != val ) {
        SetCookie(name,val);
    } else {
        DeleteCookie(name);
    }
}

function NewId() {
    // Private array of chars to use
    var chars = '0123456789abcdefgh'.split('');

    var uuid = [];

    // we do not want to have 0 as the first char in pair
    for (var i = 0; i < 16; i++) {
        uuid[2*i]   = chars[0 | (Math.random()*15+1)];
        uuid[2*i+1] = chars[0 | (Math.random()*16)];
    }
    return uuid.join('');
}
