/* Parse HTML */
function parseHTML (html)
{
	var container = new Element('div').set('html', html);
	var elements = container.getChildren();
	
	return elements;
}


/* Element */
Element.implement({
	
	getClass: function (className)
	{
		this.get('class').split(' ').each(function (classe) { 
			if (classe.contains(className))
				className = classe.substr(((classe.contains(':')) ? classe.indexOf(':') : classe.indexOf('_')) + 1);
		}.bind(this));
		
		return className;
	}
	
});



/* Formfocus */
var Formfocus = {
	
	initialize: function ()
	{
		if (Browser.Engine.trident)
		{
			$$('input, textarea').addEvent('focus', function (event)
			{
				event.target.addClass('formfocus');
			});
			
			$$('input, textarea').addEvent('blur', function (event)
			{
				event.target.removeClass('formfocus');
			});
		}
	}
}



/* Location */
var Location = new Class({
	
	Implements: [Options, Events],
	
	options: {
		zoom: 7,
		latitude: 0,
		longitude: 0,
		type: 'roadmap',
		controls: {
			navigation: {
				display: true,
				style: 'ZOOM_PAN',
				position: 'TOP_LEFT'
			},
			scale: {
				display: false,
				position: 'BOTTOM_LEFT'
			},
			maptype: {
				display: false,
				style: 'HORIZONTAL_BAR',
				position: 'TOP_RIGHT'
			}
		},
		marker: null
	},
	
	initialize: function (element, options)
	{
		this.element = element;
		this.setOptions(options);
		
		this.loadLocation();
	},
	
	loadLocation: function ()
	{
		var latlng = new google.maps.LatLng(this.options.latitude, this.options.longitude);
    	
		var map = new google.maps.Map(this.element, {
			zoom: this.options.zoom,
			center: latlng,
			navigationControl: this.options.controls.navigation.display,
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle[this.options.controls.navigation.style.toUpperCase()],
				position: google.maps.ControlPosition[this.options.controls.navigation.position.toUpperCase()]
			},
			scaleControl: this.options.controls.scale.display,
			scaleControlOptions: {
				position: google.maps.ControlPosition[this.options.controls.scale.position.toUpperCase()]
			},
			mapTypeControl: this.options.controls.maptype.display,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle[this.options.controls.maptype.style.toUpperCase()],
				position: google.maps.ControlPosition[this.options.controls.maptype.position.toUpperCase()]
			},
			mapTypeId: google.maps.MapTypeId[this.options.type.toUpperCase()]					  
		});
		
		if (this.options.marker)
		{
			var marker = new google.maps.Marker({
				position: latlng, 
				map: map, 
				title: this.options.marker.title,
				icon: this.options.marker.image
			});
		}
	}
	
});



