
function newin_openpic(url){
    $(document.body).append('<div class="lightbox" id="image_detail_lightbox"><div class="overlay" id="image_detail_overlay"></div>' +
    '<div class="box" id="image_detail_box">' +
    '<img id="image_detail_img"></img>' +
    '<div id="image_detail_close" style="text-align: center;" ><a href="#" style="color: white;">Zavřete kliknutím</a></div>' +
    '</div></div>');
    
    var image = $('#image_detail_img');
    var box = $('#image_detail_box');
    var overlay = $('#image_detail_overlay');
    var close = $('#image_detail_close');
    
    image.one('load', function(e){
        overlay.fadeTo(250, 0.8);
        image.fadeIn(250);
        box.fadeIn(250);
        
        image.removeAttr("width").removeAttr("height").css({
            width: "",
            height: ""
        });
        
        var iw = image.attr('width');
        var ih = image.attr('height');
        var winH = $(window).height();
        var winW = $(window).width();
        box.css('position', 'fixed');
        box.css('height', ih + 'px');
        box.css('width', iw + 'px');
        box.css('top', winH / 2 - box.height() / 2);
        box.css('left', winW / 2 - box.width() / 2);
        
    })
    
    image.one('click', function(e){
        newin_closepic('image_detail_img');
    })
    
    overlay.one('click', function(e){
        newin_closepic('image_detail_img');
    })
    
    close.one('click', function(e){
        newin_closepic('image_detail_img');
    })
    
    image.attr("src", url);
    return false;
}


function newin_closepic(id){
    var image = $('#image_detail_img');
    var box = $('#image_detail_box');
    var overlay = $('#image_detail_overlay');
    var close = $('#image_detail_close');
    box.fadeOut(250);
    overlay.fadeOut(250);
    image.fadeOut(250, function(){
        $('#image_detail_lightbox').remove();
    });
    return false;
}



function newin_open(id, url, params){

    newin_reload_needed = false;
    
    if (url) {
        newin_reload(id, url, params);
    }
    
    var box = '#' + id + '_box';
    box = $(box);
    
    var overlay = '#' + id + '_overlay';
    overlay = $(overlay);
    
    overlay.fadeTo(500, 0.8);
    
    
    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
    
    if (winH > 650) {
        winH = 650;
    }
    
    //Set the popup window to center
    //	box.css('position','fixed');
    box.css('height', winH - 200);
    box.css('width', 500);
    box.css('top', winH / 2 - box.height() / 2);
    box.css('left', winW / 2 - box.width() / 2);
    
    //transition effect
    box.fadeIn(500);
    return false;
}


function newin_submit(id, form, params){
    var form = '#' + form;
    form = $(form);
    var url = form.attr('action');
    var data = form.serializeArray();
    
    // add global and local data to request
    data[data.length] = params;
    data = $.param(data);
    
    var content = '#' + id + '_content';
    content = $(content);
    var html = '';
    html += '<div class="top"></div>';
    html += '<div class="in">';
    html += '<h2 class="main">Pracuji ...</h2>';
    html += '<div class="lightbox_loading"><img src="/nemyslis/images/lightbox/loading.gif" /></div>';
    html += '</div>';
    content.html(html);
    
    $.ajax({
        url: url,
        context: content,
        type: 'POST',
        data: data,
        success: function(data, textStatus, XMLHttpRequest){
            this.fadeOut(500, function(){
                $(this).html(data);
                $(this).fadeIn(500);
            });
        }
    });
    
    return false;
}

function newin_reload(id, url, params){
    var content = '#' + id + '_content';
    content = $(content);
    var html = '';
    html += '<div class="top"></div>';
    html += '<div class="in">';
    html += '<h2 class="main">Pracuji ...</h2>';
    html += '<div class="lightbox_loading"><img src="/nemyslis/images/lightbox/loading.gif" /></div>';
    html += '</div>';
    content.html(html);
    
    if (!params) 
        params = [];
    data = $.param(params);
    
    $.ajax({
        url: url,
        context: content,
        data: data,
        type: 'POST',
        success: function(data, textStatus, XMLHttpRequest){
            this.fadeOut(500, function(){
                $(this).html(data);
                $(this).fadeIn(500);
            });
        }
    });
    
    return false;
}

function newin_close(id, delay){
    if (!delay) {
        delay = 1;
    }
    
    setTimeout(function(){
        var box = '#' + id + '_box';
        box = $(box);
        var overlay = '#' + id + '_overlay';
        overlay = $(overlay);
        box.fadeOut(500, function(){
            if (newin_reload_needed) {
                window.location.reload();
            }
        });
        overlay.fadeOut(500);
    },delay)
    return false;
}
