var sizeIframe = {
	now: function() {
		var iframeId = "fantasy-log-in-iframe";
		var iframeNode = document.getElementById(iframeId);
		var iframeDoc = window.frames[iframeId].document;
		var divTags = iframeDoc.getElementsByTagName("div");
		var divHeight = divTags[0].offsetHeight;
		if (parseInt(divHeight) < 100) divHeight = 100;
		iframeNode.height = parseInt(divHeight);
		iframeNode.style.height = parseInt(divHeight) + 'px';
	}
};

nfl.fantasy = {
	StiLogin: function() {
		if (nfl.global.Authentication.isAuthenticated()) { // if the user is logged in
			console.log("USER IS LOGGED IN");
			// we hardcoded the height to 200 for now since the auto size function is not working correctly with there HTML
			
			// create an empty element node
			var iframeNode = document.createElement("iframe");

			// give it attributes 
			if (n_environment == "PROD") {
				iframeNode.setAttribute("src", "http://challengegames.nfl.com/iframe");
			}
			else {
				iframeNode.setAttribute("src", "http://stage.challengegames.nfl.com/iframe");
			}
			iframeNode.setAttribute("width", "300");
			iframeNode.setAttribute("height", "250");
			iframeNode.style.height = '250px';
			iframeNode.style.border = '0px solid white'; // needed for IE
			iframeNode.setAttribute("frameborder", "0");
			iframeNode.setAttribute("frameBorder", "0");  // needed for IE
			iframeNode.setAttribute("id", "sti-log-in-iframe");
			iframeNode.setAttribute("name", "sti-log-in-iframe");
			iframeNode.setAttribute("scrolling", "no");
			iframeNode.setAttribute("marginwidth", "0");
			iframeNode.setAttribute("marginheight", "0");
			var loggedInDiv = document.getElementById("sti-promo-logged-in");
			 
			// apply that content to the logged in div
			loggedInDiv.appendChild(iframeNode);
			
			// show the correct div tag
			$('sti-promo-logged-in').show();
			//nfl.fantasy.SizeIframeOnLoad('sti-log-in-iframe')
		}
		else { // if the user is logged out
			console.log("USER IS LOGGED OUT");
			$('sti-promo-logged-out').show();
		}
	},
	SizeIframeOnLoad: function(frame_id) {
		try { document.domain = 'nfl.com';}
		catch(e) {}
		var i = $(frame_id);
		var height		= i.offsetHeight;
		var size_frame	= function(e) {
			var iframe	= $(frame_id);
			try {
				// should use window.frames[frame_id].document.body.offsetHeight, but IE7 gets it wrong.
				height	= window.frames[frame_id].document.getElementsByTagName('div')[0].offsetHeight;
			}
			catch(e) {}
			console.log("SizeIframeOnLoad - height = " + height);
			iframe.style.height = height + 'px';
			iframe = null;
		};
		i.onload = size_frame;
		//window.sizeIframe = { now: function() {}};
		Event.observe(window, 'unload', function(e) { i = i.onload = null;});
	},
	DropDownNavigation: function(form, input) {
		$(form).observe('submit', function(e) {
			Event.stop(e);
			var target_url = $F(input);
			if (target_url) { window.location = target_url;}
		});
	},
	CollapsibleListItems: (function(){
		var COLLAPSED_CLASS			= 'collapsed';
		var COLLAPSIBLE_CLASS		= 'collapsible';
		var TOGGLE_CLASS			= 'collapse-toggle';
		var COLLAPSIBLE_TEMPLATE	= new Template('<a href="#">#{text} <span class="ascii-art">[#{symbol}]</span></a>');
		var COLLAPSE_OBJECT			= { text: 'Collapse', symbol: '-' };
		var EXPAND_OBJECT			= { text: 'Expand', symbol: '+' };
	
		function make_collapsible(li) {
			li.innerHTML += ('<div class="' + TOGGLE_CLASS + '">' + COLLAPSIBLE_TEMPLATE.evaluate(EXPAND_OBJECT) + '</div>');
			li.addClassName('collapsed');
		}
		function collapse(el) {
			var a, div, li, isCollapsed, obj;
			li	= $(el);
			div	= Element.select(li,'div.' + TOGGLE_CLASS).first();
			obj	= li.hasClassName(COLLAPSED_CLASS) ? COLLAPSE_OBJECT : EXPAND_OBJECT;
			li.toggleClassName(COLLAPSED_CLASS);
			div.innerHTML = COLLAPSIBLE_TEMPLATE.evaluate(obj);
			li = div = a = null;
		}
		function collapse_on_click(e) {
			var el = Event.element(e);
			if (el.nodeName == 'A' && el.parentNode.className != TOGGLE_CLASS) { return;}
			collapse(Event.findElement(e, 'li'));
			Event.stop(e);
		}
		return function(id) {
			var list = $(id);
			list.childElements().each(make_collapsible);
			list.observe('click', collapse_on_click);
			list.addClassName(COLLAPSIBLE_CLASS);
			nfl.fantasy.CollapsibleListItems.toggle = collapse; 
		};
	})(),
	ZebraTable: (function(e) {
		var ODD_CLASS = 'odd';
		var EVEN_CLASS = 'even';

		function highlight_row(e) {
			var tr = Event.findElement(e, 'tr');
			tr.toggleClassName('over');
			Event.stop(e);
		}

		return function(id) {
			var table = $(id);
			var rows = table.getElementsByTagName('tr');
			for (var i = 2, l = rows.length; i < l; i++) {
				rows[i].className = (i % 2 == 1) ? EVEN_CLASS : ODD_CLASS;
			}
			table.observe('mouseover', highlight_row);
			table.observe('mouseout', highlight_row);
		}
	})()
};
