/**
 * jQuery.functions - Utilities for Lambda Legal
 * (c) 2010 Jeff Simpson
 * @author Jeff Simpson
 * @version 1.0
 *
 **/

//Sponsor Image rotator from dymanic XML call
function spImageXMLCall() {
   $.get('http://www.lambdalegal.org/assets/xml/sponsor-image-links.xml', function(xml) {		
     imgList = $(xml);
     setInterval('spUpdateImage(imgList)', 2500);
   });
}

function spUpdateImage(imgList) {
	var limit = $(imgList).find("imageItem").length;
	var nxtImgRef = Math.floor(Math.random() * limit);
	var nxtImgSrc = $(imgList).find('imageItem:eq(' + nxtImgRef + ')').text()
	$("#sponsorImg").attr('src',nxtImgSrc);
}


//Dynamic tab build for related content boxes

function buildBoxTabs() {
	$("div.jqTabCollection:has(.tabContent)").each(function(index) { 
		var oList = $("<ul id='bookmarksList'>");
		$(this).find("div.tabContent").each(function() {
			oList.append($("<li><a href='#" + $(this).attr("id") + "'> " + $(this).find("h2").text() + "</a></li>"));
		});	
		$(this).prepend(oList);
	});
}

//Error 404 Page redirects following information architecture redesign from XML call
function iaRedirectsXMLCall () {
	$("#searchMatch").css("display","block");
	$.get('http://www.lambdalegal.org/assets/xml/redirects-404.xml', iaRedirect);
}

function iaRedirect(xml) {		
	var source = document.location.href;
	var destination = false;
	$(xml).find('redirect').each(function() {
		if(source == $(this).find("oldURL").text()) {
			destination = true;
			newLocation = $(this).find("newURL").text();
			location.replace(newLocation);
			//$("#updatedURL").prepend("The URL for the resource has changed to <br />" + newLocation);
	 		//$("#searchMatch").css("display","none");
	 		//$("#updatedURL").css("display","block");
			return(destination);
		}
	});	
	if (destination == false) {
 		$("#searchMatch").css("display","none");
		$("#noMatch").css("display","block");
	};
}


//Devo Tax Revision Donation Calculator
//Currently uses javascript but not jQuery
//Should be modified to use the .change() jQuery event handler
$('.devoFormTarget').change(function() {
	if (isNaN(myform.salary.value) || isNaN(myform.pretax.value))
		myform.total.value = "";
	else
		myform.total.value = (((parseFloat(myform.salary.value) - parseFloat(myform.pretax.value))*.02)/12).toFixed(2);

});
