if (Browser.Engine.trident) {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) ieversion=new Number(RegExp.$1);
}

if (Browser.Engine.trident && Browser.Engine.version <= 4) {
	window.addEvent('load', function() {
		new Asset.css('http://images.teleticketservice.com/global/css/dynamic.css');
		if (ieversion < 8) new Asset.css('http://images.teleticketservice.com/global/css/ie_dynamic.css');
	});
}
else if (Browser.Engine.trident) {
	new Asset.css('http://images.teleticketservice.com/global/css/dynamic.css');
	if (ieversion < 8) new Asset.css('http://images.teleticketservice.com/global/css/ie_dynamic.css');
}
else {
	new Asset.css('http://images.teleticketservice.com/global/css/dynamic.css');
}

window.addEvent('domready', function() {
	if ($('contactform')) new contactform;
	if ($('hotnews')) new hotnews;
	if ($('calendar')) new calendar;
	if ($('tips')) new tips;
	if ($('billboard')) new billboard('http://www.teleticketservice.com/tts/typo3conf/ext/json/genjson.php', $('billboard'));	
	new addthis();
	
	if (Browser.Engine.trident) new ie_enhancements; // hierover geen grappen...
});

var calendar = new Class({
	initialize: function() {
		$('calendar').getElement('tbody').getElements('tr').each(function(el){
			el.addEvent('click', function(){
				window.location = el.getElement('td.links').getElement('a').get('href');
			});
		});
	}
});

var addthis = new Class({
	initialize: function() {
		$(document.body).getElements('.addthis').addEvents({
		    'mouseover': function(el){
		        return addthis_open(this, '', '[URL]', '[TITLE]');
		    },
			'mouseout': function(el){
		        addthis_close();
		    },
		    'click': function(el){
		        return addthis_sendto();
		    }
		});
	}
});

var contactform = new Class({
	initialize: function() {
		$('subject').addEvent('change', function(el) { this.displayitem(el)}.bind(this));
		sel = $('subject').value;
		if (sel != 0) {
			$('contactform').getElement('.subjects').set('styles', {'display': 'block'});
			$('contactform').getElement('.subject_'+sel).set('style', 'display:block');
		}
	},
	displayitem: function(el) {
		sel = el.target.value;
		if (sel != 0) {
			$('contactform').getElement('.subjects').set('styles', {'display': 'block'});
						
			$('contactform').getElements('.subject').each(function(el, index){
				el.set('style', 'display: none');
			});
			
			$('contactform').getElement('.subject_'+sel).set('style', 'display:block');
		}
		else {
			$('contactform').getElement('.subjects').set('styles', {'display': 'none'});
		}
	}
});

var tips = new Class({
	initialize: function() {
		this.sel = 'new';
		this.displayitem(this.sel);
					
		$('tips').getElements('h2').addEvent('click', function(el) {
			newsel = el.target.getParent().get('class').split(' ');
			if (this.sel != newsel[0]) this.displayitem(newsel[0]);
		}.bind(this));
	},
	displayitem: function(sel) {
		$('tips').getElement('div.' + this.sel).removeClass('selected');
		$('tips').getElement('div.' + sel).addClass('selected');
		this.sel = sel;
	}
});

