var Kiji; if(!Kiji) Kiji = {};
/*--------------------------------------
	Kiji.Debug (FireFox Only)
--------------------------------------*/
Kiji.Debug = {};
Kiji.Debug.enableDebug = true;
Kiji.Debug.msgCount = 0;
Kiji.Debug.debugWindow = null;
Kiji.Debug.openWindow = function()
{
	Kiji.Debug.debugWindow = window.open("", "", "top=150,left=150, width=500, height=300, scrollbars=yes, resizable=yes");
	
	if (!Kiji.Debug.debugWindow.opener) Kiji.Debug.debugWindow.opener = self;
	
	Kiji.Debug.debugWindow.document.open();
	Kiji.Debug.debugWindow.document.writeln('');	
	Kiji.Debug.debugWindow.document.write('');
	Kiji.Debug.debugWindow.document.write('
Kiji Debug Window');
	Kiji.Debug.debugWindow.document.write('');
	Kiji.Debug.debugWindow.document.write('');	
	Kiji.Debug.debugWindow.document.write('');	
	Kiji.Debug.debugWindow.document.write('');
	Kiji.Debug.debugWindow.document.close();	
}
Kiji.Debug.debugOut = function(msg, bgColor, elementId)
{
	if (!Kiji.Debug.debugWindow)
	{
		Kiji.Debug.openWindow();
		if (!Kiji.Debug.debugWindow)
			return;
	}
	var div = Kiji.Debug.debugWindow.document.createElement("div");
	div.style.backgroundColor = bgColor;
	div.innerHTML = 	Kiji.Debug.msgCount + ": " + msg;
	Kiji.Debug.debugWindow.document.getElementById(elementId).appendChild(div);
}
Kiji.Debug.reportError = function(msg)
{
	if (Kiji.Debug.enableDebug)
		Kiji.Debug.debugOut(msg, '#fdd', 'errors');
	Kiji.Debug.msgCount++;
}
Kiji.Debug.reportWarning = function(msg)
{
	if (Kiji.Debug.enableDebug)
		Kiji.Debug.debugOut(msg, '#fee', 'errors');
	Kiji.Debug.msgCount++;
}
Kiji.Debug.trace = function(msg)
{
	if (Kiji.Debug.enableDebug)
		Kiji.Debug.debugOut(msg, '#eef', 'trace');
	Kiji.Debug.msgCount++;
}
/*--------------------------------------
	Kiji.Lib 
--------------------------------------*/
Kiji.Lib = {}
/*
	Extracts the name of the parent element from the objects elements id
*/
Kiji.Lib.extractParentId = function(obj)
{
	var pos = obj.id.lastIndexOf('_');
	if (pos > -1)  return obj.id.substring(0, pos);
	return "";
}
/*
	Extracts the number id from the id. This id is used to find the element in the
	event array for further use
*/
Kiji.Lib.extractNumberId = function(obj)
{
	var pos = obj.id.lastIndexOf('!');
	if (pos > -1)  return obj.id.substr(pos + 1, obj.id.length);
	return "";	
}
/*
	Returns a merge of the 2 list of properties. (destination is modified)
*/
Kiji.Lib.merge = function(destination, source)
{
	for (property in source) 
		destination[property] = source[property];
	return destination;	
}
/*
	Returns an empty property list if the object doesn't exist
*/
Kiji.Lib.empty = function(object)
{
	return object || {};
}
Kiji.Lib.Transition = function(mode, value)
{
	switch(mode)
	{
		case 'sin':
			return (-Math.cos(value*Math.PI)/2) + 0.5;
			break;
	}
	return value;
}
Kiji.Lib.getPageSize = function()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
Kiji.Lib.createElement = function(divId, parent, properties)
{
	var position = 'relative';
	var xOffset = 0;
	var yOffset = 0;
	var display = false;
	var className = 'none';
	var innerHTML = '';
	var cursor = 'auto';
	var nodePos = 'last';
	var type = 'div';
	var eWidth = 'auto';
	var eHeight = 'auto';
	
	if (properties)
	{
		if (properties['position']) position = properties['position'];
		if (properties['left']) xOffset = properties['left'];
		if (properties['top']) yOffset = properties['top'];
		if (properties['display']) display = properties['display'];
		if (properties['className']) className = properties['className'];
		if (properties['innerHTML']) innerHTML = properties['innerHTML'];
		if (properties['cursor']) cursor = properties['cursor'];
		if (properties['nodePos']) nodePos = properties['nodePos'];
		if (properties['type']) type = properties['type'];
		if (properties['width']) eWidth = properties['width'];
		if (properties['height']) eHeight = properties['height'];
	}
	var div = document.createElement(type);
	div.setAttribute('id', divId);
	if (className != 'none') div.className = className;
	div.style.position = position;
	div.style.left = xOffset + 'px';
	div.style.top = yOffset + 'px';
	div.style.width = eWidth;
	div.style.height = eHeight;	
	//div.style.overflow = '';
	
	div.style.display = display?'':'none';	
	if (innerHTML != '')
		div.innerHTML = innerHTML;
	div.style.cursor = cursor;
	
	if (properties['style'])
		Kiji.Lib.merge(div.style, properties['style']);
	
	switch (nodePos)
	{
		case 'first':
			parent.insertBefore(div, parent.firstChild);
			break;
		case 'last':
			parent.appendChild(div);
			break;
	}
	
	return div;
}
Kiji.Lib.createInput = function (inputId, parent, properties)
{
	var type = 'button';
	var value = '';
	var className = 'none';
	var cursor = 'auto';
	
	if (properties)
	{
		if (properties['type']) type = properties['type'];
		if (properties['value']) value = properties['value'];
		if (properties['className']) className = properties['className'];
		if (properties['cursor']) cursor = properties['cursor'];
	}
	
	var input = document.createElement('input');
	if (className != 'none') input.className = className;
	input.setAttribute('id', inputId);
	input.setAttribute('name', inputId);
	input.setAttribute('value', value);
	input.setAttribute('type', type);
	input.style.cursor = cursor;
	parent.appendChild(input);
}
Kiji.Lib.createButton = function (inputId, parent, properties)
{
	var type = 'button';
	var value = '';
	var className = 'none';
	var cursor = 'auto';
	
	if (properties)
	{
		if (properties['type']) type = properties['type'];
		if (properties['value']) value = properties['value'];
		if (properties['className']) className = properties['className'];
		if (properties['cursor']) cursor = properties['cursor'];
	}
	
	var input = document.createElement('a');
	if (className != 'none') input.className = className;
	input.setAttribute('id', inputId);
	input.setAttribute('href', '#');
	input.innerHTML = value;
	input.style.cursor = cursor;
	if (properties['style'])
		Kiji.Lib.merge(input.style, properties['style']);
	
	parent.appendChild(input);
}
/*--------------------------------------
	Kiji.Fx
--------------------------------------*/
/*
Kiji.Fx = {}
Kiji.Fx.initialize = function(blank_path)
{
	this.desktopLayer = Kiji.Lib.createElement('desktopLayer', document.body, 
												{
													type: 'iframe',
													style: 
													{
														zIndex: '90',
														position: 'absolute',
														top: '0px',
														left: '0px',
														display: 'none',
														width: '100%',
														height: '100%',
														backgroundColor: '#000',
														filter: 'alpha(opacity=60)',
														opacity: '0.6'
													}
												}
											  );
	this.desktopLayer.src = blank_path;
	this.desktopLayer.frameBorder = 0;
		
	this.desktopLayerAnim = null;
}
Kiji.Fx.blockWindow = function(state)
{
	if (this.desktopLayerAnim) return; // this.desktopLayerAnim.stop();	
	var size = Kiji.Lib.getPageSize();
	this.desktopLayer.style.height = size[1] + 'px';
	if (state)
		this.desktopLayerAnim = new Kiji.Fx.animParams(this.desktopLayer, 5.0, 'dissolveIn', 0.0, 0.6).start();
	else
		this.desktopLayerAnim = new Kiji.Fx.animParams(this.desktopLayer, 5.0, 'dissolveOut', 0.6, 0.0).start();		
}
Kiji.Fx.animParams = function(winObj, delay, type, rangeStart, rangeEnd)
{
	this.startTime = 0;
	this.endTime = delay * 100;
	this.animTime = 0;
	this.rangeStart = rangeStart;
	this.rangeEnd = rangeEnd;
	
	this.animType = type;
	this.obj = winObj;
	this.pos = -1;
	this.render(this.startTime, false);
}
Kiji.Fx.animParams.prototype.start = function()
{
	this.startTime = new Date().getTime();
	this.endTime += this.startTime;
	this.animTime = this.startTime;
	this.obj.style.display = '';
	this.pos = Kiji.Fx.WindowFx.add(this);
}
Kiji.Fx.animParams.prototype.stop = function()
{
	Kiji.Fx.WindowFx.drop(this.pos);
	Kiji.Fx.WindowFx.anims.compact();
}
Kiji.Fx.animParams.prototype.animate = function(currentTime)
{	
	if (currentTime >= this.startTime)
	{
		if (currentTime > this.endTime)
		{
			this.render(1, true);
			return false;
		}
		var factor = (currentTime - this.startTime) / (this.endTime - this.startTime );
		this.render(factor, false);
		return true;
	}
	return false;
}
Kiji.Fx.animParams.prototype.render = function(factor, finished)
{
	factor = Kiji.Lib.Transition('sin', factor);
	factor *= (this.rangeEnd - this.rangeStart);
	factor += this.rangeStart;
	switch(this.animType)
	{
		case 'dissolveOut':
			if (finished) 
				this.obj.style.display = 'none';
		case 'dissolveIn':
			this.obj.style.opacity = (factor < 0.00001)? 0: factor;
			this.obj.style.filter = 'alpha(opacity=' + ((factor < 0.00001)? 0: factor) * 100 + ')';			
			break;
	}
}
Kiji.Fx.WindowFx = 
{
	anims: [],
	interval: null,
	add: function(anim)
		{
			this.anims.push(anim);
			if (!this.interval)
				this.interval = setInterval(this.animate.bind(this), 40);
			return this.anims.length - 1;
		},
	drop: function(i)
		{
			delete this.anims[i];
			this.anims[i] = null; 			
		},
	animate: function()
		{
			if (this.anims.length == 0)
			{
				clearInterval(this.interval);
				this.interval = null;
				return;
			}
			var currentTime = new Date().getTime();
			var i = 0;
			var len = this.anims.length;
			while (i < len)
			{
				if (this.anims[i] == null)
				{
					i++;
					continue;
				}
				if (!this.anims[i].animate(currentTime))
					this.drop(i);				
				i++;
			}
			this.anims = this.anims.compact();			
		}	
}
*/
Kiji.Fx = {}
Kiji.Fx.initialize = function(blank_path)
{
	this.desktopLayer = Kiji.Lib.createElement('desktopLayer', document.body, 
												{
													type: 'iframe',
													style: 
													{
														zIndex: '90',
														position: 'absolute',
														top: '0px',
														left: '0px',
														display: 'none',
														width: '100%',
														height: '100%',
														backgroundColor: '#000',
														filter: 'alpha(opacity=60)',
														opacity: '0.6'
													}
												}
											  );
	this.desktopLayer.src = blank_path;
	this.desktopLayer.frameBorder = 0;
		
	//this.desktopLayerAnim = null;
}
Kiji.Fx.desktopLayerAnim = null;
Kiji.Fx.blockWindow = function(state)
{
	if (Kiji.Fx.desktopLayerAnim) 
		Kiji.Fx.desktopLayerAnim.stop();
	//var desktopLayerAnim = null;
	var size = Kiji.Lib.getPageSize();
	this.desktopLayer.style.height = size[1] + 'px';
	if (state)
		Kiji.Fx.desktopLayerAnim = new Kiji.Fx.animParams(this.desktopLayer, 5.0, 'dissolveIn', 0.0, 0.6);
	else
		Kiji.Fx.desktopLayerAnim = new Kiji.Fx.animParams(this.desktopLayer, 5.0, 'dissolveOut', 0.6, 0.0);
	Kiji.Fx.desktopLayerAnim.first = true;
	Kiji.Fx.desktopLayerAnim.start();
}
Kiji.Fx.animParams = function(winObj, delay, type, rangeStart, rangeEnd)
{
	this.startTime = 0;
	this.endTime = delay * 100;
	this.animTime = 0;
	this.rangeStart = rangeStart;
	this.rangeEnd = rangeEnd;
	
	this.animType = type;
	this.obj = winObj;
	this.pos = -1;
	this.first = false;
	//this.render(this.startTime, false);
}
Kiji.Fx.animParams.prototype.start = function()
{
	this.startTime = new Date().getTime();
	this.endTime += this.startTime;
	this.animTime = this.startTime;
	this.obj.style.display = '';/*
	if (this.first)
		this.pos = Kiji.Fx.WindowFx.addLonely(this);
	else*/
		this.pos = Kiji.Fx.WindowFx.add(this);
}
Kiji.Fx.animParams.prototype.stop = function()
{
	Kiji.Fx.WindowFx.drop(this.pos);
	//Kiji.Fx.WindowFx.anims.compact();
}
Kiji.Fx.animParams.prototype.animate = function(currentTime)
{	
	if (currentTime >= this.startTime)
	{
		if (currentTime > this.endTime)
		{
			this.render(1, true);
			return false;
		}
		var factor = (currentTime - this.startTime) / (this.endTime - this.startTime );
		this.render(factor, false);
		return true;
	}
	return false;
}
Kiji.Fx.animParams.prototype.render = function(factor, finished)
{
	factor = Kiji.Lib.Transition('sin', factor);
	factor *= (this.rangeEnd - this.rangeStart);
	factor += this.rangeStart;
	switch(this.animType)
	{
		case 'dissolveOut':
			if (finished) 
				this.obj.style.display = 'none';
		case 'dissolveIn':
			this.obj.style.opacity = (factor < 0.00001)? 0: factor;
			this.obj.style.filter = 'alpha(opacity=' + ((factor < 0.00001)? 0: factor) * 100 + ')';			
			break;
	}
}
Kiji.Fx.WindowFx = 
{
	anims: [],
	lonelyAnims: [],
	interval: null,
	add: function(anim)
		{
			this.anims.push(anim);
			if (!this.interval)
				this.interval = setInterval(this.animate.bind(this), 40);
			return this.anims.length - 1;
		},
	addLonely: function(anim)
		{
			this.lonelyAnims.push(anim);
			if (!this.interval)
				this.interval = setInterval(this.animate.bind(this), 40);
			return this.lonelyAnims.length - 1;
		},
	drop: function(i)
		{
			//delete this.anims[i];
			if (!this.anims[i]) return;
			this.anims[i] = null;
			this.anims.compact();
		},
	dropLonely: function(i)
		{
			delete this.lonelyAnims[i];
			this.lonelyAnims[i] = null; 			
		},
	animate: function()
		{
			if ( (this.anims.length == 0) && (this.lonelyAnims.length == 0) )
			{
				clearInterval(this.interval);
				this.interval = null;
				return;
			}
			var currentTime = new Date().getTime();
			var i = 0;
			var len = this.anims.length;
			while (i < len)
			{
				if (this.anims[i] == null)
				{
					i++;
					continue;
				}
				if (!this.anims[i].animate(currentTime)) this.drop(i);
				i++;
			}
			this.anims = this.anims.compact();			
			
			if (this.lonelyAnims.length > 0)
			{
				if (!this.lonelyAnims[0].animate(currentTime)) this.dropLonely(0);
				this.lonelyAnims = this.lonelyAnims.compact();
			}
		}	
}
/*--------------------------------------
	Kiji.WindowEvents 
--------------------------------------*/
Kiji.WindowEvents = {};
Kiji.WindowEvents.windows = {};
Kiji.WindowEvents.buttons = {};
Kiji.WindowEvents.windows.mouseseek = new Array;
Kiji.WindowEvents.windows.dragdrop = new Array;
Kiji.WindowEvents.buttons.mouseclick = new Array;
Kiji.WindowEvents.menus = new Array;
Kiji.WindowEvents.trees = new Array;
Kiji.WindowEvents.extractWindow = function(vector, event)
{
	var targetWindowId = Kiji.Lib.extractParentId(Event.element(event));
	return vector[targetWindowId];
}
Kiji.WindowEvents.objExtractWindow = function(vector, obj)
{
	var targetWindowId = Kiji.Lib.extractParentId(obj);
	return vector[targetWindowId];
}
Kiji.WindowEvents.appendWindow = function(a, w)
{
	a[w.windowId] = w;
}
	//  **********************
	//  Kiji.WindowEvents.windows.mousemove	
	
Kiji.WindowEvents.windows.mouseseek.cursorSeeker = function(event)
{
	try
	{
		var i = 0;
		var currentWindow = null;
		while (i 0))
	{
		var expander = Kiji.Lib.createElement(this.windowId + '_!' + this.nodeCount, fatherNode, { type: 'span', position: 'relative', innerHTML: '- ', cursor: 'pointer', display: true, nodePos: 'first' });
		this.nodeCount++;		
		expander.style.float = 'left';
		Kiji.WindowEvents.trees.appendElement(expander);
	}
	return total + 1;
}
Kiji.TreeView.prototype.expandCollapse = function(obj)
{
	var node = obj.parentNode.firstChild;
	var explode = true;
	var listElements = 0;
	while (node != null)
	{	
		if ( (node.nodeName.toLowerCase() != '#text') && (node.nodeName.toLowerCase() != 'span') )
		{
			if (node.style.display == 'none')
			{
				node.style.display = '';
				explode = false;
			}
			else
			{
				node.style.display = 'none';
				explode = true;
			}
			listElements++;
		}
		node = node.nextSibling;
	}
	if (explode && (listElements > 0))
		obj.innerHTML = '+ ';
	else
		obj.innerHTML = '- ';
}
/*
	Kiji lib&stuff initialization
*/
Kiji.initialize = function(blank_path)
{
	this.Fx.initialize(blank_path);
}