/*  Prototype JavaScript framework
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
/*--------------------------------------------------------------------------*/

//note: modified & stripped down version of prototype, to be used with moo.fx by mad4milk (http://moofx.mad4milk.net).

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this;
	return function(event) {
		__method.call(object, event || window.event);
	}
}

function $(el) {
	if (typeof el == 'string') el = document.getElementById(el);
	return el;
}

if (!window.Element) var Element = new Object();

Object.extend(Element, {
	hasClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var hasClass = false;
		element.className.split(' ').each(function(cn){
			if (cn == className) hasClass = true;
		});
		return hasClass;
	}
});

document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = [];
	$c(children).each(function(child){
		if (Element.hasClassName(child, className)) elements.push(child);
	});  
	return elements;
}

//useful array functions
Array.prototype.iterate = function(func){
	for(var i=0;i<this.length;i++) func(this[i], i);
}
if (!Array.prototype.each) Array.prototype.each = Array.prototype.iterate;

function $c(array){
	var nArray = [];
	for (var i=0;i<array.length;i++) nArray.push(array[i]);
	return nArray;
}

/*
moo.fx, simple effects library built with prototype.js (http://prototype.conio.net).
by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE.
for more info (http://moofx.mad4milk.net).
Sunday, March 05, 2006
v 1.2.3
*/

var fx = new Object();
//base
fx.Base = function(){};
fx.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		transition: fx.sinoidal
	}
	Object.extend(this.options, options || {});
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	hide: function() {
		this.now = 0;
		this.increase();
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	}
}

//stretchers
fx.Layout = Class.create();
fx.Layout.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.style.overflow = "hidden";
		this.iniWidth = this.el.offsetWidth;
		this.iniHeight = this.el.offsetHeight;
		this.setOptions(options);
	}
});

//transitions
fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}

// sliding menu script
// (c) 2006 Piotr Przybylski, http://crackpl.eu

var _menus = Array();

// modified fx.Width, deals with Opera zero width bug
fx.UniversalWidth = Class.create();
Object.extend(Object.extend(fx.UniversalWidth.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.width = this.now + 'px';
		if (this.now == 1) {
			this.el.style.display = 'none';
		}
	},

	toggle: function(){
		if (this.el.style.display == 'block') this.custom(this.iniWidth, 1);
		else {
			this.el.style.display = 'block';
			this.custom(1, this.iniWidth);
		}
	}
});

function eventListener(obj, event, func)
{
	if (obj.addEventListener) {
		obj.addEventListener(event, func, false);
	}
	else if (obj.attachEvent) {
		obj.attachEvent('on'+event, func)
	}
}

function createCookie(name, value)
{
	document.cookie = name + "=" + value;
}

function readCookie(name)
{
	nameEQ = name + "=";
	ca = document.cookie.split(';');
	for (i = 0; i < ca.length; i++)
	{
		c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function menu_init()
{
	$c(document.getElementsByClassName('links')).each(function (node){
		node.effect = new fx.UniversalWidth(node, {duration: 250});
		node.effect.iniWidth = 105;

		// Opera fix
		node.style.width = '1px';

		_menus.push(node);
	});
	mid = 0;
	$c(document.getElementsByClassName('bar')).each(function (node){
		node.style.cursor = 'pointer';
		node.mid = mid;
		if (mid == readCookie('sel_menu'))
		{
			_menus[mid].style.display = 'block';
			_menus[mid].style.width = _menus[mid].effect.iniWidth+'px';
		}
		mid++;
		eventListener(node, 'click', function(){
			_menus.each(function (m){
				if (m.style.display == 'block') {
					m.effect.toggle();
				}
			});
			sel_menu = (_menus[node.mid].style.display == 'block')
				? -1 : node.mid;
			createCookie('sel_menu', sel_menu);
			_menus[node.mid].effect.toggle();
		});
	});	
}

eventListener(window, 'load', menu_init);