var billboard = new Class({
	initialize: function(url, dest) {
		this.dest = dest;
		this.base = 'http://www.teleticketservice.com/tts/';
		
		this.url = url;
		
		this.duration = 230;
				
		if ((typeof(billboard_data) != 'undefined')) {
			this.startup();
		}
		else {
			this.dest.getElement('.billboard_image').setStyle('visibility', 'visible');
		}
	},
	go_url: function(e){
		e.stop();
		window.location = this.dest_url;
	},
	startup: function() {
		var jsonRequest = new Request.JSON({url: this.url, 
			onComplete: function(resp){
				if (resp.items && resp.items.length > 1) {
					this.construct();
					this.current_image = 0;
					this.total = resp.items.length-1;
					this.items = resp.items;
					this.preload(0, 'onStartLoaded');
				
					window.addEvent('onStartLoaded', function(){
						this.dest.getElement('img').set('src', this.base + this.items[0].image);
						this.dest_url = this.items[0].url;
						this.dest.getElement('a').set('href', this.dest_url);
						this.dest.getElement('.billboard_image').setStyle('visibility', 'visible');
					}.bind(this));
				
					this.positions = {
						'current': {
							'margin-top': '0',
							'margin-left': '0',
							'opacity': '1',
							'width': '350px'
						},
						'next': {
							'margin-top': '52px',
							'margin-left': '37px',
							'opacity': '0',
							'width': '275px'
						},
						'previous': {
							'margin-top': '-48px',
							'margin-left': '-34px',
							'opacity': '0',
							'width': '418px'
						}
					};				
				}
				else {
					this.dest.getElement('.billboard_image').setStyle('visibility', 'visible');
				}
			}.bind(this),
			onFailure: function() {
				this.dest.getElement('.billboard_image').setStyle('visibility', 'visible');
			}.bind(this),
			onCancel: function() {
				this.dest.getElement('.billboard_image').setStyle('visibility', 'visible');
			}.bind(this)		
		}).get({'cat': billboard_data['topcategory_id'], 'subcat': billboard_data['subcategory_id'], 'lang': language});
	},
	construct: function() {
		this.controls = new Element('div', {'class':'controls'}).inject(this.dest);
		this.btn_next = new Element('div', {'class':'next'}).inject(this.controls).addEvent('click', function(){
			this.next();
		}.bind(this));
		this.btn_prev = new Element('div', {'class':'prev'}).inject(this.controls).addEvent('click', function(){
			this.previous();
		}.bind(this));
		
		this.temp_a = new Element('img', {'class':'temp'}).set('style', 'visibility: hidden').inject(this.dest);
		this.temp_b = new Element('img', {'class':'temp'}).set('style', 'visibility: hidden').inject(this.dest);
		
		
		window.addEvent('prevImageLoaded', function(){
			this.temp_a.set('src', this.base + this.items[this.current_image].image);
			this.temp_a.set('styles', {'visibility':'visible', 'z-index':'50'});
			this.temp_a.setStyles(this.positions['current']);
			this.temp_b.set('src', this.base + this.items[this.prev_image].image);
			this.temp_b.set('styles', {'visibility':'visible', 'z-index':'0'});
			this.temp_b.setStyles(this.positions['previous']);
			this.dest.getElement('.billboard_image').set('style', 'visibility: hidden');
			
					
			this.temp_a_morph = new Fx.Morph(this.temp_a, {duration: this.duration, transition: Fx.Transitions.easeIn});
			this.temp_a_morph.start(this.positions['next']);
			
			this.temp_b_morph = new Fx.Morph(this.temp_b, {duration: this.duration, transition: Fx.Transitions.easeOut});
			this.temp_b_morph.start().chain(function(){	
				this.temp_b_morph.start(this.positions['current']);
				this.dest.getElement('.billboard_image').set('src', this.base + this.items[this.prev_image].image);
				this.dest_url = this.items[this.prev_image].url;
				this.dest.getElement('a').set('href', this.dest_url);
			}.bind(this)).chain(function(){	
				this.temp_a.set('style', 'visibility: hidden');
				this.temp_b.set('style', 'visibility: hidden');
				this.dest.getElement('.billboard_image').set('style', 'visibility: visible');
				this.current_image = this.prev_image;
			}.bind(this));
		}.bind(this));
		
		window.addEvent('nextImageLoaded', function(){
			this.temp_a.set('src', this.base + this.items[this.current_image].image);
			this.temp_a.set('styles', {'visibility':'visible', 'z-index':'0'});
			this.temp_a.setStyles(this.positions['current']);
			this.temp_b.set('src', this.base + this.items[this.next_image].image);
			this.temp_b.set('styles', {'visibility':'visible', 'z-index':'50'});
			this.temp_b.setStyles(this.positions['next']);
			this.dest.getElement('.billboard_image').set('style', 'visibility: hidden');
			
					
			this.temp_a_morph = new Fx.Morph(this.temp_a, {duration: this.duration, transition: Fx.Transitions.easeIn});
			this.temp_a_morph.start(this.positions['previous']);
			
			this.temp_b_morph = new Fx.Morph(this.temp_b, {duration: this.duration, transition: Fx.Transitions.easeOut});
			this.temp_b_morph.start().chain(function(){	
				this.temp_b_morph.start(this.positions['current']);
				this.dest.getElement('.billboard_image').set('src', this.base + this.items[this.next_image].image);
				this.dest_url = this.items[this.next_image].url;
				this.dest.getElement('a').set('href', this.dest_url);
			}.bind(this)).chain(function(){	
				this.temp_a.set('style', 'visibility: hidden');
				this.temp_b.set('style', 'visibility: hidden');
				this.dest.getElement('.billboard_image').set('style', 'visibility: visible');
				this.current_image = this.next_image;
			}.bind(this));
		}.bind(this));
		
	},
	preload: function(id, callback) {
		prev_image = (id == 0) ? this.total : id-1;
		next_image = (id == this.total) ? 0 : id+1;

		new Asset.image(this.base + this.items[id].image, {
			onload: function(){
				window.fireEvent(callback);
				new Asset.image(this.base + this.items[prev_image].image);
				new Asset.image(this.base + this.items[next_image].image);
			}.bind(this)
		});		
	},
	previous: function() {
		this.prev_image = (this.current_image == 0) ? this.total : this.current_image-1;
		this.preload(this.prev_image, 'prevImageLoaded');
	},
	next: function() {
		this.next_image = (this.current_image == this.total) ? 0 : this.current_image+1;
		this.preload(this.next_image, 'nextImageLoaded');
	}
});

