//----- oneoffice/beblog tool scripts -----[04.08.12]

//----- get current location -----
function checkPageLoc() {
	var pgSrc = document.location.href;
	var pgLoc = pgSrc.substring(pgSrc.indexOf(siteRoot)+siteRoot.length);
	if (pgLoc.indexOf('/') >= 0) { top.setMenuLoc(pgLoc) }
	else { activeBtns = '/' }
	//-- add current site btn --
	if (window.siteId) { activeBtns += siteId + '/'; }
}

//----- setup activeBtns -----
function setMenuLoc(loc){
	if (window.activeBtns == null) { window.activeBtns = '' }
	var bi = [0];
	for ( i=0; i<2; i++ ) {
		bi[i+1] = loc.indexOf('/',bi[i])+1;
		if (bi[i+1] > bi[i]) {
//			if (activeBtns != '') { activeBtns += ',' }
			activeBtns += '/';
			activeBtns += loc.substring(bi[i],bi[i+1]-1);
		}
	}
	activeBtns += '/';
}

//----- button/link methods -----

//-- new [begin] --
function initBtns() {
	var a = document.links;
	for (var i = 0; i < a.length; i++) {
		var img = collectElements('this.isTag(obj,"img") && this.hasAttribute(obj,"src=/_n\./")',null,null,a[i]);
		for (var j = 0; j < img.length; j++) {
			var src = file.splitSrc(img[j].src);
			var isCur = (docPath.path.indexOf(src.id) >= 0) ? true : false; //-- test for relative path
			if (!isCur) { //-- test for site related button
				for (var id in siteRoot) {
					if (src.id.indexOf('-'+id) >= 0 && docPath.paths[0] == siteRoot[id]) { isCur = true; break; }
				}
			}
			if (isCur) { img[j].src = src.btn[0] + 'a' + src.btn[1]; }
			else {
				tasks.add(img[j],'onmouseover','this.src = "' + src.btn[0] + 'o' + src.btn[1] + '"','btn');
				tasks.add(img[j],'onmouseout','this.src = "' + src.btn[0] + 'n' + src.btn[1] + '"','btn');
				file.preload.add(src.btn[0] + 'o' + src.btn[1]);
			}
		}
	}
}
//-- new [end] --

function initBtn(who, closePopups) {
	who.onload = '';
	var s = splitRovrSrc(who.src);
	if (window.activeBtns != null && activeBtns.indexOf('/'+getRovrName(who)+'/') >= 0) {
		who.src = s[0]+'a'+s[1]; return;
	}
	who.onmouseover = (closePopups) ? function() { setRovrState(who,'o'); closeAllPopups() } : function() { setRovrState(who,'o') };
	who.onmouseout = function() { setRovrState(who,'n') };
	var img = new Image(); img.src = s[0]+'o'+s[1];
}
function initLinkIcn(who) {
	who.onload = '';
	var s = splitRovrSrc(who.src);
	var a = who.parentNode;
	if (a.tagName.toLowerCase() == 'a') {
		a.onmouseover = function() { setRovrState(who,'o'); };
		a.onmouseout = function() { setRovrState(who,'n'); };
		var img = new Image(); img.src = s[0]+'o'+s[1];
	} else {
		setRovrState(who,'a');
	}
}
function setRovrState(who, state) {
	if (window.activeBtns != null && activeBtns.indexOf(getRovrName(who)) >= 0) { state = 'a' }
	var s = splitRovrSrc(who.src);
	who.src = s[0]+state+s[1];
}
function splitRovrSrc(src) {
	var i = src.lastIndexOf('_')+1;
	return [src.substring(0,i),src.substring(i+1)];
}
function getRovrName(who) {
	var s = splitRovrSrc(who.src);
	s = s[0].substring(0,s[0].lastIndexOf('_'));
	return s.substring(s.lastIndexOf('_')+1);
}

