﻿/// <reference path="jquery-1.3.2-vsdoc.js">

var _modalWins = new Array();

/**
* Closes most top modal win
**/
function closeModal() {
    var id = _modalWins.pop();
    var modal = document.getElementById("modalWin.transparentDiv" + id);
    modal.parentNode.removeChild(modal);

    var modalbg = document.getElementById("modalWin.background" + id);
    modalbg.parentNode.removeChild(modalbg);
}

/**
* Creates modal window and adds post response to content
* id - html element id
* content - content to display in modal win
* title - modal win title
**/
function openModal(id, content, title) {

    _modalWins.push(id);
    var div = $("<div />")
        .appendTo("form")
        .attr("id", "modalWin.background" + id)
        .addClass("modalWinBG")
        .addClass("fixedPosiotion")
        .css("z-index", 100 + _modalWins.length)
        .fadeTo(1, 0, function() {
            div.css("display", "block");
            div.fadeTo("fast", 0.3);
        });

    var transparentDiv = $("<div />")
        .attr("id", "modalWin.transparentDiv" + id)
        .addClass("modalWinTransparentLayer")
        .css("z-index", 100 + _modalWins.length + 1)
        .appendTo("body");

    var windowDiv = $("<div />")
        .attr("id", "modalWin.windowDiv" + id)
        .addClass("modalWin")
        .appendTo(transparentDiv);
    

    var table = $("<table />")
        .addClass("modalWin-mainTable")
        .attr("id", "modalWin.table" + id)
        .attr("cellspacing", 0)
        .attr("cellpadding", 0);

    var tbody = $("<tbody />").appendTo(table);
    var tr2 = $("<tr />").appendTo(tbody);

    if (title) {
    	var header = $("<tr />").appendTo(tbody);
    	// title
    	$("<td />")
			.addClass("modalWin-title")
			.html("<b>" + (title || "Untitled") + "</b>")
			.appendTo(header);

    	// close button
    	$("<td />")
        .addClass("modalWin-close")
			.html("X")
			.click(function() {
        		closeModal();
			})
			.appendTo(header);
    }
    var content = $("<td />")
        .addClass("modalWin-content")
        .attr("id", "modalWin.content" + id)
		.html(content)
        .appendTo(tr2);
    if (title)
    	content.attr("colspan", 2);

    table.appendTo(windowDiv);
    
    windowDiv.css("top", (($(window).height() / 2) - (table.height() / 2)) + "px")
        .css("left", (($(window).width() / 2) - (table.width() / 2)) + "px");
}