var _controller_instance = null;

var Controller = new Class({

	Implements: [Options, Chain, Events],
	
	options: {
		module: 'home',
		action: 'index',
		base: '/js/',
		modulepath: 'modules/',
		lang: 'en',
		data: false
	},
	
	child: false,
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.chain(
			function() { this.loadModule() },
			function() { this.render() }
		);
		
		this.callChain();
		
		/*$$('a[href$=advert/place]').each(function(el){
			el.set('href', 'http://www.classics.nl/advert/jsplace');
		});*/
		
		var _lrph = $('loginregisterplacehide');
	
		if ( _lrph != null ) {
			_lrph.addEvent('click', function(e){
				e = new Event(e).stop();
			
				this.setStyle('display', 'none');
				$('loginregisterplace').setStyle('display', 'block');
			});
		}
		
		var rotator = $('rotator2');
	
		if ( rotator != null && $type(rotatorimages) != 'undefined') {
			
			var myImages = new Asset.images(rotatorimages, {
				onComplete: function() {
					
					myImages[0].inject(rotator);
					myImages[1].inject(rotator, 'top');
					//return;
					var out = null;
					
					i = 2;
					last = (myImages.length - 1);
					
					var rotate = function(){
						if ( out !== null ) {
							myImages[out].dispose();
						}
						
			
						var fx = new Fx.Tween(myImages[i], {duration: 2000}).set('margin-left', (-1 * myImages[i].get('width')) );
						myImages[i].inject(rotator, 'top');
						
						//sec = i;
						
						out = ( i - 2 );
						out = (out < 0 ? last + out : out);
						
						i++;
						i = (i > last ? 0 : i);
							
						
						(function() {
							fx.start('margin-left', '0').chain(rotate);
						}).delay(2000);
					};
					
					rotate();
				}
			});
			
		}
		
		var currenttip = false;
		var tips = new Tips('.currencytip', {
			className: 'rscurrencytip',
			fixed: true,
			offset: {'x': 0, 'y': 20},
			
			onShow: function(tip, el){
				tip.setStyle('visibility', 'visible');
				tip.setStyle('opacity', 0.95);
				
				currenttip = el;
			},
			
			onHide: function(tip, el) {
				tip.setStyle('visibility', 'hidden');
				currenttip = false;
			}
		});
		
		$$(window, 'div.inner').addEvent('scroll', function(e) {
			if ( currenttip != false ) {
				//tips.elementMove({page: currenttip.getPosition()});
				tips.hide(currenttip);
			}
		});
		
		$$('.currencytip').each(function(extend){
			extend.addEvent('click', function(e){
				e = new Event(e).stop();
			});
			rel = extend.get('rel').split('||');
			
			var tiptext = rel[1] + '<ul>';
			var f = true;
			rel[2].split('::').each(function(p){
				if (f) {
					tiptext += '<li class="fixed">' + p + '</li>';
					f = false;
				} else {
					tiptext += '<li>' + p + '</li>';
				}
			});
			tiptext += '</ul>';
			extend.store('tip:title', rel[0]);
			extend.store('tip:text', tiptext);
		});

	},
	
	loadModule: function() {
		var moduleJs = new Asset.javascript(this.options.base + this.options.modulepath + this.options.module.toLowerCase() + '.js', {
			onload: function() {
				var className = this.options.module + '-module';
				className = className.camelCase();
				this.setChildClass(new window[className](this.options.data));
				this.callChain();
				this.fireEvent('onModuleReady', this.getModule());
			}.bind(this)
		});
	},
	
	setChildClass: function(c) {
		if ( $type(c) == 'object' ) this.child = c;
	},
	
	render: function() {
		if ( this.child !== false ) {
			this.child.setBase(this.options.base);
			this.child.call(this.options.action);
		}
	},
	
	getModule: function() {
		return this.child;
	}
});

function getController(options) {
	if ( _controller_instance == null ) {
		_controller_instance = new Controller(options);
	}
	
	return _controller_instance;
}
