
if(!window.console)
{
	console = {};
	console.log = alert;
}

/*
 * Core functions
 */

function $A(a)
{
	var tmp = [];
	for(var i = 0; i < a.length; i++)
		tmp[i] = a[i];
	return tmp;
};

function $extend(a, b)
{
	a = a || {}, b = b || {};
	for(var p in b)
		a[p] = b[p];
	return a;
};

function $native()
{
	for(var i = 0; i < arguments.length; i++)
		arguments[i].extend = function(o)
		{
			for(var p in o)
				if(!this.prototype[p])
					this.prototype[p] = o[p];
		};
};

$native(Array, Function, Number, String);

/*
 * Extend functions
 */

Array.extend(
{
	contains: function(i)
	{
		return this.indexOf(i) != -1;
	},

	indexOf: function(t, n)
	{
		for(var i = n || 0; i < this.length; i++)
			if(this[i] == t)
				return i;
		return -1;
	}
});

var Listeners =
{
	addEvent: function(t, fn)
	{
		if(this.addEventListener)
			this.addEventListener(t, fn, false);
		else if(this.attachEvent)
			this.attachEvent('on' + t, fn);
		return this;
	},

	removeEvent: function(t, fn)
	{
		if(this.removeEventListener)
			this.removeEventListener(t, fn, false);
		else if(this.dettachEvent)
			this.dettachEvent('on' + t, fn);
		return this;
	}
};

var El =
{
	addEvent: Listeners.addEvent,

	removeEvent: Listeners.removeEvent,

	getByClass: function(c, t)
	{
		var tmp = [];
		var els = this.getByTag(t || '*');
		for(var i = 0; i < els.length; i++)
			if(els[i].hasClass(c))
				tmp.push(els[i]);
		return tmp;
	},

	getByTag: function(t)
	{
		var tmp = [];
		var els = this.getElementsByTagName(t || '*');
		for(var i = 0; i < els.length; i++)
			tmp[i] = $extend(els[i], El);
		return tmp;
	},

	hasClass: function(c)
	{
		return this.className.contains(c, ' ');
	},

	htmlElement: true
};

El.extend = function(o)
{
	El = $extend(El, o);
};

String.extend(
{
	contains: function(t, s)
	{
		s = s || '';
		return (s+this+s).indexOf(s+t+s) != -1;
	},

	toInt: function(n)
	{
		return parseInt(this, n || 10);
	}
});

document.getByClass = El.getByClass;
document.getByTag = El.getByTag;
window.addEvent = Listeners.addEvent;
window.removeEvent = Listeners.removeEvent;

/*
 * Class and Objects
 */

/*
 * Selector functions
 */

function $(el)
{
	if(el.htmlElement)
		return el;
	el = document.getElementById(el);
	return el ? $extend(el, El) : null;
};

function $$()
{
	var elAr = [];
	for(var i = 0; i < arguments.length; i++)
	{
		var sel = arguments[i].split(' ');
		var tmp = [];
		for(var h = 0; h < sel.length; h++)
		{
			var rg;
			if(sel[h].contains('['))
			{
				rg = $$.css3;
				var m = sel[h].match(rg);
				if(!m)
					return null;
				var t = m[1], a = m[2], s = m[3], v = m[4];
				var parent = (h == 0) ? [document] : tmp;
				tmp = [];
				for(var j = 0; j < parent.length; j++)
					tmp = tmp.concat(parent[j].getByTag(t));
				var els = [];
				if(!s && !v)
				{
					for(var j = 0; j < tmp.length; j++)
						if(tmp[j].getAttribute(a) != null)
							els.push(tmp[j]);
					tmp = els;
				}
				else
				{
					switch(s)
					{
						default:
							for(var j = 0; j < tmp.length; j++)
								if(tmp[j].getAttribute(a) == v)
									els.push(tmp[j]);
							tmp = els;
							break;
					};
				}
			}
			else
			{
				rg = $$.simple;
				var m = sel[h].match(rg);
				if(!m)
					return null;
				var t = m[1] || '*', s = m[2], v = m[3];
				switch(s)
				{
					case '#':
						tmp = $(v) ? [$(v)] : [];
						break;
					case '.':
						var parent = (h == 0) ? [document] : tmp;
						tmp = [];
						for(var j = 0; j < parent.length; j++)
							tmp = tmp.concat(parent[j].getByClass(v, t));
						break;
					default:
						var parent = (h == 0) ? [document] : tmp;
						tmp = [];
						for(var j = 0; j < parent.length; j++)
							tmp = tmp.concat(parent[j].getByTag(t));
						break;
						
				};
			}
		}
		for(var h = 0; h < tmp.length; h++)
			if(!elAr.contains(tmp[h]))
				elAr.push(tmp[h]);
	}
	return elAr;
};

