//original code by James Padolsey
//http://net.tutsplus.com/tutorials/javascript-ajax/how-to-load-in-and-animate-content-with-jquery/

$(document).ready(function() {
	
	galleryHeight = $('#mainimage').height();
	
	//load image
	$('#nav_pic li a').click(function(){
	
		//console.log('thumb');
		
		window.location.hash = $(this).attr('href');
		loadTitle = $("img", this).attr('title');
		toLoad = $(this).attr('href');
		
		//console.log(loadTitle);
		//console.log(toLoad);
		
		$(this).css({
			'opacity':'.4',
			'text-decoration':'line-through'
		});
		$('#gallerypage').hide();
		$('#mainimage').hide().attr('src', toLoad).load(function(){
			$('#mainimage').fadeIn('normal');
			galleryHeight = $('#mainimage').height();
			$('#gallery').css('height', galleryHeight + 32 + "px");
			$('#gallery h2').css('height','32px').text(loadTitle);
		});
		return false;
	});
	
	
	//load page
	$('#nav_page li a').click(function(){
	
		//console.log('page');
		
		$('#gallery').css('height', 'auto');
		$('#mainimage').hide();
		
		window.location.hash = $(this).attr('href');
		var toLoad = $(this).attr('href')+'#gallerypage';
		$(this).css({'opacity':'.4','text-decoration':'line-through'});
		$('#gallery h2').css('height','0px').text("");
		$('#gallerypage').load(toLoad, function(){
			$('#gallerypage').animate({opacity: "1"},"normal").slideDown();
		});
		return false;
	});
	
	// jQuery SmoothScroll | Version 11-06-11
	// source
	// http://blog.medianotions.de/en/articles/2009/smoothscroll-for-jquery
   $('a[href*=#]').click(function() {

      // skip SmoothScroll on links inside sliders or scroll boxes also using anchors or if there is a javascript call
      if($(this).parent().hasClass('scrollable_navigation') || $(this).attr('href').indexOf('javascript')>-1) return;

      // duration in ms
      var duration=1000;

      // easing values: swing | linear
      var easing='swing';

      // get / set parameters
      var newHash=this.hash;
      var oldLocation=window.location.href.replace(window.location.hash, '');
      var newLocation=this;

      // make sure it's the same location 
      if(oldLocation+newHash==newLocation)
      {
         // get target
         var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;

         // adjust target for anchors near the bottom of the page
         if(target > $(document).height()-$(window).height()) target=$(document).height()-$(window).height();

         // set selector
         if($.browser.safari) var animationSelector='body:not(:animated)';
         else var animationSelector='html:not(:animated)';

         // animate to target and set the hash to the window.location after the animation
         $(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

            // add new hash to the browser location
            window.location.href=newLocation;
         });

         // cancel default click action
         return false;
      }
   });


});

