//////////////////////////////////////////////////////
//
//	The purpose of this file is to
//	apply a hover class on mouseover
//	on the <li> elements in the nav.
//	This is because IE doesn't put
//	the pseudo class :hover on <li> elements
//
//	This is code that uses the
//	jquery javascript library (http://jquery.com/)
//
//////////////////////////////////////////////////////
$(window).load(function() {


$('.three li a, .four li a').each(function(){
		var img = $(this).children('img')[0];
		var w =  img.width;
		var h =  img.height;
		var screen = $(this).append('<div class="screen" style="width: ' + w + 'px;height: ' + h +  'px;"></div>');
		//fade screen out for IE
		$(this).children('.screen').fadeTo(0,0.55);
		$(this).hover(function(){
			$(this).children('.screen').fadeTo('slow', 0);
		}, function(){
			$(this).children('.screen').fadeTo('slow',0.55);
			
		});
	});
	
	if($('#imageHolder p').length > 0)
	{
		$('#imageHolder p').fadeOut(0);
		$('#imageHolder p').load($('#imageNav li a')[0].href +  ' #imageHolder img', function(){
			$(this).fadeIn('slow');
		});
	}
	
	var href = "";
	$('#imageNav li a').each(function(){
		var img = $(this).children('img')[0];
		var w =  img.width;
		var h =  img.height;
		var screen = $(this).append('<div class="screen" style="width: ' + w + 'px;height: ' + h +  'px;"></div>');
		$(this).click(function(){
			return false;
		});
		//fade screen out for IE
		$(this).children('.screen').fadeTo(0,0.5);
		$(this).hover(function(){
			href = this.href;
			//$('#testOut').append('<p>' + href + '</p>');
			$(this).children('.screen').dequeue();
			$(this).children('.screen').fadeTo('slow',0);
			//$('#testOut').append('<p>hover</p>');
			$('#imageHolder p').fadeOut('slow', function(){
				$(this).load(href + " #imageHolder img", function(){
						$(this).fadeIn('slow');
				});
				
			});
			
		}, function(){
			$(this).children('.screen').dequeue();
			$(this).children('.screen').fadeTo('slow', 0.5);
		});
	});



	if (document.all&&document.getElementById) {
		// this is needed for the IE 6 pseudo hover class bug
		$(".navtop > li").hover(function() {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		});
		
		$(".navtop > li li ").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
			
		});
		
		
	} else {
		// this is need for safari when nav covers flash
		$(".navtop li ul").mouseover(function () {
			$(this).addClass("redraw");
		});
		$(".navtop li ul").mouseout(function () {
			$(this).removeClass("redraw");
		});
	}
	

});

//accordion menu
$(document).ready(function() {
		//modified accordion menu: allows for style display: none on sub uls on page load
		$('#headernav .navtop').each(function(){
			$this = $(this);
			if ($.browser.msie) {
				$this.find('a').css('zoom', '1');
			}
			$subUls = $this.find('ul');
			$topLis = $this.children('li');
			var maxHeight = 0;
			for(i = 0; i < $subUls.length; i++)
			{
				if($($subUls[i]).outerHeight() > maxHeight )
					maxHeight = $($subUls[i]).outerHeight();
			}
			maxHeight = maxHeight;
			$subUls.each(function(){
				$(this).height(maxHeight);
				$(this).hide();
			});
			$(this).children('li.selected').children('ul').show();
			$topAs = $topLis.children('a');
			$topAs.click(function(){
				if($this.find('li.selected').length == 0)
				{
					$(this).parent().addClass('selected');
					$(this).next().animate({height : "show"},300);
				}
				if(!$(this).parent().hasClass('selected')) {
					//some code taken and modified from jquery ui accordion menu
					var toClose = $this.children('li.selected').removeClass('selected').children('ul');
					var toOpen =  $(this).parent().addClass('selected').children('ul');
					var fxAttrs = [ "height", "paddingTop", "paddingBottom" ],
					showProps = {},
					percentDone;
					$.each(fxAttrs, function(i, prop) {
						var parts = ('' + $.css(toOpen[0], prop)).match(/^([\d+-.]+)(.*)$/);
						showProps[prop] = {
							value: parts[1],
							unit: parts[2] || 'px'
						};
					});
					toOpen.width( parseInt(toOpen.parent().width(),10) - parseInt(toOpen.css("paddingLeft"),10) - parseInt(toOpen.css("paddingRight"),10) - (parseInt(toOpen.css("borderLeftWidth"),10) || 0) - (parseInt(toOpen.css("borderRightWidth"),10) || 0) );
					toOpen.css({ height: 0, overflow: 'hidden' }).show();
					toClose.animate({height: "hide"},{
						duration: 'fast', 
						complete : function(){
							toClose.css({ overflow: 'hidden', display: 'none'});
						},
						easing : 'swing',
						step : function(now, settings) {
							if (settings.prop == 'height') {
								percentDone = (settings.now - settings.start) / (settings.end - settings.start);
								toOpen[0].style[settings.prop] = Math.ceil((percentDone * maxHeight)) + showProps[settings.prop].unit;
							}
							else
							{
								toOpen[0].style[settings.prop] =
									(percentDone * showProps[settings.prop].value) + showProps[settings.prop].unit;
							}
						}});
					
				}
				return false;
			});
		//$('#debug').append(maxHeight);
		});
	});

