//#starotvacie nastavenia
var IE=document.all ? true : false;
var ajaxFront=new Array();
var ajaxProgress=new Array();
var ajaxRequest=getAjax();
var popupId=new Array();
var popupDelay=new Array();
var GET=new Array();
var JVAR=new Array();
var IHELP=new Array();
var IINFO=new Array();
var BUFFER=new Array();
var PROGRESS=new Array();
PROGRESS['delay']=1;
PROGRESS['step']=5;
PROGRESS['count']=0;
PROGRESS['front']=new Array();
PROGRESS['front_count']=0;
PROGRESS['stop']=false;
var DELAY=new Array();
DELAY['alert']=3000;
DELAY['fade']=1000;
DELAY['slide']=500;
DELAY['gallery']=5000;
DELAY['checkMail']=30000;
DELAY['checkIINFO']=30000;
//#starotvacie nastavenia

//#nacitanie premennej GET
var loc=location.href.replace(URL['root'],'').split('?');
var action=loc[0].split('/');
if(loc[1])
{
    var params=loc[1].split('&');
}
if(loc[0][0]=='/')
{
    GET['location']=loc[0];
}
else
{
    GET['location']='/' + loc[0];
}
GET['item']=0;
GET['module']='';
GET['submodule']='';
switch(action[0])
{
    case 'admin':
    case 'install':
    case 'webmail':GET['section']='admin';break;
    default:GET['section']='client';break;

}
for(var i in action)
{
    if(action[i]!='admin')
    {
        if(GET['module']=='')
        {
            GET['module']=action[i];
        }
        else if(GET['submodule']=='')
        {
            GET['submodule']=action[i];
        }
        else if(GET['item']==0)
        {
            if(IsNumeric(action[i]))
            {
                GET['item']=action[i];
            }
        }
    }
    else
    {
        GET['section']='admin';
    }
}
if(params)
{
    for(var j in params)
    {
        v=params[j].split('=');
        if(v[0] && v[1] && v[0]!='module' && v[0]!='submodule' && v[0]!='item')
        {
            GET[v[0]]=v[1];
        }
    }
}
//#nacitanie premennej GET

//#vykonanie akcii po nacitani stranky
$(function(){
    $(document).ready(function(){
        //#nacitanie premennej jvar
        $('input[type="hidden"]').each(function(){
            if($(this).attr('class')=='jvar')
            {
                JVAR[$(this).attr('name')]=$(this).attr('value');
            }
        });
        
        //#nacitanie cookie form
        if(GET['take'])
        {
            var buffer=readCookie(GET['location'] + ':form');
            $('form input[type="text"], form input[type="hidden"], form textarea, form select').each(function(){
                if(buffer[$(this).attr('name')])
                {
                    $(this).val(buffer[$(this).attr('name')]);
                }
            });
        }
        
        //#zobrazenie errorov
        $('#error div').dblclick(function() {
            $(this).fadeOut();
        });
        $('.foot-fixed div.infobtn').click(function() {
            $('.foot-fixed div.infobtn').css('display','none');
            $('.foot-fixed div.info').css('display','block');
        });
        $('.foot-fixed div.info').click(function() {
            $('.foot-fixed div.info').css('display','none');
            $('.foot-fixed div.infobtn').css('display','block');
        });
    });
});
//#vykonanie akcii po nacitani stranky

function getAjax()
{
    if(window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        return new ActiveXObject('Microsoft.XMLHTTP');
    }
    return null;
}

//#return values
//#inicializacia ajaxu

function sendRequest(url,cursor)
{
    if(url.length>0)
    {
        ajaxFront[ajaxFront.length]=url;
        ajaxProgress[ajaxFront.length-1]=cursor;
        sendRequest_send();
    }
}

//#return values
//#nacitanie requestu do fronty

