﻿isNS4 = (document.layers) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;

isIE = (document.all) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;

var DHTML = (document.all || document.getElementById || document.layers);

String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/, "" );
}

//// Point

function Point ()
{
	var argv = Point.arguments;
	var argc = argv.length;
	
	this.xcoord = argc >= 1 ? argv[0] : 0;
	this.ycoord = argc >= 2 ? argv[1] : 0;
}

Point.prototype.x = function ()
{
	return (this.xcoord);
}
Point.prototype.y = function ()
{
	return (this.ycoord);
}

//// Layer

function Layer (id)
{
	this.id = id;
	this.object = getObj (id);
	this.clipX = 0;
	this.clipY = 0;
	this.openSteps = 30;
	this.doDrag = false;
}
Layer.prototype.get = function ()
{
	return (this.object);
}
Layer.prototype.hide = function ()
{
	hideObject (this.get ());
}
Layer.prototype.show = function ()
{
	showObject (this.get ());
}
Layer.prototype.slide = function (clipX, clipY)
{
	layer = this.get ();
	
	var incrementX = Math.ceil (layer.offsetWidth / this.openSteps);
	var incrementY = Math.ceil (layer.offsetHeight / this.openSteps);
	
	if (!isVisible (layer))
	{
		this.clipX = 0;
		this.clipY = 0;
		
		// set start-position
		layer.style.clip = "rect(auto, " + this.clipX + "px, " + this.clipY + "px, auto)";
		showObject (layer);
	}
	
	var loop = false;
	if (this.clipX < layer.offsetWidth)
	{
		this.clipX += incrementX;
		loop |= true;
	}
	if (this.clipY < layer.offsetHeight)
	{
		this.clipY += incrementY;
		loop |= true;
	}
	if (loop)
	{
		layer.style.clip = "rect(auto, " + this.clipX + "px, " + this.clipY + "px, auto)";
		var thisObject = this;
		var f = function () { thisObject.slide (); }
		setTimeout (f, 0);
	}
		
}

Layer.prototype.getPosition = function ()
{
	var crop = this.get ();
	
	return (new Point (crop.offsetLeft, crop.offsetTop));
}
// @param	Point where to move to
Layer.prototype.setPosition = function ()
{
	var argv = Point.arguments;
	var argc = argv.length;
	
	var crop = this.get ();
	
	if (argc == 1)
	{
		crop.style.left = argv[0].x ();
		crop.style.top = argv[0].y ();
		
		return (true);
	}
	
	return (false);
}

// @param int	width
// @param int	height
Layer.prototype.getSize = function ()
{
	var crop = this.get ();
	
	return (new Point (crop.offsetWidth, crop.offsetHeight));
}
// @param int	width
// @param int	height
Layer.prototype.setSize = function ()
{
	var argv = Point.arguments;
	var argc = argv.length;
	
	var crop = this.get ();
	
	if (argc == 2)
	{
		crop.style.right = crop.style.left + argv[0];
		crop.style.bottom = crop.style.top + argv[1];
		
		return (true);
	}
	
	return (false);
}

// @param	layer layer to use as bounding box for 'this'
Layer.prototype.setBoundingBox = function (layer)
{
	this.boundingBox = layer;
	
	return (true);
}
// @returns layer layer which acts as bounding box for 'this'
Layer.prototype.getBoundingBox = function ()
{
	return (this.boundingBox);
}

