$().ready(function(){

/*
 *		Project:	Summit Medical
 *		Componant:	Menu Functionality
 *		Purpose:	Functionality to control the main websites dropdown menu + submenu's
 *		
 *		Created:	20/09/2009
 *		Author(s):	Ricky Stevens
 *
 *		Modified:	22/09/2009
*/

	// JavaScript is enabled, so use it to overwrite default setting and center menu
	$("#navivation .middle").css('left',Math.round(($("#navivation").width() - ($("#navivation .middle .center").width() + 66)) / 2 ) + 'px');

	// go through lists, add the class 'first' to the first item in the menu
	$("#navivation .middle .center .cell ul:first").each(function() { $("li:first", this).addClass('first'); });

	// go through, add the '+' symbol to any <li> that has more than one element (<a> is first element)
	$("#navivation .middle .center .cell ul li").each(function() {
		if($(this).attr('class') != 'last' && $(this).children().size() > 1) $('a:first', this).html('<span>+</span>' + $('a:first', this).html());
		$('ul > *', this).css('display', 'none');
	});

	// show hover effects for menu cells, and load its menu
    $("#navivation .middle .center .cell").hover(
      function () {

		$('ul > *', this).css('display', 'block');
		$('ul .last .block2', this).css('width',($("#navivation .middle .center").width() - 28) + 'px');
		$('ul', this).css('width',$("#navivation .middle .center").width() + 'px').stop().animate({height: "toggle"}, 5);
		$('ul', this).stop().animate({height:"0px"}, 5, "linear", function(){ $(this).css('display', 'none'); $(this).children('ul').css('display', 'none'); });
		$('ul li a span', this).html('+');

		// set hover image, set the bottom li's width, to allow for dynamic menu widths, set the width of the dropdown menu, stop any other animations, work out its height with # of children, then animate
        $(this).css('background', 'transparent url(images/nav-04.png) repeat-x');
		$('ul .last .block2', this).css('width',($("#navivation .middle .center").width() - 28) + 'px');
		$('ul:first', this).css('width',$("#navivation .middle .center").width() + 'px').stop().animate({height: ((($('ul > *', this).length) * 23) + 1 ) + "px", opacity: 1}, 350);
      }, 
      function () {

		// remove hover event, stop any other animations, then slide up dropdown menu
		$('ul', this).stop().animate({height:"0px", opacity: 0}, { queue: false, duration: 20 }, "linear", function(){  });
		$('ul li a span', this).html('+');
		$(this).css('background', 'transparent');
      }
    );

	// clicked on a navigation link...
	$("#navivation .middle .center .cell ul > li").click(function() {

		// see if this has a sub-menu
		if($(this).attr('class') != 'last' && $(this).children().size() > 1) {

			// there are sub menu's, check to see if its currently open or closed
			if($('a:first span', this).html() == '+') {

				// its closed, open it, and adjust the menu height to compensate
				$('ul > *', this).css('display', 'block');
				$('ul', this).css('width',$("#navivation .middle .center").width() + 'px').stop().animate({height: "toggle"}, 5);
				$('ul', this).stop().animate({height:"0px"}, 5, "linear", function(){ $(this).css('display', 'none'); $(this).children('ul').css('display', 'none'); });
				$(this).parent('ul').animate({height: ((((($('ul > *', this).length) - ($('ul li ul > *', this).length)) * 23)) + $(this).parent('ul').height()) + "px", opacity: 1}, { queue: false, duration: 350 });
				$('ul:first', this).css('width',$("#navivation .middle .center").width() + 'px').stop().animate({height: (((($('ul > *', this).length) - ($('ul li ul > *', this).length)) * 23) ) + "px", opacity: 1}, 350);
				$('a:first span', this).html('-');

			}else{

				// its open, close it, and adjust the menu height to compensate
				$(this).parent('ul').animate({height: ($(this).parent('ul').height() - (((($('ul > *', this).length) - ($('ul li ul > *', this).length)) * 23))) + "px", opacity: 1}, 350);
				$('ul', this).stop().animate({height:"0px", opacity: 0}, 200, "linear", function(){ $(this).css('display', 'none'); $(this).children('ul').css('display', 'none'); });
				$(this).children('ul').css('display', 'block');
				$('span', this).html('+');
			}

			return false;

		}else if($(this).attr('class') == 'last') {

			return false;

		}else{

			// unbind all events on the li's, otherwise it'll try and close the list, rather than follow the link.
			$("#navivation .middle .center .cell ul > li").unbind();

			// take user to the URL
			window.location = $('a', this).attr('href');
			return false;
		}

	});

});