/*
--------------------------------------------------

Trails End
application.js

David Munger [david.munger@acquitygroup.com]
6/16/2009

Copyright © 2009 Acquity Group LLC

--------------------------------------------------
*/

/* Load all required functions on DOM load */
$(document).ready(function(){

	$(".leaderTypeToggle").click(function() {
		handleLeaderTypeToggle();
	});
	
	// display correct fields on page load...
	handleLeaderTypeToggle();
	
	matchHeights("#contentPrimary", "#contentSecondary");
	matchHeights("#contentPrimary .column");
	
	// Modal for Amazon gift card redemption
	$("#giftCardModal").jqm({
		modal: true,
		trigger: "a.redeemCard"
	});
	
	$("#giftCardModal.immediate").jqmShow();

	$("#giftCardModal a.close").click(function () {
		$("#giftCardModal").jqmHide();
		return false;
	});

	// Modal for Avatar builder
	$("#scoutAvatarModal").jqm({
		modal: true,
		trigger: ".openAvatarBuilder"
	});

	$("#scoutAvatarModal.immediate").jqmShow();

	$("#scoutAvatarModal a.close").click(function() {
		jwqClose();
		return false
	});
	
	// Council name input auto-complete (type-ahead)
	$("#createNewAccount-councilName, #staffSalesReport-councilName").autocomplete("../services/council_name_suggest.jsp",
		{
			maxItemsToShow: 20,
			selectFirst: true
		}
	);
	
	// show parent/guardian section for under-13 signups
	// not entirely accurate -- calculation ignores leap years
	$("#newAccount-createAccount").click(function(){
		if ($("div#under13").css("display") == "block") {
			return true;
		}
		return handleParentVerificationDiv();
	});

	//you might need to show the div on page load (if the forms already been submitted)
	handleParentVerificationDiv();
	
	// generate years for year ddls
	$("select.year").each(function(){
		var now = new Date();
		var year = now.getYear();	
		year < 1000 ? year = year + 1900 : year = year;
		for (i = 0; i < 100; i++)
		{
			var option = document.createElement("option");
			$(option).val(year - i).text(year - i).appendTo($("select.year"));
		};
	});

	// apply table striping
	$("table.stripe tbody tr:even").addClass("even");
	
	// Tooltips
	// act on each, otherwise cluetip() will only be called on the first element in the array
	$("a.tooltip").each(function(){
		$(this).cluetip({
			attribute: "rel",
			cluetipClass: "weaver",
			cluezIndex: "5000",
			dropShadowSteps: "3",
			fx: { open: "fadeIn" },
			local: true,
			waitImage: false
		});
		
		if ($(this).children("img")) {
			// remove alt attribute on hover so it doens"t show in IE
			var imgalt = $(this).children("img").attr("alt")
			$(this).hover(function(){
				$(this).children("img").attr("alt", "")
			}, function(){
				$(this).children("img").attr("alt", imgalt)
			})
		}
	});
	
	$("a.tooltip").click(function() {
		return false;
	});
	
	// Email Template chooser
	$("fieldset.chooseTemplate select").change(function() {

		var templateFieldset = $(this).parents("fieldset.chooseTemplate").eq(0);

		// first turn off the previously-selected one
		templateFieldset.find(".templateCopy").removeClass("selected");

		// now turn on the one just selected by the user
		templateFieldset.find("#" + $(this).val()).addClass("selected");

	});
	
	// Select All checkbox
	$("fieldset.selectAll input:checkbox").click(function() {

		var groupFieldset = $(this).parents("fieldset.selectAll").eq(0).parents("fieldset");
		var targetValue;

		if ($(this).is(":checked")) {
			targetValue = true;
		} else {
			targetValue = false;
		}

		groupFieldset.find(".checkboxGroup input:checkbox").each(function() {
			this.checked = targetValue;
		});

	});

	// show #updateUnit on scouts profile page
	$("a#myProfile-updateUnit").click(function(){
		if ($("#updateUnit").css("display") == "block") return false;

		slideFadeIn($("#updateUnit"));
		
		var currentPrimaryHeight = $("#contentPrimary").height();
		var currentSecondaryHeight = $("#contentSecondary").height();
		
		$("#contentPrimary").height(currentPrimaryHeight + 150);
		$("#contentSecondary").height(currentSecondaryHeight + 150);
		
		return false;
	});
	
});

