var ostwaldhelgason = {
	
	location: new Array('current'),
	type: false,
	webroot: '/',
	
	beforeLoad: function(){
		if(!$('body').hasClass('loading')){
			$('body').addClass('loading');
			return true;
		}
		return false;
	},
	beforeRender: function(oldLocation, newLocation, r){			
		$('.'+oldLocation[0]).fadeOut(500, function(){
			/* ASSIGN NEW LOCATION */
			$('body').attr('id', newLocation[0]);
			ostwaldhelgason.location = newLocation;
			/* DISPLAY CONTENT */
			ostwaldhelgason.clear();
			switch(newLocation[0]){
				case 'biography':
					$('.biography .col1 .bigtext').html(r.biography);
				break;
				case 'home':
					ostwaldhelgason.renderNews('.'+newLocation[0]+' .col2 ul', r.news);
				break;
				case 'movies':
					ostwaldhelgason.renderMovie('.'+newLocation[0]+' .col2 ol', r.movie);
				break;
				case 'news':
					ostwaldhelgason.renderNews('.'+newLocation[0]+' ol', r.news);
				break;
				case 'stockists':
					if(r.stockists){
						$('.stockists .col4').css({display:'none'});
						$('.stockists .col2, .stockists .col3').css({display:'block'});
						ostwaldhelgason.renderStockists('.'+newLocation[0]+' .col2 ol', r.stockists);
					}else{
						$('.stockists .col2, .stockists .col3').css({display:'none'});
						$('.stockists .col4').css({display:'block'});					
					}
				break;
			}
			if(r.press){
				$('.'+newLocation[0]+' .press-release-text').find('span').html(r.press);
			}
			if(r.title){
				$('.'+newLocation[0]+' .title').find('span').html(r.title);
			}
			if(r.description){
				$('.'+newLocation[0]+' .description').find('span').html(r.description);
			}
			if(r.season){
				$('.'+newLocation[0]+' .season').find('span').html(r.season);
			}
			if(r.collections){
				ostwaldhelgason.renderNavigation('.'+newLocation[0]+' .col1 ol', r.collections);
			}
			if(r.images){
				ostwaldhelgason.renderImageGrid('.'+newLocation[0]+' ol.grid', r.images)				
			}
				
			/* RENDER */
			ostwaldhelgason.typereplace();
			ostwaldhelgason.render();		
			ostwaldhelgason.renderImagesForImageGrid('.'+newLocation[0]+' ol.grid', function(){
				$('.'+newLocation[0]+' ol.grid').eq(0).find('.image').eq(0).trigger('click');
			});
																			
		});
	},
	bindEvents: function(r){
		var instance = this;
		$('#navigation ol li a').hover(function(){
			$(this).find('.hover').fadeIn(250);
		}, function(){
			if($('body').attr('id') != ($(this).parent().attr('id')).substr(4)){
				$(this).find('.hover').fadeOut(250);				
			}
		});
		$('#header a, .internal').each(function(i, link){
			$(link).click(function(){
				var to = ($(this).attr('href')).substr(2);						
				return instance.load(to.split('/'));
			});
		});
		
		$('.toggle-press a').click(function(){
			if(($(this).attr('href')).match(/press/)){
				ostwaldhelgason.showPressRelease();
			}else{
				ostwaldhelgason.hidePressRelease();
			}
			return false;
		});
		
		$('.prev, .next').click(function(){
			if(($(this).attr('href')).match('current')){
				return ostwaldhelgason.load(ostwaldhelgason.splitLocationHash($(this).attr('href')));
			}else{
				if($(this).hasClass('prev')){
					var prev = (($('.'+$('body').attr('id')+' .picture').data('index')-1) > -1) ? ($('.'+$('body').attr('id')+' .picture').data('index')-1) : ($('.image').length-1);
					$('.image').eq(prev).trigger('click');
				}else{
					var next = (($('.'+$('body').attr('id')+' .picture').data('index')+1) < $('.image').length) ? ($('.'+$('body').attr('id')+' .picture').data('index')+1) : 0;
					$('.image').eq(next).trigger('click');
				}
				return false;
			}
		});
		
	},
	clear: function(){
		$('.content ol').each(function(i, ol){
			if(!$(ol).hasClass('toggle-press')){
				$(ol).find('li').remove();				
			}
		});
	},
	hidePagination: function(){
		$('.'+$('body').attr('id')+' .pagination').animate({top: '-=30'}, 500);
	},
	hidePressRelease: function(){
		$('.'+ostwaldhelgason.location[0]+' .press-release').fadeOut(500, ostwaldhelgason.showPagination);
	},
	init: function(params){
		this.type = params.type;
		this.webroot = params.webroot;
		ostwaldhelgason.bindEvents();
		/* Some additional DOM elements */
		$('.picture').append('<img src="'+params.webroot+'images/blank.gif" class="original"/><img src="'+params.webroot+'images/blank.gif" class="fader"/>');
		/* Load */
		var load = (window.location.hash) ? (window.location.hash) : '#/home/'; 
		ostwaldhelgason.load(ostwaldhelgason.splitLocationHash(load));	
	},
	load: function(location){
		if(this.beforeLoad()){
			var target = (this.webroot+'ajax/'+location.join('/'));
			$.ajax({dataType: 'json', type: 'POST', url: target, success: function(r){
				ostwaldhelgason.beforeRender(ostwaldhelgason.location, location, r);
			}});				
		}
	},
	normalize: function(link){
		return (link.lastIndexOf('#') > -1) ? link.substr(link.lastIndexOf('#')) : link;
	},
	render: function(){
		var baseline = 0;
		var max  = 0;
		var map = this.selectLinks();
		$('.'+this.location[0]).fadeIn(500, function(){
			for(column in map){
				if(column > 0){
					var h = 0;
					switch($('body').attr('id')){
					case 'archive':
						h = 43;
					break;
					case 'movies':
						h = 61;
					break;
					case 'stockists':
						h = 25;
					break;
					}					
					map[column] = (map[column]*h) + baseline;
				}else{
					map[column] = (map[column]*18);
					baseline = map[column];
				}
				if(map[column] > max) max = map[column];
			}		
			for(var column = 0; column < map.length; column++){
				$('.'+ostwaldhelgason.location[0]+' .column').eq(column).css({top:0}).animate({top: map[column]}, 400, 'bounceout');				
			}
			/* MAX HEIGHT */
			var maxHeight = 0;
			$('.'+$('body').attr('id')+' .column').each(function(i, column){
				if($(column).height() > maxHeight) maxHeight = $(column).height();
			});
			$('#canvas, #content').each(function(i, resize){
				if($(resize).attr('id') == 'content'){
					$(resize).height(maxHeight+max);
				}else{
					$(resize).height(maxHeight+108);					
				}
			});
			$('body').removeClass('loading');		
		});			
	},
	renderNavigation: function(parent, collections){
		for(var i in collections){
			var offset = ($('#navigation ol li#nav_'+$('body').attr('id')+' a').attr('alt')).split(',');
			var title = (collections[i].columns.title);
			var subtitle = collections[i].columns.subtitle;
			switch(this.location[0]){
			case 'archive':	
				$(parent).append('<li><a href="#/'+this.location[0]+'/'+collections[i].columns.label+'/" alt="'+offset[0]+','+i+','+i+',0"><span class="first DTLNobelT">'+title.substr(0, title.indexOf('2')-1)+'</span><span class="second DTLNobelT">'+title.substr(title.indexOf('2'))+'</span><span class="first hover blue DTLNobelT">'+title.substr(0, title.indexOf('2')-1)+'</span><span class="second hover blue DTLNobelT">'+title.substr(title.indexOf('2'))+'</span></a></li>');
			break;
			case 'movies':
				$(parent).append('<li><a href="#/'+this.location[0]+'/'+collections[i].columns.label+'/" alt="'+offset[0]+','+i+','+i+',0"><span class="first DTLNobelT">'+title+'</span><span class="second DTLNobelT-Light">'+subtitle.substr(0, subtitle.indexOf('2')-1)+'</span><span class="third DTLNobelT-Light">2008</span><span class="first hover blue DTLNobelT">'+title+'</span><span class="second hover blue DTLNobelT-Light">'+subtitle.substr(0, subtitle.indexOf('2')-1)+'</span><span class="third hover blue DTLNobelT-Light">2008</span></a></li>');
			break;
			case 'stockists':
				$(parent).append('<li><a href="#/'+this.location[0]+'/'+collections[i].columns.label+'/" alt="'+offset[0]+','+i+','+i+',0"><span class="first DTLNobelT">'+title+'</span><span class="first hover blue DTLNobelT">'+title+'</span></a></li>');
			break;
			}
		}
		
		$(parent).find('a').click(function(){
			$(this).addClass('select');
			ostwaldhelgason.load(ostwaldhelgason.splitLocationHash($(this).attr('href')));
		}).hover(function(){
			$(this).find('.hover').fadeIn(250);
		}, function(){
			if(!$(this).hasClass('select')){
				$(this).find('.hover').fadeOut(250);
			}
		});
	},
	renderNews: function(parent, news){
		$(parent).empty();
		for(var x in news){
			for(var i in news[x]){
				$(parent).eq(x).append('<li><h4>'+news[x][i].columns.title+'</h4>'+news[x][i].columns.text+'</li>');				
			}
		}
		return true;
	},
	renderImageGrid: function(parent,images){
		var isOuterbound = false;
		for(var x in images){
			for(var i in images[x]){
				if(images[x][i].id){
					$(parent).eq(x).append('<li class="image'+(isOuterbound ? ' outerbound' : '')+'"><a href="#/'+this.location.join('/')+i+'/"><img src="'+this.webroot+'images/blank.gif" title="'+this.webroot+'ressources/photos/'+images[x][i].id+'/thumbnail.jpg"/></a></li>');					
				}else{
					$(parent).eq(x).append('<li class="dummy"><img src="'+this.webroot+'images/dummy.gif" /></li>');
				}
			}
		}
		$(parent).append('<li class="clear"></li>');
		$(parent).find('li.image').each(function(i, link){
			$(link).click(function(){
				$('.'+' .picture').data('index', i);
				ostwaldhelgason.showImage($(this).find('img').attr('src'));
				return false;
			});
		});
	},
	renderImagesForImageGrid: function(parent, callback){
		$(parent).find('li.image img').each(function(i, image){
			var src = $(parent).find('li.image img').eq(i).attr('title');
			if(src){
				$(parent).find('li.image img').eq(i).attr('src',src).load(function(){
					$(this).removeAttr('title').fadeIn(500, function(){
						if(i == ($(parent).find('li.image img').length-1)){
							callback();
						}
					});
				});				
			}
		});
		/* Workaround for IE, fix that! */
		window.setTimeout(function(){
			$(parent).find('li img').fadeIn(500, function(){
				callback();
			});				
		}, 3000);
	},
	renderMovie: function(parent, movie){
		$(parent).append('<li><embed width="444" height="333" flashvars="width=444&height=333&file='+movie.columns.url+'&javascriptid=movieplayer&enablejs=true" allowfullscreen="true" allowscriptaccess="always" quality="high" src="'+ostwaldhelgason.webroot+'javascript/movieplayer.swf" type="application/x-shockwave-flash"/></embed><span>By Conrad Ostwald</span></li>');	
	},
	renderStockists: function(parent, stockists){
		for(var i in stockists){
			$(parent).append('<li><a href="'+stockists[i].columns.subtitle+'/" target="_blank" class="no-type"><span class="DTLNobelT blue">'+stockists[i].columns.title+'</span></a><div class="location">'+stockists[i].columns.text+'</div></li>');
		}
	},
	selectLinks: function(){
		var map = new Array(0,0,0,0);
		var location = '#/'+(ostwaldhelgason.location).join('/');
		$('.navigation a, #navigation a').each(function(i, link){
			/* Highlight Selection */
			if(location.indexOf($(link).attr('href')) > -1){
				$(link).addClass('select');				
			}else{
				$(link).removeClass('select');
			}
			/* Move Map */
			if(location == ostwaldhelgason.normalize($(link).attr('href'))){
				map = ($(link).attr('alt')).split(',');				
			}
		});
		/* Hack for select */
		if(ostwaldhelgason.location[0] == 'archive'){
			if($('.archive .col1 .select').length == 0){
				$('.archive .col1 a').eq(0).addClass('select');				
			}
		}
		if(ostwaldhelgason.location[0] == 'movies'){
			if($('.movies .col1 .select').length == 0){
				$('.movies .col1 a').eq(0).addClass('select');				
			}
		}
		return map;
	},
	showImage: function(src){
		var fullsize = src.replace('thumbnail', 'large');
		if(fullsize != $('.'+($('body').attr('id'))+' .picture img.original').attr('src')){
			/* Hide Press-Release */
			this.hidePagination();
			if($('.'+($('body').attr('id'))+' .press-release').css('display') != 'none'){
				$('.'+($('body').attr('id'))+' .press-release').fadeOut(250);
			}
			/* Preload, FadeIn & Show */
			if(!$('.'+($('body').attr('id'))+' .picture').data('locked')){
				$('.'+($('body').attr('id'))+' .picture img.fader').attr('src', src).fadeIn(500, function(){
					var h = $('.'+$('body').attr('id')+' img.fader').height();
					ostwaldhelgason.showPagination();
					$('.'+($('body').attr('id'))+' .picture').data('locked', true);
					$('.'+($('body').attr('id'))+' .picture img.original').attr('src', fullsize).load(function(){
						$('.'+($('body').attr('id'))+' .picture img.fader').fadeOut(500, function(){
							$('.'+($('body').attr('id'))+' .picture').data('locked', false);
						});			
					});
				});		
			}
		}
	},
	showPagination: function(){
		var h = $('.'+$('body').attr('id')+' .picture img').eq(1).height();
		$('.'+$('body').attr('id')+' .pagination').css({display: 'block'}).animate({top: (h+3)+'px'}, 250);
	},
	showPressRelease: function(){
		$('.'+ostwaldhelgason.location[0]+' .press-release').fadeIn(500, ostwaldhelgason.hidePagination);
	},
	splitLocationHash: function(hash){
		var location = ((this.normalize(hash)).substr(2)).split('/');
		return location;
	},
	typereplace: function(){
		if(ostwaldhelgason.type){
			$('h3, .navigation a, #navigation a').each(function(i, t){
				if(!$(t).hasClass('no-type')){
					if($(t).find('img').length == 0){
						$(t).find('span').each(function(i2, span){
							$(span).html('<img src="'+ostwaldhelgason.webroot+'type/replace.php?text='+$(span).html()+'&c='+($(span).hasClass('blue') ? 'blue' : 'black')+'&f='+($(span).hasClass('DTLNobelT') ? 'DTLNobelT' : 'DTLNobelT-Light')+'" />');											
						});					
					}					
				}
			});
		}	
	}
}