var ShowCase = new Class({
	
	Implements: Options,
	
	moContainer: null,
	moBig: null,
	msSmallClass: null,
	moChildren: null,
	
	options: {
		'loopDelay': 2000,
		'search': 'small',
		'replace': 'normal',
		'activeClass': 'active'
	},
	
	initialize: function(oContainer, oBig, sSmallClass, options){
		this.moContainer = $(oContainer);
		this.moBig = $(oBig);
		this.msSmallClass = sSmallClass;
		
		this.setOptions(options);
		this.build();
	},
	
	build: function(){
		var o_this = this;
		this.moChildren = this.moContainer.getChildren('div.' + this.msSmallClass);
		if (this.moChildren.length == 0){
			return;
		}
		
		this.moChildren.each(function(oEl){
			// init settings
			oEl.s_showcase = oEl.get('text');
			if (oEl.getChildren('a').length > 0){
				oEl.s_url = oEl.getChildren('a')[0].get('href');
				oEl.setStyle('cursor', 'pointer');
				
				// add click
				oEl.addEvent('click', function(oEvent){
					// open in new window?
					if (oEvent.meta === true || oEvent.control === true
						|| oEvent.shift === true)
					{
						window.open(oEvent.target.s_url);
					} else {
						location = oEvent.target.s_url;
					}
				});
			}
			
			// clean element
			oEl.set('text', '');
			
			// add behaviour
			oEl.addEvent('mouseenter', function(oEvent){
				o_this.setBig(oEvent.target);
				o_this.stopLoop();
			}).addEvent('mouseleave', function(oEvent){
				o_this.startLoop();
			});
		});
		
		// init showcase
		this.nextItem();
		this.startLoop();
	},
	
	setBig: function(oEl){
		if (this.moBig.getChildren('div').length > 0){
			this.moBig.getChildren('div')[0].set('text', oEl.s_showcase);
		}
		
		var s_image_url = oEl.getStyle('background-image').replace(/^url\(([^)]+)\)$/, '$1');
		var s_big_url = s_image_url.replace(this.options.search, this.options.replace);
		this.moBig.setStyle('background-image', 'url(' + s_big_url + ')');
		
		if (arguments.length > 1 && arguments[1] === true){
			// remove highlighting from others
			var o_this = this;
			this.moContainer.getChildren('div.' + this.msSmallClass + '.' + this.options.activeClass).each(function(oEl){
				oEl.removeClass(o_this.options.activeClass);
			});
			oEl.addClass(this.options.activeClass);
		}
	},
	
	startLoop: function(){
		this.oLoopEvent = this.nextItem.periodical(this.options.loopDelay, this);
	},
	
	stopLoop: function(){
		$clear(this.oLoopEvent);
	},
	
	nextItem: function(){
		if ($type(this.miItemCounter) === false || this.miItemCounter === this.moChildren.length){
			this.miItemCounter = 0;
		}
		var o_cur_item = this.moChildren[this.miItemCounter];
		this.setBig(o_cur_item, true);
		this.miItemCounter += 1;
	}
});