/** NHmodal - modal window for web applications
    author: NH-DESIGN, s.r.o.
    web:    http://www.nh-design.sk
    e-mail: info@nh-design.sk
    ver.:   0.1
**/

var nhmodal_visible = false;

function nhmodal(url,modal) {
    
    if(nhmodal_visible == true)
        return;
            
    var modal_markup = '<div id="nhmodal-container">';
    modal_markup+= '<div class="nhmodal-top-border"><div class="top-left-corner"></div><div class="top-line"></div><div class="top-right-corner"></div></div>';
    modal_markup+= '<div id="nhmodal-content-container"><div id="nhmodal-content"></div></div>';
    modal_markup+= '<div class="nhmodal-bottom-border"><div class="bottom-left-corner"></div><div class="bottom-line"></div><div class="bottom-right-corner"></div></div>';
    modal_markup+= '</div>';
    $("body").append(modal_markup);
    nhmodal_visible = true;    
    nhmodal_center();
    $(window).bind("resize", nhmodal_center);
    
    $.post(url, function(data){$("#nhmodal-content").css("background", "white"); $("#nhmodal-content").html(data); nhmodal_center();});
    
    return false;
}

function nhmodal_center() {
    var viewportwidth;
    var viewportheight;
     
    if (typeof window.innerWidth != 'undefined') {
          viewportwidth = window.innerWidth,
          viewportheight = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0) 
    {
           viewportwidth = document.documentElement.clientWidth,
           viewportheight = document.documentElement.clientHeight
    }
    else {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    
    if($("#nhmodal-container").width() > 500) {
        $("#nhmodal-container").width('500px');
    }
    
    if($("#nhmodal-container").height() > 500) {
        $("#nhmodal-container").height('500px');
    }
    
    $(".nhmodal-top-border .top-line").width(window.parseInt($("#nhmodal-container").width()-12)+"px");
    $(".nhmodal-bottom-border .bottom-line").width(window.parseInt($("#nhmodal-container").width()-12)+"px");
    
    $("#nhmodal-container").css("top", window.parseInt(viewportheight/3 - $("#nhmodal-container").height()/2)+"px");
    $("#nhmodal-container").css("left", window.parseInt(viewportwidth/2 - $("#nhmodal-container").width()/2)+"px");
}

function nhmodal_close() {
    $("#nhmodal-container").fadeOut("fast", function() {
       $("#nhmodal-container").remove();
       nhmodal_visible = false; 
    });
}