$$.simple = /([^\#\.]*)([\#\.]?)([^\#\.]*)/;
$$.css3 = /(\w+)\[(\w+)(?:([\^\$]*)\=[\"\']*([^\"\'\]]+)[\"\']*)?\]/;

/*
 * Onload functions
 */

window.addEvent('load', function()
{
	var ref = new Widget('widget-1', 'src.txt', 30000);
	if($('select-year'))
		setupPublications();
	if($('members'))
		setupMembers();
	var link = $$('#menu a');
	var dark = 'rgb(46, 43, 31)', light = '#625b46';
	link.each(function(el)
	{
		el.onmouseover = function()
		{
			new Fx(this, { effects: { backgroundColor: [light, dark] }, duration: 300 });
		};
		el.onmouseout = function()
		{
			new Fx(this, { effects: { backgroundColor: [dark, light] } });
		};
	});
	link = $$('#links a');
	link.each(function(el)
	{
		el.onmouseover = function()
		{
			var obj = {};
			//obj['borderColor'] = ['#696047', '#a79b7d'];
			obj['color'] = ['#9c9875', '#fff'];
			new Fx(this, { effects: obj, duration: 300 });
		};
		el.onmouseout = function()
		{
			var obj = {};
			//obj['borderColor'] = ['#a79b7d', '#696047'];
			obj['color'] = ['#fff', '#9c9875'];
			new Fx(this, { effects: obj });
		};
	});
}, false);

function selectYear()
{
	var el = $('select-year');
	var yr = el.value || el[el.selectedIndex].innerHTML;
	if(yr == 'Select a Year')
		return;
	var pubs = $$('#content .publications');
	pubs.each(function(el, i)
	{
		if(el.id != yr)
			el.close();
		else
			el.open(function()
			{
				el.style['height'] = 'auto';
			});
	});
}

function setupPublications()
{
	/*
	var t = '', s = '<option>Select a Year</option>';
	for(var yr in Publications)
	{
		t += '<div id="'+yr+'" class="publications">';
		t += '<table border="0" cellspacing="0" cellpadding="0">';
		t += '<tr class="head"><td colspan="2">Group Publications in Referred Journals: '+yr+'</td></tr>';
		for(var i in Publications[yr])
		{
			var item = Publications[yr][i];
			t += '<tr><td><a href="'+item.link+'"><img alt="" src="prev-1.jpg"></a></td><td>';
			t += '<h3>'+item.title+'</h3>';
			t += '<div class="pub">';
			t += '<p><span class="wrap">'+item.summary+'</span></p>';
			t += '<p class="abstract"><span class="wrap">'+item.abstract+'</span></p>';
			t += '<p class="authors"><span class="wrap">'+item.authors+'</span></p>';
			t += '<div class="pub-fx"><a href="javascript://">Abstract</a> | <a href="javascript://">Author</a> | <a href="javascript://">Summary</a></div>';
			t += '</td></tr>';
		}
		t += '</table></div>\n';
		s += '<option>'+yr+'</option>';
	}
	$('content').innerHTML += t;
	$('select-year').innerHTML = s;
	*/	

	var pubs = $$('#content .publications');
	var pub = $$('#content .pub');
	var btn = $$('#content .pub-fx');
	btn.each(function(el, i)
	{
		var a = el.getByTag('a');
		var p = pub[i].getByTag('p');
		var d = 50;
		a[0].onclick = function()
		{
			p[0].close.delay(d, p[0]);
			p[1].open();
			p[2].close.delay(d, p[2]);
		};
		a[1].onclick = function()
		{
			p[0].close.delay(d, p[0]);
			p[1].close.delay(d, p[1]);
			p[2].open();
		};
		a[2].onclick = function()
		{
			p[0].open();
			p[1].close.delay(d, p[1]);
			p[2].close.delay(d, p[2]);
		};
	});
	var l = $('select-year').selectedIndex - 1;
	if(l < 0)
		l = 0;
	pubs.each(function(el, i)
	{
		if(i != l)
			el.close();
	});
}

function setupMembers()
{
	var td = $$('#members .data');
	var fnEnter = function()
	{
		new Fx(this.nextElement(), { effects: { backgroundColor:['#e5e1ce', '#CFCCBB'] }, duration: 300 });
	};
	var fnLeave = function()
	{
		new Fx(this.nextElement(), { effects: { backgroundColor:['#CFCCBB', '#e5e1ce'] }, duration: 300 });
	};
	td.each(function(el)
	{
		el.hover(fnEnter.bind(el), fnLeave.bind(el));
	});
}

var fps = 1000/40;

function $time(){return new Date().getTime();}

if(!Array.prototype.forEach)
Array.prototype.forEach = function(fn)
{
	for(var i = 0; i < this.length; i++)
		fn.call(this, this[i], i, this);
};

Array.extend(
{
	forEach: function(fn)
	{
		for(var i = 0; i < this.length; i++)
			fn.call(this, this[i], i, this);
	},

	map: function(fn)
	{
		var tmp = [];
		for(var i = 0; i < this.length; i++)
			tmp[i] = fn.call(this, this[i], i, this);
		return tmp;
	}
});

Array.prototype.each = Array.prototype.forEach;

El.extend(
{
	addClass: function(c)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		this.className += ' '+c;
		this.chains.start(el);
		return this;
	},

	ajax: function(u, o)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		o = o || {};
		o.onComplete = o.onComplete || function(t)
		{
			el.innerHTML = t;
		};
		var xmlHttp = (window.XMLHttpRequest) ?  new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject("Msxml2.XMLHTTP") : false);
		if(!xmlHttp)
		{
			alert('Your browser does not support ajax');
			return this;
		}
		xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.readyState == 4)
			{
				o.onComplete.call(this, xmlHttp.responseText, xmlHttp.responseXML);
				el.chains.start(el);
			}
		};
		xmlHttp.open('get', u, true);
		xmlHttp.send(null);
		return this;
	},

	alert: function(t)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		alert(t);
		this.chains.start(el);
		return this;
	},

	animate: function(o)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		new Fx(this, o)
		return this;
	},

	chains:
	{
		chain: [],
		init: function(el, f, a)
		{
			var fn = function()
			{
				return f.apply(el, a);
			};
			this.chain.push(fn);
		},
		start: function(el)
		{
			el.ready = true;
			if(this.chain.length)
				this.chain.shift().call(this);
		}
	},

	click: function(fn)
	{
		this.addEvent('click', fn, false);
		return this;
	},

	close: function(o)
	{
		o = o || function(){};
		this.animate(
		{
			effects:
			{
				//opacity: [1, 0],
				height: [0]
			},
			duration: 600,
			onComplete: o
		});
		return this;
	},

	getStyle: function(p, s)
	{
		p = p.replace(/\-\w/g, function(m)
		{
			return m.charAt(1).toUpperCase();
		});
		switch(p)
		{
			case 'height': return this.clientHeight;
			case 'width': return this.clientWidth - this.style['paddingLeft'] - this.style['paddingRight'];
			default:
				var v = this.style[p];
				if((v == '' || s) && document.defaultView)
				{
					p = p.replace(/[A-Z]/g, function(m)
					{
						return '-' + m.charAt(0).toLowerCase();
					});
					var v = document.defaultView.getComputedStyle(this, null).getPropertyValue(p);
				}
				return v;
		};
	},

	hasChild: function(el)
	{
		return $A(this.getElementsByTagName('*')).contains(el);
	},

	hide: function(d)
	{
		var cO = this.getStyle('opacity', true) || 1;
		cO = cO.toInt();
		this.animate(
		{
			effects:
			{
				opacity: [cO, 0]
			},
			duration: d
		});
		return this;
	},

	hover: function(over, out)
	{
		var fnOver = function(e)
		{
			if(e.relatedTarget != this && !this.hasChild(e.relatedTarget))
				over.call(this);
		};
		var fnOut = function(e)
		{
			if(e.relatedTarget != this && !this.hasChild(e.relatedTarget))
				out.call(this);
		};
		this.addEvent('mouseover', fnOver.bind(this));
		this.addEvent('mouseout', fnOut.bind(this));
		return this;
	},

	nextElement: function()
	{
		var el = this.nextSibling;
		while(!el.htmlElement)
			el = el.nextSibling;
		return el;
	},

	open: function(o)
	{
		o = o || function(){};
		var tH = this.scrollHeight;
		this.animate(
		{
			effects:
			{
				//opacity: [0, 1],
				height: [tH]
			},
			duration: 600,
			onComplete: o
		});
		return this;
	},

	ready: true,

	removeClass: function(c)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		this.className = (' '+this.className+' ').replace(' '+c+' ');
		this.chains.start(el);
		return this;
	},

	set: function(t)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		this.innerHTML = t;
		this.chains.start(el);
		return this;
	},

	show: function(d)
	{
		this.animate(
		{
			effects:
			{
				opacity: [0, 1]
			},
			duration: d
		});
		return this;
	},

	toggleClass: function(c)
	{
		var el = this;
		if(!this.ready)
		{
			this.chains.init(el, arguments.callee, arguments);
			return this;
		}
		this.ready = false;
		!this.hasClass(c) ? this.addClass(c) : this.removeClass(c);
		this.chains.start(el);
		return this;
	}
});

