var ok = (parseInt(navigator.appVersion) > 3);

if (ok) {
	var mainMenu = new Array();
	for( var i=0; i<5; i++){
		mainMenu[i] = new Image(); mainMenu[i].src="/admin/img/menu/main"+i+"_on.gif";
	}		

}

function mozillaRedirect(url) {
	window.location=url;
}


function refreshForm(formName, action) {
	//alert('refreshForm('+formName+', '+action+')');
	formObj = new ObjectRef(formName).elementRef;
	formObj.action=action;
	//editor.prepareSubmit(formObj);
	formObj.submit();
}

function rollCell(rollimage, cellId) {
        if (ok) {
                eval('document.getElementById("' + cellId + '").style.backgroundImage = "url(' + rollimage + ')" ');
        }
}

function roll2(imgName, rollimage, folder) {
	if (ok) {
		eval('document.images["' + imgName + '"].src="/admin/img/ref/' + folder + '/' + rollimage + '.gif"');
	}
}

function checkme(searchstring) {
	if (!searchstring) {alert('Zadejte text, kter??? chcete naj???t!'); return false}
	else {return true};
}

function showme(what) {
        if (ok) {
                eval('document.getElementById("' + what + '").style.display="inline"');
        }
}

function hideme(what) {
        if (ok) {
                eval('document.getElementById("' + what + '").style.display="none"');
        }
}

function showmeblock(what) {
        if (ok) {
                eval('document.getElementById("' + what + '").style.display="block"');
        }
}

function disableme(what) {
        if (ok) {
                eval('document.getElementById("' + what + '").disabled="disabled"');
                eval('document.getElementById("' + what + '").style.backgroundColor="InactiveCaptionText"');
        }
}

function undisableme(what) {
        if (ok) {
                eval('document.getElementById("' + what + '").disabled=""');
                eval('document.getElementById("' + what + '").style.backgroundColor="#ffffff"');
        }
}

/** JAVASCRIPTOVE MENU **/
 var bName = navigator.appName;
 var bVer = parseInt(navigator.appVersion);
 var NS4 = (bName == "Netscape" && bVer >= 4);
 var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);
 var NS3 = (bName == "Netscape" && bVer < 4);
 var IE3 = (bName == "Microsoft Internet Explorer" && bVer < 4);
 var bVer7 = navigator.appVersion;
 var IE7 = (IE4 && (bVer7.substr(bVer7.indexOf('MSIE') +5, 1) == '7'));

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else {
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
    } else {
      if( document.documentElement &&
          ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      	//IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
      }
    }
  }
  return [ scrOfX, scrOfY ];
}

/* debug information  */

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosX(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curtop += obj.x;
	return curtop;
}

function getImagePageLeft(img) {
  var x, obj;
  if (NS4) {
    if (img.container != null)
      return img.container.pageX + img.x;
    else
      return img.x;
  }
  if (IE4) {
    x = 0;
    obj = img;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    return x;
  }
  return -1;
}

function startUp(){
}

//Pop-it menu- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

var linkset=new Array()
//SPECIFY MENU SETS AND THEIR LINKS. FOLLOW SYNTAX LAID OUT
linkset[0]='<div class="menuitems"><a href="#">Smazat</a></div>'
linkset[0]+='<div class="menuitems"><a href="#">Schvalit</a></div>'
linkset[0]+='<div class="menuitems"><a href="#">Presunout do</a></div>'

////No need to edit beyond here
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all
var ns4=document.layers
if (navigator.appName.substring(0,1) == "M") {
 var ie=document.all;
}

