 var $j = jQuery.noConflict();//to avoid conflicts with other libraries 

function initMenus() {
	$j('ul.menu ul').hide();
	$j.each($j('ul.menu'), function(){
		var cookie = $j.cookie(this.id);
//		if(cookie != null && String(cookie).length < 1) {
//			$j('#' + this.id + '.expandfirst ul:first').show();
//		}
//		else {			
//			$j('#' + this.id + ' .' + cookie).next().show();
//		}
	});

	$j('a[@menuid]').click(function(){
		var thisMenuID = $j(this).attr('menuID');
		
		$j('a[@menuid]').each(function(){
			var $thisMenu = $j( '#' + $j(this).attr('menuid') );
			var $this = $j(this);			
			if ( $j(this).attr('menuid') == thisMenuID ) {//show this
				if ( !$thisMenu.is(':visible') ) {
					$this.addClass('hover');
					$thisMenu.slideDown('normal');					
				}
			} else {//hide all others
				if ( $thisMenu.is(':visible') ) {
					$thisMenu.slideUp('normal', function(){
						$this.removeClass('hover');
					});
				}
			}
		});
	})
	
	$j('#content ul.menu li a').click(
		function() {

			var checkElement = $j(this).next();
			var parent = this.parentNode.parentNode.id;

			if($j('#' + parent).hasClass('noaccordion')) {
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					if($j(this).next().is(':visible')) {
						//$j.cookie(parent, null);
					}
					else {
						//$j.cookie(parent, this.className);
					}
					$j(this).next().slideToggle('normal');
					//$j('#fullDesc').hide('slow');
					
				}				
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($j('#' + parent).hasClass('collapsible')) {
					//$j('#fullDesc').hide();
					$j('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$j('#' + parent + ' ul:visible').slideUp('normal');
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					//$j.cookie(parent, this.className);
				}
				//$j('#fullDesc').hide();
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
	
	$j('#fullDesc').hide();
	$j('#fullDesc2').hide();
	$j('#fullDesc3').hide();
	$j('#fullDesc4').hide();
	
	$j('a#readMore').click(function() {
    $j('#fullDesc').toggle('slow');
    return false;
  });
  $j('a#readMore2').click(function() {
    $j('#fullDesc2').toggle('slow');
    return false;
  });
  $j('a#readMore3').click(function() {
    $j('#fullDesc3').toggle('slow');
    return false;
  });
  $j('a#readMore4').click(function() {
    $j('#fullDesc4').toggle('slow');
    return false;
  });
}

function checkAnchors() {
	var target = location.hash;//anchor in URL
	var targetIndicator = 'scroll_';
	//"scroll" links don't actually exist on the page, but we do some custom scrolling to an element that we derive from the anchor
	if ( target.indexOf(targetIndicator) == 1 ) {
		targetEl = target.substring( targetIndicator.length + 1);//the +1 is for the '#'
		
		if ( $j('#'+targetEl+'_anchor').size() > 0 ) {
			$j.scrollTo('#'+targetEl+'_anchor', 750)
		}
	}
}

$j(document).ready(function() {
	initMenus();
	checkAnchors();
	$j('a[@href*=scroll_]').click(function(){
		setTimeout("checkAnchors();", 100)//give it just a moment to update the URL
	})
});