/*
	file: musicPopup.js
	date: September 10, 2008
	author: Jay Dansand, for Lawrence University ITS (dansandj@lawrence.edu)
	copyright: use this code exclusively for the Lawrence University MoreLight Campaign,
				and retain this header always.
				ALLOWED: editing of the window.open(...) call within the popAndGoTo(...) function.
	---
	usage:
		1) Insert this JavaScript within the HEAD element of a Lawrence MoreLight page.
		2) Create a "startMusic()" function to play music on this page if the popup is not open.
		NOTE: expects a root-level document called "PopPlayer.aspx", unless modified below
			within the popAndGoTo(...) function.
	description:
		1) Intelligently start music playing on this page depending on existence of a popup player.
		2) If the popup player has not been created, set all links to launch it on click.
	notes:
		1) Works in Safari for Mac/Win, Firefox 2/3 for Mac/Win, IE 6/7 for Win, Opera for Win.
		2) Requires aptly coded PopPlayer.aspx in the root-level of the website (unless otherwise configured).
*/

var musicOn = false;
var havePopup = false;
var popupWin = null;

function startMusic()
{
	if (document.cookie.indexOf("userClosedPopup='true'") < 0)
		SoundOnOff(true);
	else
		SoundOnOff(false);
}

function popAndGoTo(destination, forcepopup)
{ //Create a new window, start the player in it, and then move this window along to the new location
	if (musicOn == true || (forcepopup && forcepopup == true))
	{ //Show the popup if this is a forced display or the user did not close the popup window on their own
		document.cookie = "userClosedPopup='false'";
		if (popupWin) popupWin.document.cookie = "userClosedPopup='false'";
		if (popupWin == null || popupWin.document == null)
			popupWin = window.open('/PlayList.aspx', 'Player', 'left=' + (window.screen.width - 276) + ', top=' + (window.screen.height - 145) + ', height=450, width=280, status=yes, resizable=no, scrollbars=no, toolbar= no,location= no, menubar= no');
		else
			popupWin.focus();
	}
	// bhw ( focus the parent unless there is no destination specified. )
	if (destination != null)
	{
		if (popupWin != null) ASPNetMedia.Audio("HomePageAudio").StopMedia();
		window.location = destination; //Move to the new location
		if (popupWin != null) popupWin.setTimeout("window.opener.focus()", 1000); //Return focus to the main window after popping up the player window
	} else {
		if (popupWin) popupWin.focus();
	}
}

function initMusicPopup(event)
{ //This function is called when the document loads
	//Set a timeout to wait for popup communication and act accordingly:
	setTimeout("setupMusicPopup();", 2000); //Wait 2.0s; the popup polls every 1s.
}

function setupMusicPopup()
{ //This function is called 1.5s after the page loads, by which time our
	//popup (if we have one) should have communicated with us.
	//Test for that communication (to see if we have a popup still):
	
	if (popupWin != null)
	{ //We have a popup
		musicOn = popupWin.musicOn;
		havePopup = true;
	}
	else if (document.cookie.indexOf("userClosedPopup='true'") >= 0)
	{
		musicOn = false;
		havePopup = false;
	}
	else
	{ //We have no popup, so configure our links to make one:
		var links = document.getElementsByTagName("A");
		for (var i = 0; i < links.length; ++i)
		{ //Iterate through every link found,
			//and change their HREF attribute to use popAndGoto
			var href = links[i].getAttribute("HREF");
			if ( (href != null) && (href.toLowerCase().indexOf("javascript") < 0) && (href.indexOf("#") != 0) && !links[i].target)
		    {//If this link is not a javascript link already, an anchor link on this page, or a targeted link, rewrite its href
				links[i].href = "javascript:popAndGoTo('" + href + "');"
		    }
		}
		//And invoke the player:
		if (typeof(startMusic) == "function") startMusic(); //Call the auxillary function, if one exists
	}
}

function TempSoundOff()
{
	if (havePopup)
	{
		popupWin.ASPNetMedia.Audio("HomePageAudio").StopMedia();
		popupWin.musicOn = false;
	}
	else ASPNetMedia.Audio("HomePageAudio").StopMedia();
	musicOn = false;
}

function SoundOnOff(force)
{ //This function toggles sounds or sets sound = [force] (true/false = on/off)
	//The function will communicate with the popup if one exists or with the player control on the page
	//if a popup is not found.
	if (force) musicOn = (force != true) //Rig the force parameter to change "musicOn" to accomplish our desired behavior
	if (musicOn == true)
	{ //Music is currently on (or [force] was set to Music Off
		musicOn = false;
		document.cookie = "userClosedPopup='true'";
		if (havePopup == true) popupWin.SoundOnOff(false);
		else ASPNetMedia.Audio("HomePageAudio").StopMedia();
	}
	else
	{ //Music is currently off (or [force] was set to Music On)
		musicOn = true;
		document.cookie = "userClosedPopup='false'";
		if (havePopup == true) popupWin.SoundOnOff(true);
		else ASPNetMedia.Audio("HomePageAudio").PlayMedia();
	}
}
     
//Add our init function as event listener for window.onload.
//By adding the listener instead of setting body.onload,
//we effectively stack the listeners and will not disturb or
//be disturbed by other scripts trying to capture similar events.
if (document.all) //IE/Opera likes to use attachEvent:
	window.attachEvent("onload", initMusicPopup);
else //WebKit/Gecko (everything sensible) likes to use addEventListener:
	window.addEventListener("load", initMusicPopup, false);
