	/***
	general.js - GENERAL JAVASCRIPT FUNCTIONS
	v0.1 - 2008-07-17
		- Initial verion
	v0.2 - 2008-08-08
		- modified hash checker so if it finds tab nav, it will turn on the first one by default,
		  if it isn't already turned on
	v0.3 - 2008-08-14
		- extended and documented h1 and h2 flash replace scripts
	v0.4 - 2008-08-25
		- Revised header replace scripts (in generic_content) to accomodate new, alt layout with
		   square image
	v0.5 - 2008-10-03
		- Expanded "cleanForSWF" to handle HTML tags and leading/trailing space (trailing doesn't
		   seem to be working though)
		- Added "siteroot" var, to help with different server environments
	v0.6 - 2008-10-29
		- Added Cookie functions
		- Added Google Analytics tracking bits
	v0.7 - 2008-11-17
		- Added new code for photo captions
	***/
	
	var siteroot = "";
	
	/*** GLOBAL VARS ***/	
	// Object var for holding a reference to the last sub nav that was opened; ordered list HTML ELEMENT
	var lastSubNav;
	// Set Timeout object for the subnav; timeout OBJECT
	var navTimeout;
	// Int var for keeping track of active calendar in the performance mini-calendar
	var curCal = 0;
	
	/*** UTIL - ezy pzy delicious Cookie functions  ***/
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
	
	// Update - 2008-10-29
	/*** GOOGLE ANALYTICS ***/
	if (document.location.href.indexOf("utm_campaign=") > -1) {
		// location contains the GA utm banner code; write everything in there into
		createCookie("SP_BannerQuery",document.location.search);
	}

	
	/*** NAV HELPER FUNCTIONS ***/
	// Hide sub nav; exception = STRING
	function hideSubNav(exception) {
		if (lastSubNav != null && lastSubNav != $(exception)) {
			// Special handle for IE, specifically IE 6 and lower, which don't support the
			// Scriptaculous fade() effect
			if (Prototype.Browser.IE == true) {
				if (navigator.appVersion.indexOf('MSIE 7') > 0 || navigator.appVersion.indexOf('MSIE 8') > 0) {
					lastSubNav.fade({duration: 0.1});
				} else {
					lastSubNav.hide();
				}
			// All other modern browsers
			} else {
				lastSubNav.fade({duration: 0.1});
			}
		}
	}

	// Hide content tabs; keyword = STRING
	function activateTab(keyword) {
		var divActive = $("content_" + keyword);
		var anchActive = $("tab_" + keyword);
		$$(".tab_content").each( function(div) {
			if (div != divActive) {
				div.removeClassName("tab_content_on");
			}
		});
		divActive.addClassName("tab_content_on");
		$$("#tab_nav a").each( function(anch) {
			if (anch != anchActive) {
				anch.removeClassName("on");
			}
		});
		anchActive.addClassName("on");
	}

	
	/*** FLASH HELPER FUNCTIONS ***/
	// Clean up input for the text headers so it can handle funky characters
	function cleanForSWF(myString) {
		// Replace HTML
		myString = myString.replace(/<[^<>]+>/g,"");
		// Replace leading and trailing space
		myString = myString.replace(/^\s+/g,"");
		myString = myString.replace(/$\s+/g,"");
		myString = myString.replace(/$\n+/g,"");
		myString = myString.replace(/$\r+/g,"");
		
		myString = myString.replace(/&nbsp;/g, " ");
		myString = myString.replace(/%/g, "%25");
		myString = myString.replace(/\+/g, "%2B");
		myString = myString.replace(/\&/g, "%26");
		myString = myString.replace(/#/g, "%23");
		return myString;
	}
	
	/*** PERFORMANCE PAGE TEXT RESIZERS ***/
	// These functions are called by the flash movies, performance_details.swf and performance_title.swf
	// They resize the flash movies depending on how much text is inside them.
	function resizeTitle(height) {
		var lines = Math.round(height/32.1);
		if (lines > 1) {
			$("play_title").height = ($("play_title").height)*lines + 1*lines;
		}
	}
	function resizeDetails(height) {
		var lines = Math.round(height/21.1) - 3;
		if (lines > 0) {
			$("play_details").height = parseFloat($("play_details").height) + (lines*22) + 8;
		}
	}
	
	/*** CALENDAR NAV ***/
	function nextMonth(dir) {
		if ((curCal != 0 && dir == 'prev') || (curCal != ($$(".performance_calendar").length - 1) && dir == 'next')) {
			if (dir == 'prev') {
				$$(".performance_calendar")[curCal].hide();
				curCal = curCal - 1;
				$$(".performance_calendar")[curCal].style.display = "block";
			} else {
				$$(".performance_calendar")[curCal].hide();
				curCal = curCal + 1;
				$$(".performance_calendar")[curCal].style.display = "block";
			}
		}
	}
	


	/*** WINDOW ONLOAD FUNCTION ***/
	
	Event.observe(window, 'load', function() {
		var mainNav = $$(".mainnav");
		
		/*** MAIN NAV ***/
		// "Arm" the subnav lists with clear and set timeout functions
		$$(".subnav").each( function(olist) {
			olist.onmouseover = function() {
				if (navTimeout) {
					clearTimeout(navTimeout);
				}
			}
			olist.onmouseout = function() {
				navTimeout = setTimeout(function() {hideSubNav("null");},1000);
			}
		}); // End arming subnav
	
		
		// "Arm" the main nav items with mouse over and mouse out functions
		mainNav.each( function(anch) {
			// Get rid of text links inside main nav
			//anch.innerHTML = "";
			
			// String of the mainNav id, to parse and turn into a reference to the subnav id
			var theID = anch.id;
			
			anch.onmouseover = function() {
				// Strip "mainnav_"
				theID = theID.replace(/mainnav_/g,'');
				// Hide the last sub nav that was opened, unless it's the current one
				hideSubNav("sub_" + theID);
				// If we're on the homepage, hide the promo flash bits
				/* if ($("promo_hdr_1")) {
					$("promo_hdr_1").
				} */
				// Show the current subnav, if one exists
				if ($("sub_" + theID)) {
					lastSubNav = $("sub_" + theID);
					$("sub_" + theID).setStyle({
						display: 'block',
						opacity: 1.0,
						filter: 'alpha(opacity=100)'
					});
				} else {
					lastSubNav = null;
				}
				// Clear the timeout 
				if (navTimeout) {
					clearTimeout(navTimeout);
				}
			}
			anch.onmouseout = function() {
				navTimeout = setTimeout(function() {hideSubNav(null);},1000);
			}
		}); // End arming mainnav
		

		/*** TAB NAV ***/
		// Arm the tab navs
		if ($("tab_nav")) {
			$$("#tab_nav li a").each( function(anch) {
				//anch.innerHTML = "";
				anch.onclick = function() {					
					var key = anch.id.replace(/tab_/g,"");
					if ($("content_" + key)) {
						// Turn off all tabs, with the exception of the current one
						activateTab(key);
						// Turn on the current one
						//$(tabContent).addClassName("tab_content_on");
						// Turn on the current tab button
						//anch.addClassName("on");
						//return false;
					}
				}
			});
		}
		
		// Check the location hash, and activate the corresponding tab, if it exists
		if (document.location.hash != "") {
			var myHash = document.location.hash.substring(1,document.location.hash.length);
			if ($("tab_" + myHash) && $("content_" + myHash)) {
				activateTab(myHash);
			}
		// Activate the first available tab, if it isn't activated already
		} else {
			if ($("tab_nav")) {
				activateTab($$("#tab_nav li a")[0].id.substring(4,$$("#tab_nav li a")[0].id.length));
			}
		}
		
		// PERFORMANCE ONLY
		// Check doc location and open 09, 10 tab if we're on 09_season.aspx, etc.
		if (document.location.href.indexOf('09_season.aspx') > -1) {
			activateTab('season_09');
		} else if (document.location.href.indexOf('10_season.aspx') > -1) {
			activateTab('season_10');
		}
		
		
		/*** PERFORMANCE CALENDAR - Activate the first one, or the current month ***/
		// TO DO: activate for current month
		if ($$(".performance_calendar").length > 0) {
			$$(".performance_calendar")[0].style.display = "block";
		}
		
		/*** SUBSCRIBE PAGES - header replace ***/
		if ($$(".subscribe_benefits").length > 0) {
			var hdrcntr = 0;
			$$(".subscribe_benefits h3").each( function(hdr) {
				hdr.id = "sub_hdr3_" + hdrcntr;
				swfobject.embedSWF(siteroot + "/swf/body_header-v2.swf?hdr_txt=" + cleanForSWF(hdr.innerHTML), "sub_hdr3_" + hdrcntr, "510", "26", "9.0.0", false, {}, {wmode:"transparent", menu:"false"},{styleclass:"flashhdr_sub_h3"});
				hdrcntr++;
			});
		}
		
		/*** SIDE MODULE - header replace ***/
		if ($("module_side_title")) {
			swfobject.embedSWF(siteroot + "/swf/module_header_side-v1.swf?hdr_txt=" + cleanForSWF($("module_side_title").innerHTML) + "&amp;bgcolour=0xb54739", "module_side_title", "218", "26", "9.0.0", false, {}, {menu:"false",wmode:"transparent"},{});
		}
		
		/*** GENERIC PAGES - header replace ***/
		if ($$(".generic_content").length > 0) {
			// Create a counter so we can id all of these headers and target them
			var hdrcntr = 0;

			// Replace h1 inside generic content
			$$(".generic_content h1").each( function(hdr) {
				hdr.id = "hdr1_" + hdrcntr;
				// & = %26
				// + = %2B
				// % = %25
				// # = %23
				// &nbsp; = <space>
				swfobject.embedSWF(siteroot + "/swf/page_header-v1.swf?hdr_txt=" + cleanForSWF(hdr.innerHTML), "hdr1_" + hdrcntr, "600", "31", "9.0.0", false, {}, {wmode:"transparent", menu:"false"},{styleclass:"flashhdr_h1"});
				hdrcntr++;
				
			});
			
			// Reset for h2's
			hdrcntr = 0;
			
			// Replace h2's inside generic content
			$$(".generic_content h2").each( function(hdr) {
				hdr.id = "hdr2_" + hdrcntr;
				var myWidth = "600";
				if ($$(".alt_layout #masthead") != '') {
					myWidth = "310";
				}
				// Original replace script - swfobject.embedSWF("/swf/body_header-v2.swf?hdr_txt=" + cleanForSWF(hdr.innerHTML), "hdr2_" + hdrcntr, "600", "26", "9.0.0", false, {}, {wmode:"transparent", menu:"false"},{styleclass:"flashhdr_gen_h2"});
				swfobject.embedSWF(siteroot + "/swf/body_header-v2.swf?hdr_txt=" + cleanForSWF(hdr.innerHTML), "hdr2_" + hdrcntr, myWidth, "26", "9.0.0", false, {}, {wmode:"transparent", menu:"false"},{styleclass:"flashhdr_gen_h2"});
				hdrcntr++;
			});
			
			// Reset for h3's
			hdrcntr = 0;
			
			// Replace h3's inside tabs
			$$(".tab_content h3").each( function(hdr) {
				hdr.id = "hdr3_" + hdrcntr;
				swfobject.embedSWF(siteroot + "/swf/body_header-v2.swf?hdr_txt=" + cleanForSWF(hdr.innerHTML), "hdr3_" + hdrcntr, "590", "26", "9.0.0", false, {}, {wmode:"transparent", menu:"false"},{styleclass:"flashhdr_tab_h3"});
				hdrcntr++;
			});
		}
		
		/*** Footer replace ***/
		if ($("footer_modules")) {
			var hdrcntr = 0;
			var myWidth = 415;
			var myMov = "module_header-v2.swf";
			if ($("artist_landing_frame")) {
				myWidth = 273;
				myMov = "module_header_sm-v2.swf";
			}
			$$("#footer_modules h3").each( function(hdr) {
				hdr.id = "ftr_hdr3_" + hdrcntr;
				var bgcolour = "";
				if (hdr.hasClassName("highlight")) {
					bgcolour = "&bgcolour=0xb54739";
				}
				swfobject.embedSWF(siteroot + "/swf/" + myMov + "?hdr_txt=" + cleanForSWF(hdr.innerHTML) + bgcolour, "ftr_hdr3_" + hdrcntr, myWidth, "26", "9.0.0", false, {}, {menu:"false"},{});
				hdrcntr++;
				
			});
		}
		
		/*** PHOTO CREDITS ***/
		if ($("credit_anchor")) {
			// Add mouseover behaviour
			$("credit_anchor").onmouseover = function() {			
				$("credits").setStyle({
					display: 'block',
					opacity: 1.0,
					filter: 'alpha(opacity=100)'
				});
			}
			$("credit_anchor").onmouseout = function() {
				// Make the credit fade out, like the nav
				if (Prototype.Browser.IE == true) {
					if (navigator.appVersion.indexOf('MSIE 7') > 0 || navigator.appVersion.indexOf('MSIE 8') > 0) {
						$('credits').fade({duration: 0.1});
					} else {
						$('credits').hide();
					}
				// All other modern browsers
				} else {
					$('credits').fade({duration: 0.1});
				}
			}
		}
		
	}); // End onload events