/**
* script.js 2004-03-09
*/
now = new Date();
window.name = "main"+now.getTime();

function popUp(URL, name, dx, dy) {
	sOptions = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + dx + ',height=' + dy
	sOptions += ',left=' + ((screen.width-dx)/2) + ',top=' + ((screen.height-dy)/2) + ',screenX=' + ((screen.width-dx)/2) + ',screenY=' + ((screen.height-dy)/2) + ''
	popupwindow = window.open(URL, name, sOptions);
}

function submitToPopUp(action, dx, dy) {
	popUp('','popupwindow',dx,dy);
	document.docmgt.action="index.php?action="+action;
	document.docmgt.submit();
}

function submitToMain(action) {
	document.docmgt.target = window.opener ? window.opener.name : "";
	document.docmgt.action = action;
	document.docmgt.submit();
}

function checkRemove(type, message, item) {
	switch (type) {
		case "resource":
			if (confirm(message)) {
				submitToMain('index.php?action=removeresource');
			}
			break;
		case "usergroup":
			if (confirm(message)) {
				submitToMain('index.php?action=removeusergroup');
			}
			break;
		case "groupuser":
			if (confirm(message)) {
				submitToMain('index.php?action=removegroupuser');
			}
			break;
		case "group":
			if (confirm(message)) {
				submitToMain('index.php?action=delgroup');
			}
			break;
		case "user":
			if (confirm(message)) {
				submitToMain('index.php?action=deluser');
			}
			break;
		case "item":
		case "page":
		case "image":
			if (confirm(message)) {
				return false;
			} else {
				return true;
			}
			break;
	}
}

var bDone = false;

function addConfirmToLinks(sMessage){
	if (bDone == false) {
		// links
		for (var a=0; a<document.links.length; a++) {
			if (A = document.links[a]) {
				if (isItemInHtmlarea(A)) {
					//alert(A.href)
				} else {
					sOldHref = A.href
					A.href = "javascript:doConfirm('"+sOldHref+"', '"+sMessage+"')";
				}
			}
		}
		// forms
		for (var a=0; a<document.forms.length; a++) {
			if (oForm = document.forms[a]) {
				if (isItemInHtmlarea(oForm)){
					// do nothing
				} else {
					// :todo: not ns compatible, slashes
					oForm.onsubmit = function(event) { return confirm(sMessage);}
				}
			}
		}
	}
	enable(document.getElementById('savebutton'));
	enable(document.getElementById('savebutton_footer'));
	bDone = true
}

function doConfirm(sOldHref, sMessage) {
	if (confirm(sMessage)) {
		window.location = sOldHref;
	}
}

function isItemInHtmlarea(oLink) {
	var elem = oLink
	while (elem.name != "htmlarea") {
		elem = elem.parentElement
		if (elem == null)
			break
	}
	if (elem) {
		return true
	}
}

/**
* Toolbar button functions
*/
function selOn(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selOn";
	}
}

function selOff(ctrl) {
	if (ctrl) {
		ctrl.className = "selOff";
	}
}

function selDown(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selDown";
	}
}

function selUp(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selUp";
	}
}

function disable(ctrl) {
	if (ctrl && !ctrl.disabled) {
		ctrl.style.color = "graytext";
		//if (!ctrl.onclicktemp && ctrl.onclick != "") {
			ctrl.onclicktemp = ctrl.onclick
			ctrl.onclick = "";
		//}
		ctrl.disabled = true;
		// check if the control contains an image
		makeGray(ctrl, true);
	}
}

function enable(ctrl) {
	if (ctrl) {
		ctrl.style.color = "menutext";
		if (ctrl.onclicktemp && ctrl.onclick == "") {
			ctrl.onclick = ctrl.onclicktemp
		}
		ctrl.disabled = false;
		// check if the control contains a images
		makeGray(ctrl, false);
	}
}

function makeGray(el,b) {
	var sInter;
	var sPath = "";

	if (b) {
		sInter = "disabled/disabled_";
	} else {
		sInter = "";
	}
	var imgs = findChildren(el, "tagName", "IMG");
	for (var i=0; i<imgs.length; i++) {
		//imgs[i].style.filter = filtval;
		if (b) {
			if (!imgs[i].orig_src) {
				// todo check if img is disabled
				sFull = imgs[i].orig_src = imgs[i].src;
				aFull = sFull.split('/');
				sFilename = aFull[aFull.length-1];
				for (var j=0; j < (aFull.length-1); j++) {
					sPath = sPath + aFull[j]+"/";
				}
				imgs[i].src = sPath+sInter+sFilename;
			}
		} else {
			if (imgs[i].orig_src) {
				imgs[i].src = imgs[i].orig_src;
				imgs[i].orig_src = null;
			}
		}
	}
}

