var URLBackendEngine = '../ajax_backend/';

$(document).ready(function(){
    //    DD_roundies.addRule('.accordion', '10px', true);
    DD_roundies.addRule('#loginFormLayer', '10px', true);
    DD_roundies.addRule('.onenews', '10px', true);
    //    DD_roundies.addRule('.bug', '10px', true);
    DD_roundies.addRule('.pagelinkLayer', '10px', true);
    DD_roundies.addRule('#page', '25px 0 25px 0', true);
    DD_roundies.addRule('#body', '0 0 25px 0', true);
    DD_roundies.addRule('#top_background', '0 0 25px 0', true);
    //    $(document).pngFix();

    //    $(".accordion h3:first").addClass("active");
    //    $(".accordion p:not(:first)").hide();
    $(".accordion div.subc").hide();
    $(".accordion h1.contanier").click(function(){
        $(this).next("div.subc").slideToggle("slow")
        .siblings("div.subc:visible").slideUp("slow");
        $(this).toggleClass("active");
        $(this).siblings("h1").removeClass("active");
    });
    //align element in the middle of the screen
    $.fn.alignCenter = function() {
        //get margin left
        var marginLeft =  - $(this).width()/2 + 'px';
        //get margin top
        var marginTop =  - $(this).height()/2 + 'px';
        //return updated element
        return $(this).css({
            'margin-left':marginLeft,
            'margin-top':marginTop
        });
    };

    $.fn.togglePopup = function(){
        //detect whether popup is visible or not
        if($('#popup').hasClass('hidden'))
        {
            //hidden - then display
            //when IE - fade immediately
            if($.browser.msie)
            {
                $('#opaco').height($(document).height()).toggleClass('hidden');
            }
            else
            //in all the rest browsers - fade slowly
            {
                $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7);
            }

            $('#popup')
            .html($(this).html())
            .alignCenter()
            .toggleClass('hidden');
        }
        else
        {
            //visible - then hide
            $('#opaco').toggleClass('hidden').removeAttr('style');
            $('#popup').toggleClass('hidden');
        }
    };
});

function goLink(url){
    window.location.href = url;
}

function tableSort(field){
    $.post(
        URLBackendEngine+'tableSort',
        {
            'field':field
        },
        function(result){
            $('#popup').html(result.content);
        },
        'json'
        );
}

function authPls(){
    var login = $('#remontID').attr('value');
    var password = $('#pswd').attr('value');

    if(login && password){
        $.post(
            URLBackendEngine+'authPls',
            {
                'login':login,
                'password':password
            },
            function(result){
                if(result.forbiden){
                    alert('Не правельный логин/пароль ');
                    $('#auth').togglePopup();
                }else{
                    $('#auth').togglePopup();
                    $('#popup')
                    .css('width','93%')
                    .css('height','85%')
                    .css('top','275px')
                    .css('left','610px')
                    .css('padding','20px');
                    $('#tbLayer').html(result.content).togglePopup();
                }
            },
            'json'
            );
    }else{
        alert('№ накладной & пароль обязательны для заполнения');
    }
    return false;
}

function show_table(content){

}

function closeAuthWnd(){
    $('#tbLayer').togglePopup();
    $('#popup').removeAttr('style');
    $.post(URLBackendEngine+'closeAuthWnd');
}
