var GAW = {};

GAW.Frontpage = {
	images:[],
	blocks:[],
	text:null,
	image:null,
	current:0,
	numBlocks:0,
	offset:{x:38,y:8},
	autoRotate:true,
	random:true,
	autoRotateDelay:15000,
	timeout:null,
	
	init:function(){
		var _this = this;
		$(document).getElement('#header-teaser').setStyle('position','relative');
		this.blocks = $(document).getElements('#header-teaser .ce_text.block');
		this.image = new Element('div',{'id':'header-teaser-images','style':'position:relative;'}).inject('header-teaser');
		this.text = new Element('div',{'id':'header-teaser-text'}).inject('header-teaser');
		var block;
		var image;
		this.numBlocks = this.blocks.length;

		for(var i = 0; i<this.blocks.length; i++) {
			block = this.blocks[i];
			image = block.getElement('.image_container');
			image.store('index',i);
			image.setStyles({
				'display':'block',
				'position':'absolute',
				'cursor':'pointer',
				'top':i*this.offset.y,
				'left':i*this.offset.x,
				'z-index':i
			});
			image.addEvent('click',function(){
				_this.to(this.retrieve('index'));
			});
			image.inject(this.image);
			block.dispose();
			this.images.push(image);
		}

		if (this.random) {
			this.to(Math.round(Math.random()*this.numBlocks) % this.numBlocks);
		} else this.to(this.numBlocks-1);
		
	},
	to:function(i){
		if (i>=0 && i<this.numBlocks) {
			var image;
			var x;
			var y;
			var iii;
			var inv = this.images.length-1;
			var morph;
			
			for(var ii= 0; ii<this.images.length;ii++){
				iii = (i+inv) % this.numBlocks;
				
				image = this.images[ii];
				image.setStyle('z-index',iii);
				if (ii==this.current) {
					this.blocks[ii].dispose();
				}
				x = (iii)*this.offset.x;
				y = (iii)*this.offset.y;
				morph = new Fx.Morph(image,{
					duration:'short'
				});
				morph.start({
					'left':x,
					'top':y
				});
				inv--;
			}
			this.blocks[i].inject(this.text);
			this.current = i;

			if(this.timeout!=null) {
				window.clearTimeout(this.timeout);
				this.timeout = null;
			}

			if (this.autoRotate) {
				this.timeout = this.next.delay(this.autoRotateDelay,this);
			}
		}
	},
	next:function(){
		this.to((this.current+1) % this.numBlocks);
	},
	previous:function() {
		this.to((this.current-1) % this.numBlocks);
	}
}

GAW.Zoom = {
	defaultFontSize:12,
	init:function(){
		if (Cookie.read('fontSize')==null) {
			this.set(this.defaultFontSize);
		} else this.set(Cookie.read('fontSize'));
	},
	set:function(p){
		$('top').setStyle('font-size',p+'px');

		Cookie.write('fontSize',p);
	}
}

$(document).addEvent('domready',function(){
	GAW.Zoom.init();
});