/* Teaser */
var Teaser = new Class({
	
	options: {
		numof: 0,
		current: 0,
		animate: false,
		timer: null
	},
	
	initialize: function (element)
	{
		this.teaser = $(element);
		this.holder = this.teaser.getElement('.teaser_holder');
		this.items = this.holder.getElements('.teaser_item');
		
		this.options.numof = this.items.length;
		this.items[0].inject(this.holder, 'bottom');
		
		this.setTimer();
		
		this.teaser.getElements('a.teaser_nav').each(function (a)
		{
			if (a.hasClass('previous'))
				a.addEvent('click', this.prev.bindWithEvent(this, a));
			else
				a.addEvent('click', this.next.bindWithEvent(this, a));
		}.bind(this));
	},
	
	setTimer: function ()
	{
		$clear(this.options.timer);
		this.options.timer = this.nextImage.periodical(8000, this);
	},
	
	nextImage: function ()
	{
		var nextNumber = (((this.options.current + 1) <= (this.options.numof - 1)) ? this.options.current + 1 : 0);
		var activeItem = this.items[this.options.current];
		var nextItem = this.items[nextNumber];
		
		this.animate(activeItem, nextItem, 'left');
		
		this.options.current = nextNumber;
	},
	
	next: function (event, a)
	{
		if (this.options.animate == false)
		{
			var nextNumber = (((this.options.current + 1) <= (this.options.numof - 1)) ? this.options.current + 1 : 0);
			var activeItem = this.items[this.options.current];
			var nextItem = this.items[nextNumber];
			
			this.animate(activeItem, nextItem, 'left');
			
			this.options.current = nextNumber;
			this.setTimer();
		}
		
		event.stop();
	},
	
	prev: function (event, a)
	{
		if (this.options.animate == false)
		{
			var prevNumber = ((this.options.current > 0) ? this.options.current - 1 : this.options.numof - 1);
			var activeItem = this.items[this.options.current];
			var nextItem = this.items[prevNumber];
			
			this.animate(activeItem, nextItem, 'right');
			
			this.options.current = prevNumber;
			this.setTimer();
		}
		
		event.stop();
	},
	
	animate: function (activeItem, nextItem, direction)
	{
		this.options.animate = true;
		
		activeItem.set('morph', {
			duration: 600,
			transition: Fx.Transitions.Linear
		}).morph({
			'left': ['0', ((direction == 'left') ? '-150px' : '150px')],
			'opacity': [1, 0]
		});
		
		nextItem.setStyles({
			'left': ((direction == 'left') ? '345px' : '-345px'),
			'opacity': 1
		}).inject(this.holder, 'bottom').set('morph', {
			duration: 600,
			transition: Fx.Transitions.Sine.easeOut
		}).morph({
			'left': [((direction == 'left') ? '345px' : '-345px'), '0']
		}).retrieve('morph').chain(function ()
		{
			this.options.animate = false;
		}.bind(this));
	}
	
});



/* Send Form */
var Sendform = new Class({
	
	Implements: [Options, Events],
	
	options: {/*
		onProgress: $empty,
		onComplete: $empty,*/
		loading: false
	},
	
	initialize: function (form, options)
	{
		this.setOptions(options);
		
		this.form = $('formcontainer');
		this.form.store('formHeight', this.form.getElement('form').getSize().y);
		this.form.send = this.form.getElement('.send');
		this.form.send.addEvent('click', this.sendForm.bindWithEvent(this));
		this.messageBox = this.form.getElement('.messagebox');
	},
	
	sendForm: function (event)
	{
		this.form.overlay = this.buildOverlay().inject(this.form);
		var section = this.form.getElement('form').get('class');
		
		this.form.overlay.fade('in').retrieve('tween').chain(function ()
		{
			var validate = new Hash();
			
			this.form.getElement('form').getElements('input, textarea').each(function (formelement)
			{
				if (formelement.get('class').contains('required'))
				{
					var label = this.form.getElement('form').getElement('label[for=' + formelement.get('id') + ']');
					validate.include(formelement.get('name'), $H({'name': label.get('html').substr(0, label.get('html').indexOf(':')), 'required': formelement.getClass('required')}));
				}
			}.bind(this));
			
			new Request({
				method: 'post', 
				url: '?ajax=true&action=sendform',
				noCache: true,
				
				onRequest: function () { this.loader = new Loader($('main')); }.bind(this),
				onComplete: function (response) { 
					this.loader.loaded();
					this.setResponse(response);
				}.bind(this)
			}).send(this.form.toQueryString() + '&validate=' + JSON.encode(validate) + '&section=' + section);
		}.bind(this));
		
		event.stop();
	},
	
	setResponse: function (response)
	{
		var jObject = JSON.decode(response);
		
		this.form.getElement('form').getElements('.formfailure').removeClass('formfailure');
		
		if ($chk(this.messageBox.getElement('p')))
			this.messageBox.getElements('p, ul').destroy();
		
		this.messageBox.addClass('hidden');
		this.messageBox.getElement('.messagebox_inner').adopt(new Element('p').set('html', jObject['message']));
		this.messageBox.getElement('.messagebox_inner').removeClass('message_ok', 'message_error');
		
		if (jObject['type'] == 'error')
		{
			var list = new Element('ul');
			
			for (var key in jObject['fields'])
			{
				jObject['fields'][key].each(function (mss) 
				{
					new Element('li').set('html', mss).inject(list);
				});
				
				$('form_' + key).addClass('formfailure');
			}
			
			this.messageBox.getElement('.messagebox_inner').addClass('message_error').adopt(list);
		}
		else
		{
			this.messageBox.getElement('.messagebox_inner').addClass('message_ok');
			this.form.getElements('.formfailure').removeClass('formfailure');
			this.form.getElement('form').reset();
		}
		
		var mssDimensions = this.messageBox.getDimensions({
			computeSize: true,
			styles: ['border', 'padding', 'margin']
		});
		var height = mssDimensions.totalHeight + this.form.retrieve('formHeight');
		
		this.form.overlay.tween('height', height);
		this.form.tween('height', height).retrieve('tween').chain(function ()
		{
			this.messageBox.removeClass('hidden');
			this.form.overlay.fade('out').retrieve('tween').chain(function () { this.form.overlay.destroy() }.bind(this));
		}.bind(this));
	},
	
	buildOverlay: function ()
	{
		var coordinates = this.form.getCoordinates();
		var overlay = new Element('div')
		.addClass('formcontainer_overlay')
		.setStyles({
			'position': 'absolute',
			'top': coordinates.top,
			'left': coordinates.left,
			'height': coordinates.height,
			'width': coordinates.width + 1,
			'background-color': '#fff',
			'opacity': 0
		});
		
		return overlay;
	}
	
});



