var w = this, iA = {};

iA.Lists = (function () {
	function popup(o) {
		w.$(o).click(
			function () {
				var a = w.$(this), h = a.attr('class').replace(/h([0-9]{3})/, '$1'),
					width = 546, height = h ? h : 552;
				w.open(this.href, '', 'width=' + width + ',height=' + height + ',menubar=no,toolbar=no,status=no');
				return false;
			}
		);
	}
	
	function init() {
		w.$('body.completed #list > ul > li:has(ul) > a').toggle(
			function () {
				w.$(this).parent().find('ul').slideUp();
				w.$(this).parent().find('a').removeClass('active');
				return false;
			},
			function () {
				w.$(this).addClass('active');
				w.$(this).parent().children('ul').slideDown();
				return false;
			}
		);
		w.$('#list > ul > li > ul > li > a').click(
			function () {
				w.$('#project-sample ul li').remove();
				w.$(this).parent().find('ul li').clone().appendTo('#project-sample ul');
				popup('#project-sample a');
				w.$('#project-sample ul').show();
				return false;
			}
		);
		popup('body.on-the-board #list ul a, #list ul ul ul a, #project-sample a');
	}
	
	return {init: init};
}());

iA.Project = (function () {
	var a, a_id = 0, anchors = w.$('#project .gallery a'),
	interval, delay = 4000;
	
	function change(x) {
		if (this === w) {
			a_id = x;
			a = w.$('#project .gallery a:eq(' + a_id + ')');
		}
		else {
			w.clearInterval(interval);
			a = w.$(this);
		}
		
		anchors.removeClass('active');
		w.$('#display').append('<img style="display: none;" src="' + a.attr('href') + '" alt="[ ' + a.attr('title') + ' photo ]" title="' + a.attr('title') + '" height="288" width="506" />');
		a.addClass('active');
		w.$('#display img:last').fadeIn(
			function () {
				w.$('h2 strong').html(a.attr('title'));
				w.$('#display img:first').remove();
			}
		);
		return false;
	}
	
	function rotate() {
		change((a_id + 1) % anchors.length);
	}
	
	function cache() {
		w.$('body').append('<div id="cache" style="display: none;"></div>');
		anchors.each(
			function () {
				w.$('#cache').append('<img src="' + this + '" alt="" />');
			}
		);
	}
	
	function init() {
		cache();
		w.$('#project #body h2').append(' <span id="close"><a href="../" onclick="w.close(); return false;">x</a></span>');
		if (anchors.length > 1) {
			anchors.click(change);
			interval = w.setInterval(rotate, delay);
		}
	}
	
	return {init: init};
}());

w.$(w.document).ready(
	function () {
		iA.Lists.init();
		iA.Project.init();
	}
);