//----- popup methods -----
//-- right now this is only set up for single popups --
function addPopup(who) {
	if (window.popDB == null) { window.popDB = { menus:{}, curPop:null }; }
	who = who.split(',');
	for (var i=0; i<who.length; i++) {
		popDB.menus[who[i]] = { id:null, menu:null, me:null, trgr:null, foldDir:0, timer:null }
	}
}
function initPopups() {
	popDB.errors = false;
	for (var who in popDB.menus) {
		var p = popDB.menus[who];
		if ((p.menu = getById('pop_'+who)) != null) {
			p.menu.onmouseover = function () { stopPopTimer() };
			p.menu.onmouseout = function () { hidePopup() };
			p.menu.onclick = function () { this.blur();hidePopup() };
			p.me = p.menu.firstChild;
			p.id = p.menu.id.substring(p.menu.id.indexOf('_')+1);
			p.menu.style.display = 'none';
		} else { popDB.errors = true }
		if ((p.trgr = getById('popTrgr_'+who)) != null) {
			p.trgr.onmouseover = function () { showPopup(this) };
			p.trgr.onmouseout = function () { hidePopup() };
			p.trgr.onclick = function () { this.blur();hidePopup() };
			setPopTrgrState(p.trgr,'n');
		} else { popDB.errors = true }
	}
	popDB.popArea = getById('poparea');
	if (popDB.popArea != null) { popDB.popArea.style.display = 'none' }
	popDB.openMenuCnt = 0;
	popDB.curPop = null;
	popDB.oldPop = null;
//	popDB.closedPop = null;
}
function showPopup(who) {
	who = popDB.menus[who.id.substring(who.id.indexOf('_')+1)];
	popDB.oldPop = popDB.curPop;
	popDB.curPop = who;
	if (popDB.curPop != popDB.oldPop && popDB.oldPop != null) { popDB.oldPop.foldDir = -1 }
	stopPopTimer(who);
	who.foldDir = 1;
	if (popDB.foldTimer == null) { popDB.foldTimer = setInterval('foldPopups()',40) }
}
function hidePopup() {
	if (popDB.oldPop == null) { popDB.oldPop = popDB.curPop }
	popDB.oldPop.timer = setTimeout('closePopup()',200);
}
function closePopup(who) {
	if (who == null) { who = popDB.oldPop }
//	if (popDB.oldPop == popDB.closedPop) { who = popDB.curPop }
	if (who != null) {
//		popDB.closedPop = who;
		who.foldDir = -1;
		if (popDB.foldTimer == null) { popDB.foldTimer = setInterval('foldPopups()',40) }
	}
}
function closeAllPopups() {
	if (window.popDB!=null && !popDB.errors) {
		if (popDB.popArea.style.display != 'none') {
			for (var a in popDB.menus) { popDB.menus[a].foldDir = -1 }
			if (popDB.foldTimer == null) { popDB.foldTimer = setInterval('foldPopups()',40) }
		}
	}
}
function foldPopups() {
	var steps = (browserIs('ie:mac')) ? 1 : 8;
//	var step = (browserIs('ie:mac')) ? 12 : 8;
	var foldCnt = 0;
	for (var id in popDB.menus) {
		if (popDB.menus[id].foldDir == 0) { continue }
		var who = popDB.menus[id];
		if (who.menu.style.display == 'none') {
			who.menu.style.height = 0;
			setPopTrgrState(who.trgr,'o');
			setClassAttr(who.trgr,'o');
			popDB.popArea.style.display = '';
			who.menu.style.display = '';
			stopPopTimer(who);
			popDB.openMenuCnt++;
		}
		var adjH = (browserIs('ie')) ? 8 : 0; // [ie] doesn't recognize css margins!!!
		var newH = Math.round(who.foldDir*(who.me.offsetHeight+adjH)/steps)+parseInt(who.menu.style.height);
//		var newH = (who.foldDir*step) + parseInt(who.menu.style.height);
		if (newH >= who.me.offsetHeight + adjH) {
			newH = who.me.offsetHeight + adjH;
			popDB.menus[id].foldDir = 0;
		}
//		if (who.foldDir == -1 && who != popDB.curPop) { newH = 0 }
		if (newH <= 0) {
			newH = 0;
			setPopTrgrState(who.trgr,'n');
			who.menu.style.display = 'none';
			setClassAttr(who.trgr,'n');
			who.trgr.blur();
			stopPopTimer();
			popDB.menus[id].foldDir = 0;
			popDB.openMenuCnt--;
			popDB.oldPop = null;
		}
		who.menu.style.height = newH;
		who.menu.style.width = who.menu.firstChild.offsetWidth;
		var pgOffset = getPos('poparea').x;
		var btnOffset = getPos(who.trgr).x;
		var btnW = who.trgr.offsetWidth;
		who.menu.style.left = Math.round((btnOffset-pgOffset)+(btnW-who.menu.offsetWidth)/2);
		foldCnt++;
	}
	if (foldCnt == 0) { clearInterval(popDB.foldTimer); popDB.foldTimer = null }
	if (popDB.openMenuCnt == 0) { popDB.popArea.style.display = 'none' }
}
function stopPopTimer() { clearTimeout(popDB.curPop.timer) }
function setPopTrgrState(who,state) {
	setRovrState(who.firstChild,state);
}

