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

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


///////////////////////////////////////////////////////////////////////////////////////////////////
function formChangeSet() 
{
	modalId = activeDialogs[activeDialogs.length - 1];
	formChanges[modalId] = true;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
/*function formChangeInit(formId) 
{
	alert(formId);
	for(i = 0; i < document.getElementById(formId).elements.length; i++)
	{
		if(document.getElementById(formId).elements[i].type == "input")
		{
			;
			if(!document.getElementById(formId).elements[i].onchange)
				document.getElementById(formId).elements[i].onchange = 'formChangeSet();';
			else
				document.getElementById(formId).elements[i].onchange += 'formChangeSet();';
		}
	}
}*/

///////////////////////////////////////////////////////////////////////////////////////////////////
/*function formChangeCheck(formId) 
{
	if(formChanges[modalId])
	{
		alert('eee');
		return false;
	} 
	else
		return true;
}*/


///////////////////////////////////////////////////////////////////////////////////////////////////
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);
	
	formChanges[modalId] = false;
	
	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)
	{
		if(formChanges[modalId])
		{
			if(!confirm('Údaje formuláře byly změněny. Chcete opravdu zavřít dialog? Změněné údaje nebudou uloženy!'))
				return false;
		}


		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);
						loadTinyMce(parent.document);
					
					} 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];
}


function loadTinyMce(obj)
{
	tinyMCE.init(
		{
		mode : "exact",
    	elements : "wyswyg",
		language : "cs",
		theme : "advanced",   
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,wordcount,advlist,autosave",
		theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,blockquote,iespell,advhr,|,ltr,rtl,|,image",
		theme_advanced_buttons2 : "bold,italic,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,formatselect,forecolor,sub,sup,|,charmap",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		fix_table_elements : true,
		theme_advanced_resizing : true,
		theme_advanced_more_colors : false,
		init_instance_callback : "modProgressHide"
		});
}
