var menu_sport;
var clicktip_current = null;
var api_mode = false;

$(document).ready(function(){
	// if there's a logout button, make it do something
	$("a.logoutLink").click(
		function(){
			document.logout.submit();
			return false;
		}
	);
	
	//collapse-expand code for sport menu
	if($(".sportMenuHeader").length > 1){
		$(".sportMenuHeader").click(function(){
			var id = $(this).attr("id");
			var sport = id.substring(0,id.indexOf("_"));
			menu_sport = (menu_sport == sport) ? "":sport;
			updateMenus();
		}).addClass("clickable");
	}
	
	$("#clickTip a.closeWindowButton").click(function(){
		clicktip_current = null;
		return false;
	});
	
	//adjust the width of the main menu
	if($.browser.msie)
		fixMenuWidth();
		
	//download the clicktip definition file (using the cache)
	$.getScript(
	"/clicktips/",
	function(){
		setClickTipActions();
	},
	true);
	
/*
*	actions on league display. since it's a component, we need these scripts all over
*/	
	$("div.signupLinks a.button").click(
		function(){
			var elementID = $(this).attr("id");
			var leagueID = extractID(elementID);
			
			if(elementID.substr(0,4) == 'team'){
				if($("#teamQuickSignup_" + leagueID).is(":visible"))
				{
					hide($("#teamQuickSignup_" + leagueID));
				}
				else
				{
					show($("#teamQuickSignup_" + leagueID));
					hide($("#indyQuickSignup_" + leagueID));
				}
			}else{
				if($("#indyQuickSignup_" + leagueID).is(":visible"))
				{
					hide($("#indyQuickSignup_" + leagueID));
				}
				else
				{
					show($("#indyQuickSignup_" + leagueID));
					hide($("#teamQuickSignup_" + leagueID));
				}
			}
			return false;
		}
	).addClass("clickable");
	
	$("div.quickSignup a.closer").click(
		function(){
			var elementID = $(this).attr("id");
			var leagueID = extractID(elementID);
			if(elementID.substr(0,4) == 'team'){
				hide($("#teamQuickSignup_" + leagueID));
			}else{
				hide($("#indyQuickSignup_" + leagueID));
			}
			
			return false;
		}
	).addClass("clickable");
	
	$("div.showLeagueDetails").click(
		function(){
			var leagueID = extractID($(this).attr("id"));
			openDetailDisplay(leagueID);
			return false;
		}
	).addClass("clickable");
	
	$("div.hideLeagueDetails").click(
		function(){
			var leagueID = extractID($(this).attr("id"));
			closeDetailDisplay(leagueID);
			return false;
		}
	).addClass("clickable");
	
	//	some types of components don't need a border on their parent componentRow
	$("div.leagueDetailComponent").parents("div.componentRow").css('border','0');
	
	/*
	*	popups
	*/
	$("a.closeWindowButton").click (
		function ()
		{
			$(this).parents(".popup,.draggablePopup,.scalablePopup,.widePopup").each (
				function ()
				{
					hide ($(this));
				}
			);
			return false
		}
	);
	$("div.draggablePopup,div.scalablePopup,div.widePopup").draggable({handle:'div.handle'});
		
	//	detect API mode and activate special behavior
	api_mode = (top.location != self.location);
	if (api_mode)
	{
		if (typeof api_host == "string" && api_host)
		{
			//	we're in API mode, and we have a real host, so we communicate with the host window,
			//	unless it's unknown (ApiHost=O)
			if (api_host != 'O')
			{
				parent.location = api_host + "#ll_size_" + $("#pageContainer").width () + "_" + $("#pageContainer").height ();
			}
		}
		else if (typeof api_host != "string" || !api_host)
		{
			//	we're in API mode, but we don't have a host at all... the page is showing menus, etc. we need to turn it ON if we can
			//toggleApiMode (true);	//	DISABLED b/c some clients embed the whole page in a frame... what do do?
		}
	}
	else if (typeof api_host == "string" && api_host && self.location.href.indexOf ('ApiHost=X') == -1)
	{
		//	we're not currently embedded, but we ARE in API mode. 
		toggleApiMode (false);
	}
});

