jQuery(document).ready(function() {
	
	// get window hash
	var window_hash = window.location.hash;

	// go to hash if available
	function go_to_hash(window_hash) {
		if (window_hash == '' || window_hash == '#home') {
			return false;
		}
	
		// declare current as home
		var current = 'home';
		
		// figure out select from hash
		var select = window_hash.substr(1);
		
		// figure out css_class
		var cur_pos = $('#navigation li.home a').position();
		cur_pos = cur_pos.left;
		
		var select_pos = $('#navigation li.' + select + ' a').position();
		select_pos = select_pos.left;
		
		// figure out where to place the content (before or after)
		if (cur_pos > select_pos) {
			var css_class = 'before';
		}
		if (cur_pos < select_pos) {
			var css_class = 'after';
		}
		if (cur_pos == select_pos) {
			var css_class = 'no-change';
		}
		
		$('#navigation li.home a').removeClass('current');
		$('#navigation li.' + select + ' a').addClass('current');
			
		// build the content 
		build_content(css_class, current, select);
		return true;
	}
	go_to_hash(window_hash);
	
	/** replace navigation with hashes
	 */
	$('#navigation li a').each(function(index) {
		var hash = $(this).parent().attr('class');		
		$(this).attr('href', '#' + hash);
	});
	
	/** page slider
	 */
	$('#navigation li a').click(function() {
				
		// get the current position
		var current = $('#navigation li a.current').parent().attr('class');		
		var cur_pos = $('#navigation li a.current').position();
		cur_pos = cur_pos.left;
		
		// get the selected position
		var select = $(this).parent().attr('class');
		var select_pos = $(this).position();
		select_pos = select_pos.left;
		
		window.location.hash = select;
		
		// change the current class to reflect
		$('#navigation li a.current').removeClass('current');
		$(this).addClass('current');
		
		// figure out where to place the content (before or after)
		if (cur_pos > select_pos) {
			var css_class = 'before';
		}
		if (cur_pos < select_pos) {
			var css_class = 'after';
		}
		if (cur_pos == select_pos) {
			var css_class = 'no-change';
		}
	
		var existing = $('#' + select).length;

		// create new content
		if (existing == 0) {
			build_content(css_class, current, select);		
		} // end create new content		
		
		
		return false;
	});
	
	function build_content(css_class, current, select) {
		// get the url to load
		var url = '/' + select;		

		// build the new content div
		var new_content = '<div id="' + select + '" class="content ' + css_class + '"></div>';
		if (css_class == 'before') {
			$('#' + current).before(new_content);
		}
		if (css_class == 'after') {
			$('#' + current).after(new_content);			
		}

		$('#' + select).load(url + ' .content .entry', function() {
			// load gallery scripts
			if (select == 'gallery') {
				$.getScript('/workspace/shadowbox/shadowbox.js', function() {
					Shadowbox.init({
						language: 'en', 
					    players:  ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv'] 
					});
				});
			}
			// load list scripts
			if (select == 'services' || select == 'team') {
				$.getScript('/workspace/scripts/jquery.localscroll-min.js', function() {
					$('#secondary-menu').localScroll({
						target: '#entry-content',
						duration: 400
					});
				});
				$.getScript('/workspace/scripts/jquery.up-down-list.js');
			}
			// load contact scripts
			if (select == 'contact') {
				
			}			
			slide(css_class, current, select);
		}); // end loading
	}
	
	
	function slide(css_class, current, select) {
				
		// pulls content from right to left
		if (css_class == 'before') {
			$('#' + current).animate({
				marginLeft: "200%",
				marginRight: "-200%"
			  }, 1000, 'swing', function() {
				$('#' + current).remove();
				
			});
			$('.before').animate({
				marginLeft: '0%'
			}, 1000, 'swing', function() {
				$(this).removeClass(css_class);
			});
		}
		// pulls content from left to right
		if (css_class == 'after') {
			$('#' + current).animate({
				marginRight: "200",
				marginLeft: "-200%"
			  }, 1000, 'swing', function() {
				$('#' + current).remove();
				
			});
			$('.after').animate({
				marginLeft: '0%',
			}, 1000, 'swing', function() {
				$(this).removeClass(css_class);
			});
			if (select == 'contact') {
				window.location = '/contact';
			}
		}
	}
});
