/*
 *	Site Namespace
 */
var Tuck = {};

Tuck.enableMainNav = function() {
	//path to images
	var imagePath = "images/";
	
	//tag main nav with a class if Javascript is enabled
	$("#navcolumn").removeClass("nojs");
	
	//hide all nav sub items if Javascript is enabled
	$("#mainnav > li.navhead > ul").hide();
	
	//strip off all header links if Javascript is enabled
	$("#mainnav > li.navhead > a").attr("href", "#");
	
	//show the selected sub item
	$("#mainnav > li.selected > ul").show();
	
	//swap in the selected sub item head
	var selid = $("#mainnav > li.selected").attr("id");
	var selimage = imagePath + "mn_" + selid + "_sel.gif";
	$("#mainnav > li.selected").children("a").children("img").attr("src", selimage);
	
	//Rollovers
	$("#mainnav > li.navhead").hover(function() {		
		//get image names/paths for head
		var rollImage;
		rollImage = imagePath + "mn_" + $(this).attr("id") + "_roll.gif";	
		//if it's not selected or expanded
		if ((!$(this).hasClass("selected")) && (!$(this).hasClass("expanded"))) {	
			//switch the header image
			$(this).children("a").children("img").attr("src", rollImage);	
		}	
	},
	function() {
		//get image names/paths for head
		var defaultImage;
		defaultImage = imagePath + "mn_" + $(this).attr("id") + ".gif";	
		//if it's not selected or expanded
		if ((!$(this).hasClass("selected")) && (!$(this).hasClass("expanded"))) {
			//switch the header image back
			$(this).children("a").children("img").attr("src", defaultImage);
		}	
	});
	
	//map toggle function to LI click
	$("#mainnav > li.navhead").click(function() {

		var rollImage;
		rollImage = imagePath + "mn_" + $(this).attr("id") + "_roll.gif";
		var expandedImage;
		expandedImage = imagePath + "mn_" + $(this).attr("id") + "_exp.gif";
		var defaultImage;
		defaultImage = imagePath + "mn_" + $(this).attr("id") + ".gif";

		//if it's not selected
		if (! $(this).hasClass("selected")) {
			
			var ul = $(this).children("ul");
			
			//if it's collapsed, expand it:
			if (! $(this).hasClass("expanded")) {

				//first, collapse any menus already open
				$("#mainnav > li.navhead").each(function(i){
					var expul;
					var exdefaultImage;
					exdefaultImage = imagePath + "mn_" + $(this).attr("id") + ".gif";
					
					if (! $(this).hasClass("selected")) {
						if ($(this).hasClass("expanded")) {
							expul = $(this).children("ul");
							if(expul.is(":animated")) {
								expul.stop()
									.css("height", "auto")
									.slideUp("fast");
							} else {
								expul.slideUp("fast");
							};
							$(this).children("a").children("img").attr("src", exdefaultImage);
							$(this).removeClass("expanded");
						};
					};	
				});
				
				//show the submenu
				if(ul.is(":animated")) {
					ul.stop()
						.css("height", "auto")
						.slideDown("normal");
				} else {
					ul.css("display", "none");
					ul.slideDown("normal");
				};
				//switch the header image
				$(this).children("a").children("img").attr("src", expandedImage);
				$(this).addClass("expanded");
			} else { //if it's expanded, collapse it
				//hide the submenu
				if(ul.is(":animated")) {
					ul.stop()
						.css("height", "auto")
						.slideUp("fast");
				} else {
					ul.slideUp("fast");
				};
				//switch the header image back
				$(this).children("a").children("img").attr("src", defaultImage);
				$(this).removeClass("expanded");	
			};
		};	
	});
	
};



/* Rollover script */
Tuck.enableRollovers = function() {
	$("img.rollover").hover(function() {
		$(this).attr("src", $(this).attr("src").split(".").join("_roll."));
	},
	function() {
		$(this).attr("src", $(this).attr("src").split("_roll.").join("."));
	});	
};

/* Homepage Feature Paging script */
Tuck.enableHomePaging = function() {
	// Since javascript is enabled, show the paging controls
	$(".homepage #sidebar .controls").show();

	// Hide all the sections, then show the first one
	$(".homepage #features div.section").hide();
	$(".homepage #features div:first-child").show();
    
	// Get the number of sections and start the count at zero
	var total = $(".homepage #features div.section").size();
    var count = 1;

    // Set the initial values of the counter display
	$("#totalpages").text(total);
	$("#curpage").text(count);

	// Counting up
	$(".homepage #sidebar .controls #nextpage").click( function() { 	
		$(".homepage #features div:nth-child("+count+")").hide();
    	count++;
    	if (count > total) {
     		count = 1;
      	}
     	$(".homepage #features div:nth-child("+count+")").show();
		$("#curpage").text(count);
    });

	// Counting down
	$(".homepage #sidebar .controls #prevpage").click( function() { 	
		$(".homepage #features div:nth-child("+count+")").hide();
    	count--;
    	if (count < 1) {
     		count = total;
      	}
     	$(".homepage #features div:nth-child("+count+")").show();
		$("#curpage").text(count);
    });
	
};

function largePop(theURL, winName) { 
	window.open(theURL,winName,'width=600,height=550,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes'); 
}
function smallPop(theURL, winName) { 
	window.open(theURL,winName,'width=300,height=350,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes'); 
}

Tuck.stripeTable = function() {
	$("table.stripe tr").removeClass("alt");
	$("table.stripe tr:even").addClass("alt");
}

$(document).ready(function() {
	Tuck.enableMainNav();
	Tuck.enableRollovers();
	Tuck.stripeTable();
});
