/* vkDom.js */

function	vkDomClass()
{
	this.loadCallbacks = new Array();
	this.unloadCallbacks = new Array();
	this.loadFirst = true;
	this.unloadFirst = true;
	this.loaded = false;
	this.loadIE = null;
}



/**************************************************************/
/* vkDom.onLoad() handling */

vkDomClass.prototype.onLoad = function(callback)
{
	// Simply queues the callback	
	this.loadCallbacks[this.loadCallbacks.length] = callback;

	// If this is the very first time we're called, fire up the events, according to the browser
	if(this.loadFirst)
	{
		// Intercept and remember IE which requires a very special handler
		var	ua = navigator.userAgent.toLowerCase();

		this.loadIE = /msie/.test(ua) && !/opera/.test(ua);

		// Deal with mozilla & friend.
		// But avoid browsers implementing document.readyState
		if(!document['readyState'] && document.addEventListener)
			document.addEventListener('DOMContentLoaded', function() { vkDom._setLoaded() }, false);
		else if(this.loadIE)	
		{
			// Thanks to:
			// http://blog.outofhanwell.com/2006/06/08/the-windowonload-problem-revisited/

			// document.write('<scr'+'ipt id="__vkDom_onLoad" defer="true" src="//[]"></scr'+'ipt>');
			// 14/05/2008
			// "[]" caused the browser to do a DNS lookup, try a fake IP
			document.write('<scr'+'ipt id="__vkDom_onLoad" defer="true" src="//0.0.0.0"></scr'+'ipt>');


			var	trickScript = document.getElementById('__vkDom_onLoad');

			if(trickScript)
			{
				trickScript.onreadystatechange = function()
				{
					if(this.readyState == 'complete')
						vkDom._setLoaded();
				}

				// Test it right now
				trickScript.onreadystatechange();

				trickScript = null;	// ie leaks?
			}
		}
		// Other browser will use readyState or getElementsByTagName('body') which worked fine till now...

		this.loadFirst = false;
	}

	if(this.loadCallbacks.length == 1)
		setTimeout('vkDom._wait4dom()', 75);	
}


// FRIEND
vkDomClass.prototype._setLoaded = function()
{
	this.loaded = true;
}

// PRIVATE
vkDomClass.prototype._wait4dom = function()
{
	if(!this.loaded)
	{
		if(document['readyState'])
		{
			if(	document.readyState == 'loaded' ||
				document.readyState == 'complete' )
				this.loaded = true;
		}
		else if(!document.addEventListener && !this.loadIE)	// Use old technique for non-mozilla & non-ie browsers
		{
			if(	
				//document.body != null ||
				(document.getElementsByTagName('body') != null &&
				document.getElementsByTagName('body')[0] != null) 
			)
				this.loaded = true;
		}
	}

	if(this.loaded)
	{
		var	i;

		for(i = 0; i < this.loadCallbacks.length; i++)
			this.loadCallbacks[i]();

		// Resets the list
		this.loadCallbacks = new Array();
	}
	else
		setTimeout('vkDom._wait4dom()', 75);
}


/*---------------------*/

/* This previous version failed miserably with IE

vkDomClass.prototype.loaded = function()
{
	if(	
		document.body != null ||
		(document.getElementsByTagName('body') != null &&
		document.getElementsByTagName('body')[0] != null) 
	)
		return true;

	return false;
}


vkDomClass.prototype.onLoad = function(callback)
{
	this.loadCallbacks[this.loadCallbacks.length] = callback;
	if(this.loadCallbacks.length == 1)
		setTimeout('vkDom.wait4dom()', 75);	
}

vkDomClass.prototype.wait4dom = function()
{
	if(this.loaded())
	{
		var	i;

		for(i = 0; i < this.loadCallbacks.length; i++)
			this.loadCallbacks[i]();

		// Resets the list
		this.loadCallbacks = new Array();
	}
	else
		setTimeout('vkDom.wait4dom()', 75);	
}


*/


/**************************************************************/
/* vkDom.onUnload() handling */

vkDomClass.prototype.onUnload = function(callback)
{
	this.unloadCallbacks[this.unloadCallbacks.length] = callback;

	if(this.unloadFirst)
	{
		/*
		if(document.addEventListener)
			document.addEventListener('unload', function() { vkDom._onUnload(); }, false);
		}
		else */
		
		if(window.attachEvent)
			window.attachEvent('onunload', function() { vkDom._onUnload(); });
		else
			window.onunload = function() { vkDom._onUnload(); };

		this.unloadFirst = false;
	}
}

// FRIEND
vkDomClass.prototype._onUnload = function()
{
	var	i;

	for(i = 0; i < this.unloadCallbacks.length; i++)
		this.unloadCallbacks[i]();

	// Resets the list
	this.unloadCallbacks = new Array();
}








// Usefull tool

vkDomClass.prototype.html = function(str)
{
	str = ''+str;
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/\"/g, '&quot;');
	str = str.replace(/</g, '&lt;');
	return str.replace(/>/g, '&gt;');
}

