/************************************
 * ChromoSomnia (c) Stefan Ihringer *
 ************************************
 * description: various JavaScript functions used on ChromoSomnia.
 *              adjusted for use on oz.chromosomnia.org
 * last mod.:   29 Oct 2002
 */


// checking DOM capabilities. Only if this is true the DHTML effects will be enabled.
var DHTML = (document.getElementById) ? true:false;
// get the language from the path
var LOCALE = "";
if(window.location.href.indexOf("/de/") != -1) LOCALE = "de";
else if(window.location.href.indexOf("/en/") != -1) LOCALE = "en";


// allows for execution of several onload functions
var onLoadFunctions = ({
	func: new Array(),
	add: function(f)
	{
		this.func[this.func.length] = f;
	},
	exec: function()
	{
		// idea by DynAPI (dynapi.sourceforge.net)
		if(this.func.length > 0) eval(this.func.join(";"));
	}
});

// browser detection
var is   = new Object();
is.ua    = navigator.userAgent.toLowerCase();
is.major = parseInt(navigator.appVersion);
// checking ua string (taken from mozilla.org)
is.win   = ( (is.ua.indexOf("win")!=-1) || (is.ua.indexOf("16bit")!=-1) );
is.ie    = ( (is.ua.indexOf("msie") != -1) && (is.ua.indexOf("opera") == -1) && (is.ua.indexOf("konqueror") == -1) );
is.ie4   = ( is.ie && (is.major == 4) && (is.ua.indexOf("msie 4")!=-1) );
is.ie5   = ( is.ie && (is.major == 4) && (is.ua.indexOf("msie 5.0")!=-1) );
is.ie5_5 = ( is.ie && (is.major == 4) && (is.ua.indexOf("msie 5.5") !=-1));
is.ie6   = ( is.ie && (is.major == 4) && (is.ua.indexOf("msie 6.")!=-1) );
is.ie5up = ( is.ie && (is.major >= 4) && !(is.ie4) );
is.opera = ( is.ua.indexOf("opera") != -1 );
is.o6up	 = ( is.opera && (parseInt(is.ua.charAt(is.ua.indexOf("opera")+6)) >= 6) );
is.moz   = ( is.major >= 5 && (is.ua.indexOf("gecko") != -1) );


/* automatic bug fixing *******************************************************/

// Fix png alpha channels in IE using DirectX (taken from mozilla.org)
// parameters: name of image tag to be fixed
function fixPNG(name)
{
	// Older IE versions can't even fix the PNG so we display an alternative GIF image
	// Add all image names to this string where an alternative is available. Otherwise the image is
	// replaced by a transparent GIF.
	var alternativeAvailable = "||";

	if(is.win && is.ie)
	{
		var oldSrc = document.all[name].src;
		document.all[name].src = "/_img/1x1.gif";

		if(!is.ie4 && !is.ie5)
			document.all[name].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+oldSrc+"', sizingMethod='image')";
		else if(!is.ie6 && alternativeAvailable.indexOf("|"+name+"|") != -1)
			document.all[name].src = oldSrc.substr(0,oldSrc.length-4) + ".gif";
	}
}

// fix bug in Opera where some styles are lost after a window resize
function resizeHandler()
{
	if(window.innerWidth != is.windowWidth || window.innerHeight != is.windowHeight)
		location.replace(location.href);
	window.setTimeout("resizeHandler()", 1000);
}
//if(is.opera)
//{
//	is.windowWidth = window.innerWidth;
//	is.windowHeight = window.innerHeight;
//	window.setTimeout("resizeHandler()", 1000);
//}


/* image preloading ***********************************************************/

var mouseOverImageCache = new Array();
var mouseOverPreloadComplete = false;

// Preloads images for all <img> tags that have a name attribute.
// Should be called after page has loaded. It also triggers the PNG fix if neccessary.
function mouseOverPreload()
{
	if(mouseOverPreloadComplete) return;

	var i,j;
	// remember names to prevent loading multiple instances of the same image.
	// Add image names here to trick script into not preloading specific images even though they have a name.
	var imageNames = "|dropdownImg|";
	// in addition, don't preload images with this prefix. This is used in conjunction with the sitePreloader object
	var tabooPrefix = "preDot";
	// find all images with names
	for (i=0; i<document.images.length; i++)
		if (document.images[i].name)
            if (document.images[i].name.indexOf(tabooPrefix) != 0 && imageNames.indexOf("|"+document.images[i].name+"|") == -1)
            {
                var img = new MouseOverImage(document.images[i]);
                mouseOverImageCache[mouseOverImageCache.length] = img;
                imageNames = imageNames + img.name + "|";
				// fix PNG in IE?
				if(document.images[i].src.indexOf(".png") != -1) fixPNG(img.name);
            }
	mouseOverPreloadComplete = true;
}
onLoadFunctions.add("mouseOverPreload()");


/* mouseOver, mouseOut & mouseDown functions **********************************/

// Helper object to save the image's three states (low = normal, high = during mouseOver, down = pressed)
// The additional file names are constructed like this:
// normal image:	PICTURE.GIF
// mouseOver image:	PICTURE-mo.GIF
// parameter: image button OBJECT, example: new MouseOverImage(document.images[33]);
function MouseOverImage(the_image)
{
	var srcBase = the_image.src.substr(0,the_image.src.length-4);
	var srcExt = the_image.src.substr(the_image.src.length-4,the_image.src.length-1);

	// handle images that already are in their highlight-state. Don't try to load "image-a-a.gif" for example...
	var tmp = srcBase.substr(srcBase.length-3,srcBase.length);
	if(tmp == "-mo" || tmp == "-md")
		srcBase = srcBase.substr(0,srcBase.length-3);

	this.name = the_image.name;
	this.low = new Image();
	this.low.src = srcBase + srcExt;
	this.high = new Image();
	this.high.src = srcBase + "-mo" + srcExt;
	return this;
}

// mouseOver for images. Call it in the <a>-tag like this: onMouseOver="mouseOver('name')"
// parameters: as many image tag names as you like
function mouseOver()
{
	if(!mouseOverPreloadComplete || arguments.length == 0) return;
	var i,j;
	for(j=0; j<arguments.length; j++)
		for(i=0; i<mouseOverImageCache.length; i++)
			if(mouseOverImageCache[i].name == arguments[j])
			{
				document.images[arguments[j]].src = mouseOverImageCache[i].high.src;
	            if(mouseOverImageCache[i].high.src.indexOf(".png") != -1) fixPNG(arguments[j]);
				break;
			}
}

// mouseOut for images. Call it like mouseOver and be sure to specify exactly the same arguments.
function mouseOut()
{
	if(!mouseOverPreloadComplete || arguments.length == 0) return;
	var i,j;
	for(j=0; j<arguments.length; j++)
		for(i=0; i<mouseOverImageCache.length; i++)
			if(mouseOverImageCache[i].name == arguments[j])
			{
				document.images[arguments[j]].src = mouseOverImageCache[i].low.src;
				if(mouseOverImageCache[i].low.src.indexOf(".png") != -1) fixPNG(arguments[j]);
				break;
			}
}
