/**
 *  @file   	scripts.js
 *  @version    2.1.0
 *
 *  JavaScript library for narechk.net .
 *
 *
 *  Copyright (C) 2005-2006 by Narech K.
 *  All Rights Reserved.
 */


/*
// browser is Opera5 compatible
var nkHintOP5 = window.opera;
// browser is at least IE4 compatible
var nkHintIE4 = document.all;
// browser is at least NS4 compatible
var nkHintNS4 = document.layers;
// browser is at least NS6 compatible
var nkHintNS6 = document.getElementById && !document.all;
// browser can handle DHTML
var nkHintDHTML = document.getElementById || document.layers || document.all;
// browser is W3C DOM compliant
var nkHintW3CDOM = document.getElementById && document.createElement;
//*/


/* set window title */
function nkSetWindowTitle(inTitle)
{
	parent.document.title = inTitle;
}
/* open new window */
function nkOpenWindow(inURL, inTitle, inFeatures)
{
	window.open(inURL, inTitle, inFeatures);
}
/* print the current window (delayed by 1.0 sec) */
function nkPrintWindow()
{
	window.setTimeout("window.print()", 1000);
}


/* out: object & style (use: var i = new nkObject('name'); ) */
function nkObject(inId)
{
	if(document.getElementById)
	{
		this.obj = document.getElementById(inId);
		this.style = document.getElementById(inId).style;
	}
	else if(document.all)
	{
		this.obj = document.all[inId];
		this.style = document.all[inId].style;
	}
	else if(document.layers)
	{
		this.obj = nkObjectNN4(document, inId);
		this.style = this.obj;
	}
	else
	{
		this.obj = eval("document.all['" + inId + "']");
		this.style = eval("document.all['" + inId + "']");
	}
}
function nkObjectNN4(inObj, inId)
{
	var r, x = inObj.layers;
	for(var i = 0; i < x.length; i++)
	{
		if(x[i].id == inId)
			r = x[i];
		else if(x[i].layers.length)
			var t = nkObjectNN4(x[i], inId);
		if(t)
			r = t;
	}
	return r;
}
/* out: object or null if not found */
function nkGetObject(inId)
{
	if(document.getElementById)
		return document.getElementById(inId);
	else if(document.all)
		return document.all[inId];
	else if(document.layers)
		return document.layers[inId];
	else
		return eval("document.all['" + inId + "']");
}
/* out: client window width */
function nkGetClientWidth()
{
	if(self.innerWidth)
		return self.innerWidth;
	if(document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if(document.body)
		return document.body.clientWidth;
	return 0;
}
/* out: client window height */
function nkGetClientHeight()
{
	if(self.innerHeight)
		return self.innerHeight;
	if(document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if(document.body)
		return document.body.clientHeight;
	return 0;
}
/* out: client window scroll offset (e.g. how much the page has been scrolled) */
function nkGetScrollOffset()
{
	if(self.pageYOffset)
		return self.pageYOffset;
	if(document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	else if(document.body)
		return document.body.scrollTop;
	return 0;
}
/* out: total page height (e.g. the height of the body element) */
function nkGetPageHeight()
{
	if(document.body.scrollHeight > document.body.offsetHeight)
		return document.body.scrollHeight;
	return document.body.offsetHeight;
}


/* set object opacity: 0..100% */
function nkSetObjectOpacity(inObj, inOpacity)
{
	if(window.opera && window.getSelection)
	{
		// Opera v9+
		inObj.style.opacity = inOpacity / 100.0;
	}
	else if(document.all)
	{
		// IE
		inObj.style.filter = 'alpha(opacity=' + inOpacity + ')';
	}
	else if(document.getElementById && document.createElement)
	{
		// Gecko
		inObj.style.opacity = inOpacity / 100.0;
	}
	else if(document.layers)
	{
		// Netscape
		inObj.style.MozOpacity = inOpacity / 100.0;
	}
}


/* ToolTips */
var ttPref	= 'tt';
var ttLast	= -1;
var ttTemp	= -1;
var ttAlpha	= 0;
var ttDelay	= 1;


/* display fade-in/fade-out message, keep on-screen for inDelay ms */
function nkShowMessage(inTT, inDelay)
{
	ttDelay = inDelay;
	ttAlpha = 0;
	ttTemp = inTT;
	window.setTimeout("nkFadeInTT(ttTemp)", 1000);
}
/* display a tooltip message */
function nkShowTooltip(inTT)
{
	ttAlpha = 9;
	ttTemp = -1;
	nkShowTT(inTT);
}
/* hide last shown tooltip message */
function nkHideTooltip()
{
	ttAlpha = 0;
	ttTemp = -1;
	nkHideTT();
}
/* impl: display a tooltip message */
function nkShowTT(inTT)
{
	if(ttLast >= 0)
		nkHideTT();

	var ttObj = nkGetObject(ttPref + inTT);
	if(ttObj != null)
	{
		var ttLeft = nkGetClientWidth() - 40 - 328;
		var ttTop = nkGetScrollOffset() + 15;

		if(ttLeft < 0)
			ttLeft = 0;

		ttObj.style.left = ttLeft + 'px';
		ttObj.style.top = ttTop + 'px';

		nkSetObjectOpacity(ttObj, ttAlpha * 10);

		ttObj.style.visibility = "visible";
		ttLast = inTT;
	}
}
/* impl: hide last shown tooltip message */
function nkHideTT()
{
	if(ttLast >= 0)
	{
		var ttObj = nkGetObject(ttPref + ttLast);
		if(ttObj != null)
		{
			ttObj.style.visibility = "hidden";
			ttLast = -1;
			ttAlpha = 9;
		}
	}
}
/* impl: fade-in and display a tooltip message */
function nkFadeInTT(inTT)
{
	if(ttAlpha < 10)
	{
		var ttObj = nkGetObject(ttPref + inTT);
		if(ttObj != null)
		{
			if(ttAlpha == 0)
				nkShowTT(inTT);
			else
				nkSetObjectOpacity(ttObj, ttAlpha * 10);
		}
		ttAlpha++;
		window.setTimeout("nkFadeInTT(ttLast)", 50);
	}
	else
	{
		ttAlpha = 9;
		window.setTimeout("nkFadeOutTT()", ttDelay);
	}
}
/* impl: fade-out and hide last shown tooltip message */
function nkFadeOutTT()
{
	if(ttLast >= 0 && ttTemp >= 0)
	{
		if(ttAlpha > 0)
		{
			var ttObj = nkGetObject(ttPref + ttLast);
			if(ttObj != null)
				nkSetObjectOpacity(ttObj, ttAlpha * 10);
			ttAlpha--;
			window.setTimeout("nkFadeOutTT()", 50);
		}
		else
		{
			ttAlpha = 0;
			nkHideTT(ttLast);
		}
	}
}


/* out: reversed inText */
function nkTextReverse(inText)
{
    var r = "";
    var l = inText.length;
    while(l > 0)
    {
        r += inText.substring(l-1, l);
        l--;
    }
    return r;
}
/* descramble inText (constructs usable email string) */
function nkEmailDescramble(inText)
{
    var t = inText.substr(1, inText.length-2);
    var r = nkTextReverse(t);
    location.href = r;
}
/* scramble inText (analyze this, spamfucks!) */
function nkEmailScramble(inText)
{
    var t = "@" + inText + "@";
    var r = nkTextReverse(t);
    w = window.open("","displayWindow","menubar=no,scrollbars=no,status=yes,width=300,height=100");
    w.document.write("<html><head><\/head><body><center><br>" + r + "<br><\/center><\/body><\/html>");
}
/*
using the above to scramble an email address:
	<a href="#" onClick="javascript:nkEmailScramble('mailto:name@domain.com');">scramble</a>

on your page replace INSERT_SCRAMBLED below with copy & paste of the result from above:
	<a href="javascript:nkEmailDescramble('INSERT_SCRAMBLED');">descramble</a>
*/


/* out: random number in range [0..N) */
function nkRandom(inN)
{
    var n = Math.floor(Math.random() * inN);
    return n;
}


/* EOF */