/* -------------------- CUSTOM CLASSES -------------------- */

/* Radio */

var Radio = {
	
	initialize: function ()
	{
		$$('input[type=radio]').each(function (radio)
		{
			if (radio.get('checked'))
				radio.getNext('.radio_box').addClass('checked');
			
			radio.getParent('.radio').addEvent('click', this.setRadio.bindWithEvent(this, radio.getParent('.radio')));
		}.bind(this));
	},
	
	setRadio: function (event, radio)
	{
		$$('input[name=' + radio.getElement('input').get('name') + ']').each(function (input)
		{
			if (input == radio.getElement('input'))
			{
				if (!input.get('checked'))
				{
					input.set('checked', 'checked');
					input.getNext('.radio_box').addClass('checked');
				}
			}
			else
			{
				input.set('checked', false);
				input.getNext('.radio_box').removeClass('checked');
			}
		});
		
		event.stop();
	}
};



/* Post */
var Post = new Class({
	
	Implements: [Options],
	
	options: {
		type: null
	},
	
	initialize: function (form, options)
	{
		this.setOptions(options);
		this.form = form;
		this.form.send = this.form.getElement('.send');
		
		new Sendform(form, {
			onProgress: function ()
			{
				this.progress();
			}.bind(this),
			
			onComplete: function (type, message, fields, html)
			{
				this.response(type, message, fields, html);
			}.bind(this)
		});
	},
	
	progress: function ()
	{
		this.form.send.getElement('.button_inner span').removeProperty('class').addClass('loader');
	},
	
	response: function (type, message, fields, html)
	{
		var mss = (($chk($('message'))) ? $('message') : new Element('div').set('id', 'message').inject(this.form.send.getParent('.button'), 'after'));
		mss.removeProperty('class').addClass(type).empty().set('html', message);
		
		this.form.send.getElement('.button_inner span').removeProperty('class').addClass(((type == 'ok') ? 'tick' : 'exclamation'));
		
		if (this.options.type == 'weblog' && type == 'ok')
			this.setHtml(html);
		
		if (type == 'ok')
		{
			this.form.reset();
			
			(function ()
			{
				mss.fade('out').retrieve('tween').chain(function ()
				{
					this.form.send.getElement('.button_inner span').removeClass('tick').addClass(this.options.type);
					mss.destroy();
				}.bind(this));
			}).delay(4000, this);
		}
	},
	
	setHtml: function (html)
	{
		var html = parseHTML(html);
		
		var numChildren = $('comments').getElements('.comment').length;
		html.addClass('last' + ((numChildren == 0) ? ' first' : ''));
		
		if (numChildren == 0)
			$('comments').getElement('.nocomment').destroy();
		else
			$('comments').getLast('.comment').removeClass('last');
		
		html.inject($('comments').getElement('form'), 'before');
	}
	
});



