// JavaScript Document
//http://www.alistapart.com/articles/makingcompactformsmoreaccessible

// firebug degradiation
if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i <names.length; ++i) window.console[names[i]] = function() {};
}

$(document).ready(function(event){
	$("ul li:last-child").addClass("last-child");
	$("ul li:first-child").addClass("first-child");
	
	var loc = new String(location.href);
	var hash = location.hash;
	//$(".thumbs div:odd").each(function (){ $(this).addClass("even"); });
	//$("#content-main table tr:odd").addClass("odd");
	$("#nav-sub .last-child").each( function() { 
		if($(this).prev().hasClass("first-child")) {
			$(this).prev().addClass("last-child").removeClass("first-child");	
		}
	});

	$(".inputWrapper input").each(function() {
		var searchInput = "Search";
		$(this).focus( function() {
			$(this).val( (($(this).val() == searchInput) ? "" :$(this).val()) );
		})
		.blur( function() { 
			$(this).val( (($(this).val() == searchInput) ? $(this).val() :searchInput) );
		});
	});
	
	$(".ltf_text p").each( function() { 
		if($(this).text().indexOf("You are here") > -1) {
			$(this).addClass("breadcrumb");
			var bcHtml = $(this).html();
			var aSearch = "</a> - ";
			var homeIndex = bcHtml.toLowerCase().indexOf(aSearch);
			var newBc = bcHtml.substring(homeIndex + aSearch.length);
			//alert("homeIndex number " + homeIndex + " \n" + newBc);
			newBc = newBc.replace(/ - /,"<span>|</span>");
			$(".backlink").append("<span>|</span>"+newBc);
			return false;
		}
	});
	
	
	$(".ltf_text b").each( function() {
		if($(this).parent().css("background-color") == "black") {
			var $that = $(this);
			$that.parents("table").each( function() {
				$(this).addClass("listTable");
				$(this).parent().css("padding-right", "10px");
				return false;
			});
		}
	});
	//$(".answer").hide();
	$(".question span").hide();

	$(".question").each( function(){ 
		var ans = $(this).attr("id").substring(1);
		var questionId = $(this).attr("id");
		var questCurr = $(this).prev().attr("name").substring(1);
		//console.debug("realQuestion id %s question id %s  answer id %s ", questCurr, questionId, ans);
		$(this).click( function() {
			console.debug("this id %s prevId %s", $(this).attr("id"), $(this).prev().attr("name"));
			//var questCurr = 
			$(this).css("font-weight", "bold");
			$("span", this).toggle("fast");
			$("#"+ans+" span").toggle("fast").parent()
			.slideToggle("normal", function($that){  /*console.debug("%s", $(this).attr("id")); */  });
			
		});
	});
	
	
	if(hash) {
		var hashNum = parseInt(hash, 10);
		$("#"+hashNum).slideToggle("normal", function(){ $(this).css("visibility", "visible");  });
	}

	if(loc.indexOf("d_forum_search") > -1) {
		$("#search").slideDown("slow");	
	}

	$(".lhn .tout_sm_content > div").each(function() {
		if($.trim($(this).text()) == ""){
			$(this).remove();	
		}
	});

	$("a.anchorLink").anchorAnimate();

	if($.browser.mozilla) {
		$(".lhn li ul").css("padding-top", "14px"); 
		$("#nav-main #Pmc_Header1_nav_top a").css("padding-bottom", "12px");
	 }

});

function showForm(formId) {
	if($("#"+formId+":visible").length < 1) {
		$(".form-inputs").slideUp("normal");
		$("#"+formId).slideDown("slow");//, function(){ $(this).css("visibility", "visible"); });
	}
	else {
		$(".form-inputs").slideUp("normal");	
	}
}
(function($) {
	$.fn.anchorAnimate = function(settings) {

		settings = jQuery.extend({
			speed : 1100
		}, settings);	
		
		return this.each(function(){
			var caller = this
			$(caller).click(function (event) {	
				event.preventDefault()
				var locationHref = window.location.href
				var elementClick = ($(caller).attr("href") != "#") ? $(caller).attr("href") : "q"+$(caller).attr("id");
				
				var destination = $(elementClick).offset().top -200;
				//console.debug("%d %d", destination, destination-200);
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
					//window.location.hash = elementClick
				});
				return false;
			})
		});
    };
})(jQuery);		  