function expander(id)
{
	var parent = document.getElementById("archive");

	if(id == "open" || id == "close")
	{
		var expanders = sweepFor("tree_expander", parent, "span");
		var containers = sweepFor("tree_offshoot", parent);
		var count = expanders.length;
		for(var i=0; i<count; i++)
		{
			var exander = expanders[i];
			var sign = exander.getElementsByTagName("a");

			if(sign.length == 0) { continue; }

			sign = sign[0];

			var div = containers[i];
			div.style.display = (id == "close") ? "none" : "block";
			sign.innerHTML = (id == "close") ? "+" : "-";
		}
	}
	else
	{
		var div = document.getElementById(id);
		div.style.display = (getStyle(div, "display") == "block") ? "none" : "block";

		var sign = document.getElementById("expander_" + id);
		sign.innerHTML = (getStyle(div, "display") == "block") ? "-" : "+";
	}
}


function sweepFor(cName, classParent, tag)
{
	var type = tag ? tag : "div";
	var cp = classParent ? classParent.getElementsByTagName(type) : document.getElementsByTagName(type);
	var sweepEl = new Array();

	if(cp)
	{
		for(i=0; i<cp.length; i++)
		{
			if(cp[i].className == cName)
			{
				sweepEl.push(cp[i]);
			}
		}
	}

	return sweepEl;
}

var Gesture = {

	isIE :  null,
	layer : null,
	height : null,
	timer : null,
	event : null,
	buffer : 30,

	viewport : 0,
	y_start: 0,
	y_end: 0,
	t_start : 0,
	t_end : 0,
	y_diff : 0,
	t_diff : 0,
	y_old : 0,
	push : 0,
	pull_top : 0,
	pull_bottom : 0,
	dir : null,
	gragging : false,
	pad : null,

	init : function(name)
	{
		var isIE = (document.getElementById && !document.all) ? false : true;

		if(isIE) { return; }

		var g = Gesture;
		g.isIE = isIE;
		g.name = name;

		g.timer = setInterval("Gesture.hook()", 1000);
	},

	hook : function()
	{
		var success = true;
		try
		{
			var g = Gesture;
			g.layer = document.getElementById(g.name);		
		}
		catch(err) { success = false;}

		if(success) { g.build(); }
	},

	build : function()
	{
		var g = Gesture;

		g.reset();

		var z = getStyle(g.layer, "z-index");
		if(typeof(z) != "number") { z = 1; }

		g.layer.style.zIndex = z;

		g.pad = document.createElement("div");
		var pad = g.pad;

		pad.style.width = getWindow().width + "px";
		pad.style.height = getWindow().height + "px";
		pad.style.position = "fixed";
		pad.style.top = "0";
		pad.style.left = "0";
		pad.style.zIndex = (z - 1);

		g.layer.parentNode.appendChild(pad);

		g.height = parseInt(getStyle(g.layer, "height"));

		pad.onmouseover = g.mouseOver;
		pad.onmouseout = g.mouseOut;
		pad.onmousedown = g.mouseDown;
		document.onmouseup = g.mouseUp;
	},

	disableSelect : function()
	{
		var element = (document.body) ? document.body : document.getElementByTagName("body")[0];
		element.onselectstart = function () { return false; };
		element.unselectable = "on";
		element.style.MozUserSelect = "none";
		element.style.cursor = "default";
	},

	enableSelect : function(element)
	{
		var element = (document.body) ? document.body : document.getElementByTagName("body")[0];
		element.onselectstart = null;
		element.unselectable = "off";
		element.style.MozUserSelect = "";
		element.style.cursor = "auto";
	},

	mouseOver : function(e)
	{
		document.onmousemove = Gesture.mouseMove;
	},

	mouseOut : function(e)
	{
		document.onmousemove = null;
		Gesture.hideHint();
	},

	mouseDown : function(e)
	{
		var g = Gesture;

		document.onmousemove = g.mouseMove;

		var date = new Date();
		g.t_start = date.getTime();
		g.y_start = g.mouseY(e);
		g.y_old = g.y_start;
		g.viewport = getWindow().height;
		g.dragging = true;

		g.disableSelect();
		g.reset();
	},

	mouseUp : function(e)
	{
		document.onmousemove = null;

		var g = Gesture;

		if(!g.dragging) { return; }

		g.dragging = false;

		var date = new Date();
		g.t_end = date.getTime();
		g.y_end = g.mouseY(e);
		g.y_diff = g.y_end - g.y_start;
		g.t_diff = g.t_end - g.t_start;
		g.push = g.y_diff / g.t_diff * 100;

		g.reset();
		g.move();
		g.timer = setInterval("Gesture.move()", 100);

		g.enableSelect();

		g.hideHint();
	},

	mouseMove : function(e)
	{
		var g = Gesture;

		if(!g.dragging) { g.showHint(e); return; }

		var mouse_y = g.mouseY(e);

		var diff = g.y_old - mouse_y;
		var y = getScroll().y + diff;
		var offset_y = g.height - g.viewport - y;

		if(y < 0)
		{
			margin_top = parseInt(getStyle(g.layer, "margin-top"));
			margin_top = (margin_top >= g.buffer) ? g.buffer : margin_top - y;
			g.layer.style.marginTop = margin_top + "px";
		}
		else if(offset_y < 0)
		{
			margin_bottom = parseInt(getStyle(g.layer, "margin-bottom"));
			margin_bottom = (margin_bottom >= g.buffer) ? g.buffer : margin_bottom - offset_y;
			g.layer.style.marginBottom = margin_bottom + "px";
		}

		window.scrollTo(getScroll().x, y);

		// Determine direction
		var dir = (g.y_old - mouse_y < 0) ? "up" : "down";

		// If direction is changed then reset
		if(dir != g.dir)
		{
			var date = new Date();
			g.t_start = date.getTime();
			g.y_start = mouse_y;
			g.dir = dir;
		}

		// Save current position
		g.y_old = mouse_y;

		return false;
	},

	mouseX : function(e)
	{
		var x = Gesture.isIE ? event.clientX : e.clientX;
		return x;
	},

	mouseY : function(e)
	{
		var y = Gesture.isIE ? event.clientY : e.clientY;
		return y;
	},

	move : function()
	{
		var g = Gesture;

		var y = getScroll().y - g.push;

		var layer = document.getElementById(g.name);

		if(y < g.buffer || Math.abs(g.pull_top) > 0)
		{
			var margin_top = parseInt(getStyle(layer, "margin-top"));
			g.pull_top += (0 - margin_top) / 2;
			margin_top += g.pull_top;
			layer.style.marginTop = margin_top + "px";
		}

		if(y > g.height - g.viewport || Math.abs(g.pull_bottom) > 0)
		{
			var margin_bottom = parseInt(getStyle(layer, "margin-bottom"));
			g.pull_bottom += (0 - margin_bottom) / 2;
			margin_bottom += g.pull_bottom;
			layer.style.marginBottom = margin_bottom + "px";
		}

		window.scrollTo(getScroll().x, y);

		g.push *= 0.8;
		g.pull_top *= 0.5;
		g.pull_bottom *= 0.5;
		if(Math.abs(g.push) < 1 && Math.abs(g.pull_top) < 1 && Math.abs(g.pull_bottom) < 1)
		{
			//layer.style.marginTop = "0px";
			//layer.style.marginBottom = "0px";
			g.push = 0;
			g.pull_top = 0;
			g.pull_bottom = 0;
			g.reset();
		}
	},

	reset : function()
	{
		var g = Gesture;

		if(g.timer)
		{
			clearInterval(g.timer);
			g.timer = null;
		}
	},

	showHint : function(e)
	{
		var g = Gesture;

		var hint_1 = document.getElementById("gesture_hint_1");
		var hint_2 = document.getElementById("gesture_hint_2");

		hint_1.style.display = "block";
		hint_2.style.display = "block";

		var x = getScroll().x + g.mouseX(e) - (parseInt(getStyle(hint_1, "width")) / 2);

		var y = getScroll().y + g.mouseY(e);
		var y1 = y - parseInt(getStyle(hint_1, "height")) - 8;
		var y2 = y + parseInt(getStyle(hint_2, "height"));

		hint_1.style.top = y1 + "px";
		hint_1.style.left = x + "px";

		hint_2.style.top = y2 + "px";
		hint_2.style.left = x + "px";
	},

	hideHint : function()
	{
		document.getElementById("gesture_hint_1").style.display = "none";
		document.getElementById("gesture_hint_2").style.display = "none";
	}

}