function sendRequest_front()
{
    if(!ajaxFront)
    {
        return '';
    }
    for(var j=0;j<ajaxFront.length;j++)
    {
        if(ajaxFront[j].length>0)
        {
            ret=Array();
            ret['url']=ajaxFront[j];
            ret['cursor']=ajaxProgress[j];
            ajaxFront[j]='';
            ajaxProgress[j]='';
            return ret;
        }
    }
    return '';
}

//#return values
//#nacitanie requestu z fronty

function sendRequest_clear()
{
    ajaxFront=Array();
    ajaxRequest=false;
}

//#return values
//#vynulovanie fronty

function sendRequest_send()
{
    if(!ajaxRequest)
    {
        return;
    }
    if(ajaxRequest.readyState==4 || ajaxRequest.readyState==0)
    {
        var data=sendRequest_front();
        if(data['url'] && data['url'].length>0)
        {
            if(data['cursor']!=false)
            {
                $('html,a,.icon').css('cursor','wait');
            }
            $('#pageLoader').css('display','block');
            var url;
            url =URL['root_getdata'];
            url+='ajax.php';
            url+='?section=' + GET['section'];
            url+='&' + data['url'];
            url+='&anticache=' + new Date().getTime();
            ajaxRequest.open('GET',url,true);
            ajaxRequest.onreadystatechange=sendRequestOK;
            ajaxRequest.send(null);
        }
    }
}

//#return values
//#vykonanie samotneho requestu