function matchHeights() {
	
	var targetHeight = 0;
	var thisHeight = 0;
	var thisOffset = 0;
	var paddingTopString = "";
	var paddingBottomString = "";
	
	// iterate through all selectors to find the tallest one
	for (var index = 0; index < matchHeights.arguments.length; index++) {
		
		$(matchHeights.arguments[index]).each(function (i) {
			
			thisHeight = 0;
			paddingTopString = "";
			paddingBottomString = "";
			
			thisHeight = $(this).height();
			
			// get css padding, strip off "px"
			paddingTopString = $(this).css("padding-top");
			if (paddingTopString) {
				thisHeight = thisHeight + Number(paddingTopString.substr(0, paddingTopString.length - 2));
			}
			
			// get css padding, strip off "px"
			paddingBottomString = $(this).css("padding-bottom");
			if (paddingBottomString) {
				thisHeight = thisHeight + Number(paddingBottomString.substr(0, paddingBottomString.length - 2));
			}
			
			if (thisHeight > targetHeight) {
				targetHeight = thisHeight;
			}
		});

	}

	// iterate through all selectors and set to tallest height
	for (var index = 0; index < matchHeights.arguments.length; index++) {
		
		$(matchHeights.arguments[index]).each(function (i) {
			
			thisOffset = 0;
			paddingTopString = "";
			paddingBottomString = "";
			
			// get css padding, strip off "px"
			paddingTopString = $(this).css("padding-top");
			if (paddingTopString) {
				thisOffset = Number(paddingTopString.substr(0, paddingTopString.length - 2));
			}
			
			// get css padding, strip off "px"
			paddingBottomString = $(this).css("padding-bottom");
			if (paddingBottomString) {
				thisOffset = thisOffset + Number(paddingBottomString.substr(0, paddingBottomString.length - 2));
			}
			
			$(this).height(targetHeight - thisOffset);
		});	
	}
	
}

// "Son of Suckerfish" solution for hover menus in IE6
sfHover = function() {
	var sfEls = document.getElementById("primaryNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// Function called through Flash to close the modal
jwqClose = function() {
	$("#scoutAvatarModal").jqmHide();
	window.location.reload();
}

function slideFadeIn(elem) {
	if (!elem) return;
	if (!jQuery.support.opacity) {
		jQuery(elem).css("display","block");
	}
	else {
		jQuery(elem).css("opacity", "0").slideDown(300, function(){jQuery(this).animate({opacity: 1}, 300)});
	}
}

function slideFadeOut(elem) {
	if (!elem) return;
	if (!jQuery.support.opacity) {
		jQuery(elem).css("display","none");
	}
	else {
		jQuery(elem).animate({opacity:0},300,function(){jQuery(this).slideUp(300)});
	}
}

function handleParentVerificationDiv() {
	var now = new Date();
	var year = now.getYear();
	year < 1000 ? year = year + 1900 : year = year;
	var cutoffDate = Date.UTC(year-13,now.getMonth(),now.getDate());
	if (cutoffDate < Date.UTC($("select.year").val(),$("select.month").val() - 1,$("select.day").val())) {
		slideFadeIn($("div#under13"));
		var currentHeight = $("#contentPrimary").height();
		$("#contentPrimary").height(currentHeight + 100);
		
		matchHeights("#contentPrimary", "#contentSecondary");
		matchHeights("#contentPrimary .column");
		
		return false;
	// } else if ($("select.year").val() == 0 || $("select.month").val() == 0 || $("select.day").val() == 0) {
	} else {
		$("div#under13").css("display", "none");
		
		matchHeights("#contentPrimary", "#contentSecondary");
		matchHeights("#contentPrimary .column");
		
		return true;
	}
}

function handleLeaderTypeToggle() {
	$(".leaderTypeToggle").each(function() {

		var radioValue = $("input[name='" + $(this).attr("name") + "']:checked").val();

		$(".createLeaderAccountButton").show();
		$("#councilNameFieldset").show();
		if (radioValue == "5") {
			$("#unitLeaderFieldset").hide();
		} else if (radioValue == "3") {
			$("#unitLeaderFieldset").show();
		}

	});
}