vkDomClass.prototype.nl2br = function(str)
{
	str = ''+str;
	return str.replace(/\n/g, '<br />');
}

vkDomClass.prototype.htmlbr = function(str)
{
	return this.nl2br(this.html(str));
}


vkDomClass.prototype.js = function(str)
{
	str = ''+str;
	str = str.replace(/\'/g, '\\\'');
	// TODO: linebreaks and so on...
	return str;
}

vkDomClass.prototype.uri = function(str)
{
	return encodeURIComponent(str);
}


vkDomClass.prototype.el = function(id)
{
	return document.getElementById(id);
}	

vkDomClass.prototype.show = function(id)
{
	if(arguments.length == 2 && !arguments[1])
	{
		this.hide(id);
		return;
	}

	switch(this.el(id).tagName.toUpperCase())
	{
		case 'DIV':
			this.el(id).style.display = 'block';
			break;
		case 'BUTTON':
			this.el(id).style.visibility = 'visible';
			break;
	}
}

vkDomClass.prototype.hide = function(id)
{
	switch(this.el(id).tagName.toUpperCase())
	{
		case 'DIV':
			this.el(id).style.display = 'none';
			break;
		case 'BUTTON':
			this.el(id).style.visibility = 'hidden';
			break;
	}
}

// 08/11/2007 - display(bool) and visibility(bool) support

/* Pas le temps, le faire à un autre moment:!
vkDomClass.prototype.display = function(id)
{
	var	el = this.el(id);

	if(!el)
		return;

	if(arguments.length == 2 && !arguments[1])
	{
		// - Hide
		el.style.display = '';
	}
	else
	{
		// - Show
		switch(el.tagName.toUpperCase())
		{
			// Default = block elements
			// Intercept inline elements!



			case 'DIV':
			case 'BUTTON':
			// TODO: FINISH THIS LIST
				el.style.display = 'block';

			// (display = 'inline' for inline elements, and so on)...

		}

	}
}
*/

vkDomClass.prototype.visibility = function(id)
{
	var	el = this.el(id);

	if(!el)
		return;

	if(arguments.length == 2 && !arguments[1])
		el.style.visibility = 'hidden';
	else
		el.style.visibility = 'visible';
}






vkDomClass.prototype.value = function(id)
{
	return this.el(id).value;
}



vkDomClass.prototype.enable = function(id)
{
	this.el(id).disabled = false;
}

vkDomClass.prototype.disable = function(id)
{
	this.el(id).disabled = true;
}

vkDomClass.prototype.focus = function(id)
{
	if(!this.el(id).disabled)
		this.el(id).focus();
}

vkDomClass.prototype.select = function(id)
{
	this.el(id).select();
}


vkDomClass.prototype.setHtml = function(id, html)
{
	this.el(id).innerHTML = html;
}

vkDomClass.prototype.setText = function(id, text)
{
	this.el(id).innerHTML = this.html(text);
}

vkDomClass.prototype.setTextBr = function(id, text)
{
	this.el(id).innerHTML = this.htmlbr(text);
}


vkDomClass.prototype.getAbsolutePosition = function(element)
{
	if(	typeof(element) == 'string' && 
		!(element = vkDom.el(element))
	)
		return { x: 0, y : 0 };

	var r = { x: element.offsetLeft - element.scrollLeft, y: element.offsetTop - element.scrollTop };

	if(element.offsetParent) 
	{
		var tmp = this.getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
}


// Cookies related

vkDomClass.prototype.setCookie = function(name, value, seconds, path, domain)
{
	var	str;

	if(seconds)
	{
		var	expiryDate = new Date();

		expiryDate.setTime(
			expiryDate.getTime() + seconds * 1000
		);

		str = name + '=' + escape(value) + '; expires='+expiryDate.toGMTString();
	}
	else
		str = name + '=' + escape(value);
	
	if(path)
		str += '; path='+path;

	if(domain)
		str += '; domain='+domain;

	document.cookie = str;
}


vkDomClass.prototype.setSessionCookie = function(name, value, path, domain)
{
	this.setCookie(name, value, 0, path, domain);
}

vkDomClass.prototype.getCookie = function(name, value)
{
	var	i, search, parts;
	
	search = name+'=';
	parts = document.cookie.split('; ');

	for(i = 0; i < parts.length; i++)
	{
		if(parts[i].indexOf(search) == 0)
			return unescape(parts[i].substring(search.length));
	}

	return null;
}


vkDomClass.prototype.removeCookie = function(name, path, domain)
{
	this.setCookie(name, '', -1, path, domain);
}




/*
vkDomClass.prototype.getAbsoluteScrollPosition = function(element)
{
	if(	typeof(element) == 'string' && 
		!(element = vkDom.el(element))
	)
		return { x: 0, y : 0 };

	var r = { x: element.offsetLeft, y: element.offsetTop };

	if(element.offsetParent) 
	{
		var tmp = this.getAbsoluteScrollPosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
}
*/


var	vkDom = new vkDomClass();
