var numberModalWindowOpen = 0;

function openModalWindow_sk(myButtons){
    numberModalWindowOpen = numberModalWindowOpen + 1;

    // We must hide all possible video OBJECTS so they will not show over the window
    showHideVideoForModalWindow('hide');

    if(myButtons == '') myButtons = '<input type="button" value="Close" onclick="closeModalWindow_sk();" />';
    else if(myButtons == 'none') myButtons = '&nbsp;';
    
    var el = '';
    var mySize = getWindowSize();

    // Create the window with NTH = numberModalWindowOpen
    var newInput = document.createElement('div');
    newInput.setAttribute('id', 'modalWindowPosition_' + numberModalWindowOpen);
    document.getElementById('putModalWindowsInHere').appendChild(newInput);
    // Give this window its STYLES
    el = document.getElementById('modalWindowPosition_' + numberModalWindowOpen);
    el.style.position = 'absolute';
    el.style.zIndex = '350' + numberModalWindowOpen;
    el.style.width = '775px';
    el.style.height = '465px';
    el.style.border = '6px groove gray';
    el.style.background = 'silver';
    el.style.marginTop = Math.floor((parseInt(mySize.height) - parseInt(el.style.height.replace('px', ''))) / 2) + 'px';
    el.style.marginLeft = Math.floor((parseInt(mySize.width) - parseInt(el.style.width.replace('px', ''))) / 2) + 'px';
    
    // Create CONTENT ZONE DIV WRAPPER
    newInput = document.createElement('div');
    newInput.setAttribute('id', 'modalWindowContentWrapper_' + numberModalWindowOpen);
    document.getElementById('modalWindowPosition_' + numberModalWindowOpen).appendChild(newInput);
    // Give this window its STYLES
    el = document.getElementById('modalWindowContentWrapper_' + numberModalWindowOpen);
    el.style.width = '100%';
    el.style.height = '90%';
    el.style.overflow = 'auto';
    
    // Create the CONTENT DIV
    newInput = document.createElement('div');
    newInput.setAttribute('id', 'modalWindowContent_' + numberModalWindowOpen);
    document.getElementById('modalWindowContentWrapper_' + numberModalWindowOpen).appendChild(newInput);
    // Give this window its STYLES
    el = document.getElementById('modalWindowContent_' + numberModalWindowOpen);
    el.innerHTML = '<img src="images/ajax-loader.gif" alt="Loading Icon" />';
    
    // Create the BUTTON DIV
    newInput = document.createElement('div');
    newInput.setAttribute('id', 'modalWindowButtons_' + numberModalWindowOpen);
    document.getElementById('modalWindowPosition_' + numberModalWindowOpen).appendChild(newInput);
    // Give this window its STYLES
    el = document.getElementById('modalWindowButtons_' + numberModalWindowOpen);
    el.style.marginTop = '15px';
    el.style.textAlign = 'center';
    el.innerHTML = myButtons;
    
    // If this is our first Modal Window, it is probably hidden. We SHOW it
    el = document.getElementById('modalWindowShadow');
    if(el.style.display == 'none'){
        el.style.display = '';
        document.getElementById('modalWindowShadow').style.display = '';
        document.getElementById('putModalWindowsInHere').style.display = '';
    }
}

function closeModalWindow_sk(){
    var wrapper = document.getElementById('putModalWindowsInHere');
    var divToDelete = document.getElementById('modalWindowPosition_' + numberModalWindowOpen);
    wrapper.removeChild(divToDelete);
    
    numberModalWindowOpen = numberModalWindowOpen - 1;
    
    if(numberModalWindowOpen == 0){
        document.getElementById('modalWindowShadow').style.display = 'none';
        document.getElementById('putModalWindowsInHere').style.display = 'none';
    }
    
    if(numberModalWindowOpen < 1) showHideVideoForModalWindow('show');
}


function showHideVideoForModalWindow(myAction){
    var children, myName;
    // We must hide all possible video OBJECTS so they will not show over the window
    // all items should be named :: name="video_to_hide_when_modal_window"

    children = document.body.getElementsByTagName('*');
    
    // Get the list of our items
    for(var i=0; i<children.length; i++){
        myName = children[i].getAttribute("name");
        if(myName != null && myName == 'video_to_hide_when_modal_window'){
            if(myAction == "show") children[i].style.display = '';
            else children[i].style.display = 'none';
        }
    }    
}
