var Gallery = new Class({
	
	nodes: new Array(),
	duration: 500,
	itemon: null,
	itemson: new Array(),
	feed: null,
	sets: new Array(),
	container: null,
	currentset: 0,
	rows: 6,
	height: 120,
	count: 48,
	searches: new Array(),
	tiger: false,
	searchterms: new Array(),
	zoom: null,
	
	initialize: function(feed) {
		this.feed = feed;
		this.container = new Element('div').setProperty('class','boxw4 boxh4').injectInside($('images')).setStyles({position:'relative',top:0,left:0});
		this.container.effects = new Fx.Morph(this.container,{duration:this.duration});
		$('images').setStyles({overflow:'hidden',position:'relative'});
		$('next_set').addEvent('click',function(){this.nextSet()}.bind(this));
		$('next_set').addEvent('mousedown',function(){$('next_set').setStyle('opacity',.5)}.bind(this));
		$('next_set').addEvent('mouseup',function(){$('next_set').setStyle('opacity',1)}.bind(this));
		$('previous_set').addEvent('mousedown',function(){$('previous_set').setStyle('opacity',.5)}.bind(this));
		$('previous_set').addEvent('mouseup',function(){$('previous_set').setStyle('opacity',1)}.bind(this));
		$('previous_set').addEvent('click',function(){this.viewPrevSet()}.bind(this));
		$('searchbutton').addEvent('click',function(){this.search()}.bind(this));
		$('searchbutton').addEvent('mousedown',function(){$('searchbutton').setStyle('opacity',.5)}.bind(this));
		$('searchbutton').addEvent('mouseup',function(){$('searchbutton').setStyle('opacity',1)}.bind(this));
		$('searchinput').addEvent('keydown',function(e){if(e.key=='enter')this.search()}.bind(this))
		this.nextSet();
	},
	
	loadNextSet: function(set,search) {
		if(search)set = 0;
		var req = new Request.JSON({url:this.feed,onComplete:function(data){this.loadedJson(data,search)}.bind(this)}).get({start:set,keyword:search});
	},
	
	tryScroll: function(search) {
		if(search)
			this.gotoSet(this.sets.length-1)
		else if(this.sets[this.currentset+1])
			this.viewNextSet();
	},
	
	search: function() {
		var term = $('searchinput').getProperty('value');
		if(!this.searchterms.contains(term)) {
			this.searchterms.push(term);
			this.nextSet(term);
		}
	},
	
	nextSet: function(search) {
		if(!this.sets[this.currentset+1])
			this.loadNextSet(this.sets.length+1,search);
		else
			this.viewNextSet();
	},
	
	viewNextSet: function() {
		var moveto = -((this.rows*this.height)*(this.currentset+1));
		if(this.container.effects)this.container.effects.cancel();
		this.container.effects.start({top:moveto});
		if(this.sets[this.currentset+1])this.currentset++;
	},
	
	viewPrevSet: function() {
		var moveto = -((this.rows*this.height)*(this.currentset-1));
		if(this.container.effects)this.container.effects.cancel();
		if(this.currentset!=0) {
			this.container.effects.start({top:moveto});
			this.currentset--;	
		}
	},
	
	gotoSet: function(set) {
		if(this.container.effects)this.container.effects.cancel();
		this.container.effects.start({top:-(set*(this.rows*this.height))});
		if(this.sets[set])this.currentset = set;
	},
	
	loadedJson: function(json,search) {
		if(json.images.length!=0)this.createNodes(json.images,search);
	},
	
	createNodes: function(nodes,search) {
		var shudpush = false;
		var a = new Array();
		nodes.each(function(item,index) {
			if(item) {
				var i = this.createElement({small:item.asset_thumb,large:item.asset_download,title:item.asset_title},((index)==nodes.length-1)?true:false,search).injectInside(this.container);
				if(this.sets[this.sets.length-1]&&this.sets[this.sets.length-1].length<this.count)
					this.sets[this.sets.length-1].push(a)
				else
					a.push(a);
			}
		}.bind(this));
		if(a.length!=0) {
			if(this.sets[this.sets.length-1]) {
				if(this.sets[this.sets.length-1].length==this.count)this.sets.push(a);
			} else
				this.sets.push(a);
		}
	},
	
	createElement: function(url,last,search) {
		var e = new Element('div').setStyles({width:120,height:120,float:'left',position:'relative'});
		var a = new Element('a').setStyles({width:120,height:120,display:'block',position:'absolute',top:0,left:0,background:'#000',opacity:.6});
		a.setProperties({'href':url.large,rel:'lightbox[gallery]',title:url.title});
		a.effects = {up:new Fx.Morph(a,{duration:300}),down:new Fx.Morph(a,{duration:300})};
		a.addEvent('mouseenter',function(e){this.transUp(e.target)}.bind(this));
		a.addEvent('mouseleave',function(e){this.transDown(e.target)}.bind(this));
		var i = new Element('img').setProperty('src',url.small);
		i.last = last;
		i.addEvent('load',function(e){
			this.fadeUpImage(i,search);
		}.bind(this));
		i.injectInside(e);
		a.injectInside(e);
		Slimbox.scanPage();		
		return e;
	},
	
	fadeUpImage: function(image,search) {
		if(image.last)this.tryScroll(search);
	},
		
	random: function() {
		if(this.itemson.length>0) {
			this.itemson.each(function(item,index) {
				this.transDown(item);		
			}.bind(this));
		}
		for(var i=0;i<3;i++) {
			var n = this.randomImage();
			this.transUp(n);
			this.itemson[i] = n;
		}
	},
	
	transUp: function(item) { 
		if(item.effects.down)item.effects.down.cancel();
		item.effects.up.start({opacity:.1});
	},
	
	transDown: function(item) {
		if(item.effects.up)item.effects.up.cancel();
		item.effects.down.start({opacity:.6});
	},
	
	randomImage: function() {
		return this.nodes[Math.floor(Math.random()*this.nodes.length)];
	}
	
});