var hotnews = new Class({
	initialize: function() {	
		this.display_item = 0;
		news = $('hotnews').getElements('.article');
		this.newscount = news.length-1;
		
		buttons = new Element('div').set({'class': 'buttons'}).inject($('hotnews'), 'top');
		infotext = new Element('span').set({text:'/', 'class': 'info'}).inject(buttons);
		total = new Element('span').set({html: this.newscount+1}).inject(infotext);
		current = new Element('span').set({html: this.display_item+1}).inject(infotext, 'top');
		btn_prev = new Element('a').set({html: 'prev', href:'#', 'class':'btn_prev'}).inject(buttons);
		btn_next = new Element('a').set({html: 'next', href:'#', 'class':'btn_next'}).inject(buttons);

		news.each(function(item, index){
			if (index != this.display_item) item.setStyle('display', 'none');
		}, this);
		
		btn_prev.addEvent('click', function(e){
			e.stop();
			
			new_display_item = (this.display_item == 0) ? this.newscount : this.display_item-1;
			current.set({html: new_display_item+1});
			
			news[this.display_item].setStyle('display', 'none');
			news[new_display_item].setStyle('display', 'block');
			
			
			this.display_item = new_display_item;
		}.bind(this));
		
		btn_next.addEvent('click', function(e){
			e.stop();
			
			new_display_item = (this.display_item == this.newscount) ? 0 : this.display_item+1;
			current.set({html: new_display_item+1});
			
			news[this.display_item].setStyle('display', 'none');
			news[new_display_item].setStyle('display', 'block');
			
			this.display_item = new_display_item;
		}.bind(this));
	}
   
});

var ie_enhancements = new Class({
	initialize: function() {
		
		if (ieversion < 8) {
			if ($('calendar')) this.calendar();
			if ($('tips')) this.tips();
			if ($('breadcrumbs')) this.breadcrumbs();
			if ($('choose_language')) this.lan();
			this.genres();
		}
		
	}, 
	calendar: function() {
		rep = new Element('div', {'class': 'overview'});

		$('calendar').getElement('table').getElement('tbody').getElements('tr').each(function(item, index){
			tmp = new Element('div', {'class':'event'});

			item.getElements('td').each(function(item, index){
				new Element('div', {'class': item.get('class')}).set('html', item.get('html')).inject(tmp);
			});
			tmp.inject(rep);
		});

		rep.inject($('calendar'));

		$('calendar').getElement('table').destroy();
	},
	tips: function() {		
		$('tips').getElements('table').each(function(item, index){
			rep = new Element('div', {'class': 'overview'});

			item.getElement('tbody').getElements('tr').each(function(item2, index2){
				tmp = new Element('div', {'class':'event'});

				item2.getElements('td').each(function(item3, index3){
					new Element('div', {'class': item3.get('class')}).set('html', item3.get('html')).inject(tmp);
				});
				tmp.inject(rep);
			});

			rep.inject($(item), 'after');

			$('tips').getElement('table').destroy();

		});
	},
	breadcrumbs: function() {
		$('breadcrumbs').getElement('ol').getElements('li').each(function(item, index){
			new Element('span', {'class':'divider'}).set('html', ' / ').inject(item, 'before');
		});
	},
	lan: function() {
		$('choose_language').getElement('ul').getElements('li').each(function(item, index){
			if (index > 0) new Element('span', {'class':'divider'}).set('html', ' / ').inject(item, 'before');
		});
	},
	genres: function() {
		window.document.getElements('ul.genres').each(function(el){
			el.getElements('li').each(function(item, index){
				if (index > 0) new Element('span', {'class':'divider'}).set('html', ' / ').inject(item, 'before');
			})
		});
	}
});