/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <anders@nore.cc> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Anders Nore
 * ----------------------------------------------------------------------------
 *
 * This license is made by Poul-Henning Kamps. All credits to him
 * for making such a nice license.
 */

// This is for keeping track of all our menus
var menus = []
var menuindex = 0

function ScaleMenu(id, numitems, spacing, posx, posy, scales) {
	
	var thisObj = this;
	menus[menuindex] = this;
	menuindex++;
	
	// variables
	this.identifier = id || "";
	this.timeRefresh = 20;
	this.numMenuItems = numitems || 1;
	this.spacing = spacing || 20;
	this.relposy = posy || 30;
	this.relposx = posx || 0;
	this.scalesize = scales || 3;
	this.isVertical = false;
	this.isCenterFloat = true;
	
	/* private, should not be changed */
	this.tmpScale = new Array();
	this.scale = new Array();
	var timer = [];
	var lefts = [];
	var tops = [];
	var fokus = 0;
	var gAddWidth, sizex, sizey;
	
	this.init = function() {
		for(var i=0; i<this.numMenuItems; i++) {
			this.tmpScale[i] = 1;
			this.scale[i] = 1;
			timer[i] = null;
					
			var id = this.identifier + i;
			var obj = document.getElementById(id);
			sizex = obj.offsetWidth;	/* needed for rescaling and keeping center, see scaleTo() */
			sizey = obj.offsetHeight;
			
			var xPositioning = this.relposx + (this.spacing + sizex) * i;
			var yPositioning = this.relposy;
			if(this.isVertical == true) {
				xPositioning = this.relposx;
				yPositioning = this.relposy + (this.spacing + sizey) * i;
			}
			obj.style.left = lefts[i] = xPositioning ;
			obj.style.top = tops[i] = yPositioning ;
			
			gAddWidth = this.scalesize * (obj.offsetWidth / obj.offsetHeight);
				
			if(i==fokus) {
				obj.style.zIndex = 1;
			} else {
				obj.style.zIndex = 0;
			}
		}
		
		window.setInterval( function(){ thisObj.scaleTo(); }, this.timeRefresh);
	}
	
	this.rescale = function() {
		for(var num=0; num < this.numMenuItems; num++) {
			var obj = document.getElementById(this.identifier + num);
			var center = this.getCenter(obj);
			var dist = this.distance(center[0], center[1], tempX, tempY);
	
			var scTo = 1;
			if(dist < 40)
				scTo = 15;
			else if(dist > 200)
				scTo = 1;
			else {
				scTo = (40 / dist)*15;
			}
			
			/* set the new size to scale to */
			this.tmpScale[num] = Math.round(scTo);
			/*
			document.Debug.distance.value = dist;
			document.Debug.scale.value = num;*/
		
		}
	}
	
	this.scaleTo = function() {
		for(var num=0; num < this.numMenuItems; num++) {
			var obj = document.getElementById(this.identifier + num);
			if(!obj)
				document.writeln("Cannot find object: "+id+num+" in scaleTo function");
			//document.writeln("scalesize:"+this.scalesize);
			var addW = 0;
			var addH = 0;
			
			if(this.scale[num] < this.tmpScale[num]) {
				addW = gAddWidth;
				addH = this.scalesize;
				this.scale[num] += 1;
			} else if( this.scale[num] > this.tmpScale[num]) {
				addW = -gAddWidth;
				addH = -this.scalesize;
				this.scale[num] -= 1;
			} else {
				addW = 0;
				addH = 0;
			}
		
			var bredde = obj.offsetWidth;
			var hogde = obj.offsetHeight;
			obj.style.width = (bredde + addW) + "px";
			obj.style.height = (hogde + addH) + "px";
			
			if(this.isCenterFloat == true) {
				// default float to center
				obj.style.left = lefts[num] - (obj.offsetWidth - sizex)/2 + "px";
				obj.style.top = tops[num] - (this.scalesize/2) * this.scale[num] + "px";
			} else {
				if(this.isVertical == true) {
					// centers in vertical direction
					obj.style.top = tops[num] - (this.scalesize/2) * this.scale[num] + "px";
				} else {
					// centers in horizontal direction
					obj.style.left = lefts[num] - (obj.offsetWidth - sizex)/2 + "px";
				}
			}
			
			/* steal focus because we have grown bigger */
			if(this.scale[num] > this.scale[fokus]) {
				var focusItem = this.getMenuItem(fokus);
				if(!focusItem)
					document.writeln("Wrong with focus, check number of items");
				focusItem.style.zIndex = 0;
				fokus = num;
				obj.style.zIndex = 1;	
			}
		}
	}
	
	this.getMenuItem = function(num) {
		id = this.identifier + num;
		return document.getElementById(id);
	}
	
	this.distance = function(posX, posY, pos2X, pos2Y) {
		var xx = posX - pos2X;
		var yy = posY - pos2Y;
	
		return Math.sqrt(xx * xx + yy * yy);
	}
	
	// taken from www.quirksmode.com
	this.findPos = function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}
	
	this.getCenter = function(obj) {
		var pos = this.findPos(obj);
		var newwidth = obj.offsetWidth / 2;
		var newheight = obj.offsetHeight / 2;
		return [newwidth + pos[0], newheight + pos[1]];
	}
	
	this.getNumItems = function() {
		return this.numMenuItems;
	}
	
	/******* Setters *******/
	this.setRefreshTime = function(newtime) {
		this.timeRefresh = newtime;
	}
	
	this.setSpacing = function(newspacing) {
		this.spacing = newspacing;
	}
	
	this.setNumItems = function(numitems) {
		this.numMenuItems = numitems;
	}
	
	this.setPosY = function(newy) {
		this.relposy = newy;
	}
	
	this.setPosX = function(newx) {
		this.relposx = newx;
	}
	
	this.setScale = function(newscale) {
		this.scalesize = newscale;
	}
	
	this.setVertical = function(setVertical) {
		this.isVertical = setVertical;
	}
	
	this.setCenterFloat = function(centerFloat) {
		this.isCenterFloat = centerFloat;
	}
}

/*
* Most of this is taken from http://www.quirksmode.org/
* Captures the mouseposition
*/

if( window.captureEvents ) { window.captureEvents( Event.MOUSEMOVE ); }
window.onscroll = getMousePos;
window.onresize = getMousePos;
document.onmousemove = getMousePos;


var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1)


var tempX, tempY;

function getMousePos(e) {
	tempX = 0;
	tempY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		tempX = e.pageX;
		tempY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		tempX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		tempY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// tempX and tempY contain the mouse position relative to the document

	for(var i=0; i < menuindex; i++) {
		menus[i].rescale();
	}
	
}

