/* Author: Robert Carroll

*/

$(document).ready(function(){
	// load index page when the page loads
	$("#main").load("intro.html").hide().fadeIn('slow');
	
	$("#history").click(function(){
	// load about page on click
		$("#main").load("history.html").hide().fadeIn('slow');
	});
	$("#contact").click(function(){
	// load contact form onclick
		$("#main").load("email.html").hide().fadeIn('slow');
	});
	$("#curriculum").click(function(){
	// load contact form onclick
		$("#main").load("curriculum.html").hide().fadeIn('slow');
	});
	$("#clients").click(function(){
	// load contact form onclick
		$("#main").load("clients.html").hide().fadeIn('slow');
	});
	$("#work").click(function(){
	// load contact form onclick
		$("#main").load("work2.html").hide().fadeIn('slow');
	});
});




$(document).ready(function() {
	initMenus();

	$("a#inline").fancybox({
			'hideOnContentClick': true
		});
	$("a#viewImage").fancybox();

	
	
//	$('#main').load('ajax/test.html', function() {
//	  alert('Load was performed.');
//	});
						   
//	var hash = window.location.hash.substr(1);
//	var href = $('#mainMenu ul li a').each(function(){
//		var href = $(this).attr('href');
//		if(hash==href.substr(0,href.length-5)){
//			var toLoad = hash+'.html #main';
//			$('#main').load(toLoad)
//		}											
//	});
//
//	$('#mainMenu li a').click(function(){
//								  
//		var toLoad = $(this).attr('href')+' #main';
//		$('#main').hide('fast',loadContent);
//		$('#load').remove();
//		$('#main').append('<span id="load">LOADING...</span>');
//		$('#load').fadeIn('normal');
//		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
//		function loadContent() {
//			$('#main').load(toLoad,'',showNewContent())
//		}
//		function showNewContent() {
//			$('#main').show('normal',hideLoader());
//		}
//		function hideLoader() {
//			$('#load').fadeOut('normal');
//		}
//		return false;
//		
//	});

});


$(document).ready(function(){
	
	//select only RECENT items to begin with
	
	
	
	var items = $('#stage li'),
		itemsByTags = {};
	
	// Looping though all the li items:
	
	items.each(function(i){
		var elem = $(this),
			tags = elem.data('tags').split(',');
		
		// Adding a data-id attribute. Required by the Quicksand plugin:
		elem.attr('data-id',i);
		
		$.each(tags,function(key,value){
			
			// Removing extra whitespace:
			value = $.trim(value);
			
			if(!(value in itemsByTags)){
				// Create an empty array to hold this item:
				itemsByTags[value] = [];
			}
			
			// Each item is added to one array per tag:
			itemsByTags[value].push(elem);
		});
		
	});

	// Creating the "Everything" option in the menu:
	createList('',items);

	// Looping though the arrays in itemsByTags:
	$.each(itemsByTags,function(k,v){
		createList(k,v);
	});
	
	$('#filter a').live('click',function(e){
		var link = $(this);
		
		link.addClass('active').siblings().removeClass('active');
		
		// Using the Quicksand plugin to animate the li items.
		// It uses data('list') defined by our createList function:
		
		$('#stage').quicksand(link.data('list').find('li'), 
		function() { // callback function
		    $("a#viewImage").fancybox();
		  }
		);
		e.preventDefault();
	});
	
	$('#filter a:first').click();
	
	function createList(text,items){
		
		// This is a helper function that takes the
		// text of a menu button and array of li items
		
		// Creating an empty unordered list:
		var ul = $('<ul>',{'class':'hidden'});
		
		$.each(items,function(){
			// Creating a copy of each li item
			// and adding it to the list:
			
			$(this).clone().appendTo(ul);
		});

		ul.appendTo('#stage');

		// Creating a menu item. The unordered list is added
		// as a data parameter (available via .data('list'):
		
		var a = $('<a>',{
			html: text,
			href:'#',
			data: {list:ul}
		}).appendTo('#filter');
	}
});

function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('slow');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('slow');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('slow');
				checkElement.slideDown('slow');
				return false;
			}
		}
	);
}





