/*
Created By: Inventic s. r. o. (Frantisek Troster)
Website: http://inventic.eu
Date: 10/5/2006

Here are generic functions for ivolution
*/

function openTab(tabName, tabular)
{
	var tabs = getElementsByClassName($('lightbox_content'), 'div', 'tab');
			
	for(i = 0; i < tabs.length; i++)
	{
		tabs[i].className = "tab inactive";
	}

	$(tabName).className = "tab active";
		
	var tabulars = tabular.parentNode.parentNode.getElementsByTagName("li");
	for(i = 0; i < tabulars.length; i++)
	{
		tabulars[i].firstChild.className = "inactive";
	}

	tabular.className = "active";
}

function submitForm(formId)
{
	if($(formId).onsubmit())
	{
		$(formId).submit();
	}
}

function refreshList()
{
	if($("content_list_form"))
	{
		if($("content_list_form").onsubmit())
		{
			$("content_list_form").submit();
		}
	}
}

function iconMenuDeactivate()
{
	var icons = getElementsByClassName($('lightbox'), 'a', 'button');
	
	for(i = 0; i < icons.length; i++)
	{
		icons[i].style.display = 'none';
	}
}

function showHelp(e)
{
	labelElement = Event.element(e);
	
	var helpContents = getElementsByClassName(labelElement, 'span', 'helpContent');
	
	if(helpContents.length > 0)	
	{
		var helpContentString = helpContents[0].innerHTML;
	
		if($("help_box") == null)
		{
			bod 				= document.getElementsByTagName("body")[0];
			hb					= document.createElement("div");
			hb.id				= "help_box";
			bod.appendChild(hb);
		}	
		
		if (document.layers) {
		    xMousePos = document.pageX;
		    yMousePos = document.pageY;
		    xMousePosMax = window.innerWidth+window.pageXOffset;
		    yMousePosMax = window.innerHeight+window.pageYOffset;
		} else if (document.all) {
		    xMousePos = window.event.x+document.body.scrollLeft;
		    yMousePos = window.event.y+document.body.scrollTop;
		    xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
		    yMousePosMax = document.body.clientHeight+document.body.scrollTop;
		} else if (document.getElementById) {
		    // Netscape 6 behaves the same as Netscape 4 in this regard 
		    xMousePos = document.pageX;
		    yMousePos = document.pageY;
		    xMousePosMax = window.innerWidth+window.pageXOffset;
		    yMousePosMax = window.innerHeight+window.pageYOffset;
		}
	
		$("help_box").innerHTML = helpContentString;
		$("help_box").style.top = yMousePos + "px";
		$("help_box").style.left = xMousePos + "px";
		$("help_box").style.display = "block";	
	}
}

function hideHelp()
{
	$("help_box").style.display = "none";		
}

function initHelps() {
	var divsWithHelp = getElementsByClassName($('lightbox'), 'div', 'help');
	
	for(i = 0; i < divsWithHelp.length; i++) {
		var labels = divsWithHelp[i].getElementsByTagName('label');
		var label = labels[0];
		if(label.getElementsByTagName('sup').length == 0) {
			label.onmouseover = function(e) {showHelp(e)};
			label.onmouseout = function() {hideHelp()};
			label.innerHTML += '<sup>?</sup>';
			
			var $helpContentElements = getElementsByClassName(divsWithHelp[i], "div", "sf_admin_edit_help");
			if($helpContentElements.length > 0)
			{
				label.innerHTML += '<span class="helpContent">' + $helpContentElements[0].innerHTML + '</span>';
			}
		}
	}
}

/*
 * TINYMCE
 */
var activatedAreas = new Array();	

function setTextareaToTinyMCE(sEditorID) {
	var oEditor = $(sEditorID);
	
	if(oEditor) 
	{
		if(activatedAreas[sEditorID] == true)
		{
			unsetTextareaToTinyMCE(sEditorID);
		}
//			alert("nastavuju " + sEditorID);
		try 
		{
			activatedAreas[sEditorID] = true;
			tinyMCE.execCommand("mceAddControl", false, sEditorID);					
		}
		catch(e)
		{
			alert("Chyba pri aktivaci prvku " + sEditorID + "\n" + e);
		}		
	}
}

function unsetTextareaToTinyMCE(sEditorID) {
	var oEditor = document.getElementById(sEditorID);
	if(oEditor && (activatedAreas[sEditorID] == true)) 
	{
//				alert("odnastavuju " + sEditorID);
		activatedAreas[sEditorID] = false;
		try 
		{
			tinyMCE.execCommand("mceRemoveControl", false, sEditorID);
		}
		catch(e)
		{
			alert("Chyba pri deaktivaci prvku " + sEditorID + "\n" + e);
		}
	}
}

function tinymceDeactivate() {
	try {
		tinyMCE.triggerSave(true,true);			
	}
	catch(e)
	{
		alert("Chyba pri ukladani formulare\n" + e);
	}

	for(var i in activatedAreas)
	{
		if(activatedAreas[i] == true) 
		{
//					alert("jdu na " + i);
			unsetTextareaToTinyMCE(i);
		}
	}
//			alert("vse ok");
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function returnImageUrl(src, alt)
{
	top.window.srcField.value = src;
	top.window.altField.value = alt;
	top.window.close();
}