function trace(msg, append)
{
	if(append)
	{
		document.getElementById("console").innerHTML += msg + "<br>";
	}
	else
	{
		document.getElementById("console").innerHTML = msg;
	}
}


function getStyle(obj, rule)
{
	var style = "";

	try {
		if(obj.currentStyle)
		{
			/* Must convert style-side to styleSide of IE aargh!!! */
			var dashIndex = rule.indexOf("-");
			if(dashIndex > -1) { rule = (rule.substring(0, dashIndex)+(rule.substring(dashIndex+1, dashIndex+2)).toUpperCase()+rule.substring(dashIndex+2, rule.length)); }

			style = obj.currentStyle[rule];
		}
		else
		{
			style = document.defaultView.getComputedStyle(obj, null).getPropertyValue(rule);
		}
	}
	catch(e) {}

	return style;
}


function getScroll()
{
	var x = 0;
	var y = 0;

	if(typeof(window.pageYOffset) == "number")
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
	}
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}

	x = x ? x : 0;
	y = y ? y : 0;

	return { x:x, y:y }
}


function getWindow()
{
	var w = 0;
	var h = 0;

	if(self.innerHeight)
	{
		w = self.innerWidth;
		h = self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight)
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	return { width:w, height:h }
}


var thumbs = {

	add : function(img)
	{
		addEvent(img, "mouseover", thumbg.over);
		addEvent(img, "mouseout", thumbg.out);
	},

	over : function(e)
	{
		var src = e.target ? e.target : window.event.srcElement;
		src.className = "slide_thumb_over";

	},

	out : function(e)
	{
		var src = e.target ? e.target : window.event.srcElement;
		src.className = "slide_thumb";
	}

}


function addEvent(obj, event, action)
{
	if(!obj) { return; }

	if(document.addEventListener)
	{
		obj.addEventListener(event, action, false);
	}
	else if (document.attachEvent)
	{
		obj.attachEvent("on"+event, action);
	}
}


Array.prototype.findNameIndex = function(name)
{
	var err = -1;

	for (var i=0; i<thig.length; i++)
	{
		if (this[i].name == name)
		{
			return i;
		}
	}

	return err;
}


/* FUNCTION TO SWEEP FOR CLASSES */
function sweepFor(cName, classParent, type)
{
	type = type ? type : "div";
	var cp = classParent ? classParent.getElementsByTagName(type) : document.getElementsByTagName(type);
	var sweepEl = new Array();

	if(cp)
	{
		for(i=0; i<cp.length; i++)
		{
			if(cp[i].className == cName)
			{
				sweepEl.push(cp[i]);
			}
		}
	}

	return sweepEl;
}