var CurrentMousePostion = [];

if (!window.console) console = {};
console.log = console.log || function(){};
console.warn = console.warn || function(){};
console.error = console.error || function(){};
console.info = console.info || function(){};

	 // ---------------------------------------------
	 //         Names
	 // ---------------------------------------------
	 
function FormatName(Title, Name)
{
	if (Title[0] == '.')
	{
		return Name + Title.substring(1);
	}
	else
	{
		return Title + " " + Name;
	}
}

function MakeStringHarmless(String)
{
	return String.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\\/g, "&#92;").replace(/\//g, "&#47;").replace(/\[/g, "&#91;").replace(/\]/g, "&#93;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

function MakeURLHarmless(String)
{
	return String.replace(/ /g, "%20").replace(/#/g, "%23").replace(/&/g, "%26").replace(/\+/g, "%2B");
}


	 // ---------------------------------------------
	 //         Images
	 // ---------------------------------------------
 
function ShowZoomImage(ImageName)
{
	var RenderImageSizeX = document.documentElement.clientWidth;
	var RenderImageSizeY = document.documentElement.clientHeight;
	
	RenderImageSizeX = Math.floor((RenderImageSizeX - 100) / 50) * 50;
	RenderImageSizeY = Math.floor((RenderImageSizeY - 100) / 50) * 50;
	
	ThisElement = document.getElementById("Overlay");
	
	var newContent = "";
	
	newContent += '<img id="OverlayImage" src=":image:' + ImageName + ':maxsize.' +
				RenderImageSizeX + '.' + RenderImageSizeY + '" />';
				
	ThisElement.innerHTML = newContent;
	
	ThisElement.className = "Show";
}

function HideOverlay()
{
	ThisElement = document.getElementById("Overlay");
	ThisElement.className = "";
}

	 // ---------------------------------------------
	 //         Add loading events
	 // ---------------------------------------------

var __PageOnKeyFunctions = [];
var __PageOnMouseUpFunctions = [];

function AddMouseUpEvent(func)
{
    if (typeof document.onmouseup != 'function')
    {
        document.onmouseup = function(e)
		{
			var e = e || window.event;
			
			for (I = 0; I < __PageOnMouseUpFunctions.length; I++)
			{
				__PageOnMouseUpFunctions[I](e);
			}
		}
    }
      
	__PageOnMouseUpFunctions.push(func);
}

function AddKeyEvent(func)
{
    if (typeof document.onkeypress != 'function')
    {
        document.onkeypress = function(e)
		{
			var e = e || window.event;
	
			for (I = 0; I < __PageOnKeyFunctions.length; I++)
			{
				__PageOnKeyFunctions[I](e);
			}
		}
    }
      
	__PageOnKeyFunctions.push(func);
}

	 // ---------------------------------------------
	 //			Mouse
	 // ---------------------------------------------
	 
var ClientMouseX;
var ClientMouseY;
	 
document.onmousemove = function(e)
{
	var e = e || window.event;
	ClientMouseX=e.clientX;
	ClientMouseY=e.clientY;
}

	 // ---------------------------------------------
	 //			Array
	 // ---------------------------------------------

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

	 // ---------------------------------------------
	 //			HTML
	 // ---------------------------------------------

function positionOnScreen(obj)
{
	var curleft = curtop = 0;
	
	if (obj.offsetParent)
	{
		do {
			curleft += obj.offsetLeft - obj.scrollLeft;
			curtop += obj.offsetTop - obj.scrollTop;
			} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}

function getAllChildren(HTMLObject)
{
	var listOfChildNodes = HTMLObject.childNodes;
	var allNodes = new Array();
	
	for (i = 0; i < listOfChildNodes.length; i++)
		allNodes[i] = listOfChildNodes[i];
	
	for (i = 0; i < allNodes.length; i++)
	{							
		listOfChildNodes = allNodes[i].childNodes;
		for (j = 0; j < listOfChildNodes.length; j++)
			allNodes.push(listOfChildNodes[j]);
	}
	
	return allNodes;
}

function getElementsByClass(HTMLObject, Class)
{
	var Elements = getAllChildren(HTMLObject);
	var allNodes = new Array();
	
	for (i = 0; i < Elements.length; i++)
	{
		if (Elements[i].className && Elements[i].className == Class)
		{
			allNodes.push(Elements[i]);
		}
	}
	
	return allNodes;
}

	 // ---------------------------------------------
	 //         Pagevars
	 // ---------------------------------------------


var __PageVars = new Array();
var __PageHash = location.hash.substring(1);

var tmpVarList = __PageHash.split("/");

for (var I = 0; I < tmpVarList.length; I++)
{
	if (tmpVarList[I] == "")
		continue;	

	var tmpVar = tmpVarList[I].split(":");
	var tmpObj = new Object();
	tmpObj.Name	= tmpVar[0];
	tmpObj.Value	= tmpVar[1];
	__PageVars.push(tmpObj);
}


function GetPageVar(varName)
{
	for (var I = 0; I < __PageVars.length; I++)
	{
		if (__PageVars[I].Name == varName)
		{
			
			return __PageVars[I].Value;
		}
	}
	return 0;
}

function SetPageVar(varName, varValue, autoUpdate)
{
	if (autoUpdate == undefined)
		autoUpdate = false;

	for (var I = 0; I < __PageVars.length; I++)
	{
		if (__PageVars[I].Name == varName)
		{
			__PageVars[I].Value = varValue;
			varName = 0;
			break;
		}
	}
	if (varName !== 0)
	{
		var newVar = new Object();
		newVar.Name	= varName;
		newVar.Value	= varValue;
		__PageVars.push(newVar);
	}

//	alert(__PageVars[I].Name);

	if (autoUpdate)
		UpdatePageVars();
}

function RemovePageVar(varName, autoUpdate)
{
	if (autoUpdate == undefined)
		autoUpdate = false;

	for (var I = 0; I < __PageVars.length; I++)
	{
		if (__PageVars[I].Name == varName)
		{
			__PageVars.splice(I, 1);
			break;
		}
	}

	if (autoUpdate)
		UpdatePageVars();
}

function UpdatePageVars()
{
	var newLocation = "#";

	for (var I = 0; I < __PageVars.length; I++)
	{
		if (__PageVars[I].Name == "")
			continue;	

		if (newLocation != "#")
			newLocation += "/";
		newLocation += __PageVars[I].Name + ":" + __PageVars[I].Value;
	}

//	alert(newLocation);

	window.location.href = newLocation;
}

     // ---------------------------------------------
     //         XML
     // ---------------------------------------------

function CreateXMLDocument(rootTagName, namespaceURL)
{
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";

    if (document.implementation && document.implementation.createDocument)
    {
        // This is the W3C standard way to do it
        return document.implementation.createDocument(namespaceURL, rootTagName, null);
    }
    else
    {
		alert("Using non-W3C compliant browser.");
    }
};

function HasXMLChild(node, name)
{
	for (var I = 0; I < (node.childNodes).length; I++)
	{
		if (node.childNodes[I].tagName == name)
			return true;
	}
	return false;
}

function FindXMLChildren(node, name)
{
	var Return = new Array();

	for (var I = 0; I < (node.childNodes).length; I++)
	{
		if (node.childNodes[I].tagName == name)
			Return.push(node.childNodes[I]);
	}
	return Return;
}
 
     // ---------------------------------------------
     //         Javascript
     // ---------------------------------------------

function InsertIntoArray(myArray, myInsert, myObject)
{

	if (myArray.length == 0)
	{
//		alert("First...");
		myArray.push(myObject);
		return;
	}
	for (var I = myArray.length-1; I >= myInsert; I--)
	{
//		alert("Moving " + I);
		myArray[I+1] = myArray[I];
	}
//	alert("Insert at " + myInsert);
	myArray[myInsert] = myObject;
}

function DoNotPropagate(e)
{
	if (!e)
		var e = window.event;
	if (e == undefined)
		alert("e was undefined!");
	e.cancelBubble = true;
	if (e.stopPropagation)
		e.stopPropagation();
}

function getElementsByClassName(classname, node)  {
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
 
     // ---------------------------------------------
     //         Colour
     // ---------------------------------------------

function RGBtoHex(R,G,B)
{
	return toHex16(R)+toHex16(G)+toHex16(B);
}

function toHex16(N)
{
	if (N==null)
		return "00";
		
	N=parseInt(N);
	if (N==0 || isNaN(N))
		return "00";
		
	N=Math.max(0,N);
	N=Math.min(N,255);
	N=Math.round(N);
	
	return "0123456789ABCDEF".charAt((N-N%16)/16)
	     + "0123456789ABCDEF".charAt(N%16);
}

function HexToR(h)
{
	return parseInt((cutHex(h)).substring(0,2),16);
}

function HexToG(h)
{
	return parseInt((cutHex(h)).substring(2,4),16);
}

function HexToB(h)
{
	return parseInt((cutHex(h)).substring(4,6),16);
}

function cutHex(h)
{
	return (h.charAt(0)=="#") ? h.substring(1,7):h;
}

function hexToRGB(Hex)
{
	var s = cutHex(Hex);
	return [HexToR(s), HexToG(s), HexToB(s)];
}
	 
function hsvToRgb(h, s, v){
    var r, g, b;

    var i = Math.floor(h * 6);
    var f = h * 6 - i;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);

    switch(i % 6){
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }

    return [r * 255, g * 255, b * 255];
}

function rgbToHsv(r, g, b)
{
    r = r/255, g = g/255, b = b/255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, v = max;

    var d = max - min;
    s = max == 0 ? 0 : d / max;

    if(max == min){
        h = 0; // achromatic
    }else{
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [h, s, v];
}