// UNTESTED!!!
Layer.prototype.move = function (e)
{
	if (this.doDrag)
	{
		var layer = this.get ();
		var where = getMouseCoordinates (e);
		var posX = where.x ();
		var posY = where.y () - layer.offsetTop;
		
		// get bounding box within which 'this' moves
		var boundingBox = this.getBoundingBox ();
		
		var cropPosition = this.getPosition ();
		var cropSize = this.getSize ();
		
		posY = parseInt (posY - (cropSize.y () / 2)) ;
		
		if (posY + cropSize.y () > boundingBox.offsetTop + boundingBox.offsetHeight)
		{
			posY = boundingBox.offsetTop + boundingBox.offsetHeight - cropSize.y () - 2;
		}
		else if (posY < boundingBox.offsetTop)
		{
			posY = boundingBox.offsetTop;
		}
					
		posX = parseInt (posX - (cropSize.x () / 2));
		
		if (posX + cropSize.x () > boundingBox.offsetLeft + boundingBox.offsetWidth)
		{
			posX = boundingBox.offsetLeft + boundingBox.offsetWidth - cropSize.x () - 2;
		}
		else if (posX < boundingBox.offsetLeft)
		{
			posX = boundingBox.offsetLeft;
		}
		
		// move layer to new posirion
		this.setPosition (new Point (posX - boundingBox.offsetLeft, posY - boundingBox.offsetTop))
		
		return (true);
	}
	
	return (false);
}







function getObj (name, theDoc)
{
	if (document.layers)
	{
		if (!theDoc)
   		{
    		theDoc = document;
   		}
   		else if (theDoc.layers[name])
   		{
			return (theDoc.layers[name]);
		}
		else
		{
            //repeatedly run through all child layers
            for (var x = 0, y; !y && x < theDoc.layers.length; x++)
            {
                //on success, return that layer, else return nothing
                y = getObj (name, theDoc.layers[x].document);
            }
            return (y);
		}
	}
	else if (document.getElementById)
	{
		return document.getElementById (name);
	}
	else if (document.all)
	{
		return document.all[name];
	}
	return (false);
}

function showObject (obj)
{
	var retval = true;
	
	if (isNS4)
	{
		obj.visibility = "show";
	}
	else if (isNS6 || isIE5)
	{
		obj.style.visibility = "visible";
	}
	else if (isIE4)
	{
		obj.visibility = "visible";
	}
	else
	{
		retval = false;
	}
	
	return (retval);
}

function hideObject (obj)
{
	var retval = true;
	
	if (isNS4)
	{
		obj.visibility = "hide";
	}
	else if (isNS6 || isIE5)
	{
		obj.style.visibility = "hidden";
	}
	else if (isIE4)
	{
		obj.visibility = "hidden";
	}
	else
	{
		retval = false;
	}
	
	return (retval);
}

function isVisible (obj)
{
	if (isNS4)
	{
		return (obj.visibility == "show");
	}
	else if (isNS6 || isIE5)
	{
		return (obj.style.visibility == "visible");
	}
	else if (isIE4)
	{
		return (obj.visibility == "visible");
	}
}

function getDocument (obj)
{
	var retval = true;
	
	if (isNS4)
	{
		return (obj.document);
	}
	else if (isNS6)
	{
		return (obj.contentDocument);
	}
	else if (isIE4 || isIE5)
	{
		return (obj.document);
	}
	else
	{
		retval = false;
	}
	
	return (retval);
}

function replaceHTML (id, html)
{
	var theElement = getObj (id);
	var org_html = theElement.outerHTML;
	var tags = /(<[^>]*>).*?(<[^>]*>)/;
	var result = org_html.match (tags);
	
	theElement.outerHTML = result[1] + html + result[2];
}
function replaceText (id, text)
{
	getObj (id).innerHTML= text;
}
function replaceButtonText (id, text)
{
	getObj (id).innerText = text;
}

function setStatus (str)
{
	window.status = str;
	return (true);
}

// SELECT/OPTION routines
function emptySelect (id)
{
	var sel = getObj (id);

	for (i = sel.length - 1; i >= 0; i--)
	{
		sel.options[i] = null;
	}
}

// 'idx' is 1-indexed (i.e. starting at 1 rather than 0)
function setOption (id, idx)
{
	var sel = getObj (id);
	
	if (sel.length >= idx)
	{
		sel.selectedIndex = idx - 1;
	}
}

function setOptionByValue (id, value)
{
	var sel = getObj (id);

	for (i = sel.length - 1; i >= 0; i--)
	{
		if (sel.options[i].value == value)
		{
			sel.selectedIndex = i;
		}
	}
}

