App = {
    dbinvest_dialog:function(code){

        $( "#dialog-message" ).html(code);
        $( "#dialog-message" ).dialog({
            modal: true,
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                }
            }
        });

    }
}
var formValid=false;
var AjaxSaver = {
    //zapis Ajaxowy
    windowW : null,
    windowH : null,
    windowScr : null,
    docW : null,
    docH : null,
    init : function () {
        this.windowW=$(window).width(),
        this.windowH=$(window).height(),
        this.docW=$(document).width(),
        this.docH=$(document).height(),
        this.windowScr=$(window).scrollTop(),
        this._ajaxcontainer = $('<div id=ajaxcontainer></div>'),
        this._ajaxcontainer.css({

            width        : this.windowW,
            height       : this.windowH,
            position     : 'absolute',
            top          : 0,
            left         : 0,
            background	: '#fff',
            opacity      : '0'
        });
        this._ajaxcontainer.appendTo($('body'));
        this._ajaxcontainer.hide();
        if($('#adminForm').length==0){ //
            path='jquery/';
        } else {
            //jestesmy w panelu
            path='../jquery/';
        }
        this._ajaxloader=$('<div id=ajaxloader style="text-align:center;background:#EDECE7;padding:3px;border:1px solid grey;border-radius:5px;-moz-border-radius:5px"><img src="/images/ikony/loader_anim.gif" width="50" height="50" align="absmiddle"><span style="background:#fff;color:#0E3782"> Proszę czekać ...</span></div>');
        this.w=200;
        this.h=50;
        this._ajaxloader.css({
            width       : this.w,
            height      : this.h,
            position    : 'absolute',
            left        : ($(window).width()-this.w) / 2,
            top         : ($(window).height()-AjaxSaver.h) / 2 + $(window).scrollTop(),
            font        : 'bold 16px Arial',
            color       : 'black',
            background  : 'white'            
            
        });
        this._ajaxloader.appendTo($('body'));
        this._ajaxloader.hide();


        this.handler=$('<div id="handler"></div>');
        this.handler.appendTo('body')
        this.handler.ajaxStart(function(){
            AjaxSaver._ajaxcontainer.fadeIn('fast', function(){  
                });
            AjaxSaver._ajaxloader.fadeIn('fast');
        });
        this.handler.ajaxStop(function(){
            AjaxSaver._ajaxloader.fadeOut('fast', function(){
                });
            AjaxSaver._ajaxcontainer.fadeOut('fast');
        });

        $(window).resize(function() {
                
            AjaxSaver._ajaxcontainer.css({
                width        : $(window).width(),
                height       : $(window).height()
            });
            AjaxSaver._ajaxloader.css({
                left        : ($(window).width()-AjaxSaver.w) / 2,
                top         : ($(window).height()-AjaxSaver.h) / 2 + $(window).scrollTop()
            });
        });
        $(window).scroll(function() {
            AjaxSaver.windowScr=$(window).scrollTop();            
            AjaxSaver._ajaxloader.css('top',($(window).height()-AjaxSaver.h) / 2 + $(window).scrollTop());
        });
    }

}


/**
 * Oczekuje na zaladowanie i sparsowanie html`a w dokumencie strony
 */
