﻿var currentNavLeft;
var curModal;
function openModal(m) {
    if (curModal != null) {
        closeModal(function() {
             doOpenModal(m)
         }, true);
    } else {
     
         doOpenModal(m)
     }
            
}

function doOpenModal(m) {
    curModal = m;
    $("#modalBackground").css("display","block");
    $("#modalBackground").animate({ opacity: 0.7 }, 200, "linear", function() {
        curModal.css("display", "block");


        var wW = $("#container").width()
        var mW = curModal.outerWidth()
        var wH = $(window).height()
        var mH = curModal.outerHeight()
        curModal.css("left", (wW - mW) / 2);
        curModal.css("maxHeight", wH - 60);
        mH = curModal.outerHeight()
        curModal.css("top", Math.max(10, (wH - mH) / 2));
        curModal.animate({ opacity: 1 }, 200, "linear", function() {
            curModal.css("opacity","");
        });
    });
}
function closeModal(f,leaveBack) {
    curModal.animate({ opacity: 0 }, 300, "linear", function() {
        curModal.css("display", "none");
        curModal = null;
        if (leaveBack) {
            if (f!=null)
                f();
        } else {
            $("#modalBackground").animate({ opacity: 0.0 }, 300, "linear", function() {
                $("#modalBackground").css("display", "none");

                if (f != null)
                    f();
            });
        }
    });
}
$(document).ready(function() {

    $("#navLeft > div > ul > li:has(ul) > a").click(function() {
        if (currentNavLeft != null) {
            $(currentNavLeft).next().slideUp(200);
        }
        $(this).next().slideDown(500);
        currentNavLeft = $(this);

        return false;
    });
    var str = window.location + "";
    str = str.substr(str.lastIndexOf("/") + 1)
    if (str.length == 0) {
        $("#navLeft > div > ul > li:has(ul) > a:first").each(function() {

            $(this).next().show()
            currentNavLeft = $(this);
        });
    } else {
        $("#navLeft > div > ul > li:has(ul) > a").each(function() {

            if ($(this).parent().find("a").is("a[href$='" + str + "']")) {
                //alert($(this).next().html())//.slideDown(5);
                $(this).next().show()
                currentNavLeft = $(this);
            }
        });
    }
    $("a[href^='#Modal']").click(function() {
        if ($("#modalBackground").css("position") == "static")
            return true;
        openModal($($(this).attr("href")));
        return false;
    });
    $("#onlineDemo a:first").click(function() {

    });
    $(".modalBox").css("opacity", 0);
    $("#modalBackground").css("opacity", 0);
    $(".modalBox a.closeModal").click(function() { closeModal() });
    $(".modalBox input.closeModal").click(function() { closeModal() });
    $(window).resize(function() {

        if (curModal == null)
            return;
        var wW = $("#container").width()
        var mW = curModal.outerWidth()
        var wH = $(window).height()
        var mH = curModal.outerHeight()
        curModal.css("left", (wW - mW) / 2);
        curModal.css("maxHeight", wH - 60);
        mH = curModal.outerHeight()
        curModal.css("top", Math.max(10, (wH - mH) / 2));
    });
    $(".modalBox").scroll(function() {
    });
});