/* Sendtofriend Class */

var Sendtofriend = new Class({
	
	initialize: function ()
	{
		this.windowpopup = new Windowpopup();
		
		(function ()
		{
			new Request.HTML({
				onRequest: function () { this.loader = new Loader($('windowpopup')); }.bind(this),
				onComplete: function (response) { 
					this.loader.loaded();
					
					this.windowpopup.adoptHTML(response);
					this.windowpopup.resize(597, 358);
					
					this.initContent();
				}.bind(this)
			}).get('views/boxes/sendtofriend.php');
		}).delay(750, this);
	},
	
	initContent: function ()
	{
		Formfocus.initialize();
		
		//init formelements
		this.form = $('windowpopup').getElement('.windowpopup_main .body .sendtofriend');
		this.form.messageBox = this.form.getElement('.messagebox');
		
		//init textarea
		var hyperlink = document.location.href.replace('#mail', '');
		$('form_textmessage').set('value', 'Hallo, ik wil graag de pagina-link "'+ $$('h1').get('text') +'" met u delen. De link is: ['+ hyperlink +']\r\n\r\nGroeten');
		
		// init buttons
		$('windowpopup_close').addEvent('click', function (event)
		{
			this.windowpopup.hide();
			
			event.stop();
		}.bind(this));
		
		$('windowpopup_sendtofriend').addEvent('click', this.sendForm.bindWithEvent(this));
	},
	
	sendForm: function (event)
	{
		this.form.overlay = this.buildOverlay().inject(this.form);
		var section = this.form.get('class');
		
		this.form.overlay.fade('in').retrieve('tween').chain(function ()
		{
			var validate = new Hash();
					
			this.form.setStyle('visibility', 'hidden');
			this.form.getElement('form').getElements('input, textarea').each(function (formelement)
			{
				if (formelement.get('class').contains('required'))
				{
					var label = this.form.getElement('label[for=' + formelement.get('id') + ']');
					validate.include(formelement.get('name'), $H({'name': label.get('html').substr(0, label.get('html').indexOf(':')), 'required': formelement.getClass('required')}));
				}
			}.bind(this));
			
			new Request({
				method: 'post', 
				url: '?ajax=true&action=sendform',
				noCache: true,
				
				onRequest: function () { this.loader = new Loader($('windowpopup')); }.bind(this),
				onComplete: function (response) { 
					this.loader.loaded();
					this.setResponse(response);
				}.bind(this)
			}).send(this.form.getElement('form').toQueryString() + '&validate=' + JSON.encode(validate) + '&section=' + section);
		}.bind(this));
		
		event.stop();
	},
	
	setResponse: function (response)
	{
		var jObject = JSON.decode(response);
		
		this.form.getElements('.formfailure').removeClass('formfailure');
		
		if ($chk(this.form.messageBox.getElement('p')))
			this.form.messageBox.getElements('p, ul').destroy();
		
		this.form.messageBox.getElement('.messagebox_inner').adopt(new Element('p').set('html', jObject['message']));
		this.form.messageBox.getElement('.messagebox_inner').removeClass('message_ok', 'message_error');
		
		if (jObject['type'] == 'error')
		{
			var list = new Element('ul');
			
			for (var key in jObject['fields'])
			{
				jObject['fields'][key].each(function (mss) 
				{
					new Element('li').set('html', mss).inject(list);
				});
				
				$(key).addClass('formfailure');
			}
			
			this.form.messageBox.getElement('.messagebox_inner').addClass('message_error').adopt(list);
		}
		else
		{
			this.form.messageBox.getElement('.messagebox_inner').addClass('message_ok');
			this.form.getElements('.formfailure').removeClass('formfailure');
			this.form.getElement('form').destroy();
			$('windowpopup_sendtofriend').destroy();
			$('windowpopup_close').getElement('.button-middle').set('html', 'Sluiten');
		}
		
		this.form.overlay.tween('height', this.form.getSize().y);
		this.form.messageBox.removeClass('hidden').setStyle('margin-top', '-' + (this.form.messageBox.getSize().y + 40) + 'px');
		this.form.messageBox.tween('margin-top', 0).retrieve('tween').chain(function ()
		{
			this.form.erase('style').removeProperty('style');
			this.form.overlay.fade('out').retrieve('tween').chain(function () { this.form.overlay.destroy() }.bind(this));
		}.bind(this));
		
		this.windowpopup.resize(597, (this.form.getSize().y + 78));
	},
	
	buildOverlay: function ()
	{
		var coordinates = this.form.getCoordinates(this.form.getParent());
		var overlay = new Element('div')
		.addClass('formcontainer_overlay')
		.setStyles({
			'position': 'absolute',
			'top': coordinates.top,
			'left': coordinates.left,
			'height': coordinates.height,
			'width': coordinates.width + 1,
			'background-color': '#fff',
			'opacity': 0
		});
		
		return overlay;
	}
	
});


