
MochiKit.Signal.connect(window, 'onload', 
	function(){
		MochiKit.Signal.connect(window.location, 'changeLocation', showSection);
		
		showSection();
		

		if(document.getElementById('main_content') && document.getElementById('steps')){
			var linkList = document.getElementById('steps').getElementsByTagName('li');

			
			/*\
			 * The containing Fieldset of a form element (input, select, textarea) has the key as to which  
			 * link should be highlighted.
			 * When we find the parent fieldset of a form element, we inspect its' id to determine which 
			 * link to highlight
			\*/
			MochiKit.Base.map(
				function(formElement){
					MochiKit.Signal.connect(
						formElement, 
						'onfocus',
						function(e){
							
							var x = formElement;
							while(x && x.parentNode)
							{	
								if(x.tagName.toLowerCase() == "fieldset" && x.id)
								{
									highlightLinks(null, x.id);
									return;
								}
								x = x.parentNode;
							}
						}
					);
				},
				document.forms[0].elements
			);

			

			function highlightLinks(e, sectionId){
				if(e && e.src().id && !sectionId)
					sectionId = e.src().id;
					
				MochiKit.Base.map(
					function(li){
						var currentHash = li.getElementsByTagName('a')[0].hash.substring(1);

						if(sectionId == currentHash)
							li.className = 'selected';										
						else
							li.className = '';
						
					},
					linkList
				);
			}		
			

		
			MochiKit.Signal.connect(
				'main_content', 
				'onscroll', 
				function(e){
					
					var maincontent = document.getElementById('main_content');
					
					//MochiKit.Logging.log(findPosY(document.getElementById('primaryuser1')));
					//document.getElementById('footer').innerHTML = scrollingOffset.toString();// + ' '+ MochiKit.DOM.elementPosition( (i+1).toString() , 'contactform').y;
					
					MochiKit.Base.map(
						function(step){
							var stepSectionId = step.getElementsByTagName('a')[0].hash.substring(1);
							var difference = MochiKit.Style.getElementPosition(stepSectionId, maincontent).y;
							//if(stepSectionId == "3")
							//	document.getElementById('footer').innerHTML = difference;
							if(260 > difference &&  difference > -100)
							{
								//log(stepSectionId + "  " + maincontent.scrollTop + ":" + MochiKit.Style.getElementPosition(stepSectionId, maincontent).y);
								//for(var j=0; j < linkList.length; j++)
									//linkList[j].className = '';
							
								step.className = 'selected';										
							}
							else
								step.className = '';
							
						},
						linkList
					);
				}
			);
		}
	}
);


function showSection(e, sectionId){
		
	var sections = MochiKit.DOM.getElementsByTagAndClassName(null, "section");
	if(sections.length == 0)
		return;
		
	if(!sectionId && window.location.hash){
		//log("this " +this);
		sectionId = window.location.hash.substring(1);
	}
	else if(!sectionId)
	{
		// Just show the first section
		sectionId = sections[0].id;
	}
	
	log("showing section " + sectionId);

	// Show the selected section
	MochiKit.Base.map(
		function(section){
			if(section.id == sectionId)
				MochiKit.Style.showElement(section);
			else
				MochiKit.Style.hideElement(section)
		},
		sections
	);
	// Highlight the appropriate link
	MochiKit.Base.map(
		function(sectionLink){
			if(sectionLink.href.substring(sectionLink.href.indexOf('#')+1) == sectionId)
				sectionLink.parentNode.className = 'selected';
			else
				sectionLink.parentNode.className = '';
		},
		MochiKit.DOM.getElementsByTagAndClassName("a", "sectionLink")
	);
	log('finished showing section');
	
	
	//if(e)
		//e.stop();				
};