function showmenu(e,which){
	//TODO
	//zatim provizorne zakomentone nefunkcni menu
	return;

	if (!document.all&&!document.getElementById&&!document.layers)
		return

	clearhidemenu()

	menuobj=ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
	menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj

	if (ie4||ns6) {
		menuobj.innerHTML=which
	} else {
		menuobj.document.write('<layer name=gui class="provestAkci" width=165 onmouseover="clearhidemenu()" onmouseout="hidemenu()">'+which+'</layer>')
		menuobj.document.close()
	}

	menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
	menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height
//	eventX=ie4? event.clientX : ns6? e.clientX : e.x
//	eventY=ie4? event.clientY : ns6? e.clientY : e.y
//
//	//Find out how close the mouse is to the corner of the window
//	var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
//	var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY
//
//	// sirka od leveho okraje je 392 pixelu
//	//if the horizontal distance isn't enough to accomodate the width of the context menu
//	if (rightedge<menuobj.contentwidth) {
//		//move the horizontal position of the menu to the left by it's width
//		menuobj.thestyle.left=392;
//		//ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth
//	} else {
//		//position the horizontal position of the menu where the mouse was clicked
//		menuobj.thestyle.left=392;
//		//ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX
//	}
//
//	//same concept with the vertical position
//	imgProvest = document.images['foot_provest'];
//	if (bottomedge<menuobj.contentheight) {
//		menuobj.thestyle.top= ie? findPosY(imgProvest)-62 : findPosY(imgProvest)-47;
//		//ie4? document.body.scrollTop+eventY-menuobj.contentheight : ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight
//	} else {
//		menuobj.thestyle.top= ie? findPosY(imgProvest)-62 : findPosY(imgProvest)-47;
//	}

	//imgProvest = document.images['foot_provest'];
//	menuobj.thestyle.top= findPosY(imgProvest) - menuobj.contentheight;
	//menuobj.thestyle.left= findPosX(imgProvest);

	menuobj.thestyle.top= "10";
	menuobj.thestyle.left= "10";

	//menuobj.thestyle.top= ie? 399: 364;
	// ie? document.body.clientHeight-100 : document.body.clientHeight-200
	//ie4? document.body.scrollTop+event.clientY : ns6? window.pageYOffset+eventY : eventY
	menuobj.thestyle.visibility="visible"
	return false
}

function hidemenu(){
	if (window.menuobj) {
		menuobj.thestyle.visibility=(ie4||ns6)? "hidden" : "hide"
	}
}

function clearhidemenu(){
	if (window.delayhide)
		clearTimeout(delayhide)
}

function delayhidemenu(){
	if (ie4||ns6||ns4) 
		delayhide=setTimeout("hidemenu()",500)
}


function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function dynamichide(e){
if (ie4&&!menuobj.contains(e.toElement))
hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
hidemenu()
}


function highlightmenu(e,state){
if (document.all)
source_el=event.srcElement
else if (document.getElementById)
source_el=e.target
if (source_el.className=="menuitems"){
source_el.id=(state=="on")? "mouseoverstyle" : ""
}
else{
while(source_el.id!="popmenu"){
source_el=document.getElementById? source_el.parentNode : source_el.parentElement
if (source_el.className=="menuitems"){
source_el.id=(state=="on")? "mouseoverstyle" : ""
}
}
}
}

function prepareDropElement(toDrop) {
	toDrop.style.display="none";
	var inputElems = toDrop.getElementsByTagName("input");
	for (i=0; i<inputElems.length; i++) {
		if (inputElems[i].name=="operation") {
			inputElems[i].value="remove";
		}
	}
}

function prepareDropImage(toDrop) {
	toDrop.style.display="none";
	var inputElems = toDrop.getElementsByTagName("input");
	for (i=0; i<inputElems.length; i++) {
		if (inputElems[i].name=="operation" && inputElems[i].value=="update") {
			inputElems[i].value="remove";
		}
	}
}


function removeElement(param) {
	var elem;
	if (typeof param == "string") {
		elem = document.getElementById(param);
	} else {
		elem = param;
	}
	elem.parentNode.removeChild(elem);
	return true;
}

function moveElementUp(param) {
	var elem;
	if (typeof param == "string") {
		elem = document.getElementById(param);
	} else {
		elem = param;
	}
	var parent = elem.parentNode;
	var previous = elem.previousSibling;
	if( isNonmovable(previous)) {
		alert('This is first element');
	} else {
		//alert('inserting before' + previous.innerHTML);
		parent.removeChild(elem);
		parent.insertBefore(elem, previous);
	}
	return true;
}

function moveElementDown(param) {
	var elem;
	if (typeof param == "string") {
		elem = document.getElementById(param);
	} else {
		elem = param;
	}
	var parent = elem.parentNode;
	var next = elem.nextSibling;
	if( isNonmovable(next)) {
		alert('This is last element');
	} else {
		parent.removeChild(elem);
		if( next.nextSibling != null ){
			parent.insertBefore(elem, next.nextSibling);
		} else {
			parent.appendChild(elem)
		}
	}
	return true;
}