/* Tabs Class */

var Tabs = new Class({
	
	initialize: function (element)
	{
		this.tabs = $(element).getElements('li');
		this.tabsections = $$('.tabsection');
		
		this.tabs.each(function (tab)
		{
			tab.section = this.getSection(tab.getElement('a').get('href').substr(tab.getElement('a').get('href').indexOf('#') + 1));
		}.bind(this));
		
		this.tabs.getElement('a').addEvent('click', function (event)
		{
			var tab = event.target.getParent('li');
			
			if (!tab.hasClass('active'))
				this.activateTab(tab);
		}.bind(this));
	},
	
	activateTab: function (tab)
	{
		var activeTab = this.getActiveTab();
		
		tab.addClass('active');
		tab.section.removeClass('hidden');
			
		activeTab.removeClass('active');
		activeTab.section.addClass('hidden');
		
		
		
		//var section = this.getSection(tab.get('href').substr(tab.get('href').indexOf('#') + 1));
		
		//var activeTab = this.getActiveTab().getElement('a');
		//var activeSection = this.getSection(activeTab.get('href').substr(activeTab.get('href').indexOf('#') + 1));
		
		
		/*
		tab.getParent('li').addClass('active');
		
		activeTab.getParent('li').removeClass('active');
		activeSection.fade('out').retrieve('tween').chain(function ()
		{
			activeSection.addClass('hidden');
			
			section.set('opacity', 0).removeClass('hidden').fade('in').retrieve('tween').chain(function ()
			{
				section.erase('style').removeProperty('style');
			});
		}.bind(this));
		*/
	},
	
	getActiveTab: function ()
	{
		var tab = this.tabs.filter(function (tab)
		{
			return (tab.hasClass('active'));
		});
		
		return tab[0];
	},
	
	getSection: function (className)
	{
		var section = this.tabsections.filter(function (section)
		{
			return (section.hasClass(className));
		});
		
		return section[0];
	}
	
});


/* Printpage Class */