Function.extend(
{
	bind: function(o)
	{
		var f = this;
		return function()
		{
			f.apply(o, arguments);
		};
	},

	delay: function(t, o)
	{
		var f1 = this;
		var f2 = function()
		{
			return f1.apply(o || this);
		};
		return setTimeout(f2, t);
	},

	periodical: function(t, o)
	{
		var f1 = this;
		var f2 = function()
		{
			return f1.apply(o || this);
		};
		return setInterval(f2, t);
	}
});

String.extend(
{
	toRgb: function()
	{
		if(this.contains('rgb'))
			return this.match(/rgb\((\d+)\,\s?(\d+)\,\s?(\d+)\)/).slice(1).map(function(n)
			{
				return n.toInt();
			});
		else if(this.contains('#'))
			return this.match(/\#(\w{1,2})(\w{1,2})(\w{1,2})/).slice(1).map(function(n)
			{
				return (n.length == 1) ? (n+n).toInt(16) : n.toInt(16);
			});
	}
});

Number.prototype.toInt = String.prototype.toInt;

function Class(o)
{
	var k = function()
	{
		return this.init ? this.init.apply(this, arguments) : this;
	}
	k.prototype = o; return k;
}

var Fx = new Class(
{
	init: function(el, options)
	{
		this.el = $(el);
		Garbage.add(this.el);
		clearInterval(this.el.timer);
		this.style = options.effects;
		this.from = {}; this.to = {}; this.unit = {};
		for(var p in this.style)
		{
			this.from[p] = this.style[p][0]; this.to[p] = this.style[p][1];
			if(!this.to[p] && this.to[p] != 0)
				this.to[p] = this.from[p], this.from[p] = this.el.getStyle(p);
			if(p == 'opacity' || p.toLowerCase().contains('color'))
				this.unit[p] = '';
			else
				this.unit[p] = 'px', this.from[p] = this.from[p].toInt();
		}
		this.now = {};
		this.d = options.duration || 600;
		this.o = options.onComplete || function(){};
		this.time = new Date().getTime();
		this.el.timer = this.step.periodical(fps, this);
		return this;
	},

	calc: function()
	{
		for(var p in this.from)
		{
			var i = this.delta;
			var delta = (i <= 0.5) ? Math.pow(2*i, 2) / 2 : (2 - Math.pow((2 * (1 - i)), 2)) / 2;
			if(!p.toLowerCase().contains('color'))
				this.now[p] = ((this.to[p] - this.from[p]) * delta) + this.from[p];
			else
			{
				var f = this.from[p].toRgb();
				var t = this.to[p].toRgb();
				var tmp = [];
				for(var h = 0; h < f.length; h++)
					tmp[h] = (((t[h] - f[h]) * delta) + f[h]).toInt();
				this.now[p] = 'rgb(' + tmp.join() + ')';
			}
		}
	},

	increase: function()
	{
		for(var p in this.now)
		{
			this.el.style[p] = this.now[p] + this.unit[p];
		}
	},

	set: function()
	{
		this.now = this.to;
		this.increase();
	},

	step: function()
	{
		var time = new Date().getTime();
		if(time < (this.time + this.d))
		{
			this.delta = (time - this.time) / this.d;
			this.calc();
			this.increase();
		}
		else
		{
			clearInterval(this.el.timer);
			this.set();
			this.o.call(this, this.el);
			this.el.chains.start(this.el);
		}
	}
});

var Widget = new Class(
{
	init: function(el, u, d)
	{
		this.el = $(el);
		var str;
		var obj = this;
		this.el.hide().ajax(u,
		{
			onComplete: function(t)
			{
				obj.t = eval(t);
				obj.current = 0;
				obj.change();
				obj.timer = obj.change.periodical(d || 5000, obj);
			}
		});
		return this;
	},

	change: function()
	{
		this.el.hide().set('&gt; ' + this.t[this.current++]).show(1000);
		if(this.current > (this.t.length - 1))
			this.current = 0;
	}
});

Garbage =
{
	elem: [],
	add: function(el)
	{
		if(!this.elem.contains(el))
			this.elem.push(el);
	},

	clean: function()
	{
		for(var i = 0; i < this.elem.length; i++)
			clearInterval(this.elem[i].timer);
	}
};

window.addEvent('unload', function()
{
	var link = $('menu').getElementsByTagName('a');
	link.each = Array.prototype.each;
	link.each(function(el)
	{
		el.onmouseover = null; el.onmouseout = null;
	});
	Garbage.clean();
}, false);
