function dom_init() {
	
	var slide_speed 		= 500,
		page_title_height 	= $("h2.page_title").outerHeight(),
		footer_spacer_height= $('#footer_spacer').height(),
		count_pages			= $('body > h2').size(),
		image_files			= ['jpg', 'png', 'gif'],
		target_new_files	= ['pdf', 'doc', 'rtf'],
		screen_height 		= $(window).height(),
		debug				= true, 
		debug				= false, 
		can_animate			= true,
		i					= 0,
		create_back_link    = true,
		$cache_container	= $('<div id="cache"></div>').prependTo('body');
	
	// --------------------------------------------------------
	// Grid
	// --------------------------------------------------------		
	
	var $grid = $('<div id="grid"></div>').appendTo('body');	
	$grid
		.css('position', 'absolute')
		.css('top', '0')
		.css('right', '0')
		.css('bottom', '0')
		.css('left', '0')
		.css('pointer-events', 'none')
		.css('opacity', '0.45')
		.css('z-index', '60')
		.css('background-image', 'url("http://www.artschooluk.org/wordpress/wp-content/themes/artschooltheme/layout/images/960grid.png")')
		
	// show hide grid on 'f2' key
	$(window).keypress( function(e){	
		var keycode = (e.keyCode ? e.keyCode : e.which);
		//alert( keycode )
		if( keycode == 113 ){
			$('body').toggleClass('debug')
			return false;
		}
	});		
	

	
	// --------------------------------------------------------
	// Custom scrollbars
	// --------------------------------------------------------	
	
	$('XXX.page_content').scroll( function(){
	
		var $scrollbar_wrapper	= $(this).find('.scrollbar_wrapper')
		var scrolltop 			= $(this).scrollTop();			 
		var c_height 				= $(this).find('.content_wrapper').height();
		var s_height 			= $scrollbar_wrapper.height();
		
		var handle_height		= parseInt( c_height / s_height ) + 100;
		
		$scrollbar_wrapper.find('.test').html( c_height + ' <br />' + scrolltop + '<br/>' + s_height + '<br /><br />' + handle_height )
		$scrollbar_wrapper.find('.handle').height( handle_height )
	
	});
	

	// --------------------------------------------------------
	// Newsletter subscribtion via Ajax
	// --------------------------------------------------------		
	$('#newsletter_form input[type=submit]').live( 'click', function(){
	
		var values		= $(this).parents('form').serialize()
		var $mail_input	= $(this).parents('form').find('input[name=ne]')
		
		if( is_valide_email( $mail_input.val() ) ){
			
			$.post('http://www.artschooluk.org/wordpress/', values, function(data) {
				alert('Thank you for subscribing.');
				$mail_input.val('').blur()				
			});
		
		}else{
		
			alert('Please enter a valid mail adress!');
		
		}
		
		return false;
	
	})
	
	
	// --------------------------------------------------------
	// Search
	// --------------------------------------------------------	
	var search_box_slide_speed  = 150,
		search_box_start_width	= '69px',
		original_page_href;
	$('.search_box').addClass('closed')	
	$('.search_box input').val('')	// reset value on load
	$('.search_box input').one('focus', function(){ // remember the default href of the page_content
	
		var $this 				= $(this),
			original_page_href	= $this.parents('.page_content').attr('href')
			
		// save original href
		$this.next('a').attr('href', original_page_href );
	
	})
	$('.search_box').click( function(){
		
		var $this = $(this);
		
		if( $this.hasClass('closed') ){
		
			$this.addClass('open').removeClass('closed')

			// slide search box
			$this.animate({
				width: '270px'
				}, search_box_slide_speed, function() {
					$(this).removeClass('animating')
				});				
			
			// slide input + focus
			$this.find('input').focus().animate({
				width: '170px'
				}, search_box_slide_speed, function() {	
					$(this).focus()
				});	
			
		}
		
		return false;
	
	});
	$('.search_box input').live('focusout', function(){
	
		var $this = $(this)
			value = jQuery.trim( $this.val() );	
			
		if( !value ){
			
			// search box
			$this.parent().animate({
				width: search_box_start_width,
				}, search_box_slide_speed, function() {
					$(this).addClass('closed').removeClass('open')
				});	
				
			// input
			$this.animate({
				width: '0px'
				}, search_box_slide_speed, function() {				
				});
			
		}
	
	})
	$('.search_box a').click( function(){	// reset default value of search input, reload container
		
		var $this = $(this),
			$input = $this.prev('input'),
			value = jQuery.trim( $input.val() ),
			this_categorie	= $this.parents('.page_content:first').prev().attr('id');
			
		// clear input
		$input.focus().val('').blur()
		
		// change hash
		if(value) {
			window.location.href = blogurl + '#' + this_categorie + '/';		
		}
		
		return false;
	
	});	
	var search_timer;
	$('.search_box input').keyup( function(){
	
		var $this 		= $(this),			
			$container 	= $(this).parents('.page_content'),
			wait		= 500,
			categorie	= $container.prev().attr('id');
		var href		= blogurl + '#' + categorie + '/?search=',
			in_cat		= $this.attr('name'),
			value 		= jQuery.trim( $this.val() );			
			value		= value.replace(/ /g, '+');
			new_href	= href + value + '&' + in_cat;
		
		// start ajax-request nur wenn in einem bestimmten zeitraum nichts eingegeben wurde 
		if(search_timer)window.clearTimeout(search_timer);
		search_timer = window.setTimeout( function() {
			
			// kürze letztes '&' weg
			if( new_href.substring( new_href.length-1, new_href.length ) == '&' ){
			
				new_href = new_href.substring( 0, new_href.length-1 )			
				
			}
			
			// start suche, wenn suchstring mehr als 3 zeichen hat 
			if( value.length >= 3 ){

				window.location.href = new_href;				

				if(search_timer)window.clearTimeout(search_timer);
				
			}
			
		}, wait );
	
	});


	
	// --------------------------------------------------------	
	// Layout page_titles + page_contents, 
	// --------------------------------------------------------
	$('body > h2').each( function(){ // set h2.page-titles start top-positions and section.page_contents margin-top
	
		var $this 			= $(this), 		// the title
			$content		= $this.next() 	// the content
			new_top 		= i * page_title_height, 
			new_padding_top = parseInt( $content.css('padding-top') ) + new_top + page_title_height;
			
		$this
			.addClass('closed')	
			.css('top', new_top  + 'px' )
			
		$this.next()
			.css('top', 0  + 'px' )
		
		i++;
	
	});
	$(window).resize( function(){ // re-positionate h2 and content on resize
	
		screen_height 		= $(window).height()
		
		$('body > h2.open').nextAll('h2').each( function(){
		
			var $this 		= $(this), 
				this_index  = $('body > h2').index( $this ), 
				new_top		= screen_height - ( page_title_height * (count_pages - this_index) )				
			
			$this.css('top', new_top + 'px' )
				.next('section').css('top', new_top + 'px' )
		
		});
	
	})
	
	
	
	// --------------------------------------------------------	
	// Page-Title Links
	// --------------------------------------------------------		
	$('body > h2 a').click( function(){ // handle click on h2.page_title	
		
		var $this 					= $(this).parent(), 
			this_categorie			= $this.attr('id'),
			$page_content_container = $this.next('section.page_content')
		
		$('section.page_content').show() // remake special ie treatment
		
		if(debug) alert( 'click auf body > h2 a')
		
		//if( can_animate && $this.hasClass('closed') ){	// to prevent multiple animations, only one animation at the same time
		if( can_animate ){	// to prevent multiple animations, only one animation at the same time
		
			//can_animate = false;
			
			// change hash
			window.location.href = blogurl + '#' + this_categorie + '/';

			// reset search input

		}
		
		return false;
	
	})	
	
	
	
	// --------------------------------------------------------	
	// Content Links
	// --------------------------------------------------------	
	$('section.page_content .content_wrapper a').live('click', function(){ // '.page_content a' click > load page_content via ajax
		
		if(debug) alert('click on .content_wrapper a');
		
		var $this 					= $(this),		
			$page_content_container = $this.parents('.page_content'),
			back_href 				= $page_content_container.attr('href'),
			new_href 				= $this.attr('href').replace( '#', '');
			new_href 				= blogurl + new_href.replace( blogurl, '#');
		
		// close search field
		$page_content_container.find('.search_box.open input').focus().val('').blur();
		
		// click on archive at picture-map and a-z
		if( $(this).parents('ul.archive').size() ){
		
			var post_href	= $(this).attr('href');
				new_href 	= blogurl + post_href.replace( blogurl, '#');
		
		}		
		
		// click on a image link > load href to img src
		if( jQuery.inArray( getFileExtension(new_href), image_files ) > -1 ){
			
			//$this.parents('.page_content:first').html('<img src="'+new_href+'" />');			
			//return false;
			
			//alert('image click')
			
			return false;
		
		}		
		
		// click on a pdf etc link > opens href in new tab
		if( jQuery.inArray( getFileExtension(new_href), target_new_files ) > -1 ){
			
			$this.attr('target', '_blank').trigger('click');			
			return false;
		
		}		
		
		// click on external link > opens href in new tab
		if( $this.hasClass('external') ){
			
			$this.attr('target', '_blank').trigger('click');			
			return false;
		
		}		
		
		// was it a link to ABOUT or CONTACT > open the categorie ABOUT+CONTACT
		if( getCategorieFromHref(new_href) == 'about' || getCategorieFromHref(new_href) == 'contact' ){

			openCategorie( $('#about-contact') );
			window.location.href = blogurl + '#about-contact/';
			return false;
			
		}
		
		window.location.href = new_href;
		
		return false;		
	
	});	
	
	
	
	// --------------------------------------------------------	
	// back_link
	// --------------------------------------------------------	
	$('section.page_content > a.back_link').live('click', function(){
	
		if(debug) alert('click on a.back_link');	
		
		var $this 					= $(this),
			href					= $this.attr('href'),
			$page_content_container = $this.parents('.page_content')			
		
		create_back_link 			= false;		
		window.location.href 		= href;
		
		// load back content
		//loadAjaxContent ( $page_content_container, href )
		
		return false;
	
	})
	
	
	
	// --------------------------------------------------------	
	// HashChange
	// --------------------------------------------------------	
	$(window).hashchange( function(){		
		
		var categorie = getCategorieFromUrl();
		
		// Open Categorie 	
		if( categorie ){
			
			if(debug) alert('hashchange() > categorie: ' + categorie)
			
			// open only if closed
			if( $('#' + categorie + '.closed').size() ){
				openCategorie( $('#' + categorie + '.closed') )
			}
			
			loadAjaxContent( $('#' + categorie).next(), window.location.href )
			return false;
			
		// Open HOME as default
		}else{
		
			if(debug) alert('hashchange() > no categorie')
			window.location.href = blogurl + '#home/';
		
			//categorie = 'home';
			//openCategorie( $('#' + categorie) )
			//window.location.href = blogurl + '#home/';
			//loadAjaxContent( $('#' + categorie).next(), blogurl + '#home/' )
			return false;
		
		}
		
	})
	$(window).hashchange(); // Trigger the event (useful on page load).
	
	
	// --------------------------------------------------------	
	// Functions and Expressions
	// --------------------------------------------------------	
	
	// Creating custom :external selector  >  $('a:external')
	$.expr[':'].external = function(obj){
		return !obj.href.match(/^mailto\:/)
				&& (obj.hostname != location.hostname);
	};
	
	// Creating custom highlight function selector  >  $('li').hightlight()
	jQuery.fn.highlight=function(b){
		function a(e,j){var l=0;if(e.nodeType==3){var k=e.data.toUpperCase().indexOf(j);if(k>=0){var h=document.createElement("span");h.className="highlight";var f=e.splitText(k);var c=f.splitText(j.length);var d=f.cloneNode(true);h.appendChild(d);f.parentNode.replaceChild(h,f);l=1}}else{if(e.nodeType==1&&e.childNodes&&!/(script|style)/i.test(e.tagName)){for(var g=0;g<e.childNodes.length;++g){g+=a(e.childNodes[g],j)}}}return l}return this.each(function(){a(this,b.toUpperCase())})};jQuery.fn.removeHighlight=function(){return this.find("span.highlight").each(function(){this.parentNode.firstChild.nodeName;with(this.parentNode){replaceChild(this.firstChild,this);normalize()}}).end()
	};
	
	function openCategorie( categorie ){
		
		if(debug) alert( 'F: openCategorie( ' + $(categorie).attr('id') + ' )' )
		
		var $this_h2 			= categorie, 
			$this_page_content	= $this_h2.next('section.page_content'),
			this_h2_top			= parseInt( $this_h2.css('top') ),
			$h2_above 			= $this_h2.prevAll('h2'),	// all elements above
			$h2_below 			= $this_h2.nextAll('h2'),	// all elements below
			this_h2_start_top	= $this_h2.height() * ( $h2_above.size() ),
			title_height		= $this_h2.height(),
			$open_search_input	= $this_page_content.find('.search_box.open input')

		// clear search input
		$open_search_input.val('');
		
		// slide search box
		$open_search_input.parent().animate({
			width: search_box_start_width,
			}, search_box_slide_speed, function() {
				$(this).addClass('closed').removeClass('open')
			});	
			
		// slide search input
		$open_search_input.animate({
			width: '0px'
			}, search_box_slide_speed, function() {				
			});		
			
		// this h2 wird animiert
		$this_h2.addClass('open').removeClass('closed').each( function(){

			var $this		= $(this),
				this_id		= $this.attr('id'),
				current_top = parseInt( $this.css('top') ),
				new_top		= $this.height() * ( $this.prevAll('h2').size() )
				
				if( current_top != new_top ){

					//$h2_above.first().addClass('first_prev')
					
					//alert( 'this: ' + this_id + ' : ' + current_top + ' > ' + new_top )
					$this.addClass('animating').animate({
						top: new_top + 'px'
						}, slide_speed, function() {						
							$(this).removeClass('animating') 
							can_animate = true
						});									
					
				}
				
				// ausnahme: content wird immer animiert, doof, aber weiss nichts besseres
				$this.next().add('attr', '123').addClass('animating').animate({
					top: 0 + 'px'
					}, slide_speed, function() {						
						$(this).removeClass('animating') 
						can_animate = true
					});					
		
		});	

		// below elements werden animiert
		$h2_above.addClass('closed').removeClass('open').each( function(){
		
			var $this	 	= $(this),
				this_id		= $this.attr('id'),
				current_top = parseInt( $this.css('top') ),
				new_top  	= $this.prevAll('h2').size() * title_height
				
				if( current_top != new_top ){
				
					//alert( 'above: ' + this_id + ' : ' + current_top + ' > ' + new_top )
					$this.addClass('animating').animate({
						top: new_top + 'px'
						}, slide_speed, function() {						
							$(this).removeClass('animating') 
							can_animate = true
						});
					$this.next().addClass('animating').animate({
						top: new_top + 'px',
						}, slide_speed, function() {						
							$(this).removeClass('animating') 
							can_animate = true
						});							
				
				}								
				
		});			
		
		// below elements werden animiert
		$h2_below.addClass('closed').removeClass('open').each( function(){
		
			var $this	 	= $(this),
				this_id		= $this.attr('id'),
				current_top = parseInt( $this.css('top') ),
				new_top  	= $(window).height() - ( $this.nextAll('h2').size() * title_height) - title_height;
				
				if( current_top != new_top ){
				
					//alert( 'below: ' + this_id + ' : ' + current_top + ' > ' + new_top )				
					$this.addClass('animating').animate({
						top: new_top + 'px'
						}, slide_speed, function() {						
							$(this).removeClass('animating') 
							can_animate = true
						});	
					$this.next().addClass('animating').animate({
						top: new_top + 'px'
						}, slide_speed, function() {						
							$(this).removeClass('animating') 
							can_animate = true
						});							
				}
				
		});
		
		return false;
		
	}

	function loadAjaxContent ( container, href ){	
		
		if(debug) alert( 'loadAjaxContent() ' + href )
			
		var href 				= href.replace('#' , ''),
			current_href 		= $(container).attr('href'),
			current_href 		= $(container).attr('href'),
			start_href 			= blogurl + $(container).prev().attr('id') + '/',
			$content_wrapper 	= container.find('.content_wrapper')			
			
			//alert( 'start: ' + start_href )
			//alert( 'current: ' + current_href )
			//alert( 'new: ' + href )
		
		// insert a.back_link < only if not triggered by a.back_link
		if( current_href && create_back_link ){
			
			var $back_link = $('<a href="#" class="back_link"></a>').appendTo( $(container) ),					
				new_href   = current_href.replace( '#', '');
				new_href   = blogurl + new_href.replace( blogurl, '#');			
			
			$back_link.attr('href', new_href )
			if(debug) alert('back link insert')
			
		}else{
			
			$(container).find('a.back_link:last').remove()
			if(debug) alert('no back link insert')
			create_back_link = true;
			
		}
		
		// if it's categorie startpage > remove all back_links
		if( href == start_href ){
			var $back_links = $(container).find('a.back_link')
			$back_links.remove();
		}		
		
		// load only if content isn't already inside 
		if( current_href != href ){		
			
			// is it cached allready?
			var $cached = $('1'); //$cache_container.children('div[href="'+href+'"]')
			if( $cached.size() ){
			
				if(debug) alert( 'was cached' );				
				$content_wrapper.html( $cached.html() ) // load content from cache_container
			
			}else{			
			
				container
					.addClass('loading')
					.attr('href', href)
					
				// blende $content_wrapper aus
				$content_wrapper				
					.animate({
						opacity: 0
					}, 150, function(){				
						$(this).parent().scrollTop(0);				
					})
					
				// lade content via ajax
				$.ajax({
					url: href,
					statusCode: {
					301: function() {
							alert('page not found');
						}
					},			
					success: function( data ){

						container.removeClass('loading');	
						
						// fade $content_wrapper ein
						$content_wrapper
							.html( data )
							.animate({
								opacity: 1
							}, 150, function(){})
						
						// worldpress adds these silly attributes
						$content_wrapper.find('img').removeAttr('height').removeAttr('width'); 
						
						// find external links
						$content_wrapper.find('a:external').addClass('external');
						
						// insert image titles
						$content_wrapper.find('img').each( function(){
						
							var $this = $(this),
								title = $this.attr('title')
							
							if(title){
								$this.after('<div class="image_description">' + title + '</div>')
							}
						
						});
						
						// resize (video) iframes in overview
						$content_wrapper.find('ul.archive iframe').each( function(){
						
							var $this = $(this)
							
							$this.attr('width', '290');
							$this.attr('height', '300');
						
						});					
						
						// resize (video) iframes in detail
						$content_wrapper.find('article iframe').each( function(){
						
							var $this = $(this)
							
							$this.attr('width', '100%');
							$this.attr('height', '500');
						
						});

						// hightlight search terms
						if( gup( href, 's') ){
						
							var terms = gup( href, 's').split('+')
							
							$.each(terms, function(index, value) { 
								$content_wrapper.highlight( value )
							});
						
						}				
						
						// cache it
						// var $cache = $('<div href="' + href + '">' + $content_wrapper.html() + '</div>').appendTo( $cache_container )
						
						// handle more-links > causes problems with the '#'-sign
						$content_wrapper.find('a.more-link').each( function(){
						
							var $this 		= $(this),
								post_link 	= $this.parents('article:first').find('header a').attr('href');
								
							$this.attr('href', post_link);
						
						})
						
					}
					
				});		
			
			}
			
		}

	}
	
	jQuery.fn.wrapText = function(html){
		
		$(this)
			.contents()
			.filter(function() {
				return this.nodeType == Node.TEXT_NODE;
			}).wrap( html ) ;		
		
		/*
		return this.each(function(){
			$(this.childNodes).filter("[@nodeType===3]").each(function(){				
				$(this).wrap(html)				
				//if($.trim(this.nodeValue).length > 0) $(this).wrap(html)				
			})
		});
		*/
	};	

	function getCategorieFromUrl(){

		var current_categorie = window.location.href.replace( blogurl , '');
			current_categorie = current_categorie.replace( '#' , '');
		var	slashpos  = current_categorie.indexOf('/');
		if(slashpos > 0) current_categorie = current_categorie.substr(0, slashpos);

		if(current_categorie){
			return current_categorie;
		}
		
	}
	
	function getCategorieFromHref ( href ){

		var categorie = href.replace( blogurl , '');
			categorie = categorie.replace( '#' , '');
		var	slashpos  = categorie.indexOf('/');
		if(slashpos > 0) categorie = categorie.substr(0, slashpos);

		return categorie;
	}

	function gup ( href, name ){

	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( href );
	  if( results == null )
		return "";
	  else
		return results[1];
		
	}

	function getFileExtension (filename) {

		var extension = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
			extension = extension + '';
		
		if( extension.indexOf("/") == -1 ){
		
			return extension;
			//return 'no slash ' + extension.indexOf('/') + ' ' + extension;
		
		}else{
		
			return undefined;
			//return 'with slash ' + extension.indexOf('/') + ' ' + extension;
		
		}
		
	}
	
	function is_valide_email( email ) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return reg.test(email);
	}	

	
	if( window.location.href.indexOf('#') == -1 ){
	
		//var href     = window.location.href.replace( blogurl , '');		
		//window.location.href = blogurl + '#' + href + '/';
	
	}
	
	
}