$(document).ready(function(){
    if($('#mainADGRATIS').length>0){
        
        $('#mainADGRATIS').css({
            
            'background-image':'url(DBfiles/banery/ad-abonament-GRATIS-sprite.gif)'
        })
        $('#mainADGRATIS').mouseover(function(){
            $(this).css('background-position','bottom')
        }).mouseout(function(){
            $(this).css('background-position','top')
        })
    }
    
    //AjaxSaver.init();

    //galeria obrazów PrettyPhoto
    $("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square',/* light_rounded / dark_rounded / light_square / dark_square / facebook */
        overlay_gallery: false,
        allow_resize: false,
        default_width: 800,
        default_height: 800
    });

 
    

    

    //wyszukiwarka

    var DateOptionsSearchOld= {
        firstDay: 1,
        monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
        dayNames: ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
        dayNamesMin: ['Nie', 'Po', 'Wt', 'Śr', 'Czw', 'Pi', 'So'],
        dateFormat: 'yy-mm-dd',
        minDate: '-6m',
        maxDate: '+0d',
        prevText: 'Poprzedni',
        nextText: 'Następny'
    }
    var DateOptionsSearch= {
        firstDay: 1,
        monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
        dayNames: ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
        dayNamesMin: ['Nie', 'Po', 'Wt', 'Śr', 'Czw', 'Pi', 'So'],
        dateFormat: 'yy-mm-dd',        
        maxDate: '+0d',
        prevText: 'Poprzedni',
        nextText: 'Następny'
    }
	
    $(function() {
        /*$("#data_dodania_od").datepicker(DateOptionsSearch);
        $("#data_dodania_do").datepicker(DateOptionsSearch);
        $("#data_skladania_od").datepicker(DateOptionsSearch);
        $("#data_skladania_do").datepicker(DateOptionsSearch);
		
        $("#data_skladania_do").datepicker(DateOptionsSearch);*/
        $('.searchFormData').datepicker(DateOptionsSearch);
    });


   
   

    $('#szukajArchiwumAktualnosci').click(function(){
        location.href='?sec=aktualnosciArchiwum&r='+$('#rok').val();//+'&m='+miesiac;
    })

    $('#UserEditSubmitBTN').click(function(){
        $.ajax({
            url : '/jquery/dane-uzytkownika-zapis.php',
            data : $('#frmDaneUzytkownika').serialize(), // MEtoda serialize automatycznie zmienia dane wypełnionego formularza do formatu JSON - #m-folder-form - id forma
            type : 'POST',
            dataType : 'json',
            success : function(response) {
                // Dla urozmaicenia:
                if (response.result == 1) {
                    
                    $('#submitresponse').html(response.html)
                }
                
            }
        });
    })

    $('#RejestracjaSubmitBTN').click(function(){
        $.ajax({
            url : '/jquery/rejestracja-waliduj.php',
            data : $('#frmDaneUzytkownika').serialize(), // MEtoda serialize automatycznie zmienia dane wypełnionego formularza do formatu JSON - #m-folder-form - id forma
            type : 'POST',
            dataType : 'json',
            success : function(response) {
                // Dla urozmaicenia:
                if (response.result == 1) {
                    //obiekt z branżami ustawionymi przez użytkownika
                    //$('#submitresponse').html(response.html)
                               
                    if(response.ok==1) {
                        //zatwierdzenie formularza
                        $("#frmDaneUzytkownika").submit();
                    } else {
                        $('#submitresponse').html(response.html);
                        App.dbinvest_dialog(response.html);
                    }


                }
                $('#BR-nav input').attr('disabled',false);
            }
        });
    })
    
    //jqueryTOOLS===============
    //
    $('.tooltips').tooltip({
        delay: 0,

        effect: 'fade',

        tipClass: 'tooltip',
        position: "bottom right",
        onShow: function() {
            this.getTrigger().fadeTo("slow", 0.8);
        }


    })

    //wyszukiwarka
    $('.przynajmniejJedno').click(function(){
        var formId='#frmSearch-'+$('#po').val();
        var checkName=$(this).attr('name');
       
        
        var k=0;
        var c=0;
        $(formId+' .przynajmniejJedno').each(function(){
            if($(this).attr('name')==checkName){
                k++;
                if($(this).attr('checked')){
                    c++;
                }
            }
        })
        
        checkName=checkName.substring(0,checkName.length-2);
        var warn=$(formId+' .'+checkName+"-warning");
        if(c>0) warn.css('display','none');
        else warn.css('display','block');
    })


    if($("#searchFormTabs").length>0){
        var startIndex;
        if($('#poStart').val()=='p')
            startIndex=0;
        if($('#poStart').val()=='i')
            startIndex=1;
        if($('#poStart').val()=='f')
            startIndex=2;
        
        $("ul.tabs").tabs("div.panes > div",{
            'initialIndex':startIndex,
            onClick : function(t,index){                
                if(index==0) $('#po').val('p')
                if(index==1) $('#po').val('i')
                if(index==2) $('#po').val('f')
            }
        });

    }

    if($("#konfiguracjaFormTabs").length>0){
        
        var startIndex;
        
            startIndex=0;
        

        $("ul.tabs").tabs("div.panes > div",{
            'initialIndex':startIndex,
            onClick : function(t,index){
                if(index==0) $('#po').val('p')
                if(index==1) $('#po').val('i')
                if(index==2) $('#po').val('f')
            }
        });

    }

    
    if(('.w_wszystkie').length>0){
                
         $('.w_wszystkie').click(function(){
             
            var formId='#'+$(this).parents('form').attr('id');
            var isChecked=$(formId+' .w_wszystkie').is(':checked');
            $(formId+' .wojCheck').attr('checked',isChecked);
        })
        $('.wojCheck').click(function(){
            var formId='#'+$(this).parents('form').attr('id');
            $(formId+' .w_wszystkie').attr('checked',$(formId+' input.wojCheck:checked').length==$(formId+' input.wojCheck').length);
        })
         
    }
    if(('.w_wszystkie-i').length>0){

         $('.w_wszystkie-i').click(function(){
             
            var formId='#'+$(this).parents('form').attr('id');
            $(formId+' .wojCheck-i').attr('checked',$(formId+' .w_wszystkie-i').is(':checked'));
        })
        $('.wojCheck-i').click(function(){
            var formId='#'+$(this).parents('form').attr('id');
            $(formId+' .w_wszystkie-i').attr('checked',$(formId+' input.wojCheck-i:checked').length==$(formId+' input.wojCheck-i').length);
        })

    }
    if(('.w_wszystkie-n').length>0){

         $('.w_wszystkie-n').click(function(){
            var formId='#'+$(this).parents('form').attr('id');
            $(formId+' .wojCheck-n').attr('checked',$(formId+' .w_wszystkie-n').is(':checked'));
        })
        $('.wojCheck-n').click(function(){
            var formId='#'+$(this).parents('form').attr('id');
            $(formId+' .w_wszystkie-n').attr('checked',$(formId+' input.wojCheck-n:checked').length==$(formId+' input.wojCheck-n').length);
        })

    }

     
     
    function wojCheckError(){
        //sprawdzam, który to formularz
        var formId='#frmSearch-'+$('#po').val();
        //alert(formId);


        if($(formId+' .woj-warning').length>0) {
            if($(formId+' input.wojCheck:checked').length==0){
                $(formId+' .woj-warning').css('display','block')
            } else {
                $(' .woj-warning').css('display','none')
            }
            $(formId+' .w_wszystkie').attr('checked',$(formId+' input.wojCheck:checked').length==$(formId+' input.wojCheck').length);
        }
    }

    $('.hiliteswitch').css({
        'text-decoration':'line-through'
    })
    $('.hiliteswitch').click(function(){
        if($('span.hilite').length>0){
            $('span.hilite').removeClass().addClass('nohilite');
            $('.hiliteswitch').css('text-decoration','');
        } else if($('span.nohilite').length>0){
            $('span.nohilite').removeClass().addClass('hilite');
            $('.hiliteswitch').css('text-decoration','line-through');
        }
        return false;
    })
    
    /*firmy*/
    if($("#dialog-firma").length>0){
        $("#dialog-firma").dialog({
                autoOpen: false,
                top:20,
                width: 750,
                height: 650,
                
                modal: true,
                overlay: {
                    opacity: 0.8,
                    background: "black"
                },

                buttons: {
                        
                        
                        "Zamknij": function() {


                                
                                $( this ).dialog( "close" );

                                


                        }
                },
                close: function() {
                    //oczyszczenie pól


                }
        });
        $('.firma').click(function(){
            var firma_id=$(this).attr('rel');
            $.ajax({
            url : 'jquery/dane_firmy.php?firma_id='+firma_id,
            data : $('#f1').serialize(),
            type : 'POST',
            dataType : 'json',
            success : function(response) {
                    // Dla urozmaicenia:
                    if (response.result == 1) {

                        $("#dane_firmy").html(response.html)
                        $("#dialog-firma").dialog('open');

                    }
                }
            });
            
        })
    }
});