function addOption (id, text, value, selected)
{
	var sel = getObj (id);
	
	sel.options[sel.length] = new Option (text, value);
	if (selected == "true")
	{
		sel.selectedIndex = sel.length - 1;
	}
}

// 'idx' is 1-indexed (i.e. starting at 1 rather than 0)
function insertOption (id, idx, text, value, selected)
{
	var sel = getObj (id);
	
	idx--;
	if (idx && idx < sel.length)
	{
		for (i = sel.length - 1; i >= idx; i--)
		{
			sel.options[i + 1] = sel.options[i];
		}
		sel.options[i] = new Option (text, value);
	}
	
	if (selected)
	{
		sel.selectedIndex = i;
	}
}

// 'idx' is 1-indexed (i.e. starting at 1 rather than 0)
function removeOption (id, idx)
{
	var sel = getObj (id);
	
	idx--;
	if (idx && idx < sel.length)
	{
		sel.options[idx] = null;
	}
}

function removeClass (id, theClass)
{
	var theObj = getObj (id);
    theObj.className = theObj.className.replace (theClass, "").trim ();
}


function addClass (id, theClass)
{
	var theObj = getObj (id);
	
    removeClass (id, theClass);
    theObj.className = (theObj.className + " " + theClass).trim ();
}

function addslashes (str)
{
	str = str.replace (/\|/g, "\\|");
	str = str.replace (/\./g, "\\.");
	str = str.replace (/\$/g, "\\$");
	str = str.replace (/\?/g, "\\?");
	str = str.replace (/\'/g, "\\'");
	str = str.replace (/\"/g, "\\\"");
	str = str.replace (/\(/g, "\\(");
	str = str.replace (/\)/g, "\\)");
	str = str.replace (/\[/g, "\\[");
	str = str.replace (/\]/g, "\\]");
	
	return (str);
}
function stripslashes (str)
{
	str = str.replace (/\\\|/g, "|");
	str = str.replace (/\\\./g, ".");
	str = str.replace (/\\\$/g, "$");
	str = str.replace (/\\\?/g, "?");
	str = str.replace (/\\\'/g, "'");
	str = str.replace (/\\\"/g, "\"");
	str = str.replace (/\\\(/g, "(");
	str = str.replace (/\\\)/g, ")");
	str = str.replace (/\\\[/g, "[");
	str = str.replace (/\\\]/g, "]");
	
	return (str);
}

function urldecode (text)
{
	return (unescape (text).replace (/\+/g, ' '));
}

function getMouseCoordinates (e)
{
	if (!e)
	{
		if (window.event)
		{
			//DOM
			e = window.event;
		}
		else
		{
			//TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
			return (new Point ());
		}
	}
  
	if (typeof (e.pageX) == 'number' )
	{
		//NS 4, NS 6+, Mozilla 0.9+
		var xcoord = e.pageX;
		var ycoord = e.pageY;
	}
	else
	{
		if (typeof (e.clientX) == 'number')
		{
			//IE, Opera, NS 6+, Mozilla 0.9+
			//except that NS 6+ and Mozilla 0.9+ did pageX ...
			var xcoord = e.clientX;
			var ycoord = e.clientY;
			
			if (!((window.navigator.userAgent.indexOf ('Opera') + 1) ||
				(window.ScriptEngine && ScriptEngine ().indexOf ('InScript') + 1) ||
				 window.navigator.vendor == 'KDE'
				)
			   )
			{
				if (document.body && (document.body.scrollLeft || document.body.scrollTop))
				{
					//IE 4, 5 & 6 (in non-standards compliant mode)
					xcoord += document.body.scrollLeft;
					ycoord += document.body.scrollTop;
				}
				else if (document.documentElement &&
						 (document.documentElement.scrollLeft || document.documentElement.scrollTop)
						)
				{
					//IE 6 (in standards compliant mode)
					xcoord += document.documentElement.scrollLeft;
					ycoord += document.documentElement.scrollTop;
				}
			}
		}
		else
		{
			//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE MOUSE COORDINATES
			return (new Point ());
		}
	}
  
	return (new Point (xcoord, ycoord));
}