function findChildren(el, type, value) {
	var children = el.childNodes;
	var tmp = new Array();
	if (!children || !children.length) {
		return tmp
	}
	var j=0;

	for (var i=0; i<children.length; i++) {
		if (eval("children[i]." + type + "==\"" + value + "\"")) {
			tmp[tmp.length] = children[i];
		}
		tmp = tmp.concat(findChildren(children[i], type, value));
	}
	return tmp;
}

/**
* Toolbar pulldown menu
*/
function menuOn(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOn";
	}
}

function menuOff(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOff";
	}
}

function menuDown(ctrl) {
}

function menuUp(ctrl) {
}

function showMenu(oMenu, oButton)
{
	oMenu.style.display  = 'block'
	oMenu.style.position = 'absolute'
	iPosition = getPosition(oButton);
	oMenu.style.left = iPosition[0];
	oButton.height = 20;
	oMenu.style.top = iPosition[1] + oButton.height;
}

function hideMenu(oMenu)
{
	oMenu.style.display='none'
}

var sMenu;

function toggleMenu(sDropMenu, oButton)
{
	event.cancelBubble = true;
	document.onclick = onclickhandler
	sMenu = sDropMenu;
	oMenu = document.getElementById(sDropMenu)
	if (oMenu.style.display=='none' || !oMenu.style.display) {
		showMenu(oMenu, oButton)
	}
	else {
		hideMenu(oMenu)
	}
}

function onclickhandler(event) {
	if (sMenu) {
		hideMenu(document.getElementById(sMenu));
	}
}

function getPosition(oButton) {
	if( oButton.offsetParent ) {
		for( var posX = 0, posY = 0; oButton.offsetParent; oButton = oButton.offsetParent ) {
			posX += oButton.offsetLeft;
			posY += oButton.offsetTop;
		}
		return [ posX, posY ];
	} else {
		if( !oButton.x && !oButton.y ) {
			return [0,0];
		} else {
			return [ oButton.x, oButton.y ];
		}
	}
}

function printWindow()
{
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) {
		window.print()
	}
}

var bAllPara


function selectParagraph(sId)
{
	if (getCookie('adminmode') == 'true') {
		if (document.getElementById('toolbardiv' + sId)) {
			document.getElementById('toolbardiv' + sId).style.display = 'inline';
			if (getCookie('allpara') != 'true') {
				document.getElementById('pc_paragraph'+sId).style.backgroundColor='#F3F3F3';
			}
		}
	}
}

function unselectParagraph(sId)
{
	if (
		document.getElementById('toolbardiv'+sId)
	    && (getCookie('allpara') != 'true' || getCookie('adminmode') != 'true')
	)
	{
		document.getElementById('toolbardiv'+sId).style.display = 'none';
		document.getElementById('pc_paragraph'+sId).style.backgroundColor = 'transparent';
	}
}

function toggleAllPara()
{
	var bAllPara
	if (getCookie('allpara') == 'true') {
		setAllPara('false')
		document.getElementById('allpara').className = 'selOff'
	} else {
		setAllPara('true')
		document.getElementById('allpara').className = 'selDown'
	}
}

function setAllPara(bAllPara)
{
	if (bAllPara == 'true') {
		setCookie('allpara', 'true', 7);
		setParaIds(aParaIds);
	} else {
		removeCookie('allpara');
		setParaIds(aParaIds);
	}
}

function setParaIds(aParaIds)
{
	bShow = isShowAll()
	for (var i = 0; i < aParaIds.length; i++) {
		if (bShow == true && getCookie('adminmode') == 'true') {
			selectParagraph(aParaIds[i])
		}
		else {
			unselectParagraph(aParaIds[i])
		}
	}
}

function isShowAll()
{
	if (getCookie('allpara') == 'true' && getCookie('adminmode') == 'true') {
		bShow = true;
		if (document.getElementById('allpara')) {
			document.getElementById('allpara').className = 'selDown'
		}
	} else {
		bShow = false;
		if (document.getElementById('allpara')) {
			document.getElementById('allpara').className = 'selOff'
		}
	}
	return bShow
}

function setAdminMode(bAdminMode)
{
	oMenu = document.getElementById('toptoolbar')
	if (oMenu) {
		if (bAdminMode == 'true') {
			oMenu.style.display = 'block'
			setCookie('adminmode', 'true', 7);
		}
		else {
			oMenu.style.display='none'
			removeCookie('adminmode');
		}
	}
	setParaIds(aParaIds);
}

function toggleAdminMode()
{
	if (getCookie('adminmode') == 'true') {
		setAdminMode('false')
	} else {
		setAdminMode('true')
	}
}

// Cookie handling
function setCookie( sName, sValue, nDays ) {
	var expires = "";
	if ( nDays ) {
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}
	document.cookie = sName + "=" + sValue + expires + "; path=/";
};

function getCookie(sName) {
	var re  = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};

function removeCookie( name ) {
	setCookie( name, "", -1 );
};
