$(document).ready(function() {
	$(".topnav-" + currentPage['section']).addClass('thisSection');
	
	var sectionTopnav = $(".thisSection a")
	$('.thisSection a').addClass("hover");
	myBGC = $(sectionTopnav).css("backgroundColor");
	$(sectionTopnav).css("backgroundColor", myBGC);
	$(sectionTopnav).dequeue();
	$(sectionTopnav).animate({width: '100px', height: '48px', backgroundColor: '#df973d'}, 'fast', 'easeOutSine');
	
	
	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 needed for safari when nav covers flash
		$(".navtop li ul").hover(function () {
			$(this).addClass("redraw");
		},
		function () {
			$(this).removeClass("redraw");
		});
	}
	
	/*$('#topnav li a').ifixpng(); */
	$('#topnav li a').each(function(){
		this.myBGC = $(this).css("backgroundColor");
		$(this).css("backgroundColor", this.myBGC);
		if (!($(this).parent().hasClass("thisSection"))) {
			$(this).hover(function(){
				$(this).dequeue();
				$(this).animate({width: '100px', height: '48px', backgroundColor: '#df973d'}, 'fast', 'easeOutSine');
				
			}, function(){
				$(this).dequeue();
				$(this).animate({width: '50px', height: '33px', backgroundColor: this.myBGC}, 'fast', 'easeOutSine');	
			});
		}
	});
	
	var h="0123456789ABCDEF"
	var Fullrgb = "000000";
	
	function rgbmerge(r, g, b){ return (r << 16 | g << 8 | b); }
	
	function gradient(startcolor, endcolor, numCol)
	{
		if (numCol <= 0) numCol = 1;
		var color;
		
		var start_red = startcolor >> 16;
		var start_green = (startcolor & 0x00FF00) >> 8;
		var start_blue = startcolor & 0x0000FF;
		
		var end_red = endcolor >> 16;
		var end_green= (endcolor & 0x00FF00) >> 8;
		var end_blue = endcolor & 0x0000FF;
		
		var incRed = Math.floor((end_red - start_red) / numCol);
		var incGreen = Math.floor((end_green - start_green) / numCol);
		var incBlue = Math.floor((end_blue - start_blue) / numCol);
	
		var colorArray = new Array(numCol);
		
		for(var x=0; x < numCol; x++) {
		
			if(start_red + incRed >= 0x00 && start_red + incRed <= 0xFF)
						start_red += incRed;
			if(start_green + incGreen >= 0x00 && start_green + incGreen <= 0xFF)
						start_green += incGreen;
			if(start_blue + incBlue >= 0x00 && start_blue + incBlue <= 0xFF)
						start_blue += incBlue;
			
			color = (rgbmerge(start_red, start_green, start_blue)).toString(16);
			color = Fullrgb.substring(0, 5 - color.length + 1) + color;
			colorArray[x] = color;
			//alert(color);
		}
	
		return colorArray;
	}
	
	var gradColors = gradient(0xD4CBC1, 0x968570, $('#sidenav li').length - 2);
	
	var navColors = new Array('D4CBC1');
	navColors = navColors.concat(gradColors.concat(Array('968570')));
	var i = 0;
	
	
		/*$('#topnav li a').ifixpng();
	$('#topnav li a').each(function(){
		this.myBGC = $(this).css("backgroundColor");
		$(this).css("backgroundColor", this.myBGC);
		if (!($(this).parent().hasClass("thisSection"))) {
			$(this).hover(function(){
				$(this).dequeue();
				//$(this).children('.screen').fadeTo('slow', 100);
				$(this).animate({width: '100px', height: '48px', backgroundColor: '#df973d'}, 'fast', 'easeOutSine');
				
			}, function(){
				$(this).dequeue();
				$(this).animate({width: '50px', height: '33px', backgroundColor: this.myBGC}, 'fast', 'easeOutSine');
				
			});
		}
	});
	 */
	$('#sidenav li a').each(function(){
		var navabbr = $(this).attr('name');
	    var navname = $(this).html();
	    var navhref = $(this).attr('href');

		$(this).html('<img src="/image/'+ escape(navabbr.substring(0,5).toUpperCase()) +'/off/'+ escape(navColors[i]) +'.png">');
		$(this).css({backgroundColor: '#' + navColors[i]});

		var screen = $('<div class="screen" style="position: absolute; top: 0; width: ' + 50 + 'px;height: ' + 26 +  'px;"><img src="/image/'+ escape(navname.toUpperCase()) +'/on/transparent.png"></div>').appendTo(this);
		
		//fade screen out for IE
		$(this).children('.screen').fadeTo(0,0);
		if (($(this).parent().hasClass("currentSideNav"))) {
			//$(this).children('.screen').dequeue();
			$(this).children('.screen').animate({"width": "143px", "opacity": 1, "left": "-76px"}, "fast");
		} else {
			$(this).hover(function(){
				$(this).children('.screen').dequeue();
				//$(this).children('.screen').fadeTo('slow', 100);
				$(this).children('.screen').animate({'width': '143px', 'opacity': 1, 'left': "-76px"}, 300, 'easeOutSine');
			}, function(){
				$(this).children('.screen').dequeue();
				/*$(this).children('.screen').fadeTo('slow',0);*/
				$(this).children('.screen').animate({'width': '50px', 'opacity': 0, 'left': "0px"}, 300, 'easeOutSine');
			});
		}
		
		$(this).click(function(){top.location= navhref;});
		i++;
	});
/*
	$('#sidenav li a').each(function(){
		var bgImage = $(this).css('backgroundImage');
		var screen = $('<div class="screen" style="width: ' + 50 + 'px;height: ' + 26 +  'px;"></div>').appendTo(this);
		screenBgImage  = bgImage.replace(/.gif/, "_on.gif");
		screen.css('backgroundImage',screenBgImage);
		//fade screen out for IE
		$(this).children('.screen').fadeTo(0,0);
		$(this).hover(function(){
			$(this).children('.screen').dequeue();
			//$(this).children('.screen').fadeTo('slow', 100);
			$(this).children('.screen').animate({width: '143px', opacity: 1, left: "-75px"}, 'fast', 'easeOutSine');
			
		}, function(){
			$(this).children('.screen').dequeue();
			//$(this).children('.screen').fadeTo('slow',0);
			$(this).children('.screen').animate({width: '50px', opacity: 0, left: "0px"}, 'fast');
			
		});
	});
*/
	
	
	$('#headernav 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: ' + 40 + 'px;height: ' + 33 +  'px;"></div>');
		//fade screen out for IE
		$(this).children('.screen').fadeTo(0,0);
		$(this).hover(function(){
			$(this).children('.screen').dequeue();
			$(this).dequeue();
			//$(this).children('.screen').fadeTo('slow', 100);
			$(this).children('.screen').animate({width: '100px', height: '48px', opacity: 1}, 'fast', 'easeOutSine');
			$(this).animate({width: '100px'},'fast', 'easeOutSine');
		}, function(){
			$(this).children('.screen').dequeue();
			$(this).dequeue();
			/*$(this).children('.screen').fadeTo('slow',0);*/
			$(this).children('.screen').animate({width: '40px', height: '33px', opacity: 0}, 'fast', 'easeOutSine');
			$(this).animate({width: '40px'},'fast', 'easeOutSine');
			
		});
	});
});