function isNonmovable(elem){
	if( elem == null ){
		return true;
	}
	var classAttribute = elem.attributes.getNamedItem('class');
	if( classAttribute!= null && classAttribute.value == 'nonmovableElement'){
		return true;
	}
	var idAttribute = elem.attributes.getNamedItem('id');
	if( idAttribute != null && (idAttribute.value == 'multiformRow' || idAttribute.value == 'multiformItemRow' || idAttribute.value == 'multiformCategoryRow' )) {
		return true;
	}
	return false;
}

/*
function updateArticleTypeDefinitionRow(elem, value) {
		var categoryElementClasses = ['articleType-namespace','category-empty'];
		var itemElementClasses=['articleType-group','articleType-defaultValue','articleType-prefixSeparator']

		var showClasses;
		var hideClasses;
		if( value == 'category' ) {
			showClasses = categoryElementClasses;
			hideClasses = itemElementClasses;
		} else {
			showClasses = itemElementClasses;
			hideClasses = categoryElementClasses;
		}
		
		for(i=0; i<elem.childNodes.length; i++) {
			var td = elem.childNodes[i];
			if( td.attributes != null ){
				var tdClass = td.getAttribute('class');
				if( tdClass != null ){
//					alert('autobus'+elem+' value='+value + '   ' + td +'  ' +tdClass);
					if( hideClasses.contains(tdClass) ) {
						td.style.display='none';
					}
					if( showClasses.contains(tdClass) ) {
						td.style.display='';
					}
				}
			}
		}
}

//lepsi nez todle by byla Mapa, kterou se mi v Javascriptu nepodarilo najit a stejne jsou to pole zanedbatelne delky, tak je to jedno
Array.prototype.contains = function(value){
	for(l=0; l<this.length; l++){
		if( this[l] == value ) {
			return true;
		}
	}
	return false;
}
*/

function articleFooterAction(action) {
	var form = document.getElementById('ArticleMultipleForm');
	form.action.value=action;
	form.submit();
}

function questionaryFooterAction(action) {
	var form = document.getElementById('QuestionaryMultipleForm');
	form.action.value=action;
	form.submit();
}

function footerMultipleAction(action, formType) {
	var form = document.getElementById('' + formType + 'MultipleForm');
	form.action.value=action;
	form.submit();
}

function resetBannerFile(displayNodeId, hideNodeId) {
	var displayNode = document.getElementById(displayNodeId);
	displayNode.style.display='';
	
	var hideNode = document.getElementById(hideNodeId);
	hideNode.style.display='none';
	
	var form = document.getElementById('BannerCreateForm');
	if (typeof form.imageId != "undefined") {
		form.imageId.value ='';
	}  else {
		form.downloadId.value ='';
	}
}

function resetInquiryFile(displayNodeId, hideNodeId) {
	var displayNode = document.getElementById(displayNodeId);
	displayNode.style.display='';
	
	var hideNode = document.getElementById(hideNodeId);
	hideNode.style.display='none';
	
	var form = document.getElementById('InquiryCreateForm');
	form.imageId.value ='';
}

function resetFile(displayNodeId, hideNodeId, formId) {
	var displayNode = document.getElementById(displayNodeId);
	displayNode.style.display='';
	
	var hideNode = document.getElementById(hideNodeId);
	hideNode.style.display='none';
	
	var form = document.getElementById(formId);
	form.imageId.value ='';
}

