/**
 *	@author dan entous
 *	@version 2011-11-11 11:54 GMT +1
 */
$( document ).ready( function(){
	
	Pages.initialize();
	
	$("a[rel='external']").click( function( event ) {
		event.preventDefault();
		window.open( this );
	});
	
	$('span.addr').each( function() {
		$(this).attr("id", "addr0");
		var spt = "#" + $(this).attr("id");
		var to = ' [at] ';
		var period = ' [.] ';
		var addr = $(spt).text()
			.replace(to, "@")
			.replace(period, ".")
			.replace(period, ".");
		$(spt).after('<a href="mailto:' + addr + '">' + addr +'</a>');
		$(spt).remove();
	});
	
});


$(window).bind('load', function() {
	
	// http://www.appelsiini.net/2007/6/sequentially-preloading-images
	var preload = [];
	
	// Add urls to preload array
	preload.push( '/images/backvoet.jpg' );
	preload.push( '/images/backbil.jpg' );
	preload.push( '/images/backknieen.jpg' );
	preload.push( '/images/backhiel.jpg' );
	preload.push( '/images/backbend.jpg' );
	preload.push( '/images/backvhoofd.jpg' );
	preload.push( '/images/backvoethand.jpg' );
	preload.push( '/images/korte_prinsengracht.jpg' );
	preload.push( '/images/ceintuurbaan.jpg' );
	
	// Load the preload images sequentially
	var img = document.createElement( 'img' );
	
	$(img).bind( 'load', function() {
	
		if ( preload[0] ) {
			this.src = preload.shift();
		}
	}).trigger('load');
	
});


var Pages = new function Pages() {
	
	this.default_page = 'page-1';
	this.current_page = '';
	this.page_to_show = '';
	
	
	this.initialize = function() {
		
		setInterval( "Pages.checkHash()", 300 );	
		
	};
	
	
	this.getHash = function() {
		
		return self.document.location.hash.substring(1);
		
	};
	
	
	this.checkHash = function() {
		
		var page = document.location.pathname;
		this.page_to_show = this.getHash();
		
		if ( this.page_to_show.length < 1 ) {
			
			this.current_page = this.default_page;
			this.page_to_show = this.current_page;
			
		}
		
		if ( '/class-schedule.html' === page || '/les-schema.html' === page ) {
			
			this.setActiveSchedule( page + '#' + this.page_to_show );
			
		}
		
		this.fadeOutPage();
		
	};
	
	
	this.fadeOutPage = function() {
		
		if ( $( '#' + this.page_to_show ).css('display') == 'block' ) {
			return;
		}
		
		var self = this;
		
		if ( $('.pagination').length > 0 ) {
			
			$.each( $('.pagination').parent('div'), function( index, div ) {
			
				if ( $(div).css('display') == 'block' ) {
					
					$(div).fadeOut( 'fast', function() {
					
						self.fadeInPage();
						return false;
					
					});
					
				}
				
			});
			
		} else if ( $('.pagination-small').length > 0 ) {
			
			$.each( $('.pagination-small').parent('div'), function( index, div ) {
			
				if ( $(div).css('display') == 'block' ) {
					
					$(div).fadeOut( 'fast', function() {
					
						self.fadeInPage();
						return false;
					
					});
					
				}
				
			});
			
		}
		
		if ( $('#' + this.page_to_show).css('display') == 'none' ) {
		
			this.fadeInPage();
		
		}
		
	};
	
	
	this.fadeInPage = function() {
	
		var self = this;
		
		$( '#' + this.page_to_show ).fadeIn( 'slow', function() {
			
			self.current_page = self.page_to_show;
			
		});
	
	};
	
	this.setActiveSchedule = function( page_to_match ) {
		
		var $menu = jQuery('#menu a');
		
		$menu.each( function( index, value ) {
			
			var $elm = jQuery(value);
			
			if ( page_to_match === $elm.attr('href') ) {
				
				$elm.addClass('selected');
			
			} else {
				
				$elm.removeClass('selected');
				
			}
			
		});
		
	}
	
	
};
