iam_start = function(){
	body = $('iam-body')
	background = body.getStyle('background-image').replace(/(url\(http:\/\/iamblog\.de\/h\/)(.*)(\))/i, "$2");
	personalbackground = Cookie.get('my_iam_url');
	
	button_mouseover = new Asset.image('http://iamblog.de/i/change2.png');
	loading = new Asset.image('http://iamblog.de/i/loading.gif');
	arrow_left = new Asset.image('http://iamblog.de/i/arrow_left.png');
	arrow_right = new Asset.image('http://iamblog.de/i/arrow_right.png');
	json = "";
	
	win_height = window.getHeight();
	win_width = window.getWidth();
	if( win_height%2 != 0 ){
		win_height -= 1;
	}
	if( win_width%2 != 0 ){
		win_width -= 1;
	}
	a = 300;
	b = 380;
	saved = {};
	global_list_offset = 0;
	offset = 0;
	
	set_background = function(name, bg_id){
		new Asset.image('http://iamblog.de/h/' + name, {
			id: 'thefamousbackground', 
			title: name,
			onload: function(){
				body.setStyle('background-image', 'url(http://iamblog.de/h/' + name + ')');
				$E('h1#header em').setStyle('background-image', 'url(http://iamblog.de/h/' + name + ')');
				$$('ul#menu li a').each(function(x){
					x.setStyle('background-image', 'url(http://iamblog.de/h/' + name + ')');
				});
				$$('ul#menu li a em').each(function(y){
					y.setStyle('background-image', 'url(http://iamblog.de/h/' + name + ')');
				});
				if(bg_id && $(bg_id)){
					$(bg_id).remove();
				}
			}
		});
	}
	
	if( personalbackground == false ){
		Cookie.set('my_iam_url', background, {domain: 'iamblog.de', duration:7, path: "/"});
		personalbackground = background;
	}else{
		set_background(personalbackground);
	}	
	if( Cookie.get('HUD_pos_x') == false ){
		saved.x = (0.5 * win_width) - 0.5*a;	
		saved.y = (0.5 * win_height) - 0.5*b;
	}else{
		saved.x = Cookie.get('HUD_pos_x');
		saved.y = Cookie.get('HUD_pos_y');
	};
	direction = 'in';
	s = 0;
	t = 0;	
	var setNumber = 1;

	drawer = new Element('div', {
		'styles': {
			'position': 'absolute',
			'top': s + 20,
			'left': t + 20,
			'border': '0',
			'height': '0',
			'width': '0',
			'overflow': 'hidden',
			'background': 'transparent url(http://iamblog.de/i/HUD.png)',
			'z-index':9000
		},
		'class': 'js-dom'
	});
	
	drawer_content = new Element('div', {
		'styles': {
			'position': 'relative',
			'top': '38px',
			'right': '20px',
			'bottom': '20px',
			'left': '20px',
			'width': '260px',
			'height': '320px',
			'color': '#fff',
			'overflow': 'hidden'
		},
		'class': 'js-dom',
		'id': 'drawer_content'
	});
	
	scrollbox = new Element('div', {
		'styles':{
			'overflow':'hidden',
			'width': '260px',
			'height': '320px'
		},
		'class': 'js-dom',
		'id':'scrollbox'
	}).injectInside(drawer_content);
	
	scrollpanel = new Element('div', {
		'styles':{
			'overflow':'hidden',
			'width': '260px',
			'height': '320px'
		},
		'class': 'js-dom',
		'id':'scrollpanel'
	}).injectInside(scrollbox);
	
	superscroll = new Fx.Scroll(scrollbox, {
		wait: false,
		duration: 700,
		transition: Fx.Transitions.Elastic.easeOut,
	 	offset: {'x': 0, 'y': 0}
	});

	var show_elements = function(which,parentelement){
		prev_src = 'http://iamblog.de/h/' + which.substr(0, which.length - 4) + '.preview.' + which.substr(which.length - 3);
		el_id = "notyour" + which.substr(0, which.length - 4);

		var style_basic_desc = "";
		var style_title = "";
		var style_author = "";
		var style_basic_desc = which.substr(11, which.length - 15);
		var style_basic_desc = style_basic_desc.split("_by_");
		var style_title = style_basic_desc[0].replace(/_/g, " ");
		var style_author = style_basic_desc[1];
		var style_desc = '«' + style_title + '» von ' + style_author;
	
		new Asset.image(prev_src, {
			title: style_desc,
			onload: function(){
				this.setStyle('background', 'transparent');
			}
		}).addEvent('click', function(){
			Cookie.set('my_iam_url', which, {domain: 'iamblog.de', duration:1, path: "/"});
			new Element('img',{
				'src': 'http://iamblog.de/i/loading.gif',
				'class': 'js-dom',
				'styles': {
					'height': '16px',
					'width': '16px',
					'margin': '0',
					'position': 'absolute',
					'bottom': '14px',
					'left': '122px',
				},
				'id': el_id
			}).injectInside(drawer_content);
			set_background(which, el_id);
		}).setStyles({
			'height': 50,
			'width': 250,
			'display':'block',
			'margin': '5px 5px 0',
			'cursor': 'pointer',
			'background': 'url(http://iamblog.de/i/loading.gif) center no-repeat'
		}).injectInside(parentelement);
	}
	
	control_show  = new Element('div', {
		'styles': {
			'position': 'absolute',
			'left':0,
			'top':0,
			'border': 0,
			'height': '40px',
			//'width': '40px',
			'width':'160px',
			'background': 'url(http://iamblog.de/i/change1.png)',
			'z-index':9500
		},
		'class': 'js-dom',
		'events': {
			'click': function(){
				new Json.Remote("http://iamblog.de/h/index.php", {
					onComplete: function(json){
						var howmanypanels = Math.ceil(json.backgrounds.length/5);
						var c_bckgrounds = 0;
						var boxcount = 0;

						scrollpanel.empty();
						scrollpanel.setStyle('width', howmanypanels * 260);

						json.backgrounds.each(function(el){
							if( c_bckgrounds == 0 || c_bckgrounds >= 5 ){
								c_bckgrounds = 0;
								box = new Element('div', {
									'styles':{
										'width':'260px',
										'overflow':'hidden',
										'float':'left'
									},
									'id': 'box-' + boxcount
								}).injectInside(scrollpanel);
								show_elements(el,box);
								c_bckgrounds++;
								boxcount++;
							}else if( c_bckgrounds < 5 ){
								show_elements(el,box);
								c_bckgrounds++;
							}
						});
						
						var arrow_prev = new Element('div', {
							'styles': {
								'position':'absolute',
								'bottom':'0',
								'right':'145px',
								'height':'40px',
								'width':'40px',
								'background': 'url(http://iamblog.de/i/arrow_left.png) center no-repeat',
								'z-index':'2000',
								'cursor':'pointer'
							},
							'title':'Ältere Hintergründe',
							'id':'arrow_prev',
							'class': 'js-dom',
							'events': {
								'click': function(event){
									if( setNumber > 1){
										event = new Event(event).stop();
										setNumber--;
										superscroll.toElement('box-' + (setNumber-1));
									}
									if( setNumber == 1 ){
										this.remove();
										arrow_next.injectInside(drawer_content);
									}
								}
							}
						});

						var arrow_next = new Element('div', {
							'styles': {
								'position':'absolute',
								'bottom':'0',
								'left':'145px',
								'height':'40px',
								'width':'40px',
								'background': 'url(http://iamblog.de/i/arrow_right.png) center no-repeat',
								'z-index':'2000',
								'cursor':'pointer'
							},
							'title': 'Neuere Hintergründe',
							'id':'arrow_next',
							'class': 'js-dom',
							'events': {
								'click': function(event){
									if( setNumber < howmanypanels){
										event = new Event(event).stop();
										setNumber++;
										superscroll.toElement('box-' + (setNumber-1));
									}
									if( setNumber == howmanypanels ){
										this.remove();
									}
									if( setNumber >= howmanypanels - 1 ){
										arrow_prev.injectInside(drawer_content);
									}
								}
							}
						}).injectInside(drawer_content);

						drawer_move('show');
					}
				}).send();
				(function(){control_show.remove()}).delay(100);
			},
			'mouseenter': function(){
				control_show.setStyles({
					//'width': '160px',
					'background': 'url(http://iamblog.de/i/change2.png)'
				});
			},
			'mouseleave': function(){
				control_show.setStyles({
					//'width': '40px',
					'background': 'url(http://iamblog.de/i/change1.png)'
				});
			}
		}
	}).injectInside(body);
	
	new Drag.Base(drawer, {
		onComplete: function(){
			d_left = drawer.getLeft();
			d_top = drawer.getTop();

			Cookie.set('HUD_pos_x', d_left, {domain: 'iamblog.de', duration:365, path: "/"});
			Cookie.set('HUD_pos_y', d_top, {domain: 'iamblog.de', duration:365, path: "/"});
		},
		limit: {
			x: [0, win_width - a],
			y: [0, win_height - b]
		}
	});
	
	var drawer_move = function(direction){
		dur = 300;
		move_drawer = new Fx.Styles(drawer, {
			duration:dur, 
			transition: Fx.Transitions.Sine.easeOut,
			onComplete: function(){
				if( direction == 'hide' ){
					control_show.setStyles({
						//'width': '40px',
						'background': 'url(http://iamblog.de/i/change1.png)'
					});
					control_show.injectAfter(drawer);
				}
			}
		});
		
		if( direction == 'hide' ){
			saved = drawer.getPosition();
			move_drawer.start({
				'height': 1,
				'width': 1,
				'top': '20px',
				'left': '20px',
				'opacity': 0.2
			});
		}
		
		if( direction == 'show' ){
			move_drawer.start({
				'top': saved.y,
				'left': saved.x,
				'height': b,
				'width': a,
				'opacity': 1
			});
		}
	}
	
	drawer_close = new Element('div', {
		'styles': {
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'border': '0',
			'height': '38px',
			'width': '45px',
			'padding': '0',
			'overflow': 'hidden',
			'z-index':9000

		},
		'class': 'js-dom',
		'events': {
			'click': function(){			
				drawer_move('hide');
				if($('arrow_prev')){
					arrow_prev.remove();
				};
				if($('arrow_next')){
					arrow_next.remove();
				};
				setNumber = 1;
				superscroll.toLeft();
			}
		}
	});
	
	drawer_headline = new Element('div', {
		'styles': {
			'position': 'absolute',
			'top': '20px',
			'right': '40px',
			'left': '40px',
			'width': '220px',
			'text-align': 'center',
			'height': '18px',
			'line-height': '18px',
			'font-size': '11px',
			'color': '#fff'
		},
		'class': 'js-dom'
	}).appendText('Seitenhintergrund auswählen').injectInside(drawer);

	drawer.injectTop(body);
	drawer_close.injectTop(drawer);
	drawer_content.injectInside(drawer);
}
window.addEvent('domready', iam_start);