function myCustomCleanup(type, value) {

	switch (type) {
		case "get_from_editor":
		case "insert_to_editor":
			// target="_blank" -> onclick="return !window.open(this.href, '_blank');"
			value = value.replace(/target="_?blank"/gi, 'onclick="return !window.open(this.href, \'_blank\');"');
			// odstraneni nevalididty - tabulka uvnitr odstavce
			value = value.replace(/<p>\s*<table/gi, '<table');
			value = value.replace(/<\/table>\s*<\/p>/gi, '</table>');
			break;
		case "submit_content":
			break;
		case "get_from_editor_dom":
		case "insert_to_editor_dom":
	
			var dom = tinyMCE.activeEditor.dom;
			
			// align -> text-align pro <p>, <div>
			var elemsString = 'p,div';
			var s = [];
			for (var j=0, tagNames = elemsString.split(',');  j < tagNames.length;  j++)
				for (var i=0, pom = value.getElementsByTagName(tagNames[j]);  i < pom.length;  i++)
					s.push(pom[i]);
			for (var i=0; i < s.length; i++) {
				var align = dom.getAttrib(s[i], 'align');
				if (align) {
					s[i].style.textAlign = align;
					s[i].removeAttribute('align');
				}
			}
			
			// align -> text-align pro <table>
			s = value.getElementsByTagName('table');
			for (var i=0; i<s.length; i++) {
				var align = dom.getAttrib(s[i], 'align');
				if (align) {
					s[i].removeAttribute('align');
					switch (align) {
						case 'left':
							s[i].style.marginLeft = "";
							s[i].style.marginRight = "";
							break;
						case 'right':
							s[i].style.marginLeft = "auto";
							s[i].style.marginRight = "10px";
							break;
						case 'justify':
						case 'center':
							s[i].style.marginLeft = "auto";
							s[i].style.marginRight = "auto";
							break;
					}
	
					// Add text-align to parent element
					var parentEl = dom.getParent(s[i], dom.isBlock);
					if (parentEl)
						parentEl.style.textAlign = align;
				}
			}
			
			// kontrola class u nadpisu
			elemsString = 'h1,h2,h3,h4,h5,h6';
			s = [];
			for (var j=0, tagNames = elemsString.split(',');  j < tagNames.length;  j++)
				for (var i=0, pom = value.getElementsByTagName(tagNames[j]);  i < pom.length;  i++)
					s.push(pom[i]);
			for (var i=0; i < s.length; i++) {
				var actualClass = dom.getAttrib(s[i], 'class');
				var correctClass = 'we' + s[i].tagName.toUpperCase();
				if (actualClass != correctClass) {
					var num = parseInt(s[i].tagName.substr(1));
					if (num == 6) {
						var p = dom.create('p');
						p.style.fontWeight = "bold";
						dom.replace(p, s[i], true);
					} else {
						num = num +1;
						var h = dom.create('h' + num);
						h.className = 'weH' + num;
						dom.replace(h, s[i], true);
					}
				}
			}
	
			// kontrola atributu obrazku
			s = value.getElementsByTagName('img');
			for (var i=0; i < s.length; i++) {
				var actualAlign = dom.getAttrib(s[i], 'align');
				var actualVspace = dom.getAttrib(s[i], 'vspace');
				var actualHspace = dom.getAttrib(s[i], 'hspace');
				var actualBorder = dom.getAttrib(s[i], 'border');
				s[i].removeAttribute('align');
				s[i].removeAttribute('vspace');
				s[i].removeAttribute('hspace');
				s[i].removeAttribute('border');
				
				if (actualAlign) {
					if (tinymce.isIE)
						s[i].style.styleFloat = actualAlign;
					else
						s[i].style.cssFloat = actualAlign;
				}
				if (actualVspace) {
					s[i].style.marginTop = actualVspace + 'px';
					s[i].style.marginBottom = actualVspace + 'px';
				}
				if (actualHspace) {
					s[i].style.marginLeft = actualHspace + 'px';
					s[i].style.marginRight = actualHspace + 'px';
				}
				if (actualBorder) {
					s[i].style.borderWidth = actualBorder + 'px';
					s[i].style.borderStyle = 'solid';
				}
			}
			
			// width, height -> style pro <table>
			elemsString = 'table,td,tr';
			s = [];
			for (var j=0, tagNames = elemsString.split(',');  j < tagNames.length;  j++)
				for (var i=0, pom = value.getElementsByTagName(tagNames[j]);  i < pom.length;  i++)
					s.push(pom[i]);
			for (var i=0; i < s.length; i++) {
				var width = dom.getAttrib(s[i], 'width');
				if (width) {
					s[i].removeAttribute('width');
					s[i].style.width = width.indexOf('%') != -1 ? width : width.replace(/[^0-9]/gi, '') + "px";
				}
				var height = dom.getAttrib(s[i], 'height');
				if (height) {
					s[i].removeAttribute('height');
					s[i].style.height = height.indexOf('%') != -1 ? height : height.replace(/[^0-9]/gi, '') + "px";
				}
			}
	
			// type -> list-style-type pro <ul>, <ol>
			elemsString = 'ul,ol';
			s = [];
			for (var j=0, tagNames = elemsString.split(',');  j < tagNames.length;  j++)
				for (var i=0, pom = value.getElementsByTagName(tagNames[j]);  i < pom.length;  i++)
					s.push(pom[i]);
			for (var i=0; i < s.length; i++) {
				var listType = s[i].getAttribute('type');
				var newListType;
				switch (listType) {
					case 'disc' :
					case 'circle' :
					case 'square' : newListType = listType; break;
					case '1' : newListType = 'decimal'; break;
					case 'A' : newListType = 'upper-alpha'; break;
					case 'a' : newListType = 'lower-alpha'; break;
					case 'I' : newListType = 'upper-roman'; break;
					case 'i' : newListType = 'lower-roman'; break;
					default : newListType = '';
				}
				if (listType) {
					s[i].style.listStyleType = newListType;
					s[i].removeAttribute('type');
				}
			}
	
			break;
		case "setup_content_dom":
			break;
		case "submit_content_dom":
			break;
	}

	return value;
}