var Printpage = new Class({
	
	Implements: [Options],
	
	options: {
		printarea: null
	},
	
	initialize: function (options)
	{
		this.setOptions(options);
		
		this.windowpopup = new Windowpopup();
		
		(function ()
		{
			new Request.HTML({
				onRequest: function () { this.loader = new Loader($('windowpopup')); }.bind(this),
				onComplete: function (response) { 
					this.loader.loaded();
					
					this.windowpopup.adoptHTML(response);
					this.windowpopup.resize(600, 550);
					
					this.initContent();
				}.bind(this)
			}).get('views/boxes/print.php');
		}).delay(750, this);
	},
	
	initContent: function ()
	{
		this.printviewer = $('windowpopup').getElement('.windowpopup_main .body .printviewer');
		
		// init content
		this.options.printarea.clone(true, true).inject(this.printviewer.getElement('.printviewer_content'));
		this.printviewer.getElement('.printviewer_content').setStyles(this.options.printarea.getStyles('width', 'height'));
		this.printviewer.getElement('.printviewer_inner').setStyle('cursor', 'url(images/cursors/openhand.cur), default');
		this.printviewer.getElement('.printviewer_overlay').setStyle('opacity', 0.000001);
		
		new Drag(this.printviewer.getElement('.printviewer_inner'), {
			snap: 0,
			style: false,
			invert: true,
			modifiers: {x: 'scrollLeft', y: 'scrollTop'},
			
			onBeforeStart: function (element)
			{
				element.ondragstart = function () { return false; };
			},
			
			onStart: function (element)
			{
				element.setStyle('cursor', 'url(images/cursors/closedhand.cur), default');
			},
			
			onComplete: function (element)
			{
				element.setStyle('cursor', 'url(images/cursors/openhand.cur), default');
			}
		});
		
		//set iframe
		this.iframe = new IFrame({
			id: 'contentPrint',
			frameborder: '0'
		}).inject(this.printviewer, 'top');
		
		this.initIframe();
		
		// init buttons
		$('windowpopup_close').addEvent('click', function ()
		{
			this.windowpopup.hide();
		}.bind(this));
		
		$('windowpopup_print').addEvent('click', this.printPage.bind(this));
	},
	
	initIframe: function ()
	{
		var printcontents = '<div'+ (($chk(this.options.printarea.get('id'))) ? ' id="' + this.options.printarea.get('id') + '"' : '') + '' + (($chk(this.options.printarea.get('style'))) ? ' style="' + this.options.printarea.get('style') + '"' : '') + '">' + this.options.printarea.get('html') + '</div>';
		var documentTemplate = '<html><head><link href="styles/style.css" media="all" type="text/css" rel="stylesheet"><style type="text/css">@media print { body { background-color: #fff }}</style></head><body>'+ printcontents +'</body></html>';
		
		this.win = this.iframe.contentWindow;
		this.doc = this.win.document;
		
		this.doc.open();
		this.doc.write(documentTemplate);
		this.doc.close();
	},
	
	printPage: function ()
	{
		this.win.focus();
		this.win.print();
	}
	
});


/* Bookmark Class */

var Bookmark = new Class({
	
	initialize: function ()
	{
		var docHref = document.location.href;
		var docTitle = document.title;
		
		switch (Browser.Engine.name)
		{
			case 'trident':
				window.external.AddFavorite(docHref, docTitle);
				break;
			
			case 'gecko':
				window.sidebar.addPanel(docTitle, docHref, '');
				break
			
			case 'presto':
				var elem = document.createElement('a');
				elem.setAttribute('href', docHref);
				elem.setAttribute('title', docTitle);
				elem.setAttribute('rel', 'sidebar');
				elem.click();
				break;
			
			case 'webkit':
				this.windowpopup = new Windowpopup();
		
				(function ()
				{
					new Request.HTML({
						onRequest: function () { this.loader = new Loader($('windowpopup')); }.bind(this),
						onComplete: function (response) { 
							this.loader.loaded();
							
							this.windowpopup.adoptHTML(response);
							this.windowpopup.resize(400, 125);
							
							this.initContent();
						}.bind(this)
					}).get('views/boxes/bookmark.php?platform=' + Browser.Platform.name);
				}).delay(750, this);
				break;
		}
	},
	
	initContent: function ()
	{
		var hide = function (event) {
			if (event.control && event.key == 'd')
			{
				$(document.body).removeEvent('keydown', hide);
				this.windowpopup.hide();
			}
		}.bind(this);
		
		//init buttons
		$('windowpopup_close').addEvent('click', function ()
		{
			this.windowpopup.hide();
		}.bind(this));
		
		$(document.body).addEvent('keydown', hide);
	}
	
});



/* Windowpopup class */

