
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// POUPUP DIALOG
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

var activeElement;
var returnValue;
var activeDialogs = new Array();


function modPopupDialogKeyPressEvent(e) 
{
	var kCode  = (window.event) ? event.keyCode : e.keyCode; // MSIE or Firefox?
	var cEsc = (window.event) ? 27 : e.DOM_VK_ESCAPE // MSIE : Firefox
	
	if(kCode == cEsc) modPopupDialogHide();
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Zobrazeni stranky jako modalniho dialogu
// modalId = zadejte libovolne jedinecne ID pro odliseni dialogu
// pageName = cesta k nacite strance
// titleText = titulek dialogu
function modPopupDialogShow(modalId, pageName, titleText, _width, _height, _top, _left)
{
	if(_width == null) _width = 700;
	
	scroll(0,0);
	
	if(!document.getElementById('modalDialog_' + modalId))
	{
		// Konstruktor dialogu
		var elMDB = document.createElement('div');
		elMDB.className = 'modalBackground';
		elMDB.id = 'modalBackground_' + modalId;
		elMDB.style.zIndex = 100 + activeDialogs.length * 4;
		elMDB.onclick = function(){ modPopupDialogHide(modalId) };
		
		var elMD = document.createElement('div');
		elMD.className = 'modalDialog';
		elMD.id = 'modalDialog_' + modalId;
		elMD.style.zIndex = 101 + activeDialogs.length * 4;
		
		var elMDT = document.createElement('div');
		elMDT.className = 'modalDialogTitle';
		elMDT.id = 'modalDialogTitle_' + modalId;
		//elMDT.onclick =  function(){ alert('a'); }
		
		var elMDC = document.createElement('div');
		elMDC.className  = 'modalDialogClose';
		elMDC.title = 'Zavřít okno';
		elMDC.onclick = function(){ modPopupDialogHide(modalId) };
		
		var elMDCT = document.createElement('div');
		elMDCT.className = 'modalDialogContent';
		elMDCT.id = 'modalDialogContent_' + modalId;
		elMDCT.innerHTML = '<div style="padding:20px; text-align:center;">Čekejte prosím, načítám dialog...</div><div class="content" align="center"><img src="./script/progress/progress1.gif" border="0" alt="" /></div>';
		
		elMD.appendChild(elMDT);
		elMD.appendChild(elMDC);
		elMD.appendChild(elMDCT);
		
		document.getElementById('modalDialogs').appendChild(elMDB);
		document.getElementById('modalDialogs').appendChild(elMD);
		
		if(modalId) activeDialogs.push(modalId);
		
		var dialog = elMD;
		var windowSize;
		
	}
	
	modPopupDialogLoadContent('modalDialogContent_' + modalId, /*'./' + */pageName);
	
	if(dialog)
	{
		dialog.style.width = _width + 'px';
		_height = dialog.style.innerHeight;
	}
	
	if(window.innerWidth != undefined)
		windowSize = [window.innerWidth, window.innerHeight];
	else{
		var D = document.documentElement;
		if(D) windowSize = [D.clientWidth,D.clientHeight];
	}
	
	if(document.viewport)
	{
		var scrollOffsets = document.viewport.getScrollOffsets();

		if(_top == null) _top = scrollOffsets[1] + 70;
		if(_left == null) _left = (windowSize[0] / 2) - (_width / 2);
	} else {
		if(_top == null) _top = 70;
		if(_left == null) _left = (windowSize[0]/2) - (_width / 2);
	}
	
	
	
	if(dialog)
	{
		dialog.style.top = _top + 'px';
		dialog.style.left = _left + 'px';
		dialog.style.visibility = 'visible';
		dialog.style.display = 'block';
		elMDB.style.display = 'block';
	}
	
	if(titleText) 
		document.getElementById('modalDialogTitle_' + modalId).innerHTML = modPopupDialogStrReplace('+', ' ', decodeURI(titleText));
	
	return false;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Zobrazeni stranky jako modalniho dialogu
function modPopupDialogReload(pageName)
{
	var modalId = activeDialogs[activeDialogs.length - 1];
	var dialog = document.getElementById('modalDialog_' + modalId);
	var windowSize;
	
	modPopupDialogLoadContent('modalDialogContent_' + modalId, /*'./' +*/ pageName);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Skryti modalniho dialogu
function modPopupDialogHide(modalId)
{
	if(!modalId)
	{
		modalId = activeDialogs[activeDialogs.length - 1];
		activeDialogs.splice(activeDialogs.length - 1, 1);
	}
	
	if(modalId)
	{
		document.getElementById('modalDialogs').removeChild(document.getElementById('modalDialog_' + modalId));
		document.getElementById('modalDialogs').removeChild(document.getElementById('modalBackground_' + modalId));
	}
	
	if(document.getElementById('calendar')) document.getElementById('calendar').style.display = 'none'; // skryti kalendare
}


///////////////////////////////////////////////////////////////////////////////////////////////////
function modPopupDialogSetTitle(modalId, titleText, appendTitle)
{
	if(appendTitle)
		document.getElementById('modalDialogTitle_' + modalId).innerHTML = document.getElementById('modalDialogTitle_' + modalId).innerHTML + modPopupDialogStrReplace('+', ' ', decodeURI(titleText));
	else
		document.getElementById('modalDialogTitle_' + modalId).innerHTML = modPopupDialogStrReplace('+', ' ', decodeURI(titleText));
}


////////////////////////////////////////////////////////////////////////////////////////////
// Ajax: nacteni obsahu do DIVu
function modPopupDialogLoadContent(divId, sourceFile)
{
	var httpRequest;
	
	if(!document.getElementById(divId)) return;
	
	if (sourceFile != 0)
	{
		if (window.ActiveXObject)
		{
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpRequest = new XMLHttpRequest();
		}
		httpRequest.open("GET", sourceFile, true);
		httpRequest.onreadystatechange = function () 
			{ 
				if (httpRequest.readyState == 4)
				{
					if(httpRequest.status == 200)
					{
						var obj = document.getElementById(divId);
						obj.innerHTML = modPopupDialogParseScript(httpRequest.responseText);							
					} else {
						var obj = document.getElementById(divId);
						obj.innerHTML = "<h2>Chyba při načítání stránky</h2>" + httpRequest.status + ": " + httpRequest.statusText;
					}
				}
			};

		httpRequest.send(null);
	} else {
		document.getElementById(divId).innerHTML = "";
	}
}


////////////////////////////////////////////////////////////////////////////////////////////
// Ajax: prelozeni scriptu v obsahu
function modPopupDialogParseScript(_source)
{
		var source = _source;
		var scripts = new Array();
 
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
 
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
 
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		// Return the cleaned source
		return source;
}


function modPopupDialogStrReplace(search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