function myCustomExecCommandHandler(editor_id, elm, command, user_interface, value) {
	switch (command) {
		case "mceSetStyleInfo":
			return true;
		case "mceSetCSSClass":
			var ed = tinyMCE.getInstanceById(editor_id);
			var dom = ed.dom;
			var e = dom.getParent(ed.selection.getNode(), dom.isBlock);
			
			if (value == '' && !e.nodeName.match(/^p$/i)){
				var p = dom.create('p');
				dom.replace(p, e, true);
				ed.nodeChanged();
				return true;
			} else if (value.match(/^weH/)) {
				var tagName = value.substr(2,2);
				var h = dom.create(tagName);
				h.className = value;
				dom.replace(h, e, true);
				ed.nodeChanged();
				return true;
			}
			return false;
	}

	return false; // Pass to next handler in chain
}


function setDescrArticle(formElem, langId, sectionId){
	var ed;
	if (tinyMCE.get('descrArticleTinyEditor')) {
		ed = tinyMCE.get('descrArticleTinyEditor');
	} else {
		ed = new tinymce.Editor("descrArticleTinyEditor");
		ed.render(1);
	}
	ed.windowManager.open({
		file : '/admin/prepareInsertDescrArticle.do?langId=' + langId + 
		      ((sectionId == "") ? "" : "&sectionId=" + sectionId),
		width : 780,
		height : 380,
		resizable : 1,
		scrollbars : 1
	}, {
		action : function(param){ actionSetDescrArticle(formElem, param); }
	});
}

function resetDescrArticle() {
	document.getElementById('relationshipReferenceId').value='';
	document.getElementById('descrArticleTitle').value='';
}

function actionSetDescrArticle(formElem,param) {
	if (param == null ){
		// uzivatel zmackl cancel
		return;
	} else {
		formElem.relationshipReferenceId.value=param.articleId;
		formElem.descrArticleTitleDisabled.value=param.title;
	}
}

function setInquiryAnswerDescrArticle(answerElem, sectionId){
	var ed;
	if (tinyMCE.get('inquiryAnswerTinyEditor')) {
		ed = tinyMCE.get('inquiryAnswerTinyEditor');
	} else {
		ed = new tinymce.Editor("inquiryAnswerTinyEditor");
		ed.render(1);
	}
	ed.windowManager.open({
		file : '/admin/prepareInsertDescrArticle.do' +
		       ((sectionId == "") ? "" : "?sectionId=" + sectionId),
		width : 780,
		height : 380,
		resizable : 1,
		scrollbars : 1
	}, {
		action : function(param){ actionSetInquiryAnswerDescrArticle(answerElem, param) }
	});
}

function actionSetInquiryAnswerDescrArticle(answerElem, param) {
	if (param == null ){
		// uzivatel zmackl cancel
		return;
	} else {
	var children = answerElem.childNodes;
		for (var a=0; a<children.length; a++) {
			if (!(children[a].name)) continue;
			if (children[a].name=="answerDescrArticleId") {
				children[a].value=param.articleId;
			}
			if (children[a].name=="answerDescrArticleTitle") {
				children[a].value=param.title;
			}
		}
	}
}

function checkAllBoxes(formId, newChecked) {
	var formElem = document.getElementById(formId);
	var inputElems = formElem.getElementsByTagName("input");
	for (i=0; i<inputElems.length; i++) {
		if (inputElems[i].type == "checkbox") {
			inputElems[i].checked = newChecked;
		}
	}
}

