$(document).ready(function() {

function open(elem) {
	elem.removeClass('subClosed').addClass('subOpen');
	elem.siblings().slideDown();
}

function close(elem) {
	elem.removeClass('subOpen').addClass('subClosed');
	elem.siblings().slideUp();
}

/* init sidebar menu */
$('.sidebarNav li:has(ul)').each(function(i) {

	$(this).children().filter('a:first').addClass('subClosed').toggle		(
      function () {
        open($(this));
      },
      function () {
        close($(this));
      });


	if (i===navOpen || navOpen===true) {
			$(this).children().filter('a:first').trigger('click')// add trigger
	} else {
			$(this).children('ul').hide(); // hide child ul
	}

});

// top menu animations
$('#mainNav a:not(.highlight)').hover(
	function() {
  $(this).animate({ 
        top: "0px"
      }, 250 );
  },
	function() {
		$(this).animate({ 
        top: "8px"
      }, 100 );
	});

// scrolling stuff

var elements = ['box1','box2','box3','box4'];
for (i=0; i<elements.length;i++) {
	var id=elements[i]; // name of id in HTML
	var $id=$("#" + id); // jquery object
  if ($id.length > 0) { // check to see exists

	if ($id.find('li').size() > 1 ) { //make sure more than one list item
		$id.addClass('scroller');

		//build nav controls
		var navhtml = '<span class="' + id + 'prev"><img src="images/arrowPrev.gif" width="6" height="16" alt="Previous" /></span>&nbsp;&nbsp;<span class="' + id + 'next">Next&nbsp;&nbsp;<img src="images/arrowNext.gif" width="6" height="16" alt="Next" /></span>&nbsp;&nbsp;&nbsp;&nbsp;';
		var header=$id.prev();
		var span=header.find(".elementNav");
		if (span.length>0) {
	 		span.prepend(navhtml);
		} else {
			navhtml = '<span class="elementNav">' +navhtml + '</span>';
			header.append(navhtml);
		}
		$('#' + id).serialScroll({
			items: 'li',
			prev: '.' + id + 'prev',
			next: '.' + id + 'next',
			duration: 500,
			cycle:true,
			force: true,
			interval: 10000
		});
  }
}
}

// glossary builder
$('#mainContent a[href*="#"]').each(function(i) {
	$(this).addClass('glossaryTerm');
	var url=$(this).attr('href');
	var hashIndex=url.lastIndexOf('#');
	var hash=url.slice(hashIndex);
	var tipHTML='<div class="toolTip" id="tip' + i + '"></div>';
  $('body').append(tipHTML);

	var domObj=$('#tip' + i);
	domObj.load(url + ' ' + hash);

	domObj.hover(
		function(e) {
			$(this).show();
		},
		function(e) {
			$(this).hide();
		}
	);

	$(this).hover(
		function(e) {
			var x = e.pageX - 170; 
   		var y = e.pageY - 50;
			var tipCSS = {
				'top':y,
				'left':x,
				'display':'block'
			};
			domObj.css(tipCSS);
		},
		function(e) {
			domObj.hide();
		}
	);

});

//overlay
$("a[rel^='prettyPhoto']").prettyPhoto();

//Welcome box links in new window
$('.boxNote a[href^=http://]').attr('target','_blank');
});


