/*
 * isquared.nl common JavaScript functions
 *
 * Hessel Schut, hessel@schut.be, 2007-09-22
 * 
 */


function rt(w,h) {
  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
      top.outerWidth=w;
      top.outerHeight=h;
    }
    else top.resizeTo(w,h);
  };
}

function cc() {
  var cl = document.getElementsByTagName('div');
  for (var i=0; i<cl.length; i++) {
    if ( 
      cl.item(i).className == 'cell' && 
      cl.item(i).innerHTML.indexOf('2') == 0 ) 
    {
      // randomPaletteColor() is defined in /js/common/colors.js
      cl.item(i).style.backgroundColor = randomPaletteColor();
    };
  };
}

function directNav() {
	// handle keyword navigation in index.html url query string
	var u = document.location.href;
	var p = u.indexOf('=');
	if (u.match('/\?/')) {
		var s = u.indexOf('?') + 1;
		if (p > 0) {
			// strip trailing equals signs in keywords
			var k = u.substr(s, p - s);
		} else {
			var k = u.substr(s);
		};
	};
/*
	var na = document.getElementById(k);
	if (na) {
		if (na.tagName == "A") {
			var t = document.getElementById("dispfrm");
			t.src=na.href;
			t.style.zIndex=8;
		};
	};
*/

	document.location.href="index.html?" + k + "=";	

}

function checkForIndex() {
	// test for index.html, do direct nav from index if missing
	// modified 2008-11-04 : hessch
	var idx = "http://isquared.nl/index.html";

/* 	No idea what's wrong with this, but since we're not happy
	with document.location, let's just evaluate that instead...
	var nav = window.opener.document.getElementById("nav");
	if (nav == null) { 
*/
	var loc = window.location;
	// ah, riiiight an url is not a string, it's an url...
	if (loc.toString().indexOf(idx) == -1) {
		// there is no index in my location, and therefore 
		// no menu, do something about that:

		window.location = idx + "?" + document.body.id + "=";

		// index.html takes care of the id parameter and
		// will map it to the appropiate pagelink id.
	};
}

function buryDispFrame() {
	// handle a 'close' of the display iframe
	document.location = 'about:blank';
	parent.document.getElementById('dispfrm').style.zIndex=-1;
}

function raiseDispFrame(dispURL) {
	// present dispURL in the display iframe
	document.location = dispURL;
	parent.document.getElementById('dispfrm').style.zIndex=8;
}

function ixRaiseDFrame() {
	document.getElementById("dispfrm").style.zIndex=8;
}

function insertCloseBoxHTML() {
 	var cBox = document.createElement('SPAN');
	cBox.setAttribute('id', 'closebox');
	var bA = document.createElement('A'); // back anchor
	bA.setAttribute('href', 'javascript:history.back()');
	var cA = document.createElement('A'); // close anchor
	cA.setAttribute('href', 'javascript:buryDispFrame()');
        bA.innerHTML = 'Back'; cA.innerHTML = 'Close';
        cBox.className = 'closebox';
        document.body.appendChild(cBox);
	cBox.appendChild(bA); cBox.appendChild(cA);

	setInterval( "document.getElementById('closebox').style.top = document.body.scrollTop + 10", 250 );
}

function nonPGATgtRw() {
	var aa = document.getElementsByTagName('a');
	var re = '/^http:\/\/isquared\.nl\/pages/.*/';
	var js = '/javascript:.*/';
	for (var i=0; i<aa.length; i++) {
		if ( ! aa[i].href.match(js) ) {
			aa[i].setAttribute('target','new');
		}
	};
};


function pagesMain() {
	checkForIndex();	// fix missing index.html
}

function indexMain() {
	AppendEq();		// query string workaround
}

function AppendEq() {
	var re = '/index.html\?[_a-z0-9]+$/';
	var uri = document.location.href;
	if ( uri.match(re) ) {
		document.location.href = uri + "=";
	};
};


