jQuery(function() {
	jQuery(window).scroll(function() {
		updateHeaderAndFooter();
	});
	
	
	var info = {
		header : jQuery(".header"),
		footer : jQuery(".footer"),
		mainMenuArea : jQuery(".main-menu-area"),
		subMenuArea : jQuery(".sub-menu-area"),
		logoArea : jQuery(".logo-area")
	};
	
	info.headerHeight = info.header.outerHeight();
	info.footerHeight = info.footer.outerHeight();
	info.mainMenuAreaHeight = info.mainMenuArea.outerHeight();
	info.mainMenuAreaMinHeight = 33;
	info.subMenuAreaHeight = info.subMenuArea.outerHeight();
	info.subMenuAreaMinHeight = 35;
	info.headerMinHeight = info.mainMenuAreaMinHeight+info.subMenuAreaMinHeight;
	
	function updateHeaderAndFooter() {
		var scrollY = window.scrollY;
		
		var headerHeight = Math.max(info.headerMinHeight,info.headerHeight-scrollY);
		
		//@todo animate to this height
		info.header.css("height",headerHeight);
		
		//var subMenuHeight = Math.max(info.subMenuAreaMinHeight,info.subMenuAreaHeight-scrollY);
		//
		//scrollY = Math.max(0,scrollY-(info.subMenuAreaHeight-info.subMenuAreaMinHeight));
		//console.log(scrollY+" "+subMenuHeight);
		
		var minTop = info.mainMenuAreaMinHeight-info.mainMenuAreaHeight;
		var newTop = Math.max(minTop,-scrollY);
		
		//@todo animate to this top
		info.mainMenuArea.css("top",newTop);
		
	}
	
	if( document.referrer == "" ) {
		jQuery(".logo").css({"top":-200}).animate( {"top": 0}, 900, "easeOutSine");
	
		var i=0;
		jQuery(".menu-item").each(function() {
			jQuery(this).find("a").css({"position":"relative","top":-100}).delay(100*i+800).animate( {"top": 0}, 600, "easeOutBack" );
			jQuery(this).css({"opacity":0}).delay(100*i+800).animate( {"opacity":1}, 600, "easeOutSine" );
			i++;
		})
	}
	if( jQuery(".labs-sign").length > 0 ) {
		var signColorDiv, signHue;
		signColorDiv = jQuery(".labs-sign .color");
		signHue = 0;
		window.setInterval(function() {
			var rgb = hsvToRgb(signHue,100,75);
			
			signHue = (signHue+1)%360;
			signColorDiv.css("background-color","rgb("+rgb[0]+","+rgb[1]+","+rgb[2]+")");
		}, 100);
	}
	
	if( jQuery(".docs").length > 0 ) {
		var content = jQuery(".docs").contents();
		var docs = jQuery("<div>");
		var docsMenu = jQuery("<div>");
		var anchor = jQuery("<a>");
		docsMenu.addClass("docs-menu");
		docs.append(content);
		jQuery(".docs").css({"position":"relative","left":0,"top":0});
		jQuery(".docs").append(docsMenu);
		jQuery(".docs").append(docs);
		docsMenu.css({"position":"absolute","left":10,"top":10,"width":160});
		docs.css({"padding-left":170});
		docs.find("h2").each(function() {
			var text = $(this).text();
			$(this).attr("id","doc:"+text);
			var content = $(this).contents();
			var link = $("<a>");
			link.attr("href","#doc:"+text);
			link.append(content);
			$(this).append(link);
			docsMenu.append($(this).html());
			
			//console.dir($(this).offset().top);
			//window.scrollTo(0,360);
		});
		
		jQuery(window).scroll(function() {
			updateDocMenu();
		});
		
		function updateDocMenu() {
			var offset = Math.max(0,window.scrollY-316);
			docsMenu.css({"top":offset+10});
		}
	}
});


/**
 * HSV to RGB color conversion
 *
 * H runs from 0 to 360 degrees
 * S and V run from 0 to 100
 * 
 * Ported from the excellent java algorithm by Eugene Vishnevsky at:
 * http://www.cs.rit.edu/~ncs/color/t_convert.html
 */
function hsvToRgb(h, s, v) {
	var r, g, b;
	var i;
	var f, p, q, t;
	
	// Make sure our arguments stay in-range
	h = Math.max(0, Math.min(360, h));
	s = Math.max(0, Math.min(100, s));
	v = Math.max(0, Math.min(100, v));
	
	// We accept saturation and value arguments from 0 to 100 because that's
	// how Photoshop represents those values. Internally, however, the
	// saturation and value are calculated from a range of 0 to 1. We make
	// That conversion here.
	s /= 100;
	v /= 100;
	
	if(s == 0) {
		// Achromatic (grey)
		r = g = b = v;
		return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
	}
	
	h /= 60; // sector 0 to 5
	i = Math.floor(h);
	f = h - i; // factorial part of h
	p = v * (1 - s);
	q = v * (1 - s * f);
	t = v * (1 - s * (1 - f));

	switch(i) {
		case 0:
			r = v;
			g = t;
			b = p;
			break;
			
		case 1:
			r = q;
			g = v;
			b = p;
			break;
			
		case 2:
			r = p;
			g = v;
			b = t;
			break;
			
		case 3:
			r = p;
			g = q;
			b = v;
			break;
			
		case 4:
			r = t;
			g = p;
			b = v;
			break;
			
		default: // case 5:
			r = v;
			g = p;
			b = q;
	}
	
	return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
