// $Id$

$(function(){
	vBody = $("body");
	
	vBody.addClass("scripted"); // activate scripted styles etc.
	
	if($.browser.msie && ($.browser.version < 7))
		IEHacks();	
	
	
	if(vBody.hasClass("home"))
	{
		$("#tourList > li").click(function(pEvent){
			window.location = $(this).find("a").attr("href");
		});
	}	
	if(vBody.hasClass("expanded"))
	{
		vTabList = $("<ul id='tabs'></ul>");
		$("#primary h2[id]").each(function(i){
			
			vId = this.id;
			vLI = $("<li></li>").append($("<a></a>").attr("href", "#"+vId).text($(this).text()).click(tabClick));
			
			vHeadingReached = false;
			
			vContent = $(this).nextAll().filter(function(i){ // extract to next heading
				if(!vHeadingReached && $(this).is("h2"))
					vHeadingReached = true;
				return !vHeadingReached;
			});
			$(this).remove();
			vDiv = vContent.wrapAll($("<div></div>").attr("id", vId)).parent().addClass("tab");
			
			if(!i)
				vLI.children().add(vDiv).addClass("active");
						
			vTabList.append(vLI);			
		});
		
		$("#primary").prepend(vTabList);
	}
	focusForm("#basicSearch");
	focusForm("form.dataEntry");
});

function tabClick()
{
	vId = $(this).attr("href").substring(1);

	$("#tabs a, #primary div.tab").removeClass("active");
	$("#" + vId).add(this).addClass("active");
	return false;
}

function focusForm(pQuery)
{
	vForm = $(pQuery);
	if(vForm.length)
		vForm.find("input:first").focus();	
}

function IEHacks() // upgrade your browser
{
	$("#tourList > li:first-child, #tourList ul li:first-child").addClass("firstChild");
	$("#tourList > li").hover(IEHover, IEHover);
}

function IEHover()
{
	$(this).toggleClass("hover");
}
