// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 47;
// -----------------------------------------------------------------------------

var sPrcsrURL 		= "inside-energy/homePageVideoMenu.php";
var oCourseBundle	= null;
var sFlashMovieName = "IECTarget";
var debug = false;

//*******************************************************************************************
function main()
{
	
}
//*******************************************************************************************
/*
 *	GETFLASHMOVIE
 *	==========================================================
 *	This function returns the flash movie object from the 
 *	page.
 *
 */
function getFlashMovie() 
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[sFlashMovieName] : document[sFlashMovieName];
}
//*******************************************************************************************
function addVideo(iUserID, sVideoID)
{
	if(debug)
	{
		getFlashMovie().debug("Inside the addVideo function in javascript");
		getFlashMovie().debug("iUserID = "+iUserID +", "+"sVideoID = "+ sVideoID);
	}
	//store the given data in the database, pass it to the php file

	var processPassed = function(oBundle)
	{
		if(oBundle.a == false)
		{//the video is not in the database, ask flash for info on it so it can be added
			if(debug)
			{
				getFlashMovie().debug("Video is not in the database, need to add it");
			}
			getFlashMovie().requestVideoInfo(sVideoID);
		}
		else
		{//the video is in the database, play it
			// Due to a bug in MSIE, use a deferred object to call the actual swap fcn.
			if(debug)
			{
				getFlashMovie().debug("Video is in the database, trying to show it.");
			}
			callLater(0, sytHelper, sVideoID);//I commented this out so I stop sending requests to vimeo while I am testing
		}
	};
	var processFailed = function(err)
	{
		if(debug)
		{
			getFlashMovie().debug("addVideo failed in javascript");
		}
		//alert(err);
	};
	
	// Call the Mochi function to request a JSON response.
	var d = loadJSONDoc(sPrcsrURL + "?a=addVideo" + "&u=" + iUserID + "&v=" + sVideoID);
	
	// Setup the callback functions for the response.
	d.addCallbacks(processPassed, processFailed);
}
//*******************************************************************************************
function newUser(iTimeZoneOffset)
{//returns the next integer for a user id, gets this value from the database
	if(debug)
	{
		getFlashMovie().debug("Inside newUser in js");
	}
	
	var processPassed = function(oBundle)
	{
		if(debug)
		{
			getFlashMovie().debug("newUser returned from php: "+oBundle.id);
		}
		getFlashMovie().sendNextID(oBundle.id);
	};
	var processFailed = function(err)
	{
		if(debug)
		{
			getFlashMovie().debug("newUser failed from php, iID = "+oBundle.id);
		}
		alert(err);
	};
	
	// Call the Mochi function to request a JSON response.
	var d = loadJSONDoc(sPrcsrURL + "?a=newUser" + "&tz=" + iTimeZoneOffset);
	
	// Setup the callback functions for the response.
	d.addCallbacks(processPassed, processFailed);
}
//*******************************************************************************************
function newVisit(iUserID)
{//record that this user just visited
	if(debug)
	{
		getFlashMovie().debug("Inside newVisit in js");
	}
	
	// Call the Mochi function to request a JSON response.
	var d = loadJSONDoc(sPrcsrURL + "?a=newVisit" + "&u=" + iUserID);
}
//*******************************************************************************************
function removeVideo(iUserID, sVideoID)
{
	//record that this user stopped this video
	var d =loadJSONDoc(sPrcsrURL + "?a=removeVideo" + "&u=" + iUserID + "&v=" + sVideoID);

	
	// Remove the video that is currently in the projector.
	replaceChildNodes($('IEProjector'));
	
	// Add the movie player target container back to the page.	
	oNewProjTarget = createDOM('div', {id:'IEPTarget'}, '');
	appendChildNodes('IEProjector', oNewProjTarget);
}
//*******************************************************************************************
function sytHelper( sInVideoID )
{
	// Remove the "Turn on JavaScript" backup image for slow loading pages; 
	// don't want to spook people with a false error.
	//replaceChildNodes($('IEProjector'));
	
	// Add the YouTube Flash movie.
	/*
	var so = new SWFObject("http://vimeo.com/moogaloop.swf?clip_id=" + sInVideoID + "&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1", "SWFMovie", "340", "255", "9.0.47", "#FFFFFF");
	so.addParam("allowfullscreen","true");
	so.addParam("allowscriptaccess","always");
	so.addParam("movie", "http://vimeo.com/moogaloop.swf?clip_id=1515303&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1");
	so.write("IEProjector");
	*/
	flashvars = {};
	params = {allowfullscreen:"true", allowscriptaccess:"always", movie:"http://vimeo.com/moogaloop.swf?clip_id=" + sInVideoID + "&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1"};
	attributes = {};

	// Since the user has an acceptible version of flash, embed the movie.
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id=" + sInVideoID + "&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1", "IEPTarget", "340", "255", "9.0.47", "", flashvars, params, attributes);

}
//*******************************************************************************************
function LoadPlayer()
{
	var iPlayerVersion = (Number(swfobject.getFlashPlayerVersion().major) * 10000) + (Number(swfobject.getFlashPlayerVersion().minor) * 100) + (Number(swfobject.getFlashPlayerVersion().release));
	var iNeededVersion = (requiredMajorVersion * 10000) + (requiredMinorVersion * 100) + (requiredRevision);
	
	if( Number(iPlayerVersion) >= Number(iNeededVersion) )
	{
		flashvars = {};
		params = {wmode: "transparent"};
		attributes = {};

		// Since the user has an acceptible version of flash, embed the movie.
		swfobject.embedSWF("inside-energy/videos/2285 - large flash section 007.swf", "IECTarget", "540", "255", "9.0.47", false, flashvars, params, attributes);
	}
	else
	{
		// Since the user needs better version of flash, put a display saying so.
		replaceChildNodes($('IECTarget'));
		document.write('<a href="http://www.adobe.com/go/getflashplayer" target="_blank"><img src="inside-energy/images/download-flash_540x255.jpg" alt="Placeholder image telling users to install the Flash plugin" name="listingPhoto" width="540" height="255" border="0" /></a>');
	}
}
//*******************************************************************************************
function newVideoAdded(sVideoID, sVideoLength)
{//a new video needs to be added to the database
	iLength = parseInt(sVideoLength);
	var d = loadJSONDoc(sPrcsrURL + "?a=newVideoAdded" + "&v=" + sVideoID + "&l=" + iLength);
	
	var processPassed = function(oBundle)
	{
		if(debug)
		{
			getFlashMovie().debug("Adding the video was successful, now showing it");
		}
		//now show the video
		callLater(0, sytHelper, sVideoID);
	};
	var processFailed = function(err)
	{
		if(debug)
		{
			getFlashMovie().debug("Adding the video failed");
		}
		//alert(err);
	};
}
//*******************************************************************************************
function getVideoRating(iUserID, sVideoID)
{//find the rating stats for this video and for this video/user
	getFlashMovie().debug("Inside getVideoRating");
	
	var processPassed = function(oBundle)
	{
		if(debug)
		{
			getFlashMovie().debug("Getting the rating passed");
		}
		getFlashMovie().ratingFromDB(oBundle.nr, oBundle.ar, oBundle.ur);
	};
	var processFailed = function(err)
	{
		if(debug)
		{
			getFlashMovie().debug("Getting the rating failed");
		}
	};
	
	var d =loadJSONDoc(sPrcsrURL + "?a=getVideoRating" + "&u=" + iUserID + "&v=" + sVideoID);
	
	// Setup the callback functions for the response.
	d.addCallbacks(processPassed, processFailed);
}
//*******************************************************************************************
function submitRating(iUserID, sVideoID, iRating)
{//find the rating stats for this video and for this video/user
	var processPassed = function(oBundle)
	{
		if(debug)
		{
			getFlashMovie().debug("The rating was submitted");
		}
		getFlashMovie().ratingFromDB(oBundle.nr, oBundle.ar, oBundle.ur);
	};
	var processFailed = function(err)
	{
		if(debug)
		{
			getFlashMovie().debug("The rating failed being submitted");
			getFlashMovie().debug("err = "+err);
			getFlashMovie().debug("numRatings = "+oBundle.nr,+", averageRatings = "+oBundle.ar );
			getFlashMovie().debug(d);
		}
	};
		
	var d =loadJSONDoc(sPrcsrURL + "?a=submitRating" + "&u=" + iUserID + "&v=" + sVideoID + "&r=" + iRating);
		
	// Setup the callback functions for the response.
	d.addCallbacks(processPassed, processFailed);
}
//*******************************************************************************************

