var ShowCase = new Class({
	
	Implements: Options,
	
	options: {
		'regex': /normal\.jpg$/,
		'overview': null,
		'walkDelay': 5000,
		'activeClass': 'active'
	},
	
	initialize: function(oContainer, options){
		this.moContainer = $(oContainer);
		this.moChildren = this.moContainer.getElements('div.sc_image');
		this.setOptions(options);
		
		this.build();
	},
	
	build: function(){
		if (!this.moContainer){
			return;
		}
		
		this.moLabel = this.moContainer.getElement('div.sc_label');
		
		this.moContainer.getElements('div.sc_image img.loading').each(function(oImg){
			// bind events
			oImg.addEvent('mouseover', this.handleMouseOver.bind(this));
			oImg.addEvent('mouseout', this.handleMouseOut.bind(this));
			
			var o_container = oImg.getParent('div.sc_image');
			
			if ($type(oImg.get('width')) === null){
				// after files are loaded, enhance them
				oImg.addEvent('load', function(){
					this.enhanceImage(o_container, oImg);
				}.bind(this));
				return;
			}
			
			// image is already loaded, enhance now
			this.enhanceImage(o_container, oImg);
		}, this);
		
		this.nextItem();
		this.startWalking();
	},
	
	nextItem: function(){
		// start over when first time or at the end of list
		if ($type(this.miItemCounter) === false || this.miItemCounter === this.moChildren.length){
			this.miItemCounter = 0;
		}
		
		var o_cur_item = this.moChildren[this.miItemCounter];
		if (!o_cur_item){
			return;
		}
		
		this.setActive(o_cur_item, true);
		this.miItemCounter += 1;
	},
	
	startWalking: function(){
		this.moWalkEvent = this.nextItem.periodical(this.options.walkDelay, this);
	},
	
	stopWalking: function(){
		$clear(this.moWalkEvent);
	},
	
	setActive: function(oEl, bReset){
		bReset = ($type(bReset) && bReset) || false;
		
		if (bReset === true){
			this.moContainer.getElements('div.sc_image.active').each(function(oActive){
				oActive.removeClass(this.options.activeClass);
			}, this);
		}
		
		oEl.addClass(this.options.activeClass);
		this.moLabel.getElements('a')[0].set('text', oEl.getElement('img').get('alt'));
		this.moLabel.getElements('a')[0].set('href', '/' + oEl.getElement('img').get('alt'));
		this.changeOverviewImage(oEl.getElement('img'));
	},
	
	enhanceImage: function(oCont, oImg){
		// enhance image
		var o_gay_img = new GayImage({
			'imageSource': oImg.get('src'),
			'container': oCont,
			'valign': 'face',
			'width': 38,
			'height': 38,
			'onComplete': function(){
				oImg.removeClass('loading');
			}
		});
		o_gay_img.setImage(oImg, false);
	},

	handleMouseOver: function(oEvent){
		this.stopWalking();
		this.setActive($(oEvent.target).getParent('div.sc_image'));
	},
	
	handleMouseOut: function(){
		this.startWalking();
	},
	
	changeOverviewImage: function(oImg){
		var s_new_image = oImg.get('src').replace(this.options.regex, 'original.jpg'),
			o_overview = $(this.options.overview),
			o_asset;
		
		if (o_overview.get('src') === s_new_image){
			return;
		}
			
		o_asset = new Asset.image(s_new_image, {
			'onload': function(oNewImg){
				var o_parent = o_overview.getParent('div.sc_ov_image'),
					o_gay_img;
				
				if (o_parent === null){
					return;
				}
				
				// fade current image
				o_overview.set('tween', {
					'duration': 100,
					'onComplete': function(){
						// destroy image
						if (o_overview.getParent('a') !== null){
							o_overview.getParent('a').destroy();
						} else {
							o_overview.destroy();
						}
						
						// create link 
						var o_link = new Element('a', {
							'href': '/' + oImg.get('alt')
						});
						
						oNewImg.set('id', this.options.overview);
						o_link.adopt(oNewImg);
						o_link.inject(o_parent, 'top');
						o_parent.removeClass('loading');
						
						o_gay_img = new GayImage({
							'imageSource': oNewImg.get('src'),
							'container': o_parent,
							'valign': 'face',
							'width': o_parent.getSize().x,
							'height': o_parent.getSize().y
						});
						o_gay_img.setImage(oNewImg, false);
					}.bind(this)
				});
				
				o_overview.fade('out');
			}.bind(this)
		});
	}
});
