/* 
 * JK
 * Funkcje JS niektórych akcji na stronie.
 * - pole wyszukiwania
 * - zwijanie metryki
 * - zmiana rozmiaru czcionki
 * Niektóre zupełnie nowe, inne zastępujące poprzednie rozwiązania.
 *
 * Jeśli obrazek w treści ma rozmiar przekraczajacy rozmiar pola dla dokumentu,
 * wtedy po najechaniu myszką na taki dokument otwórz go w nowym oknie, lub na wierzchu,
 * w nowym elemencie HTML
 *
 */

var max = 32;
var min = 8;
var step = 2;

function sizeUp() {
    $('div#docContent').each( function(){
        var size = parseFloat($(this).css('font-size'));
        if ((size + step) < max)
            $(this).css('font-size', size + step);
    });
}

function sizeDown() {
    $('div#docContent').each( function(){
        var size = parseFloat($(this).css('font-size'));
        if ((size - step) > min)
            $(this).css('font-size', size - step);
    });
}

function documentPopup(){
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();

    var width = windowWidth - 100;

    var left = (windowWidth - width) / 2;
    if (left < 0) left = 0;

    var height = windowHeight-100;

    var top = (windowHeight - height) / 2;
    if (top < 0) top = 0;

    var options = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes";
    var position= "width="+width+",height="+height+",left="+left+",top="+top;
    var myWindow = window.open('', "dokument", options+","+position);

    var stylePath = $('[rel="stylesheet"]').attr("href");

    var locationArray = document.location.href.split("/");
    var count = locationArray.length;
    var location = "";

    for (var i=1; i<count-1; i++){ /* pomiń pierwszy i ostatni */
        location += locationArray[i] + "/";
    }

    var style = '<head><link rel="stylesheet" href="'
        + "http:/" + location + stylePath + '" type="text/css"></head>';

    var contents = $('#docContent').parent().html();

    contents = '<body><div id="TRESC" style="width:100%;">'
        +'<div class="tresc2" style="width:100%;">'
        +'<div class="boxdivtresc" style="width:100%;">'
        +'<div id="docContent" style="width:100%;">'
        + contents
        + '</div></div></div></div></body>';
    
    myWindow.document.write(style + contents);
}

function clearField(el){
    if ($(el).val() == 'Szukaj...'){
        $(el).val('');
    }
}

function setField(el){
    if ($(el).val() == ''){
        $(el).val('Szukaj...');
    }
}

function printDoc(){
    NewWindow = window.open('','','width='+785+',height='+555+
                ',,scrollbars=yes,resizable=yes,fullscreen=no');
    NewWindow.document.open();

    var document = $('#docContent').html();
    var title    = $('.tytulTresc').html();
    var metryka  = $('div.metrykazmian').html();

    if (title == null) title = '';
    if (metryka == null) metryka = '';
    
    var html = '<!DOCTYPE html><head><meta charset="UTF-8">'+
    '<title>wydruk dokumentu</title></head><body>'+
    '<br><a href="javascript:window.close()" >Zamknij okno</a><br>'+
    '<div style="text-align:left"><h1>'+title+'</h1>'
    +document+'</div>'+'<div>'+metryka+'</div></body></html>';

    NewWindow.document.write(html);
    NewWindow.window.print();
    NewWindow.document.close();
}

function toogleMetric(){
    $('.metrykazmian_div1').slideToggle(200, resizeShadows);
}

function resizeShadows(){
    var height = $("#main").height();
    $('#shadowl').height(height);
    $('#shadowr').height(height);
}

function imagePreview(){
    var message = 'Kliknij aby zamknąć';

    $('#docContent img').each(function(){
        if ($(this).width() > 600){
            /* przeskaluj obrazek tak aby jego szerokośc mieściła się w polu dokumentu */
            var imgHeight = $(this).height();
            var imgWidth  = $(this).width();
            var imgRatio = imgHeight / imgWidth;

            imgWidth  = 580;
            imgHeight = imgRatio * imgWidth;

            $(this).height(imgHeight);
            $(this).width(imgWidth);
            
            /* po najechaniu myszką wyświetl zdjęcie nad stroną */
            $(this).click(function(){
                $('#preview').remove();
                var top = $(window).scrollTop();
                $("body").append("<p id='preview' style='top:" + top + "px'><img src='"
                    + ($(this).attr('src'))
                    + "' alt='"+message+"' title='"+message+"' />"+message+"</p>");
                $('#preview').show();

                $('#preview').click(function(){
                    $('#preview').hide();
                });
            });
        }
    });
}

function createUserMenuPermissionButtons(){
    if ($('#markall').length == 0) return;
    $('.collapse, .expand').each(function(){
        $(this).append('     <button type="button" class="nicebtn btnmark">&#x2713;</button>'
                    +'<button type="button" class="nicebtn btnunmark">&#x2610;</button>');
    });
}

function bindUserMenuMarkBtns(){
    if ($('#markall').length == 0) return;
    $('#markall').click(function(){
        $('.userScrollData [class^="syseditmenu"] [type="checkbox"]').attr('checked', 'checked');
    });
    $('#unmarkall').click(function(){
        $('.userScrollData [class^="syseditmenu"] [type="checkbox"]').removeAttr('checked');
    });

    $('.btnmark').click(function(e){
        e.stopPropagation();
        $(this).parent().next('ul').find('[type="checkbox"]').attr('checked', 'checked');
    });
    $('.btnunmark').click(function(e){
        e.stopPropagation();
        $(this).parent().next('ul').find('[type="checkbox"]').removeAttr('checked');
    });
}

function deleteQuestion(){
    $('input.deleteDoc').click(function(){
       return confirm("Usunąć dokument (operacji nie można cofnąć)?");
    });
    $('input.bt_19_delete').click(function(){
       return confirm("Na pewno usunąć (operacji nie można cofnąć)?");
    });
}

$(document).ready(function() {
    $('.sizeup').click(function(){
      sizeUp();
    });
    $('.sizedown').click(function(){
      sizeDown();
    });
    $('#documentPopup').click(function(){
        documentPopup();
    });
    $('#id_seek_sites1').focus( // pole wyszukiwania
        function(){clearField(this);}
    );
    $('#id_seek_sites1').blur( // pole wyszukiwania
        function(){setField(this);}
    );
    $('#id_adv_seek_sites1').focus( // pole zaawansowanego wyszukiwania
        function(){clearField(this);}
    );
    $('#id_adv_seek_sites1').blur( // pole zaawansowanego wyszukiwania
        function(){setField(this);}
    );
    $('.printme').click( // przycisk drukowania
        function(){printDoc();}
    );

    /* znajduje wszytskie linki w treści dokumentu
     * i dodaje im atrybut target="_blank"
     */
    $('div[id^="print"] a').each( function(){
        $(this).attr('target', '_blank');
        $(this).attr('type', 'application/force-download');
    });

    createUserMenuPermissionButtons();
    bindUserMenuMarkBtns();

    deleteQuestion();
    imagePreview();
    resizeShadows();
});
