/**
 * Copyright (C) Eurowebsites
 * All rights reserved.
 */

var Site = new Object({
	start: function()
	{
		this.body			= document.body;
		//this.printCss		= new Asset.css('/static/styles/print.css');
		$('page-print-link').addEvent('click', function(){
			if (document.body.id != 'printbody')
				window.open(
					this.get('href'),
					'printpreview',
					'location=no,menubar=no,status=no,width=' + ($('sitewrap').getWidth() + 20)
				);
			else
				window.print();
			return false;
		});
		if ($('page-close-link'))
			$('page-close-link').addEvent('click',function(){window.close();});
	},
	togglePrintView: function()
	{
		if ($('printbody'))
		{
			this.body.removeAttribute('id');
			this.printButton.set('text', this.printButton.retrieve('defaultText'));
		}
		else
		{
			this.body.setAttribute('id', 'printbody');
			this.printButton.set('text', this.printButton.retrieve('altText'));
		}
	}
});
window.addEvent('domready', function(){ Site.start(); });

var STR_PAD_LEFT	= 1;
var STR_PAD_RIGHT	= 2;
var STR_PAD_BOTH	= 3;
function str_pad(str, len, pad, dir)	{ Utils.pad(str, len, pad, dir); }
function log(message)					{ Utils.log(0, message); }
function info(message)					{ Utils.log(1, message); }
function warn(message)					{ Utils.log(2, message); }
function error(message)					{ Utils.log(3, message); }
function MakeSlug(str)					{ return Utils.makeSlug(str); }

var Utils	= {
	pad: function(str, len, pad, dir)
	{
		if (typeof(str) != "string")	str	= str.toString();
		if (typeof(len) == "undefined")	len	= 0;
		if (typeof(pad) == "undefined")	pad	= ' ';
		if (typeof(dir) == "undefined")	dir	= STR_PAD_RIGHT;
		if (len + 1 < str.length)		return str;
		switch (dir)
		{
			case STR_PAD_LEFT:
				str	= Array(len + 1 - str.length).join(pad) + str;
				break;
			case STR_PAD_BOTH:
				var padlen	= 0;
				var right	= Math.ceil((padlen = len - str.length) / 2);
				str			= Array((padlen - right)+1).join(pad) + str + Array(right+1).join(pad);
				break;
			default:	// padding right
				str	= str + Array(len + 1 - str.length).join(pad);
				break;
		}
		return str;
	},
	makeSlug: function(str)
	{
		str = str.clean().replace("/[\"']+/i", '');
		str = str.replace("/[^a-z0-9\\d]+/i", '-');
		str = str.toLowerCase().trim();
		while (str.substr(0,1) == '-')
			str	= str.substr(1);
		while (str.substr(str.length - 1, 1) == '-')
			str	= str.substr(0, str.length - 1);
		return str;
	},
	timeconv: function(stamp)
	{
		// Convert to milliseconds, unless you want to end up in januari 1970
		stamp		= stamp.toInt() * 1000;
		var dateObj	= new Date(stamp);
		if (arguments[1])
			return this.pad(dateObj.getHours(), 2, '0', STR_PAD_LEFT)+':'+this.pad(dateObj.getMinutes(), 2, '0', STR_PAD_LEFT);
		return dateObj.getHours()+':'+this.pad(dateObj.getMinutes(), 2, '0', STR_PAD_LEFT);
	},
	log: function(severity, message)
	{
		if (!window.console)
			return;
		switch (severity)
		{
			case 3:	console.error(message);	break;
			case 2:	console.warn(message);	break;
			case 1:	console.info(message);	break;
			case 0:	console.log(message);	break;
		}
	}
};

var ImageRotate	= new Class({
	container: null,
	images: [],
	activeIndex: 0,
	nextIndex: 1,
	timeOut: 5000,
	initialize: function(container, images, timeOut)
	{
		if (typeof container == 'string')
			container	= $(container);
		if (!container)
		{
			error('Container not found, exiting');
			return;
		}
		if (images.length < 2)
		{
			error('Image array contains too few images (min. 2)');
			//return;
		}
		this.container	= container;
		this.container.empty();
		this.timeOut	= timeOut;

		images.each(function(image){
			log('New image: '+image);
			var i = new Element('img', {src:image, alt:'img', style:'opacity:0;'});
			i.set('tween', {duration:800});
			i.inject(this.container);
			this.images.extend([i]);
		}.bind(this));
		this.images[this.activeIndex].setStyle('opacity', 1);
		this.rotate.periodical(this.timeOut, this);
	},
	rotate: function()
	{
		this.images[this.activeIndex].tween('opacity', 0);
		this.images[this.nextIndex].tween('opacity', 1);
		this.activeIndex	= this.nextIndex;
		this.nextIndex		= this.activeIndex == this.images.length - 1 ? 0 : this.activeIndex + 1;
	}
});
