/**
 * wings.js
 * 
 * Requires jquery-1.2.6.js.
 *
 * Copyright (c) Wings Hockey Club. All rights reserved.
 * Author: SKresge
 */

//--------------------------------------------------------------------------------
// Email obfuscating function
//--------------------------------------------------------------------------------
var email_codes = {"411":"Rhh@n7upsqdmaq7duzs9.o1y","Website":"fspbwcs@pawrusfocsafw3ub.q51"};
/**
 * obfuscateEmail
 * @desc - Obfuscates an email address to deter bots from spamming
 * @param coded {string} The coded value of the email address
 * @returns {string} The uncoded email address (to be dynamically inserted in the page)
 */
obfuscateEmail = function (coded) {
 	var cipher = "aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA1234567890";
	var shift = coded.length;
	var address = "";
	for (i=0; i<coded.length; i++) {
		if (cipher.indexOf(coded.charAt(i))==-1) {
			ltr = coded.charAt(i);
			address += (ltr);
		} else {     
			ltr = (cipher.indexOf(coded.charAt(i))-shift+cipher.length) % cipher.length;
			address += (cipher.charAt(ltr));
		}				
	}
	return address;
}
//--------------------------------------------------------------------------------
// subnav common functions
//--------------------------------------------------------------------------------
/**
 * resetSubNav
 * @desc - Set all subnav li elements to the 'off' state
 */
resetSubNav = function () {
	$(".boxMenu li").each(function(){
		var mystr = encodeURI($(this).html());
		$(this).html(decodeURI(mystr.replace(/%20%C2%BB/, '')));
		$(this).removeClass("on");
	});
}
/**
 * setCurrentSubNav
 * @desc - Set the 'on' state for the clicked subnav li
 * @param obj {object} The li clicked
 */
setCurrentSubNav = function (obj) {
	$(obj).addClass("on");
	$(obj).html($(obj).html()+' &raquo;');
}
//--------------------------------------------------------------------------------
// calendar functions
//--------------------------------------------------------------------------------
/**
 * calendarGoTo
 * @desc - Navigate calendar (submit) to selected month
 * @param m {int} The numerical representation of the month to go to
 * @param Y {int} The numerical representation of the year to go to
 */
calendarGoTo = function (m,Y) {
	document.frm_schedule.m.value = m;
	document.frm_schedule.Y.value = Y;
	document.frm_schedule.submit();
}

//--------------------------------------------------------------------------------
/**
 * Assign functions and other misc. stuff when the DOM is ready
 */
var bwname = $.browser.name;
var bwversion = $.browser.versionNumber;
$(document).ready(function(){
	// init cal opt lightbox
	$("#calendar_options").jqm({overlay:40.01, modal:false});
	// write mailto addresses
	$("a[class^='email']").each( function (){
		var address = obfuscateEmail(email_codes[this.className.substr(5)]);
		this.href = "mailto:"+address;
	});
	// team page stuff
	$("#team_nav .boxMenu li").each( function (){
		$(this).click( function (){
			var d_id = $(this).parents("div.boxMenu")[0].id;
			var t = d_id.replace(/team_menu_/, '');
			var m = ($(this).hasClass("players")) ? 1 : 2;
			$("#team_content").load("ajax/team-loader.php", {team:t, mode:m});
			resetSubNav();
			setCurrentSubNav(this);
		});
	});
	// info page stuff
	$("#info_nav .boxMenu li").each( function (){
		$(this).click( function (){
			if (!$(this).attr('class').match(/ on/)) {
				var id_part = $(this).attr('class').replace(/ on/, '');
				resetSubNav();
				$("#info_content div").hide();
				$("#info_"+id_part).show();
				setCurrentSubNav(this);
			}
		});
	});
	$("span.locationInfo").click( function() {
		var ids = this.id.replace(/loc_/, '');
		var l_id = ids.replace(/\d+_/,'');
		if ($("#loc_info_"+ids).html() == '') {
			$("#loc_info_"+ids).load("ajax/location-data.php", {id:l_id});
			$("#loc_info_"+ids).show();
			$(this).html("less...");
		} else if ($(this).html() == "more...") {
			$("#loc_info_"+ids).show();
			$(this).html("less...");
		} else {
			$("#loc_info_"+ids).hide();
			$(this).html("more...");
		}
	});
});