//----- display methods -----
function toggleDisplay(who,state) {
	if (state == null) { state = (who.style.display == 'none') ? true : false; }
	if (typeof who == "string") {
		who = getById(who);
	} else {
		who = who.nextSibling;
		while (who.tagName == null) { who = who.nextSibling }
	}
	who.style.display = (state) ? '' : 'none';
}
function toggleQA(who,state) {
	if (state == null) { state = (who.id == '') ? true : false; }
	toggleDisplay(who,state);
	who.id = (state) ? 'o' : '';
	who.blur();
}
function showAllQA(show) {
	if (!window.questions) { // collect questions
		questions = [];
		for (var i = 0; i < document.links.length; i++) {
			var cls = getClassAttr(document.links[i]);
			if (cls == 'question') { questions[questions.length] = document.links[i]; }
		}
	}
	show = (show) ? true : false;
	for (var i = 0; i < questions.length; i++) { toggleQA(questions[i],show); }
}
//----- new window methods -----
function openSubWindow(winName,link){
	var w,h,p1,p2,params;
	if (link == null) { link = '' }
	switch (winName) {
		case 'est' : w = 650; h = 500; p1 = 'status=0'; break;
		case 'sub' : w = 800; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable,status=1'; break;
		case 'dir' : w = 720; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable,status=1'; break;
		case 'toi' : w = 600; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable,status=1'; break;
		case 'view' : w = 200; h = 200; p1 = 'scrollbars,resizable,status=0'; break;
		case 'sitemap' : w = 600; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable,status=1'; break;
		default : w = 0; h = 0; p = ''; p1='titlebar,toolbar,location,menubar,directories,copyhistory,status,resizable,scrollbars=1'; if (winName == null) { winName ='new' }
	}
	winName += '_win';
	if (w > 0 && h > 0) {
		var x = (screen.width - w) / 2;
		var y = (screen.height - h) / 2;
		if (browserIs('ie')) { y -= 32 }
		p2 = ',height=' + h + ',width=' + w + ',top=' + y + ',left=' + x;
	}
	if (window[winName] != null) {
		window[winName].focus();
	} else { w = window.open(link,winName,p1+p2) }
	closeAllPopups();
}

//----- misc methods -----
function checkLocationPD(who) {
	if (who.value == "-") { who.selectedIndex = 0 }
}
function slideCheck() {
	var src=document.location.href;
	var ai=src.indexOf('?')+1;
	if (ai > 1) {
		var a = getById(src.substring(ai));
		if (a != null || a != '') { slideTo(a) }
	}
}

