/**
 * block info
 * name: class of the block (.name .content)
 * position: left position within main element
 * page: ajax page to load
 * module: use module instead of page for program/news content
 * extend: used for names within another name (category = news, for instance)
 * next: next page in the list to load
 * 
 * The slightly indented blocks are special blocks.
 */
var blocks = {
	home : 			{name: 'home', position: 0, page: 'page/home', next: 'news'},
	 news : 		{name: 'news', position: -1300, module: 'news', next: 'about'},
	 category : 	{name: 'category', position: -1300, extend: 'news', page: 'page/category', next: 'about'},
	about : 		{name: 'about', position: -2600, page: 'page/about', next: 'partners'},
	partners : 		{name: 'partners', position: -3900, page: 'page/partners', next: 'organization'},
	organization : 	{name: 'organization', position: -5200, page: 'page/organization', next: 'program'},
	 program : 		{name: 'program', position: -6500, module: 'program', next: 'press'},
	press : 		{name: 'press', position: -7800, page: 'page/press', next: 'venues'},
	venues : 		{name: 'venues', position: -9100, page: 'page/venues', next: 'tickets'},
	tickets : 		{name: 'tickets', position: -10400, page: 'page/tickets', next: 'timetable'},
	timetable : 	{name: 'timetable', position: -11700, page: 'page/timetable', next: 'participants'},
	participants : 	{name: 'participants', position: -13000, page: 'page/participants', next: 'support'},
	support : 		{name: 'support', position: -14300, page: 'page/support', next: 'become'},
	become : 		{name: 'become', position: -15600, page: 'page/become', next: 'contribute'},
	contribute : 	{name: 'contribute', position: -16900, page: 'page/contribute', next: 'disclaimer'},
	disclaimer : 	{name: 'disclaimer', position: -18200, page: 'page/disclaimer', next: 'practical'},
	practical : 	{name: 'practical', position: -19500, page: 'page/practical', next: 'workshops'},
	workshops : 	{name: 'workshops', position: -20800, page: 'page/workshops', next: 'keynotes'},
	keynotes : 		{name: 'keynotes', position: -22100, page: 'page/keynotes', next: 'sideevents'},
	sideevents : 	{name: 'sideevents', position: -23400, page: 'page/sideevents', next: 'betanight'},
	betanight : 	{name: 'betanight', position: -24700, page: 'page/betanight'}
	
}

function loadContent(element, url, secondary){
	if (secondary == true)
		$(element).html('<div id="ajaxloader"></div>');
	
	$(element).load(url, function(){
		
		
		
		if (url == 'page/home'){
			$(".carousel").carousel({
				loop: true,
		        autoSlide: true,
		        prevBtn: '<div class="prev_btn">&nbsp;</div>',
		        nextBtn: '<div class="next_btn">&nbsp;</div>'
			});
		}
	});
}

function loadWindow(type, view){
	closeWindow();
	$('<div class="' + type + ' easyremove"><div id="' + type + '_detail"></div></div>').prependTo('.main');
	$('#' + type + '_detail').load('page/' + view + '/', function(){
		$('#' + type + '_detail').fadeIn('normal');		
	});	
}

function loadModule(type, id, secondary){
	if (secondary == true)
		$('#' + type + '_detail').html('<div id="ajaxloader"></div>');
	
	closeWindow();
	$('<div class="' + type + '_block easyremove"><div id="' + type + '_detail"></div></div>').prependTo('.main');
	
	$('#' + type + '_detail').load('page/' + type + '_detail/' + id, function(){
		$('#' + type + '_detail').fadeIn('normal');
		var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
		if (window.addthis){
		    window.addthis = null;
		}
		$.getScript( script );
	});
}

function unloadContent(active){	
	//Only two are active at any time, unload all blocks that don't match first or second
	$.each(blocks, function() {
		if ($.inArray(this.name, active) == -1){
			if (this.module == 'news'){
				$('#load_news').empty();
			}
			else if (this.module == 'program'){
				$('#program_main').empty();
				$('#program_activities').empty();
			}
			else{
				$('.' + this.name + ' .content').empty();
			}
		}
	});	
}

function moveProgram(direction, id){
	var programList = $(id).height()*-1;
	var offset = $(id).offset();
	
	//There's a default offset of 302px
	offset.top = offset.top - 302;
	
	if (direction == 'up'){		
		if (offset.top >= -410){
			$(id).animate({top: '0'}, {queue:false,duration:500});
		}
		else{
			$(id).animate({top: '+=410'}, {queue:false,duration:500});
		}
	}
	else if (direction == 'down'){
		if (offset.top > (programList+410))
			$(id).animate({top: '-=410'}, {queue:false,duration:500});
	}
}

function hashchange(){
	var url = window.location;
	var anchor = url.hash;
	var segment = anchor.split("/");

	$.each(blocks, function() {
		if ('#' + this.name == segment[0]){
			name = this.name;
			position = this.position;
			module = this.module;
			page = this.page;
			extend = this.extend;
			next = this.next;
			return true;
		}
	});
	
	if (name){
		//Load content
		var offset = $('.main').offset();
		if (offset.left != position){
			$('#sascha').css('display', 'block'); //place overlay, only applicable during a move
			if (module == 'news') loadContent('#load_news', 'page/news');
			if (module == 'program'){ loadContent('#program_main', 'page/program/main'); loadContent('#program_activities', 'page/program/activities');	}
			if (page) loadContent('.' + name + ' .content', page);
			
			if (next == 'news') loadContent('#load_news', 'page/news', true);
			if (next == 'program'){ loadContent('#program_main', 'page/program/main', true); loadContent('#program_activities', 'page/program/activities', true); }
			if (next != 'news' && next != 'program') loadContent('.' + next + ' .content', 'page/' + next, true);
		}
		
		if (name == 'category') name = 'news';
		
		//Perform animation
		$('.main').animate({left: position}, 1000, function(){ $('#sascha').css('display', 'none'); unloadContent([name, next]); });
		
		//Load overlay if applicable
		if (module && segment[1])
			loadModule(module, segment[1]);
		
		if (extend && segment[1])
			loadContent('#load_' + extend, page + '/' + segment[1]);		
	}
	
	//Cancel overlays, but don't animate
	if (segment[0] == '#_'){
		closeWindow();
	}
}

function closeWindow(){
	$('.easyremove')
		.fadeOut('normal', function(){ 
			$(this).remove(); 
		});	
}

$(document).ready(function(){
						   
	//Scrollbar shizzle
	$('.programma-workshops').scrollbarPaper();
	$('.press-block').scrollbarPaper();
	

						   
	//Load starting block content
	loadContent('.home .content', 'page/home');
	loadContent('#load_news', 'page/news');
	
	//Click on a block to move to its position
	var mainPosition = $('.main').offset();
	$.each(blocks, function() {
		var block = this;
		$('.' + block.name).click(function(){ 
			if (mainPosition.left != block.position)
				window.location.hash = '#' + block.name; 
		});
	});
	
	//Check current URL, move to the correct position and load appropriate content
	hashchange();
	
	//Menu buttons	
	$('.main-menu').hover(function(){
		$(this).animate({height:'120px'},{queue:false,duration:500});
	}, function(){
		$(this).animate({height:'40px'},{queue:false,duration:500});
	});
	
	//Countdown
	var austDay = new Date("June 11, 2010 00:00:00");
	$('#defaultCountdown').countdown({until: austDay, format: 'dHM'});
	$('#year').text(austDay);
	
});

$(window).bind('hashchange', function(){
	hashchange();
});



