var currentGPSData = new Array();
$(function(){
    $( "#tabs" ).bind('tabsselect', function(event, ui) {
        switch($(ui.tab).attr('href')){
            case '#tracker_admin':{
                    trackerAdmin();
                    break;
                }
            case '#user_adnin':{
                    userAdmin();
                    break;
                }
            case '#tracker_view':{
                    initMap();
                    trackerGridInit();
                    break;
                }
            default:break;
        }
    })
     $( "#tabs" ).bind('tabsshow', function(event, ui) {
        switch($(ui.tab).attr('href')){
            case '#tracker_admin':{
                    break;
                }
            case '#user_adnin':{
                    break;
                }
            case '#tracker_view':{
                    yamap.redraw(true,function(){
                        $(document).trigger('onMapRedraw');
                    });
                    break;
                }
            default:break;
        }
     })
    $("#tabs").tabs({
        fx: {
            opacity: 'toggle'
        }
    });
    $(document).bind('onUserLoad',function(){
        if($.user && $.user.role_fis_name == 'user'){
            $("#tabs").tabs('select','2');
        }
    });
    $("input[type=button], .btn").button();
    if($("message").html() == null){
        $("message").css("display","none");
    }
    $('#registrationform').dialog({
        width:500,
        modal:true,
        title:"Регистрация",
        resizable:false,
        buttons: {
            'Регистрировать':function(){
                $('#registrationform form').submit();
            }
        }
    })
    
    $('#alert').dialog({
        width:500,
        autoOpen:false,
        show: 'clip',
        hide: 'clip',
        modal:false,
        title:"Предупреждение",
        resizable:false,
        position: ['right','top'],
        zIndex: 9999,
        beforeclose:function(event,ui){
            if($('#alert').parent().css('display') == 'none')$('#alert').dialog('hide',null);
        }
    });
    $('#confirm').dialog({
        width:500,
        autoOpen:false,
        show: 'clip',
        hide: 'clip',
        modal:true,
        title:"Предупреждение",
        resizable:false
    });
})

function showAlert(message,isInfo, timeout){
    if(!isInfo)
        message = '<div id="message" class="ui-state-error ui-corner-all"><p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-alert"></span> <span id="warn">'+message+'</span></p></div>';
    $('#alert').html(message);
    $('#alert').dialog('open');
    timeout = timeout?timeout:3000;
    setTimeout(function(){$('#alert').dialog('isOpen')?$('#alert').dialog('close'):''}, timeout);
}
function showConfirm(message,okFunction){
    $('#confirm').html(message);
    $('#confirm').dialog("option", "buttons", { 
        "Отменить":function(){
            $('#confirm').dialog('close');
        },
        "Ok": function(){
            if(okFunction)okFunction();
            $('#confirm').dialog('close');
        }
    });
    $('#confirm').dialog('open');
}
function showLoadMask(show){
    show?$("#loadMask").css("display",""):$("#loadMask").css("display","none");
}
function UTCDateToString(UTCDate){
    var date = new Date();
    try{
        if(isNaN(UTCDate))return '';
        date = new Date(new Number(UTCDate)*1000);
    }catch(e){
        showAlert('Ошибка преобразовании даты: '+e);
    }
    return date.getHours()+':'+String(((date.getMinutes()+1)<10)?'0':'')+date.getMinutes()+':'+String(((date.getSeconds()+1)<10)?'0':'')+date.getSeconds()+' '+date.getDate()+'.'
    +String(((date.getMonth()+1)<10)?'0':'')+(date.getMonth()+1)+'.'+date.getFullYear();
}
function createPanel(title,content,id,css){
    var panel = document.createElement('div');
    panel.id = id;
    var pTitle = document.createElement('div');
    var pContent = document.createElement('div');
    $(panel).addClass('ui-dialog ui-widget ui-widget-content ui-corner-all');
    $(panel).css('position', 'relative');
    $(panel).css('margin-bottom','4px');
    if(css)
        $.each(css, function(k,v){
            $(panel).css(k,v);
        });
    $(pTitle).addClass('ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix');
    $(pContent).addClass('ui-widget-content');
    $(pTitle).html(title);
    $(pContent).html(content);
    $(pContent).addClass('ui-dialog-content ui-widget-content');
    $(panel).append(pTitle);
    $(panel).append(pContent);
    return panel;
}