var Windowpopup = new Class({
	
	initialize: function ()
	{
		this.overlay = new Element('div').set('id', 'windowpopup_overlay').setStyle('opacity', 0);
		this.win = new Element('div').set('id', 'windowpopup').addClass('hidden');
		
		var shadow = new Element('div').addClass('windowpopup_shadow').inject(this.win);
		var main = new Element('div').addClass('windowpopup_main').adopt(new Element('div').setStyle('opacity', 0)).inject(this.win);
		
		['tl', 'tr', 't', 'ml', 'mr', 'bl', 'br', 'b'].each(function (dir)
		{
			new Element('div').addClass('windowpopup_bg-' + dir).inject(shadow);
		});
		
		this.win.main = main.getElement('div');
		
		$(document.body).adopt(this.overlay, this.win);
		
		this.show();
	},
	
	adoptHTML: function (response)
	{
		this.win.main.adopt(response);
	},
	
	show: function ()
	{
		this.overlay.fade(0.7).retrieve('tween').chain(function () { this.win.removeClass('hidden'); }.bind(this));
	},
	
	hide: function ()
	{
		this.win.destroy();
		this.overlay.fade('out').retrieve('tween').chain(function () { this.element.destroy(); });
	},
	
	resize: function (width, height)
	{
		new Fx.Morph(this.win, {
			transition: Fx.Transitions.Cubic.easeInOut,
			
			onComplete: function ()
			{
				this.win.main.fade('in').retrieve('tween').chain(function () { this.element.erase('style').removeProperty('style'); });
			}.bind(this)
		}).start({
			'height': height,
			'width': width,
			'margin': '-' + Math.round(height * 0.5) + 'px 0 0 -' + Math.round(width * 0.5) + 'px'
		});
	}
	
});



/* Slidecarousel class */

var Slidecarousel = new Class({
	
	Implements: [Options, Events],
	
	options: {
		periodic: false,
		periodicId: null,
		speed: 5,
		transition: Fx.Transitions.Linear,
		hold: false,
		blockWidth: 0,
		running: false
	},
	
	initialize: function (content, knobs, options)
	{
		this.setOptions(options);
		this.options.blockWidth = content.getFirst().getComputedSize().totalWidth;
		
		this.content = content;
		this.content.setStyle('width', this.options.blockWidth * this.content.getChildren().length);
		
		knobs.each(function (knob, nr)
		{
			knob.addEvents({
				'mousedown': function ()
				{
					if (!this.options.running)
					{
						this.options.hold = true;
						this.slide(((knob.hasClass('slidecarousel_prev')) ? 'prev' : 'next'));
					}
					
					this.unsetPeriodic();
				}.bind(this),
				
				'mouseup': this.cancel.bind(this),
				'click': function (event) { event = new Event(event).stop(); }
			});
		}.bind(this));
		
		this.setPeriodic();
	},
	
	slide: function (direction)
	{
		if (direction == 'next')
		{
			this.content.getLast().inject(this.content, 'top');
			this.content.setStyle('margin-left', this.options.blockWidth * -1);
		}
		
		new Fx.Tween(this.content, {
			duration: this.options.speed * 50,
			transition: this.options.transition,
			
			onStart: function ()
			{
				this.options.running = true;
			}.bind(this),
			
			onComplete: function ()
			{
				if (direction == 'prev')
				{
					this.content.getFirst().inject(this.content, 'bottom');
					this.content.setStyle('margin-left', '0');
				}
				
				this.options.running = false;
				
				if (this.options.hold)
					this.slide(direction);
			}.bind(this)
		}).start('margin-left', ((direction == 'prev') ? this.options.blockWidth * -1 : 0));
	},
	
	setPeriodic: function ()
	{
		if (this.options.periodic)
			this.options.periodicId = this.slide.periodical(10000, this, 'next');
	},
	
	unsetPeriodic: function ()
	{
		$clear(this.options.periodicId);
	},
	
	cancel: function ()
	{
		this.options.hold = false;
		this.setPeriodic();
	}
	
});



/* Loader class */

var Loader = new Class({
	
	initialize: function (element)
	{
		var elCoordinates = element.getCoordinates();
		
		if ($chk($('loader')))
			$('loader').destroy();
		
		this.loader = new Element('div').set('id', 'loader').setStyles({
			'top': elCoordinates.top + ((elCoordinates.height * 0.5) - 11),
			'left': elCoordinates.left + ((elCoordinates.width * 0.5) - 11)
		}).inject($(document.body), 'bottom');
	},
	
	loaded: function ()
	{
		this.loader.destroy();
	}
	
});