
<!-- $Id: BlogGlobal.js,v 1.3 2006/07/06 00:09:49 nico Exp $ -->

function replace(string,text,by) 
{
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


function getCookieValue(name)
{
	ret = -1;
	nstart = document.cookie.indexOf(name+'=');
   if (nstart >= 0)
	{
		nstart += name.length + 1;
		nend = document.cookie.indexOf(';', nstart);
		if (nend == -1) { nend = document.cookie.length;}
		ret=replace(unescape(document.cookie.substring(nstart, nend)), '+', ' ');
	}
	return ret;
}

function fillForm()
{
	if ("undefined" == typeof(document.GuestBook)) 
	{
		if ("undefined" == typeof(document.Contact)) 
		{
			if ("undefined" == typeof(document.formular)) 
			{
				if ("undefined" == typeof(document.Abo)) 
				{
				}
				else
				{
					tofill = document.Abo;
				}
			}
			else
			{
				tofill = document.formular;
			}
		}
		else
		{
			tofill = document.Contact;
		}
	}
	else
	{
		tofill = document.GuestBook;
	}
	if ("undefined" != typeof(tofill))
	{
		username = getCookieValue('ReaderName');
		if (username != -1)
		{
			if ("undefined" != typeof(tofill.username)) 
			{
				tofill.username.value=username;
			}
		}
		email = getCookieValue('ReaderEmail');
		if (email != -1)
		{
			if ("undefined" != typeof(tofill.email)) 
			{
				tofill.email.value=email;
			}
		}
		website = getCookieValue('ReaderWebsite');
		if (website != -1)
		{
			if ("undefined" != typeof(tofill.website)) 
			{
				tofill.website.value=website;
			}
		}
	}
}

function addEvent(obj, evType, fn)
{ 
	if(obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent('on'+evType, fn); 
		return r; 
	} 
	else 
	{ 
		return false; 
	} 
} 

var orignalscaled = Array();
function maxImgWidth()
{ 
	if ("undefined" == typeof(contentwidth))
	{ 
		val = 420;
	} 
	else
	{
		val = contentwidth;
	}	
	images = document.getElementsByTagName('img'); 
	match = 'contentimage_ID';
	for(i=0; i < images.length; i++)
	{ 
		if (images[i].className == 'contentimage' && images[i].width > val)
		{ 
			images[i].width = val; 
		} 
		else if (images[i].className.substring(0, match.length) == match)
		{ 
			if (images[i].width > val)
			{
				images[i].width = val; 
			}	
			id = images[i].className.substring(match.length, images[i].className.length);
			orignalscaled[id] = Array(images[i].width, images[i].height);
		} 
	} 
	return false; 
} 


function scale2thumbnail()
{ 
	match = 'thumbnail_ID';
	images = document.getElementsByTagName('img'); 
	for(i=0; i < images.length; i++)
	{ 
		if (images[i].className.substring(0, match.length) == match)
		{ 
			id = images[i].className.substring(match.length, images[i].className.length);
			orignalscaled[id] = Array(images[i].width, images[i].height);
			if (images[i].height > 50 || images[i].width > 50)
			{
				if (images[i].height > images[i].width)
				{
					factor = images[i].height / 50;
					images[i].height = images[i].height / factor;
				}
				else
				{
					factor = images[i].width / 50;
					images[i].width = images[i].width / factor;
				}
			}
		} 
	} 
	return false; 
} 

var openedpictures = Array();
function openpicture(id, url, wname)
{
   opts = 'left=0,top=0,scrollbars=no,status=no,resizable=yes,menubar=no,location=no,directories=no,toolbar=no,dependent=yes,alwaysRaised=yes';
	x = orignalscaled[id][0] + 20;
   opts = opts + ',width=' + x + ',height='+orignalscaled[id][1];
	if ("undefined" != typeof(openedpictures[wname]))
	{
		if (!openedpictures[wname].closed)
		{
   		openedpictures[wname].close();
		}
	}
	openedpictures[wname] = window.open(url, wname, opts)
	openedpictures[wname].focus();
	return false;
}

addEvent(window, "load", fillForm);
addEvent(window, "load", maxImgWidth);
addEvent(window, "load", scale2thumbnail);


