DOM = document.getElementById ? true : false;
NS4 = document.layers ? true : false;
IE = document.all ? true : false;

if (!document.getElementById) {
  if (document.all) {
    document.getElementById = function(id) {
      return document.all[id];
    }
  } else {
    document.getElementById = function(id) {
      var o = document.layers[id];
      if (o)
        return o;
      o = document.links[id];
      return o;
    }
  }
}


Array.prototype.clone = function() {
	return [].concat(this);
}
Array.prototype.make = function(from) {
	if (!from || !from.length)
		return [];
	var ret = [];
	for(var i=0, imax = from.length; i<imax; i++)
		ret.push(from[i]);
	return ret;
}
Array.prototype.contains = function(value) {
	for(var i=0, imax = this.length; i<imax && this[i] != value; i++);
	
	return i == imax ? false:i;
}

Function.prototype.setOrigin = function() {
	var _func = this;
	var _args = [].make(arguments);
	var _obj = _args.shift();
	return function() {
		return _func.apply(_obj, _args.concat([].make(arguments)));
	}
}

String.prototype.trim = function() {
  return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.addsep = function(psep,pstr) {
	return this+(this.length ? psep:'')+pstr;
}

String.prototype.urlencode = function()
{
	var ret = null;
	var i = 0;
	
	ret = "";
	for(i=0;i<this.length;$i++) {
		alert(this.charCodeAt(i));
		if (this.charAt(i) > 128) {
			//
		}
	}
}

String.prototype.replaceTokens = function(pwhat,debug) {
	var pos1 = this.indexOf('%',0);
	var pos2 = 0;
	var i = 0;
	var val = null;
	
	if (pos1 == -1)
		return this;

	str = this.slice(pos2,pos1);
	
	while(pos1 != -1 && i<2000) {
		pos2 = this.indexOf('%',pos1+1);
		if (pos2 != -1) {
			vkey = this.slice(pos1+1,pos2);
			if (vkey.substr(0,2) == '==') {
				//alert('#aaa#'.replace(/[#]/g,'pwhat.'));
				//alert('p:'+pwhat.disabled+':'+pwhat.Click);
				//alert('v:'+vkey.substr(2).replace(/[#]/g,'pwhat.'));
				eval('try { val = '+vkey.substr(2).replace(/[#]/g,'pwhat.')+'; } catch(e) { val = vkey; }');
				//alert('r:'+val);
				if (val != val)
					val = null;
			} else if (vkey.match(/^([a-z_][a-z0-9_]+)$/i)) {
				if (debug) alert(vkey);
				eval('try { val = pwhat.'+vkey+'; } catch(e) { val = null; }');
			} else val = null;
			if (val || val == '' || val == 0)
				str += val;
			else
				str += this.slice(pos1,pos2--);
			pos1 = this.indexOf('%',pos2+1);
			if (pos1 != -1)
				str += this.slice(pos2+1,pos1);
			else
			{
				str += this.slice(pos2+1);
			}
		} else {
			str += this.slice(pos1);
			pos1 = -1;
		}
		i++;
	}
	
	return str;
}

String.prototype.replace_props = function(pwhat,debug) {
	return this.replaceTokens(pwhat, debug);
}

String.prototype.replace_params = function(pwhat) {
	return this.replaceTokens(pwhat);
}

function Geometry() {
	this.initialize.apply(this, arguments);
}
Geometry.prototype = {
	initialize: function() {
		var values;
		if (arguments[0] instanceof Array)
			values = arguments[0];
		else if (arguments[0] instanceof Geometry)
			values = arguments[0].toArray(); 
		else
			values = arguments;
	
		this.x1 = odef(values[0], 0);
		this.y1 = odef(values[1], 0);
		this.x2 = odef(values[2], this.x1);
		this.y2 = odef(values[3], this.y1);
		
		this.x = this.x1;
		this.y = this.y1;
	
		this.width = this.x2 - this.x1;
		this.height = this.y2 - this.y1;
	},
	isPoint: function(box) {
		var box = box || this;
		
		return box.width == 0 && box.height == 0;
	},
	isLine: function(box) {
		var box = box || this;
		
		return (box.width == 0 || box.height == 0) && !this.isPoint(box);
	},
	isInside: function(position, box) {
		var box = box || this;
		return box.x1 <= position.x && box.x2 >= position.x && box.y1 <= position.y && box.y2 >= position.y2
	},
	getHorizontalOrientation: function(position, box) {
		var box = box || this;
		if ((position.x - box.x1) < (box.x2 - position.x))
			return -1; 
		if ((position.x - box.x1) > (box.x2 - position.x))
			return 1;
		return 0;
	},
	getVerticalOrientation: function(position, box) {
		var box = box || this;
		if ((position.y - box.y1) < (box.y2 - position.y))
			return -1; 
		if ((position.y - box.y1) > (box.y2 - position.y))
			return 1;
		return 0;
	},
	toArray: function() {
		return [this.x1, this.y1, this.x2, this.y2, this.width, this.height];
	}
}

function MyEvent() {
}
MyEvent.prototype.getTarget = function(e)
{
	var ret;
	if (!e)
		var e = window.event;
	if (e.target) 
		ret = e.target;
	else if (e.srcElement) 
		ret = e.srcElement;
	if (ret.nodeType == 3) // Safari fix
		ret = ret.parentNode;
		
	return ret;
}
MyEvent.prototype.getPressedKey = function(e) {
	var ret;
	if (!e) 
		var e = window.event;
	if (e.keyCode) 
		ret = e.keyCode;
	else if (e.which) 
		ret = e.which;
	else if (e.button)
		ret = e.button;
	
	//return String.fromCharCode(ret);
	return ret;
}
MyEvent.prototype.getMouseX = function(e) {
	if (!e) 
		var e = window.event;
	return e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
}
MyEvent.prototype.getMouseY = function(e) {
	if (!e) 
		var e = window.event;
	return e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
}
MyEvent.prototype.getMousePosition = function(e) {
	if (!e) 
		var e = window.event;
	return new Geometry(this.getMouseX(e), this.getMouseY(e));
}
MyEvent.prototype.stop = function(e) {
	if (!e) 
		var e = window.event;
	
	if (e.stopPropagation)
		e.stopPropagation();
	else
		e.cancelBubble = true;
}
MyEvent.prototype.cancel = function(e) {
	if (!e) 
		var e = window.event;
	
	if (e.preventDefault)
		e.preventDefault();
	else
		e.returnValue = false;
}


function Html()
{
	// Browser check
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5') != -1);
	this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
	this.isMSIE6 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 6') != -1);
	this.isMSIE6_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 6.0') != -1);
	this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
	this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
	this.isMac = navigator.userAgent.indexOf('Mac') != -1;
}
Html.prototype.getElementById = function(obj) {
	//alert('Typeof:'+typeof(obj));
	if (typeof(obj) == 'string')
		var o = document.getElementById(obj);
	else if (typeof(obj) == 'object')
		var o = obj;
	else
		return false;

	//alert(o);
	if (!o)
		return false;

	return o;
}
Html.prototype.findElementFn = function(obj, iterator, fn) {
	var o = this.getElementById(obj);
	var result = null;
	var maxrun = 100000;
	var abort = false;
	do {
		abort = fn(o);
		if (!abort)
			o = iterator(o);
	} while(!abort && o && maxrun--);
	
	if (abort)
		result = o;
	return result;
}
Html.prototype.findElementsFn = function(obj, iterator, fn) {
	var o = this.getElementById(obj);
	var result = new Array();
	var maxrun = 100000;
	var abort = false;
	do {
		abort = fn(o);
		if (abort)
			result[result.length()] = o;
		o = iterator(o);
	} while(o && maxrun--);
	
	return result;
}
Html.prototype.iterate = function(obj, property, callback) {
	var o = this.getElementById(obj);
	
	while(o && callback(o))
		o = o[property];
}
Html.prototype.addEvent = function(obj, name, handler, capture) {
	var o = this.getElementById(obj);
	if (!o)
		return false;
	capture = capture || false;
		
	if (this.isMSIE)
		o.attachEvent("on" + name, handler);
	else
		o.addEventListener(name, handler, capture);
	
	//alert(o);
}
Html.prototype.hasAttrib = function(obj, name) {
	var o = this.getElementById(obj);
	if (!o)
		return false;

	if (o.hasAttribute)
		return o.hasAttribute(name);
	else
		return o[name];
}
Html.prototype.getAttrib = function(obj, name) {
	var o = this.getElementById(obj);
	if (!o)
		return false;

	return o.getAttribute(name);
}
Html.prototype.setAttrib = function(obj, name, value) {
	var o = this.getElementById(obj);
	if (!o)
		return false;

	//alert(name+':'+value);
	
	o.setAttribute(name, value);
	return true;
}
Html.prototype.getClass = function(obj) {
	var o = document.getElementById(obj);
	if (!o)
		return false;
		
	return o.className;
}
Html.prototype.setClass = function(obj, value) {
	var o = this.getElementById(obj);
	if (!o)
		return false;
	
	//alert(name+':'+value);
	o.className = value;
	return true;
}
Html.prototype.getStyle = function(obj, name) {
	var o = this.getElementById(obj);
	if (!o)
		return false;
	
	var st = o.style[name];
	if (!st) {
		if (document.defaultView && document.defaultView.getComputedStyle) {
			var css = document.defaultView.getComputedStyle(o, null);
			st = css ? css[name] : null;
		} else if (o.currentStyle) {
			st = o.currentStyle[name];
		}
	}
	if (['top','left','width','height'].contains(name)!==false && st == 'auto')
		st = '0px';
	
	return st;
}
Html.prototype.setStyle = function(obj, name, value)
{
	var o = this.getElementById(obj);
	if (!o)
		return false;
	
	//alert(name+':'+value);
	if (name == 'opacity' && this.isMSIE) {
		name = 'filter';
		value = 'alpha(opacity='+(value*100)+')';
	}
	o.style[name] = value;
	return true;
}
Html.prototype.setDisplay = function(obj, state, mode)
{
	return this.setStyle(obj, 'display', state ? odef(mode, "block") : "none");
}
Html.prototype.setVisibility = function(obj, state)
{
	return this.setStyle(obj, 'visibility', state ? "visible" : "hidden");
}
Html.prototype.getOffset = function(obj,uobj)
{
	if (!obj || !obj.offsetParent)
		return null;
	objLeft		= obj.offsetLeft;
	objTop		= obj.offsetTop;
	objParent	= obj.offsetParent;
	while(objParent && (!uobj || objParent != uobj)) {
		objLeft		+= objParent.offsetLeft-objParent.scrollLeft;
		objTop		+= objParent.offsetTop-objParent.scrollTop;
		objParent	 = objParent.offsetParent;
	}
	return new Geometry(objLeft, objTop);
}
Html.prototype.getBoundingBox = function(obj) {
	var o = this.getElementById(obj);
	var pos = this.getOffset(o);
	
	return new Geometry(pos.x, pos.y, pos.x+o.offsetWidth, pos.y+o.offsetHeight);
}
Html.prototype.isInside = function(position, obj) {
	var dim = this.getBoundingBox(obj);
	
	return dim.isInside(position);
}
Html.prototype.setOffset = function(obj, offset)
{
	var o = this.getElementById(obj);
	if (!o)
		return false;

	if (offset instanceof Array)
		offset = new Geometry(offset);
	
	o.posx = offset.x;
	o.posy = offset.y;
		
	this.setStyle(o, 'left', o.posx+'px');
	this.setStyle(o, 'top', o.posy+'px');
}
Html.prototype.getDocumentElement = function(obj)
{
	if (Html.isMSIE6)
		return obj.documentElement;
	else
		return obj.body;
}
Html.prototype.getScrollParams = function(obj)
{
	var o = this.getElementById(obj);
	
	var ret = new Object();
	
	ret.left = o.scrollLeft;
	ret.top = o.scrollTop;
	ret.height = o.scrollHeight;
	ret.width = o.scrollWidth;
	
	return ret;
}

Html.prototype.getWindowInnerSize = function()
{
	var ret = new Object();
	
	if (Html.isMSIE6)
	{
		ret.width = document.documentElement.clientWidth;
		ret.height = document.documentElement.clientHeight;
	} else if (Html.isMSIE)
	{
		ret.width = document.body.clientWidht;
		ret.height = document.body.clientHeight;
	} else
	{
		ret.width = window.innerWidth;
		ret.height = window.innerHeight; 
	}
	
	return ret;
}
Html.prototype.getFlashMovie = function(flashname)
{
/*  if (window.document[flashname]) 
  {
      return window.document[flashname];
  }*/
  if (!Html.isMSIE && !Html.isSafari)
  {
    if (document.embeds && document.embeds[flashname])
      return document.embeds[flashname]; 
  }
  else
  {
    return document.getElementById(flashname);
  }
}

Html.prototype.replaceInner = function(obj, data) {
	var o = this.getElementById(obj);

	if (o.innerHTML)
		o.innerHTML = data;	
}

Html.prototype.addBookmark = function(url, caption, balert) {
    if (Html.isMSIE) {
        window.external.AddFavorite(url, caption);
    } else if (typeof window.opera!="undefined") {
        alert(balert.replace(/%keyshortcut%/, '[Ctrl] + [T]'));
    } else {
        alert(balert.replace(/%keyshortcut%/, '[Ctrl] + [D]'));
    }
}

function odef(obj,def)
{
	var ret=null;
	if (!def && def != '' && def != 0)
		def = null;
	if (obj || obj==0)
		ret = obj;
	else
		ret = def;
	return ret;
}

SetVisibility = function(id, show) {
  var o = document.getElementById(id);
  if (!o)
    return false;
  var s = !o.style ? o : o.style;
  s.visibility = show ? (!o.style ? "show" : "visible") : (!o.style ? "hide" : "hidden");
  return true;
}

SetDisplay = function(id, show) {
  var o = document.getElementById(id);
  if (!o)
    return false;
  var s = !o.style ? o : o.style;
  s.display = show ? (!o.style ? "block" : (IE ? "block":"")) : (!o.style ? "none" : "none");
  return true;
}

function GetOffset(obj,uobj)
{
	return Html.getOffset(obj, uobj);
}

// Attention!
//   if you move an absolute div, set the posx, posy property to reflect the new position

SelectRemover.selects = Array();
SelectRemover.objects = Array();
SelectRemover.process = function()
{
	if (!Html.isMSIE)
		return true;
		
	//alert('Processing selects!');
	sdb = 0;
	ddb = 0;
	for(i in this.selects)
	{
		var item = this.selects[i];
		
		var conflict = false;
 		var robj = item.refobj;
 		
 		if (robj && robj.style.visiblity == 'hidden')
 			continue;

		itemw = item.offsetWidth;
		itemh = item.offsetHeight;
		if (robj)
		{
			item.posx = item.origx + robj.posx;
			item.posy = item.origy + robj.posy;
			if ((robj.posx + robj.offsetWidth) < (item.posx + itemw))
				itemw = robj.offsetWidth - item.posx;
			if ((robj.posy + robj.offsetHeight) < (item.posy + itemh))
				itemh = robj.offsetHeight - item.posy;
		} else
		{
			item.posx = item.origx;
			item.posy = item.origy;
		}	
		sdb++;

		//s = 'Select: '+item.id+' X:'+item.posx+' Y:'+item.posy+' W:'+itemw+' H:'+itemh+'\n';

		item.overobj = Array();
		for(d in this.objects)
		{
			ddb++;
			div = this.objects[d];
			//s += ' Area: '+div.id+' X:'+div.posx+' Y:'+div.posy+'\n';
			if (div == robj)
				continue
			if (div.style.visibility == 'hidden')
				continue;
			if (conflict = !((item.posx > div.posx+div.offsetWidth || div.posx > item.posx+itemw) || (item.posy > div.posy+div.offsetHeight || div.posy > item.posy+itemh) || (robj && (robj == div || robj.style.zIndex > div.style.zIndex))))
				break;
		}
		item.style.visibility = (conflict ? 'hidden':'');
		
		//alert(s);
	}
}

function SelectRemover()
{
	//alert('SelectRemover Initializing...');

	if (!Html.isMSIE)
		return true;

	SelectRemover.selects = Array();
	sels = document.all.tags('SELECT');
	for(i=0; i<sels.length; i++)
	{
		// find container absolute div if exist...
		var robj = sels[i].offsetParent;
		//alert(sels[i].name);
		while(robj && (!robj.style || (robj.style.position != 'absolute')))
		{
			robj = robj.offsetParent;
		}
		//if (robj) alert(robj.id);
		if (robj)
		{
			p = Html.getOffset(sels[i],robj);
			sels[i].origx = p.x;
			sels[i].origy = p.y;
		} else
		{
			p = Html.getOffset(sels[i]);
			sels[i].origx = p.x;
			sels[i].origy = p.y;
		}
		sels[i].refobj = robj;
		SelectRemover.selects[i] = sels[i];
	}
	SelectRemover.objects = Array();
	divs = document.all.tags('DIV');
	for(i=0; i<divs.length; i++)
	{
		if (divs[i].style.position == 'absolute')
		{
			p = Html.getOffset(divs[i]);
			divs[i].posx = p.x;
			divs[i].posy = p.y;
			SelectRemover.objects[i] = divs[i];
		}
	}
}

function Debug() {
	this.objects = new Array();
}
Debug.prototype.clearObjects = function() {
	this.object = new Array();
}
Debug.prototype.parseObject = function(obj, name, pad, level) {
	if (!pad)
		pad = '';
	if (!level)
		level = 0;
		
	str = pad;
	if (name)
		str += name+': ';

	if (level > 1)
		return str+" recursion limit reached\n";

	switch(typeof(obj)) {
		case 'string':
			str += "'"+obj.toString()+"'\n";
			break;
		case 'number':
			str += obj.toString()+"\n";
			break;
		case 'object':
			if (obj instanceof Date) {
				str += obj.toString();
			} else if (obj instanceof Array) {
				str += obj.toString()+" [\n";
				for(i in obj) {
					str += this.parseObject(obj[i], i, pad+'    ', level+1);
				}
				str += pad+"]\n";
			} else if (obj instanceof Object) {
				str += obj.toString()+" {\n";
				for(p in obj) {
					if (!(obj[p] instanceof Function)) {
						if (obj[p])
							str += this.parseObject(obj[p].toString(), p, pad+'    ', level+1);
					} else
						str += pad+'    '+p+" : method\n";
				}
				str += pad+"}\n";
			}
			break;
	}
	return str;
}
Debug.prototype.alertObject = function(obj, name) {
	d = new Date();
	
	this.objects[this.objects.length] = d.toDateString()+' '+d.toTimeString()+' '+this.parseObject(obj, name);

	oDiv = Html.getElementById('JSDebug');
	if (!oDiv) {	
		dobj = Html.getDocumentElement(document);
		//alert(dobj);
		
		oDiv = document.createElement("DIV");
		oDiv.name = 'JSDebug';
		oDiv.id = 'JSDebug';
	
		oDiv.style['position'] = 'absolute';
		oDiv.style['top'] = 0;
		oDiv.style['left'] = 0;
		oDiv.style['width'] = '700px';
		oDiv.style['height'] = 'auto';
		oDiv.style['backgroundColor'] = '#f0f0f0';
		oDiv.style['borderWidth'] = '1px';
		oDiv.style['borderStyle'] = 'solid';
		oDiv.style['borderColor'] = '#000000';
		dobj.appendChild(oDiv);
	}
	outHTML = "<div style=\"height: 22px; padding: 2px; background: #dddddd;\">Javascript Debug<div style=\"float: right;\"><a href=\"#\" onClick=\"Html.setDisplay('JSDebug', 0); return false;\">Close</a></div></div>\n<div style=\"height: 350px; padding: 2px; overflow: auto;\">";
	for(dt in this.objects)
		outHTML += this.objects[dt].replace(/\n/g, '<br\>').replace(/ /g, '&nbsp;')+"<br/><br/>";
	outHTML += "</div>";
	
	oDiv.innerHTML = outHTML;
	Html.setDisplay('JSDebug', 1);
}

function ContentControl()
{
}

ContentControl.prototype.resizeWindowToContent = function()
{
	dd = new Object();
	
	dd.width = document.body.offsetWidth;
	dd.height = document.body.offsetHeight;

	// FIXME: we need to exactly now the value of the margin becouse it is not included in the result.
	dd.width += 20; // margin quick hack
	dd.height += 20; 

	
	dobj = Html.getDocumentElement(document);
	
	wd = Html.getWindowInnerSize();
	sd = Html.getScrollParams(dobj);

	//alert(dd.width+','+dd.height+' : '+wd.width+','+wd.height+' : '+sd.width+','+sd.height);
	if (wd.height < dd.height)
		window.resizeBy(dd.width-wd.width, dd.height-wd.height);
}


var MyEvent = new MyEvent();
var Html = new Html();
var CC =  new ContentControl()
var Debug = new Debug();

Html.addEvent(window, 'load', SelectRemover);

var cmni_nocache = Math.random()*100000;
var loaded = false;