//	redirect to turn api mode on or off... NOTE: it won't work of the page doesn't accept query string args,
//		in which case we give up after noticing our switch is already present
function toggleApiMode (fApiMode)
{
	//	the query string ApiHost=X exits API mode, while ApiHost=O enters it (with unspecified host)
	var sSwitch = fApiMode ? 'O' : 'X';
	sSwitch = 'ApiHost=' + sSwitch;
	if (self.location.href.indexOf (sSwitch) == -1)
	{
		var sSeparator = (self.location.href.indexOf('?') == -1) ? '?' : '&';
		self.location.href = self.location.href + sSeparator + sSwitch;
	}
}

//	collapse and expand sport menu
function updateMenus(){
	$("#sportMenus ul").each(function(){
		hide($(this),100);
	});
	$(".sportMenuHeader").css("margin-top",0);	//	put them all in place
	if(menu_sport != ""){
		$("#" + menu_sport + "_Menu").each(function(){
			show($(this),100);
		});
		$("#" + menu_sport + "_MenuHeader").css("margin-top",15);	//	pull the selected one down a bit
	}
	$(".sportMenuHeader:eq(0)").css("margin-top",0);				//	unless it's the first one.
}

//	adjust the width of the main menu in ie, because it doesn't support display:table
function fixMenuWidth(){
	var menuWidth = 50;
	$("#mainMenu li").each(function(){
		menuWidth = menuWidth + parseInt($(this).width());
	});
	$("#mainMenu ul").width(menuWidth);
}

//	set the click tip actions. this is not done in the $(document).ready function because
//	the js file with clicktip definitions is loaded inside that script. this is the callback
function setClickTipActions(){
	$("a.clickTip").click(function(e){
		hide($("#clickTip"));
		var clickID = $(this).attr("rel");
		if(this == clicktip_current){
			hide($("#clickTip"),150);
			clicktip_current = null;
			return false;
		}else{
			clicktip_current = this;
		}

		if(clicktips[clickID] == undefined){
			$("#clickTip h4").html("oops");
			$("#clickTip div.clickTipBody").html("We realize this isn't very helpful, and we'll fix it ASAP. For now, please see our online help documentation...");
			$("#clickTip a.more").attr("href","/help/");
		}else{
			$("#clickTip h4").html(clicktips[clickID]["Title"]);
			$("#clickTip div.clickTipBody").html(clicktips[clickID]["Body"].replace(/\n/g,'<br />'));
			if(clicktips[clickID]["Link"] == ""){
				$("#clickTip a.more").html("");
			}else{
				$("#clickTip a.more").attr("href",clicktips[clickID]["Link"]);
				$("#clickTip a.more").html("learn more");
			}
		}
		var x = e.pageX + 10;
		var y = e.pageY + 10;
		if(x > document.body.clientWidth - 320){
			x = x - $("#clickTip").width() - 15;
		}
		$("#clickTip").css("left",x);
		$("#clickTip").css("top",y);
		show($("#clickTip"));
		
		return false;
	});
}

//	for pages that allow a 'filter by sport' feature, this
//	updates the look of the controls to match the currently
//	selected sport
function setSportButtonStatuses(sport)
{
	var buttonImage = 'navbutton';
	var selectedImage = 'square-s';
	var selectedSport = (sport == '') ? 'allsports':sport.replace(' ','-');
	
	$("#sportFilterBox img.sport_filter_button").each(
		function()
		{
			//	figure out which sport this one is
			var thisSport = $(this).attr("id");
			thisSport = thisSport.substr(thisSport.lastIndexOf('_') + 1);
			
			//	and which image is currently there
			var imgSource = $(this).attr("src");
			
			if (selectedSport == thisSport)
			{
				imgSource = imgSource.replace(buttonImage,selectedImage);
				$(this).attr("src",imgSource);
				$(this).addClass("current");
			}
			else
			{
				imgSource = imgSource.replace(selectedImage,buttonImage);
				$(this).attr("src",imgSource);
				$(this).removeClass("current");
			}
		}
	);
}

/*
*	used by league display
*/
function openDetailDisplay(leagueID){
	hide($("#showDetails_" + leagueID));
	show($("#hideDetails_" + leagueID));
	$("#leagueDetails_" + leagueID).show (300);
}
function closeDetailDisplay(leagueID){
	if(leagueID == 0)
		return;
	$("#leagueDetails_" + leagueID).slideUp (300);
	show($("#showDetails_" + leagueID));
	hide($("#hideDetails_" + leagueID));
}

//so the getScript calls will use the cache
$.getScript = function(url, callback, cache){ $.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache }); };
