/*---< tools.js >----------------------[05.09.21]
|
| Site specific js components
|
+----< dependencies >----------------------------
|
| >> tools.js
|
+----< notes >-----------------------------------
|
| Javascript 1.4 compliant
|
+------------------------------------------------
| (c)2003,2004,2005 Inter-Planning Inc.
| http://www.inter-garden.com
+----------------------------------------------*/


/*------------------------------------------------------------
| window tools
------------------------------------------------------------*/

function openSubWindow(winName,link) {
	var w,h,p1,p2,params;
	if (link == null) { link = ''; }
	if (winName == null) { winName = 'win_new'; }
	switch (winName) {
		case 'win_sub' : w = 640; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable,status=1'; break;
		case 'win_blog' : w = 900; h = Math.round(screen.height*0.9); p1 = 'titlebar,toolbar,location,menubar,directories,copyhistory,status,resizable,scrollbars=1'; break;
		default : w = 0; h = 0; p = ''; p1='titlebar,toolbar,location,menubar,directories,copyhistory,status,resizable,scrollbars=1';
	}
	if (w > 0 && h > 0) {
		var x = (screen.width - w) / 2;
		var y = (screen.height - h) / 2;
		if (browser.ie) { y -= 32; }
		p2 = ',height=' + h + ',width=' + w + ',top=' + y + ',left=' + x;
	}
	if (window[winName] != null) { // I don't think this works!!
		top.blur();
		window[winName].focus();
	} else {
		w = window.open(link,winName,p1+p2)
		w.focus();
	}
}

function initWindowTargets(branch) {

// !!! unfortunatly form "onsubmit" events cannot be set via "tasks.add()" !!!
// FIX: use "submitForm()" or place openSubWindow() directly in <form> tag.

	var a = (!branch) ? document.links : collectElements('this.isTag(obj,"a")',null,null,branch);
	for (var i = 0; i < a.length; i++) {
		if (a[i].target.match(/^win_/)) {
			tasks.replace(a[i],'onclick','openSubWindow("'+ a[i].target +'")','winTarget');
		}
	}
}

/*------------------------------------------------------------
| form tools
------------------------------------------------------------*/

function submitForm(val,f) {
	f = getForm(f);
	setFormValues(val,f);
	if (f.target.match(/^win_/)) { openSubWindow(f.target); }
	f.submit();
}

function setFormValues(val,f) {
	if (!val) { return; }
	f = getForm(f);
	val = val.split(',');
	for (i = 0; i< val.length; i++) {
		var v = val[i].split('=');
		if (v.length == 2 && f[v[0]]) {
			f[v[0]].value = v[1];
		}
	}
}

function getForm(f) {
	if (f && f.action) { return f; }
	if (!f) { f = 0; }
	f = document.forms[f];
	if (!f) { f = getById(f); }
	if (!f) { alert('ERROR: getForm() -> Form Object not found!'); }
	return f;
}

//----- <select> tools -----

function checkLocationPD(who) {
	if (who.value == "-") { who.selectedIndex = 0 }
}

/*------------------------------------------------------------
| misc tools
------------------------------------------------------------*/

function toggleDisplay(who) {
	if (typeof who == "string") {
		who = getById(who);
	} else {
		who = who.nextSibling;
		while (who.tagName == null) { who = who.nextSibling }
	}
	who.style.display = (who.style.display == 'none') ? '' : 'none';
}

//---------- slider ----------[begin]
var slideTimer;
var sliding = false;

function slideTo(who) {
	var sPos = getPageOffset();
	var ePos = getPos(who);
	var steps = 6;
	sliding = true;
	ePos.y -= 4;
	goSlide(sPos.x, sPos.y, 0, ePos.y, steps);
}

function goSlide(curX, curY, endX, endY, steps){
	var newX, newY;
	if (slideTimer) { clearTimeout(slideTimer) }
	if (!endX || endX < 0) { endX = 0 }
	if (!endY || endY < 0) { endY = 0 }
	if (!curX) { curX = getPageOffset().x }
	if (!curY) { curY = getPageOffset().y }
	if (!steps) { steps = 6 }
	curX += (endX - curX) / steps;
	curY += (endY - curY) / steps;
	if (curX < 0) { curX = 0 }
	if (curY < 0) { curY = 0 }
	newX = Math.round(curX);
	newY = Math.round(curY);
	window.scrollTo(newX, newY);
	if (newX != endX || newY != endY) {
		slideTimer = setTimeout("goSlide("+curX+","+curY+","+endX+","+endY+","+steps+")",15);
	} else {
		sliding = false;
	}
}
function getPageOffset() {
	var x, y;
	x = (document.body.scrollLeft) ? document.body.scrollLeft : ((window.pageXOffset) ? window.pageXOffset : 0 );
	y = (document.body.scrollTop) ? document.body.scrollTop : ((window.pageYOffset) ? window.pageYOffset : 0 );
	return new vector(x,y);
}
//---------- slider ----------[end]
