/**
 * Controller class
 * 
 * base class for page specific controllers
 */
var Controller = new Class({
	
	Implements: Options,
	
	options: {
		useSifr	: true
	},
	
	initialize: function() {
		this.setupSifr();
		this.setupTicker();
		
		$$('#ads a').addEvent('click', this.popUp.bindWithEvent(this));
		$$('a.popup').addEvent('click', this.popUp.bindWithEvent(this));
	},
	
	setupSifr: function() {
		if (this.options.useSifr) {
			var longview = {src: '/flash/longview.swf'};
			sIFR.compatMode = true;
			sIFR.activate();
			
			sIFR.replace(longview, {
				selector: '.module>h3',
				wmode: 'transparent',
				css: [
					'.sIFR-root { color: #FFFFFF; letter-spacing: 1; }',
					'.sIFR-root a { color: #FFFFFF; text-decoration: none; }',
					'.sIFR-root a:hover { color: #FFFFFF; text-decoration: none; }'	
				]
			});
			sIFR.replace(longview, {
				selector: '.module>h2',
				wmode: 'transparent',
				css: '.sIFR-root { color: #FFFFFF; letter-spacing: 1; }'
			});
		}
	},
	
	setupTicker: function() {
		var ticker = $('ticker');
		if (ticker) {
			this.ticker = new Ticker(ticker);
			ticker.getElements('a').addEvent('click', this.popUp.bindWithEvent(this));
		}
	},
	
	popUp: function(ev) {
		var url = (ev.target.href) ? ev.target.href : $(ev.target).getParent('a').href;
		window.open(url);
		ev.preventDefault();
	}
	
});

/**
 * HomeController class
 * 
 * features of the homepage
 */
var HomeController = new Class({
	
	Extends : Controller,
	
	initialize: function() {
		this.parent();
		this.slideshow = new Slideshow('slideshow', {width : 530, delay : 7000});
	}
	
});

var AdminController = new Class({
	
	Extends : Controller,
	
	initialize: function() {
		
		tinyMCE.init({
			mode : "textareas"
		});
		
	},
	
	showImages: function() {
		this.dialog.open();
		this.dialog.center();
	},
	
	iframeLoaded: function() {
		this.imagesIframe.contentWindow.addEvent('selected', this.addImage.bind(this));
	},
	
	addImage: function(src) {
		this.editor.exec('img', src);
	},
	
	updateEditor: function() {
		this.editor.toTextarea();
	}
	
});

var ImagesController = new Class({
	
	Extends: Controller,
	
	initialize: function() {
		$$('#photos a').each(function(link) {
			link.addEvent('click', this.selected.bindWithEvent(this, [link]));
		}, this);
	},
	
	selected: function(ev, link) {
		ev.preventDefault();
		window.fireEvent('selected', link.href);
	}
	
});

/* initialisation */

Controller.setup = function() {
	
	switch (document.body.id) {
		case 'home':	return new HomeController();
		case 'admin':	return new AdminController();
		case 'images':	return new ImagesController();
		default:		return new Controller();
	}
	
}

var app;
window.addEvent('domready', function() {
	app = Controller.setup();
});