// Copyright (c) 2003-2005 Juerg Lehni, Lineto.com. All rights reserved.
// Copying and reuse is strictly prohibited.

// variables to send with each post / get request. application dependent!
function addQueryParams(ps) {
	ps.pathDate = pathItems[0];
	ps.format = imageFormat;
	if (sendData) {
		ps.sendData = sendData.join('|');
		sendData = null;
	}
}

function onNavigSelect(sel, params, noWait) {
	clearTimeout(this.timeout);
	var path = sel.path;
	if (path) {
		var url = urlPrefix + path;
		var ch = sel.item[3];
		if (typeof ch == "string" && ch.charAt(0) == '#') {
			gui.page.stopLoading();
			ch = ch.substring(1, ch.length);
			if (ch.indexOf('openWin') != -1) eval(ch);
			else this.timeout = setTimeout('eval("' + ch + '")', 10);
		} else {
			if (sel.item[2] || gui.edit) {
				if (params && params.indexOf('back=') != -1) params += '&backId=' + loadedId;
				currPath = path;
				var str = "load(gui.page, '" + url.replace(/'/g, "\\'") + "content" + (params ? '?' + params : '') + "')";
				if (noWait) eval(str);
				else this.timeout = setTimeout(str, 500);
			}
		}
		if (this.form.url) this.form.url.value = hostName + url;
	}
}

function createPatterns() {
	var e = fix.pattern ? 'gif' : imageFormat;
	var s = [cbeNewImage("p0", null)];
	for (var i = 1; i < 36; i++) s[i] = cbeNewImage("p" + i, "/img/patterns/" + i + "." + e);
	return s;
}

function selectPath(path, refresh, dontLoad) {
	gui.finder.selectPath(path, refresh, dontLoad);
}

function winResizeListener(e) {
	if (gui.page.scrollbars) gui.page.layoutScrollbars();
}

function getFinderItem(back) {
	var sel = gui.finder.currSel;
	return sel.items[sel.level - back];
}

function loadAction(object, action, level) {
	gui.page.stopLoading();
	var w = gui.finder, sel;
	if (level == null) {
		sel = w.loadedSel;
		level = -2;
	}
	if (!sel) {
		if (level < 0) level += w.currSel.level + 1;
		else level--;
		sel = w.getSelection(level);
	}
	load(object, urlPrefix + sel.path + action);
	return sel;
}

writerItems = writerFont = writerWeight = null;
function hideWriter(){} // redef in writer.js!

function writer() {
	gui.page.stopLoading();
	if (!gui.writer) loadAction("writer", 'writer?load=1', 0);
	else showWriter();
}

function renderButton(value, action, id) {
	var str = '<a href="#" onClick="return false;"';
	if (action) str += ' onMouseUp="' + action + '"';
	if (id) str += ' id="' + id + '"';
	if (!is.nav4) str += ' class="button" style="display:block"';
	str += '>' + value + '</a>';
	return str;
}

function search() {
	hideWriter();
	if (!gui.search) {
		var str = 
'<form name="searchForm" target="" action="javascript:doSearch()" onSubmit="return doSearch()" method="post" autocomplete="off">\
<table border="0" cellpadding="0" cellspacing="4">\
<tr valign="middle">\
<td class="text"><input type="text" name="search" size="32" maxlength="255" value=""></td>\
<td class="buttonText" align="right">' + renderButton("Search", "doSearch()") + '</td>\
</tr>\
<tr><td colspan="2">' +
gui.finder.renderSelect('results', 8, 300, { onClick: "chooseSearch()", onChangee: "chooseSearch()", onKeyDown: "chooseSearch()"  }) +
'</td></tr>\
</table>';
	if (!is.nav4) str += '</form>';
		var win = new Window("search", -20, -40, null, null, skins.system.hor, CLOSABLE | FIXCONTENT | VISIBLE, str);
		win.form = cbeGetFormByName('searchForm');
	} else with (gui.search) {
		moveToFront();
		show(true);
	}
}

function doSearch() {
	var w =  gui.search;
	var frm = w.form;
	if (!w.http) w.http = new HttpRequest();
	with (w.http) {
		init(urlPrefix + '/search', 'post');
		add('query', frm.search.value);
		send();
	}
}

function setSearch(results) {
	var sel = gui.search.form.results;
	sel.selectedIndex = -1;
	sel.options.length = 0;
	for (var i in results)
		sel.options[i] = new Option(results[i]);
}

function chooseSearch() {
	var sel = gui.search.form.results;
	if (sel.selectedIndex >= 0) {
		var path = sel.options[sel.selectedIndex].text;	
		if (path && path != currPath) selectPath(path);
	}
}

function tryFont() {
	writerWeight = getFinderItem(1)[0];
	writerFont = getFinderItem(2)[0];
	writer();
}

function updateInfos(hiddens) {
	for (var i in infos) {
		i = infos[i];
		var w = i.win;
		if (w) {
			if (hiddens) {
				if (w.visible) {
					w.show(0);
					push(hiddens, w);
				}
			} else if (w.visible && i.dispId != i.nodeId) {
				if (i.nodeId != null) {
					i.dispId = i.nodeId;
					if (i.set) i.set(i);
					else w.setHtml(i.content, null, true);
				} else if (i.stayId == null || i.stayId != loadedId) {
					i.stayId = i.dispId = null;
					w.show(0);
				}
			}
		}
	}
	return hiddens;
}

function setSample(info, id) {
	if (id == null)
		id = info.lastSample ? info.lastSample : language;
	var html = '<table cellpadding="0" cellspacing="0" border="0"><tr><td align="right"><span class="small">';
	var samps = [];
	if (info.hasSample) samps = [
			{ id: 'li', name: 'Lorem' },
			{ id: 'en', name: 'English' },
			{ id: 'de', name: 'Deutsch' },
			{ id: 'fr', name: 'Fran\xe7ais' },
			{ id: 'es', name: 'Espa\xf1ol' }
		];
	else if (id != 'cm') id = 'cm';
	push(samps, { id: 'cm', name: 'Character Map' });
	for (var i in samps) {
		if (i > 0) html += ' / ';
		var s = samps[i];
		if (s.id == id) html += s.name;
		else html += '<a href="javascript:setSample(infos.sample,\'' + s.id + '\')">' + s.name + '</a>';
	}
	if (id != 'cm') language = id;
	info.lastSample = id;
	html += '</span></td></tr><tr><td><img src="/img/spacer.gif" width="1" height="9" border="0" alt=""></td></tr><tr><td>';
	html += '<img src="/img/samples/' + info.content + '_' + id + '.gif" width="349" height="381" border="0" alt="">';
	html += '</td></tr></table>';
	gui.sample.setHtml(html, null, true);
}

function setInfo(name, id, param) {
	hideWriter();
	gui.page.stopLoading();
	var i = infos[name], id1, id2;
	if (id == null) {
		id1 = getFinderItem(1)[1];
		id2 = getFinderItem(2)[1];
	} else id1 = id2 = id;
	if (i.content != null && (i.nodeId == id1 || i.nodeId == id2)) {
		if (!i.win) {
			i.create(i);
			i.win = gui[name];
			if (i.win) gui.finder.stayOnTop();
		}
		var w = i.win;
		if (w) {
			i.dispId = i.nodeId;
			if (i.set) i.set(i, param);                                                                         
			else w.setHtml(i.content, null, true);
			w.zoom(true);
			w.show(true);
			if (i.autoSize) w.autoSize();
			w.moveToFront();
		}
	} else gui.finder.currSel = loadAction(gui.page, 'content?format=' + imageFormat + '&info=' + name);
}

function setLabel(str, noSearch) {
	var p = gui.page, l = p.label;
	if (!noSearch) str += '<a href="javascript:search()">Search</a>';
	if (is.nav4) {
		str = '<div class="small">' + str + '</div>';
		l.setHtml(str, 3, true);
		var w = l.obj.calcWidth(), h = l.obj.calcHeight();
		l.moveTo(p.width() - w - 20, p.height() - h - 20);
		l.resizeTo(w, h);
		l.obj.background('white');
	} else l.setHtml(str, null, true);
}

function formatTime(v) {
	var s = v.toString();
	return s.length == 1 ? '0' + s : s;
}

function updateTimer() {
	var date = new Date();
	var diff = new Date(date.getTime() - timer.start);
	timer.doc.title = formatTime(date.getHours() + timer.fix) + (timer.blink ? ':' : ' ') + formatTime(date.getMinutes()) + slogan + formatTime(diff.getMinutes()) + ':' + formatTime(diff.getSeconds());
	timer.blink = !timer.blink;
	if (fix.autoscroll) self.scrollTo(0, 0);
}

function writeTo(u, d) {
	var l="\x6D\x61\x69";
	l+="\x6C\x74\x6F\x3A"+u;
	window.location=l+"\x40"+d;
}

function openWin(url, title, width, height, ps) {
	if (!ps) ps = {};
	var win = window.open(url, title, "width=" + width + ",height=" + height + ",left=" + ((ps.left == null) ? Math.round((screen.width - width) / 2) : ps.left) + ",top=" + ((ps.top == null) ? Math.round((screen.height - height) / 2 - 40) : ps.top) + ",toolbar=0,menubar=0,location=0,status=" + (ps.status ? 1 : 0) + ",resizable=" + (ps.resizable ? 1 : 0) + ",scrollbars=" + (ps.scrollbars ? 1 : 0));
	if (!ps.noFocus) win.focus();
	return win;
}

function blurForm(frm) {
	var es = frm.elements;
	for (var i = 0; i < es.length; i++) if (es[i].blur) es[i].blur();
}

function blurAllForms() {
	var fs = document.forms;
	if (fs) for (var i = 0; i < fs.length; i++) blurForm(fs[i]);
}

function push(a, o) {
	var i = a.length;
	a[i] = o;
	return i;
}

function splice(a, i, c, o) {
	if (a.splice) {
		if (o) a.splice(i, c, o);
		else a.splice(i, c);
	} else {
		var r=[], l=a.length;
		for (var j=0;j<c;j++) push(r, a[i+j]);
		if (o) c--;
		if (c>0) for (j=i+c;j<l;j++) a[j-c]=a[j];
		else if (c<0) for (j=l;j>=i-c;j--) a[j]=a[j+c];
		if (o) a[i] = o;
		l-=c;
		a.length=i>l?i+1:l;
		return r;
	}
}

function clone(o) {
	var r = o.length ? [] : {};
	for (var i in o) r[i] = o[i];
	return r;
}

function getNow() {
	return new Date().getTime();
}