function uid()
{
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

//#return values
//#vygeneruje nahodny retazec

function alert_ok(data)
{
    var id=uid();
    create_popup_sound('alert-ok',data,id);
    window.setTimeout("destruct('p-" + id + "')",DELAY['alert']);
}

function alert_ok_delay(data)
{
    alert_ok(data);
}
function alert_error(data){
    create_popup_sound('alert-error',data);
}
function alert_error_delay(data)
{
    popupDelay[popupDelay.length]=(popupDelay.length + 1) * 150;
    window.setTimeout("alert_error('" + data + "');",popupDelay[popupDelay.length-1]);
}
function alert_info(data){
    create_popup_sound('alert-info',data);
}
function alert_info_delay(data)
{
    alert_info(data);
}
function alert_help(data){
    create_popup_sound('alert-help',data);
}
function alert_progress(data)
{
    PROGRESS['front'][PROGRESS['front'].length]=data;
    create_popup('alert-progress',data,'alert-progress');
}

//#return values
//#zakladne hlasky

function ajax_removes(what,id)
{
    close_popup(id);
    $('.item-' + what).each(function()
    {
        if($(this).find('.checkbox input[type=checkbox]').is(':checked'))
        {
            var request='';
            request+='action=remove';
            request+='&id=' + $(this).attr('id');
            sendRequest(request);
        }
    });
}

//#return values
//#request pre vymazanie poloziek

function ajax_remove(id)
{
    var request='';
    request+='action=remove';
    request+='&id=' + id;
    sendRequest(request);
    close_popup(id);
}

//#return values
//#request pre vymazanie polozky

function ajax_empty(id)
{
    var request='';
    request+='action=empty';
    request+='&id=' + id;
    sendRequest(request);
    close_popup(id);
}

//#return values
//#request pre vyprazdnenie polozky

function ajax_copy()
{
    $('.item-content').each(function()
    {
        if($(this).find('.checkbox input[type=checkbox]').is(':checked'))
        {
            var request='';
            request+='action=main';
            request+='&request=copy';
            request+='&id=' + $(this).attr('rel');
            sendRequest(request);
        }
    });
}

//#return values
//#request pre skopyrovanie poloziek

function ajax_pack()
{
    var items='';
    $('.item-content').each(function()
    {
        if($(this).find('.checkbox input[type=checkbox]').is(':checked'))
        {
            items+=$(this).attr('id') + '|';
        }
    });
    var request='';
    request+='action=gallery';
    request+='&request=pack';
    request+='&id=' + items;
    sendRequest(request);
    startProgress(request,'slow',true);
}

//#return values
//#request pre zbalenie poloziek

function ajax(type,id)
{
    var request='';
    switch(type)
    {
        //#newslatters
        case 'newslatters-bulletin-send':
            request ='action=newslatters';
            request+='&request=bulletin-send';
            request+='&id=' + id;
            break;
        //#users
        case 'users-contact-convert':
            request ='action=users';
            request+='&request=contact-convert';
            request+='&id=' + id;
            break;
        //#system
        case 'update':
            request ='action=update';
            request+='&request=update';
            break;
        case 'generate-project':
            request ='action=install';
            request+='&request=generate';
            request+='&id=' + id;
            alert_progress(get_string('exporting-project'));
            startProgress('','slow',true);
            break;
        //#eshop
        case 'eshop-cart-remove':
            request ='action=eshop';
            request+='&request=cart-remove';
            request+='&id=' + id;
            break;
        case 'eshop-cart-clear':
            request ='action=eshop';
            request+='&request=cart-clear';
            break;
        case 'eshop-product-send-offer':
            request ='action=eshop';
            request+='&request=product-send-offer';
            request+='&id=' + id;
            break;
        //#transfer
        case 'transfer-import':
            request ='action=transfer';
            request+='&request=import';
            request+='&id=' + id;
            alert_progress(get_string('importing'));
            startProgress(request,'slow',true);
            break;
        //#gallery
        case 'gallery-download':
            request ='action=gallery';
            request+='&request=download';
            request+='&value=' + get_input('popupGalleryUrl');
            request+='&id=' + GET['item'];
            alert_progress(get_string('downloading'));
            startProgress(false,'slow',true);
            break;
        //#crm
        case 'crm-calendar-event-set':
            request ='action=crm';
            request+='&request=calendar-event-set';
            request+='&id=' + id;
            request+='&value=';
            request+=get_text('#dialogCrmCalendarEventAdd #add-name') + '|';
            request+=get_text('#dialogCrmCalendarEventAdd #add-where') + '|';
            request+=get_text('#dialogCrmCalendarEventAdd #add-description') + '|';
            request+=get_radio('add_permissions') + '|';
            request+=get_text('#dialogCrmCalendarEventAdd #add-from') + '|';
            request+=get_text('#dialogCrmCalendarEventAdd #add-to') + '|';
            request+=get_text('#dialogCrmCalendarEventAdd #add-group');
            break;
        case 'crm-calendar-event-get':
            request ='action=crm';
            request+='&request=calendar-event-get';
            request+='&id=' + id;
            break;
        case 'crm-calendar-event-view':
            request ='action=crm';
            request+='&request=calendar-event-view';
            request+='&id=' + id;
            break;
        //business
        case 'business-order-status':
            request ='action=business';
            request+='&request=order-status-set';
            request+='&id=' + id;
            request+='&value=' + get_radio('order-status-' + id);
            break;
        //game
        case 'game-reason-get':
            request ='action=game';
            request+='&request=reason-get';
            request+='&id=' + id;
            break;
        case 'game-player-kick-add':
            request ='action=game';
            request+='&request=player-kick-add';
            request+='&value=' + GET['item'];
            request+='&id=' + id;
            break;
        case 'game-player-ban-add':
            request ='action=game';
            request+='&request=player-ban-add';
            request+='&id=' + id;
            request+='&value=';
            request+=GET['item'] + '|';
            request+=get_text('#dialogGamePlayerBanAdd #add-reason') + '|';
            request+=get_text('#dialogGamePlayerBanAdd #add-time') + '|';
            request+=get_checkbox('add-time-forever');
            break;
        case 'game-player-unban':
            request ='action=game';
            request+='&request=player-unban';
            request+='&id=' + id;
            break;
        case 'game-server-maps-get':
            request ='action=game';
            request+='&request=server-maps-get';
            request+='&id=' + id;
            break;
        case 'game-server-map-change':
            request ='action=game';
            request+='&request=server-map-change';
            request+='&value=' + get_radio('add-map');
            request+='&id=' + id;
            break;
        case 'game-server-start':
            if(!action){var action='start';}
        case 'game-server-restart':
            if(!action){var action='restart';}
        case 'game-server-stop':
            if(!action){var action='stop';}
            request ='action=game';
            request+='&request=server-' + action;
            request+='&id=' + id;
            break;
        //restaurant
        case 'restaurant-cart-clear':
            request ='action=restaurant';
            request+='&request=cart-clear';
            break;
        case 'restaurant-cart-remove':
            request ='action=restaurant';
            request+='&request=cart-remove';
            request+='&id=' + id;
            break;
        default:
            alert_error(get_string('undefined-request'));
            break;
    }
    if(request.length>0)
    {
        sendRequest(request);
    }
    close_popup(id);
}

//#return values
//#pomocna funkcia pred poslanim poziadavku (koli zavretiu popup okienka)

function readCookie(name)
{
    var buffer=new Array();
    var cookie=$.cookie(name);
    if(cookie && cookie.length>0)
    {
        var varr='';
        var vall='';
        var actt='var';
        for(var i=0;i<cookie.length;i++)
        {
            if(cookie[i]==':' && cookie[i-1]!=='\\')
            {
                actt='val';
            }
            else if(cookie[i]==';' && cookie[i-1]!=='\\')
            {
                buffer[varr]=vall;
                actt='var';
                varr='';
                vall='';
            }
            else
            {
                if(!(cookie[i]=='\\' && (cookie[i+1]==':' || cookie[i+1]==';')))
                {
                    switch(actt)
                    {
                        case 'var':varr+=cookie[i];break;
                        case 'val':vall+=cookie[i];break;
                    }
                }
            }
        }
    }
    return buffer;
}

//#return values
//#nacita obsah daneho cookie

function setCookie(name,array)
{
    var buffer='';
    for(var j in array)
    {
        var buf='';
        var val=array[j];
        for(var i=0;i<val.length;i++)
        {
            if(val[i]==':' || val[i]==';')
            {
                buf+='\\' + val[i];
            }
            else
            {
                buf+=val[i];
            }
        }
        buffer+=j + ':' + buf + ';';
    }

    $.cookie(
        name,
        buffer,
        {
            expires:    7,
            path:       '/',
            domain:     URL['root'].replace('http://','').replace('/',''),
            secure:     false
        }
    );
}

//#return values
//#nastavi cookie

function clearCookie()
{
    $.cookie('cookie',null);
}

//#return values
//#odstrani vsetky cookie

function get_identifier(string)
{
    var split=string.split('-');
    for(var i=0;i<split.length;i++)
    {
        if(IsNumeric(split[i]))
        {
            return split[i];
        }
    }
    return 0;
}

//#return values
//ziska identifikator z retazca

function get_checkbox(id)
{
    return document.getElementById(id).checked;
}

//#return values
//#ziska hodnotu checkbox

function set_checkbox(id,setting)
{
    if(setting==1)
    {
        document.getElementById(id).checked=true;
    }
    else
    {
        document.getElementById(id).checked=false;
    }
}

//#return values
//#nastavit checkbox

function get_radio(name)
{
    var radio=document.getElementsByName(name);
    for(var i=0;i<radio.length;i++)
    {
        if(radio[i].checked)
        {
            return radio[i].value;
        }
    }
    return false;
}

//#return values
//#ziskat hodnotu radia

function set_radio(name,value)
{
    var radio=document.getElementsByName(name);
    for(var i=0;i<radio.length;i++)
    {
        radio[i].checked=false;
        if(radio[i].value==value)
        {
            radio[i].checked=true;
        }
    }
}

//#return values
//#nastavit radio

function set_select(id,value)
{
    var select=document.getElementById(id);
    for(var i=0;i<select.length;i++)
    {
        if(select[i].value==value)
        {
            select.selectedIndex=i;
        }
    }
}

//#return values
//#nastavit hodnotu selektu

function set_select_class(cls,val)
{
    $(cls).each(function()
    {
        if($(this).val()==val)
        {
            $(this).attr('selected','selected')
        }
        else
        {
            $(this).attr('selected','')
        }
    });
}

function get_select(id)
{
    var select=document.getElementById(id);
    for(var i=0;i<select.length;i++)
    {
        if(select.selectedIndex==i)
        {
            return select[i].value;
        }
    }
    return false;
}

//#return values
//#ziskat hodnotu selektu

function get_input(id)
{
    return document.getElementById(id).value;
}

//#return values
//#ziska hodnotu z inputu

function set_input(id,value)
{
    document.getElementById(id).value=value;
}

//#return values
//#ziska hodnotu z inputu

function get_string(string)
{
    if(LANG[string])
    {
        return LANG[string];
    }
    else
    {
        return '[' + string + ']';
    }
}

//#return values
//#ziska hodnotu z premennej LANG

function set_color(id,code)
{
    if(id=='chat')
    {
        if(window.setColor)
        {
            setColor(code);
        }
    }
    else if(code!='close')
    {
        if(code!='transparent')
        {
            code='#' + code;
            $('#' + id).css('color',code);
        }
        $('#' + id).val(code);
    }
    $('#boxcolor-' + id).remove();
}

//##return values
//##nastavi farbu

function set_smille(code)
{
    $('#add_message').val($('#add_message').val() + ' ' + code);
    $('#add_message').focus();
    close_popup('smilles');
}

//#return values
//#nastavi smajla

function debug(object)
{
//    if(object.constructor==Array)
//    {
//        var tmp="\n";
//        for(var i in object)
//        {
//            tmp+=object[i] + "\n";
//        }
//        tmp+="\n";
//        object=tmp;
//    }
    alert($.dump(object));
}

//#return values
//#vypise objekt pomocou alertu

function create_popup_sound(type,data,id)
{
    //create_popup(type,data,id);
    //return;
    switch(type)
    {
        case 'file-url':    sound('info');  break;
        case 'remove':      sound('info');  break;
        case 'removes':     sound('info');  break;
        case 'empty':       sound('info');  break;
        case 'alert-error': sound('error'); break;
        case 'alert-info':  sound('info');  break;
        case 'alert-help':  sound('info');  break;
        case 'alert-ok':    sound('info');  break;
    }
    create_popup(type,data,id);
    //window.setTimeout("create_popup('" + type + "','" + data + "','" + id + "');",500);
}

//#return values
//#vytvori popup so zvukom

function create_popup(type,data,id,width,height)
{
    if(!data){data='';}
    if(!id || id==''){id=uid();}
    if(!width || width==''){width=400;}
    if(!height || height==''){height=130;}
    var tpl;
    switch(type)
    {
        case 'remove':                  tpl=get_p_remove(id,data);          break;
        case 'removes':                 tpl=get_p_removes(id,data);         break;
        case 'bulletin-send':           tpl=get_p_bulletin_send(id);        break;
        case 'order-status':            tpl=get_p_order_status(id);         break;
        case 'cart-remove':             tpl=get_p_cart_remove(id,data);     break;
        case 'cart-clear':              tpl=get_p_cart_clear(id,data);      break;
        case 'cart-add':                tpl=get_p_cart_add(id,data);        break;
        case 'alert-ok':                tpl=get_p_ok_alert(id,data);        break;
        case 'alert-error':             tpl=get_p_error_alert(id,data);     break;
        case 'alert-info':              tpl=get_p_info_alert(id,data);      break;
        case 'alert-help':              tpl=get_p_help_alert(id,data);      break;
        case 'alert-progress':          tpl=get_p_progress_alert(id,data);  break;
        case 'filter':                  tpl=get_p_filter(id,data);          break;
        case 'update':                  tpl=get_p_update(id);               break;
        case 'import':                  tpl=get_p_import(id);               break;
        case 'generate-project':        tpl=get_p_project_generate(id);     break;
        case 'generate-pack':           tpl=get_p_pack_generate(id);        break;
        case 'smilles':                 tpl=get_p_smilles(id,data);         break;
        case 'friend-add':              tpl=get_p_friend_add(id);           break;
        case 'store-cart':              tpl=get_p_store_cart(id,data);      break;
        case 'file-url':                tpl=get_p_file_url(id);             break;
        case 'contact-convert':         tpl=get_p_contact_convert(id);      break;
        case 'product-send-offer':      tpl=get_p_send_offer(id);           break;
        case 'player-kick':             tpl=get_p_player_kick(id);          break;
        case 'player-unban':            tpl=get_p_player_unban(id);         break;
        case 'server-start':            tpl=get_p_server_start(id);         break;
        case 'server-restart':          tpl=get_p_server_restart(id);       break;
        case 'server-stop':             tpl=get_p_server_stop(id);          break;
        case 'empty':                   tpl=get_p_empty(id,data);           break;
        default:                        tpl=get_p_undefined(id,type);       break;
    }
    var extra_A=0;
    var extra_L=0;
    var extra_T=0;
    extra_A=popupId.length;
    while(extra_A>10)
    {
        extra_A=extra_A-10;
        extra_L=extra_L + 50;
        extra_T=extra_T + 0;
    }
    var left=(($(window).width()-width)/2 + extra_L) + extra_A * 15;
    var top=(($(window).height()-height)/2 + extra_T) + extra_A * 15;
    var div=document.createElement('div');
    div.setAttribute('id','p-' + id);
    div.style.left=left + 'px';
    div.style.top=top + 'px';
    div.style.width=width + 'px';
    div.style.height=height + 'px';
    div.style.display='none';
    div.className='report';
    div.innerHTML=tpl;
    $('body').append(div);
    if(type=='alert-error' || type=='alert-info' || type=='alert-help')
    {
        popupId[popupId.length]=id;
    }
    $('#p-' + id).css('display','block');
    if(window.open_disappear)
    {
        open_disappear();
    }
    if(window.open_autocomplete)
    {
        open_autocomplete();
    }
}

//#return values
//#konstruktor okna
//#id - ID popup elementu
//#flag - 1 alebo 0, otvorit resp. zavriet popup
//#type - vyber sablony
//#data - volitelny argument ak je potrebne prenasat udaje
//#width_b - sirka okna
//#height_b -vyska okna

function close_popup(id)
{
    popupId=removeVal(id,popupId);
    $('#p-' + id).remove();
}

//#return values
//#zavretie popupu

function destruct(id)
{
    $('#' + id).fadeOut();
    $('#' + id).delay(DELAY['fade']).remove();
}

//#return values
//#odstranenie elementu na zaklade id

function clear_flybox(id)
{
    $('#' + id).parent().find('div.flybox').remove();
}

//#return values
//#zrusi flybox

function trim(str)
{
    return str.replace(/^\s+|\s+$/g,'');
}

//#return values
//#undefined :)

function get_position(tooltip,kmouse)
{
    var border_top=$(window).scrollTop();
    var border_right=$(window).width();
    var left_pos;
    var top_pos;
    var offset=15;
    if(border_right-(offset*2)>=tooltip.width()+kmouse.pageX)
    {
        left_pos=kmouse.pageX+offset;
    }
    else
    {
        left_pos=border_right-tooltip.width()-offset;
    }
    if(border_top+(offset*2)>=kmouse.pageY-tooltip.height())
    {
        top_pos=border_top+offset;
    }
    else
    {
        top_pos=kmouse.pageY-tooltip.height()-offset;
    }
    return Array(
        left_pos,
        top_pos
    );
}

//#return values
//#vypocita poziciu mysky a vrati ju

function ReplaceAll(Source,stringToFind,stringToReplace)
{
    var temp=Source;
    var index=temp.indexOf(stringToFind);
    while(index != -1)
    {
        temp=temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

//#return values
//#zameni vsetky vyskyty retazca

function IsNumeric(value)
{
   return (value - 0) == value && value.length > 0;
}

//#return values
//#true - ak je hodnota cislo
//#false - ak hodnota nie je cislo

function isEmpty(obj)
{
    for(var prop in obj)
    {
        if(obj.hasOwnProperty(prop))
        {
            return false;
        }
    }
    return true;
}

//#return values
//#true - ak je premenna prazdna
//#false - ak nie je premenna prazdna

function inArray(string,array)
{
    for(var key in array)
    {
        if(array[key]==string)
        {
            return true;
        }
    }
    return false;
}

//#return values
//#true - ak sa string v poli nachadza
//#false - ak sa string v poli nenachadza

function arrayColumn(array,column)
{
    var ret=Array();
    for(var key in array)
    {
        ret[key]=array[key][column];
    }
    return ret;
}

//#return values
//#vrati stlpec z viacrozmerneho pola

function arraySearch(string,array)
{
    for(var key in array)
    {
        if(array[key]==string)
        {
            return key;
        }
    }
    return false;
}

//#return values
//#key - ak sa string v poli nachadza
//#false - ak sa string v poli nenachadza

function removeKey(key,array)
{
    var r=new Array();
    for(var k in array)
    {
        if(k!=key)
        {
            r[k]=array[k];
        }
    }
    return r;
}

//#return values
//#odstrani prvok z pola na zaklade kluca

function removeVal(val,array)
{
    var ret=new Array();
    for(var key in array)
    {
        if(array[key]!=val)
        {
            ret[key]=array[key];
        }
    }
    return ret;
}

//#return values
//#odstrani prvok z pola

function cloneItem(pattern,insert)
{
    $(pattern).clone(true).insertAfter(insert);
}

function cloneItemId(pattern,id,insert)
{
    $(pattern).clone(true).attr('id',id).insertAfter(insert);
}

//#return values
//#vyklonuje prvok

function value_select(data,id,empty)
{
    var tpl='';
    if(id)
    {
        if(id.substring(0,1)!='.')
        {
            id='#' + id;
        }
        if(empty==true)
        {
            $(id + ' option').remove();
        }
        for(var i in data)
        {
            tpl='<option value="' + data[i]['id'] + '">';
            tpl+=data[i]['name'];
            tpl+='</option>';
            $(id).append(tpl);
        }
    }
    else
    {
        for(var i in data)
        {
            tpl+='<option value="' + data[i]['id'] + '">';
            tpl+=data[i]['name'];
            tpl+='</option>';
        }
    }
    return tpl;
}

//#return values
//#naplni select datami

function stripslashes(str)
{
    return (str+'').replace(/\\(.?)/g, function (s, n1)
    {
        switch(n1)
        {
            case '\\':  return '\\';    break;
            case '0':   return '\u0000';break;
            case '':    return '';       break;
            default:    return n1;       break;
        }
    });
}

//#return values
//#odstrani spatne lomitka z retazca

function keyPress(e)
{
    var code=0;
    if(window.event)
    {
        code=e.keyCode;
    }
    else if(e.which)
    {
        code=e.which;
    }
    return code;
}

//#return values
//#vrati znak nacitany z klavesnice

function get_text(id)
{
    if($(id).length>0)
    {
        var value=$(id).val();
        value=escape(value);
        value=trim(value);
        return value;
    }
    return '';
}

//#return values
//#vrati text z inputu

function get_cover(action)
{
    if(action==true && $('#p-cover').length==0)
    {
        $('body').append('<div id="p-cover"></div>');
        $('#p-cover').css({
            width:          $('body').width(),
            height:         $('body').height(),
            backgroundColor:'blue',
            position:       'fixed',
            top:            '0px',
            left:           '0px',
            opacity:        0.1            
        })
    }
    else if(action==false)
    {
        $('#p-cover').remove();
    }
}

//#return values
//#vytvori cover

function get_params(identifier)
{
    parts=identifier.split('-');
    return parts[parts.length-1];
}

function sound(sound)
{
    var id='sound-' + sound + '-' + uid();
    if($('#' + id).length==0)
    {
        var tpl;
        tpl ='<audio id="' + id + '" controls="controls" preload="auto">';
        tpl+='<source src="' + URL['root_public'] + 'sounds/' + sound + '.wav"></source>';
        tpl+='<source src="' + URL['root_public'] + 'sounds/' + sound + '.ogg"></source>';
        tpl+='</audio>';
        $('body').append(tpl);
    }
    $('#' + id)[0].play();
    window.setTimeout("$('#" + id + "').remove();",3000);
    
    //tpl='<embed ';
    //tpl+='id="' + id + '"';
    //tpl+='class="sound"';
    //tpl+='src="' + URL['root_public'] + 'sounds/' + sound + '"';
    //tpl+='hidden="true" ';
    //tpl+='autostart="true" ';
    //tpl+='loop="false"';
    //tpl+='/>'
    //$('body').append(tpl);
    //window.setTimeout("$('#" + id + "').remove();",3000);
}

function html_entity_decode(string)
{
    return $('<div />').html(string).text();
}

//#return values
//#dekoduje html entity na platne znaky

function string_to_float(value)
{
    return parseFloat(value.replace(',','.'));
}

//#return avlues
//#prevedie retazec na float

function row_click(url,arg_1,arg_2)
{
    if($('#add_media').length>0 && $('#view_media').length>0 && arg_1.length>0 && arg_2.length>0)
    {
        $('#add_media').val(arg_2);
        if(arg_1.length>0)
        {
            $('#view_media').html(arg_1);
        }
    }
    if($('#add_imgall').length>0 && $('#view_imgall').length>0 && arg_1.length>0 && arg_2.length>0)
    {
        $('#add_imgall').val(arg_2);
        if(arg_1.length>0)
        {
            $('#view_imgall').html(arg_1);
        }
    }
    if($('#add_image').length>0 && arg_1.length>0)
    {
        $('#add_image').val(arg_1);
        $('#view_image').attr('src',URL['root_img'] + arg_1 + '/size/~xl/');
    }
    if($('#add_images').length>0 && url.length>0 && arg_1.length>0 && arg_2.length>0)
    {
        if($('#add_images #gallery-image-' + arg_1).length==0)
        {
            var tmp='';
            tmp+='<div id="gallery-image-' + arg_1 + '">';
            tmp+='<span class="img">';
            tmp+='<a href="javascript:void(0)" class="expansion-a" rel="' + URL['root_img'] + arg_1 + '/size/~x/">';
            tmp+='<img src="' + URL['root_img'] + arg_1 + '/size/~s/' + '" alt="" />';
            tmp+='</a>';
            tmp+='</span>';
            tmp+='<span class="text">' + arg_2 + '</span>';
            tmp+='<span class="remove">';
            tmp+='<a href="javascript:destruct(\'gallery-image-' + arg_1 + '\');" title="' + get_string('clear') + '">';
            tmp+='<img src="' + URL['root_admin_img'] + 'ico-remove.png" alt="' + get_string('clear') + '" />';
            tmp+='</a>';
            tmp+='</span>';
            tmp+='<input type="hidden" name="add_images[' + arg_1 + '][id_image]" value="' + arg_1 + '" />';
            tmp+='<input type="hidden" name="add_images[' + arg_1 + '][name]" value="' + arg_2.replace('"',"'") + '" />';
            tmp+='</div>';
            $('#add_images').append(tmp);

            a_expansion();
        }
        else
        {
            alert_info(get_string('galley-image-exist'));
        }
    }
}

//##return values
//##vyber fotografie z iframu

//#return values
//#pripravi hodnotu pre odoslanie metodou ajax

//#safe-mode
//#$(window).unload(function()
//#{
    //#alert('hmm');
    //#window.stop();
    //#alert_progress('Server je pravdepodobne zaneprazdneny');
    //#sendRequest('action=load');
    //#alert('hmm2');
//#});
//#safe-mode;




