$(document).ready(function(){
 $("ul.sidenav li").hover(function(){
  $(this).find("div").stop()
  .animate({top: 20, opacity: 1}, "fast")
  .css("display","block")
 }, function(){
  $(this).find("div").stop()
  .animate({top: 10, opacity: 0}, "fast")
 });

 APHGallery.load ('slides');
});

$(function() {
 var $menu = $('#ldd_menu');
 /**
  * for each list element,
  * we show the submenu when hovering and
  * expand the span element (title) to 510px
  */
 $menu.children('li').each(function(){
  var $this = $(this);
  var $span = $this.children('span');
  $span.data('width',$span.width());
  
  $this.bind('mouseenter',function(){
   $menu.find('.ldd_submenu').stop(true,true).hide();
   $span.stop().animate({'width':'160px'},00,function(){
    $this.find('.ldd_submenu').slideDown(500);
   });
  }).bind('mouseleave',function(){
   $this.find('.ldd_submenu').stop(true,true).hide();
   $span.stop().animate({'width':$span.data('width')+'px'},300);
  });
 });
});

var APHSite =
{
	redirect: function (url)
	{
		window.location.href = url;
	}
}

var APHGallery =
{
	currentRow: false,
	locked: false,
	
	init: function ()
	{
		this.load ('gallery');
	},
	
	load: function (name)
	{
		$("a[rel='lightbox["+name+"]']").colorbox (
		{
			current: '{current}/{total}',
			maxWidth: '100%',
			maxHeight: '100%',
			//minWidth: 480,
			title: function ()
			{
				var href = $(this).next ().next ().find ('a').attr ('href');
				var link = href ? ' <a href="'+href+'"><img valign="middle" src="APH/ext/icons/splashy/download.png" alt="" /></a>' : '';
				
				return $(this).children ('img').attr ('alt')+link;
			}
		});
	},
	
	fadeRow: function (current, next)
	{
		var easing = 'swing', duration = 600;
		
		current.fadeOut (duration, easing, function ()
		{
			next.fadeIn (duration, easing);
			APHGallery.locked = false;
		});
	},
	
	nextRow: function ()
	{
		if (!APHGallery.currentRow)
		{
			APHGallery.currentRow = 1;
		}
		
		var currentRow = $('#images'+APHGallery.currentRow);
		var nextRow = currentRow.next ();
		
		if (nextRow.length && !APHGallery.locked)
		{
			APHGallery.locked = true;
			APHGallery.fadeRow (currentRow, nextRow);
			APHGallery.currentRow++;
			
			$('#prev img').show ();
			if (!nextRow.next ().attr ('id'))
			{
				$('#next img').hide ();
			}
		}
	},
	
	prevRow: function ()
	{
		if (!APHGallery.currentRow)
		{
			return false;
		}
		
		var currentRow = $('#images'+APHGallery.currentRow);
		var prevRow = currentRow.prev ();
		
		if (prevRow.length && !APHGallery.locked)
		{
			APHGallery.locked = true;
			APHGallery.fadeRow (currentRow, prevRow);
			APHGallery.currentRow--;
			
			$('#next img').show ();
			if (!prevRow.prev ().attr ('id'))
			{
				$('#prev img').hide ();
			}
		}
	}
};
