Array.prototype.findThis = function(what) {
	for(var i=0; i < this.length; i++) if (this[i] == what) return true;
	return false;
}
window.addHandler = function(handler, code) {
	if (window.addEventListener) {
		window.addEventListener(handler, code, false);
	} else if (window.attachEvent) {
		window.attachEvent('on' + handler, code);
	} else if (!this.findHandler(handler, code)) {
		this.customHandlers.push(new Array(handler, code));
	}
}
window.removeHandler = function(handler, code) {
	if (window.removeEventListener) {
		window.removeEventListener(handler, code, false);
	} else if (window.detachEvent) {
		window.detachEvent('on' + handler, code);
	} else if (!this.findHandler(handler, code)) {
		for(var i=0; i < this.customHandlers.length; i++)
			if (this.customHandlers[i][0] == handler)
				this.customHandlers[i][1]();
	}
}
if (!window.addEventListener && !window.attachEvent) {
	window.customHandlers = new Array();
	window.findHandler = function(handler, code) {
		for(var i=0; i < this.customHandlers.length; i++) {
			if ((this.customHandlers[i][0] == handler) && ((typeof code == 'undefined') || (this.customHandlers[i][1] == code))) return true;
		}
		return false;
	}
	window.callHandlers = function(handler) {
		for(var i=0; i < this.customHandlers.length; i++)
			if (this.customHandlers[i][0] == handler)
				this.customHandlers[i][1]();
	}
	window.addHandler('load', function(){
		var phd = new Array('load');
		alert(window.customHandlers.length);
		for(var i=0; i < window.customHandlers.length; i++) {
			alert(window.customHandlers[i]);
			if (!phd.findThis(window.customHandlers[i][0])) {
				eval('window.on' + window.customHandlers[i][0] + '=function(){this.callHandlers(\''+window.customHandlers[i][0]+'\')};');
			}
		}
	});
	window.onload = function(){this.callHandlers('load')};
}


function openWindow(url,name,width,height) {
	var properties = new Array();
	if(typeof(width) != 'undefined') properties.push('width:' + width);
	if(typeof(height) != 'undefined') properties.push('height:' + height);
	window.open(url,typeof(name)!='undefined'?name:'newWindow',properties.join(';'));
}
function showDocumentProperties() {
	document.getElementById('documentProperties').style.display = 'block';
	window.justOpenedProperties = true;
	centerDocumentProperties();
	window.addHandler('scroll', centerDocumentProperties);
}
function centerDocumentProperties(forced) {
	var obj = document.getElementById('documentProperties');
	if (obj && document.body && document.documentElement) {
		obj.style.left = Math.round((document.body.clientWidth/2)-(obj.clientWidth/2)) + 'px';
		if (window.justOpenedProperties) {
			obj.style.top = document.documentElement.scrollTop + Math.round((document.documentElement.clientHeight/2)-(obj.clientHeight/2)) + 'px';
			window.justOpenedProperties = false;
		} else {
			if (!window.moveTimer)
				moveDocumentProperties();
		}
	}
}

function hideDocumentProperties() {
	document.getElementById('documentProperties').style.display='none';
	window.removeHandler('scroll', centerDocumentProperties);
}

function moveDocumentProperties() {
	var obj = document.getElementById('documentProperties');
	var newPos = document.documentElement.scrollTop + Math.round((document.documentElement.clientHeight/2)-(obj.clientHeight/2))
	var distance = newPos - obj.offsetTop;
	var moveDir = (distance > 0)?getSpeed(distance):0-getSpeed(distance);
	obj.style.top = (obj.offsetTop + moveDir) + 'px';
	if (((moveDir > 0) && (obj.offsetTop >= newPos)) || ((moveDir < 0) && (obj.offsetTop <= newPos))) {
		window.moveTimer = false;
	} else {
		window.moveTimer = setTimeout('moveDocumentProperties();',20);
	}
}
function getSpeed(dist) {
	return Math.ceil(Math.abs(dist) / 8);
}

//
// Add some useful functions to javascript objects
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
Array.prototype.find = function(needle) {
	var i;
	for(i=0;i<this.length;i++)if(this[i]==needle)return i;
	return false;
}
function getClasses(obj) {
	return (obj&&obj.className)?obj.className.split(' '):new Array();
}
function hasClass(obj, className) {
	return getClasses(obj).find(className)!=false;
}
function replaceClass(obj, oldClass, newClass, appendWhenNotFound) {
	if(!obj||!obj.className)return;
	var classes = getClasses(obj);
	var index = classes.find(oldClass);
	if (index != false) {
		classes[index] = newClass;
	} else if (appendWhenNotFound) {
		classes.push(newClass);
	}
	obj.className = classes.join(' ');
}

function centerMediaPlayer(forced) {
	var obj = document.getElementById('mediaPlayerControls');
	if (obj && document.body && document.documentElement) {
		obj.style.left = '0px';
		if (window.justOpenedMediaPlayer) {
			obj.style.border = "1px solid black";
			obj.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight - obj.offsetHeight - 25 ) + 'px';
			window.justOpenedMediaPlayer = false;
		} else {
			if (!window.moveMediaPlayerTimer)
				moveMediaPlayer();
		}
	}
}

function moveMediaPlayer() {
	var obj = document.getElementById('mediaPlayerControls');
//	var newPos = document.documentElement.scrollTop + Math.round((document.documentElement.clientHeight/2)-(obj.clientHeight/2))
	var newPos = document.documentElement.scrollTop + document.documentElement.clientHeight - obj.offsetHeight - 25;
	var distance = newPos - obj.offsetTop;
	var moveDir = (distance > 0)?getMPSpeed(distance):0-getMPSpeed(distance);
	obj.style.top = (obj.offsetTop + moveDir) + 'px';
	if (((moveDir > 0) && (obj.offsetTop >= newPos)) || ((moveDir < 0) && (obj.offsetTop <= newPos))) {
		window.moveMediaPlayerTimer = false;
	} else {
		window.moveMediaPlayerTimer = setTimeout('moveMediaPlayer();',20);
	}
}
function getMPSpeed(dist) {
	return Math.ceil(Math.abs(dist) / 4);
}
function validateEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at) == -1) return false;
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) return false;
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) return false;
	if (str.indexOf(at,(lat+1)) != -1) return false;
	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) return false;
	if (str.indexOf(dot,(lat+2)) == -1) return false;
	if (str.indexOf(" ") != -1) return false;
	return true;			
}

