
var IGallery = {

	init: function () {
		
		// scan anchors for those opening a MOOdalBox
		this.holder = $('i-holder');
		this.links = [];
		this.active = 0;
		$A($$('a')).each(function(el, i){
			if(el.parentNode.parentNode && el.href && el.parentNode.parentNode.className == "i-list") {
				el.index = this.links.length;
				el.onclick = this.click.pass(el, this);
				this.links.push(el);
			}
		}, this);
		
		$('i-next').onclick = this.nclick.pass(false,  this);
		$('i-prev').onclick = this.pclick.pass(false,  this);
		
		// init the effects
		this.fx = {
			holder: 	this.holder.effect('opacity', { duration: 500 }).start(0, 1)
		};
	},
	
	nclick: function(el) {
		if (this.links[this.active+1])
		{
			this.active++;
			return this.open(this.links[this.active].href, this.links[this.active].rel);
		}
	},
	pclick: function(el) {
		if (this.links[this.active-1])
		{
			this.active--;
			return this.open(this.links[this.active].href, this.links[this.active].rel);
		}
	},
	
	click: function(link) {
		this.active = link.index;
		return this.open(link.href, link.rel);
	},

	open: function(sLinkHref, sLinkRel) {		
		this.href = sLinkHref;
		this.rel = sLinkRel;
		//this.active = this.index;
		if (this.href.indexOf("#") == -1)
		{
			var _width = "389";
			var _height = "264";
			if (this.rel)
			{
				var sets = this.rel.split("x");
				if (sets.length == 2)
				{
					_width = sets[0];
					_height = sets[1];
				}
			}
			this.holder.setHTML('<img src="'+this.href+'" alt="" width="'+_width+'" height="'+_height+'"  /> ');
			this.fx.holder.start(0, 1);
		}
		return false;
	}
};

// startup
Window.onDomReady(IGallery.init.bind(IGallery));