$(document).ready(function(){

$(function(){
  
  // Keep a mapping of url-to-container for caching purposes.
  var cache = {
    // If url is '' (no fragment), display this div's content.
    '': $('#frontpage') 
  };
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {
    
    // Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
    var url = $.param.fragment();
    
    // Remove .bbq-current class from any previously "current" link(s).
    $( '#menu a.clicked' ).removeClass( 'clicked' );
    
    // Hide any visible ajax content.
	if ($('#frontpage').is(':visible')) { $('#content').children(':visible').hide();
	} else {
    $( '#content' ).children( ':visible' ).fadeOut(800);
	}
    
    // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
    url && $( 'a[href="#' + url + '"]' ).addClass( 'clicked' );
    
    if ( cache[ url ] ) {
      // Since the element is already in the cache, it doesn't need to be
      // created, so instead of creating it again, let's just show it!
      cache[ url ].fadeIn(800);
      
    } else {
      // Create container for this url's content and store a reference to it in
      // the cache.
      cache[ url ] = $( '<div class="section"/>' )
        
        // Append the content container to the parent container.
        .hide().appendTo( '#content' ).fadeIn(800)
        
        // Load external content via AJAX. Note that in order to keep this
        // example streamlined, only the content in .infobox is shown. You'll
        // want to change this based on your needs.
        .load( url, function(){
          // Content loaded, hide "loading" content.
			$('.gallery ul li > a').click(function() {
				if ($(this).parent().next().length === 0) {
					$(this).parent().fadeOut(500);
					$(this).parent().parent().children('li:first').fadeIn();
				} else {
				$(this).parent().fadeOut(500);
				$(this).parent().next().fadeIn(500);
				}
			})
			
			$('.prevproject').click(function() {
				if ($(this).parent().parent().parent().prev().length === 0) {
					$(this).parent().parent().parent().fadeOut(500);
					$(this).parent().parent().parent().parent().children('li:last').fadeIn();
				} else {
				$(this).parent().parent().parent().fadeOut(500);
				$(this).parent().parent().parent().prev().fadeIn(500);
				}
			})
			$('.nextproject').click(function() {
				if ($(this).parent().parent().parent().next().length === 0) {
					$(this).parent().parent().parent().fadeOut(500);
					$(this).parent().parent().parent().parent().children('li:first').fadeIn();
				} else {
				$(this).parent().parent().parent().fadeOut(500);
				$(this).parent().parent().parent().next().fadeIn(500);
				}
			})
			
			$('.nextproject').hover(function() {
				$(this).stop(true,true).animate({backgroundColor: '#929292', color: '#fff'});
			},
				function() {
				$(this).stop(true,true).animate({backgroundColor: '#fff', color: '#666'});
			});
			$('.prevproject').hover(function() {
				$(this).stop(true,true).animate({backgroundColor: '#929292', color: '#fff'});
			},
				function() {
				$(this).stop(true,true).animate({backgroundColor: '#fff', color: '#666'})
			});
/*
			$('.gallery ul li').each(function() {
				$(this).width($(this).children().children().width() + 26);
				$(this).css('margin-left',-$(this).children().children().width() / 2 - 13);
			});
*/
			$('.gallery ul li:first-child').css('display','block');
			$('.gallery ul li').not(':first-child').css('display','none');
			$('.gallery').parent().attr('id',$(this).attr('id'));
			$(function() {

				// if the function argument is given to overlay,
				// it is assumed to be the onBeforeLoad event listener
				$("a[rel]").overlay({
			
					mask: '#000',
			
					onBeforeLoad: function() {
			
						// grab wrapper element inside content
						var wrap = this.getOverlay().find(".contentWrap");
			
						// load the page specified in the trigger
						wrap.load(this.getTrigger().attr("href"));
					}
			
				});
			});
        });
    }
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );
  
});


/*
$('#logo').click(function() { 
	$('#content > div').fadeOut(800);
	$('#frontpage').fadeIn(800);
	$('.clicked').animate({color: '#929292'}, 800);
	$('.clicked').removeClass('clicked');
});

$('#menu ul li a').click(function(){
	if ($(this).hasClass('clicked')) {
		return false;
	} else {
		$('.clicked').animate({color: '#929292'}, 800);
		$('.clicked').removeClass('clicked');
        $(this).addClass('clicked');
		$(this).css('color','#555');
	}
});

$('.contact').click(function() { 
	if($('#contact').is(':hidden')) {
		$('#content > div').fadeOut(800);
		$('#contact').fadeIn(800);
	} else {
		return false;
	}
});

$('.cv').click(function() { 
	if($('#cv').is(':hidden')) {
		$('#content > div').fadeOut(800);
		$('#cv').fadeIn(800);
	} else {
		return false;
	}
});

$('.portfolios').click(function() { 
	if($('#portfolios').is(':hidden')) {
		$('#content > div').fadeOut(800);
		$('#portfolios').fadeIn(800);
	} else {
		return false;
	}
});

*/



/*
$('.bettydew').click(function() {
	$('#content > div').fadeOut(800);
	$('#bettydew').fadeIn(800);
})
 */
$('.gallery ul li > a').click(function() {
	if ($(this).parent().next().length === 0) {
		$(this).parent().fadeOut(500);
		$(this).parent().parent().children('li:first').fadeIn();
	} else {
	$(this).parent().fadeOut(500);
	$(this).parent().next().fadeIn(500);
	}
})

$('.prevproject').click(function() {
	if ($(this).parent().parent().parent().prev().length === 0) {
		$(this).parent().parent().parent().fadeOut(500);
		$(this).parent().parent().parent().parent().children('li:last').fadeIn();
	} else {
	$(this).parent().parent().parent().fadeOut(500);
	$(this).parent().parent().parent().prev().fadeIn(500);
	}
})
$('.nextproject').click(function() {
	if ($(this).parent().parent().parent().next().length === 0) {
		$(this).parent().parent().parent().fadeOut(500);
		$(this).parent().parent().parent().parent().children('li:first').fadeIn();
	} else {
	$(this).parent().parent().parent().fadeOut(500);
	$(this).parent().parent().parent().next().fadeIn(500);
	}
})

$('.nextproject').hover(function() {
    $(this).stop(true,true).animate({backgroundColor: '#929292', color: '#fff'});
},
	function() {
    $(this).stop(true,true).animate({backgroundColor: '#fff', color: '#666'});
});
$('.prevproject').hover(function() {
    $(this).stop(true,true).animate({backgroundColor: '#929292', color: '#fff'});
},
	function() {
    $(this).stop(true,true).animate({backgroundColor: '#fff', color: '#666'})
});


});
