/*
	*****************************************
	GENERAL NOTES:
	*****************************************
	
	The following javascript functionality serves in conjunction with the videoPlayer and carousel
	flash components. There are a number of functions that need to be called between flash and
	javascript, a naming convention has been implemented to indicate which is which.
	
	"js_" indicates functions that are called from flash to javascript
	"fl_" indicates functions that are called from javascript to flash
	
	ADD THIS:
	The video player uses the 'addThis' widget to display a dropDown widget. AddThis isn't designed to
	work automatically with flash content so there are a number of functions dedicated to getting it to
	work appropriately
	
	*****************************************
	fl_ FUNCTIONS:
	*****************************************
	
	FUNCTION NAME:
	
		fl_setVideo
	
	DESCRIPTION:
	
		Plays the specified video in the videoPlayer. If the videoPlayer is showing the
		pre-roll the video will commence afterwards.
	
	PARAMETERS:
	
		video : Object
			video.id : int - database id for the video
			video.uri : String	- url of the flv		
			video.trackingID : Sting - (optional) tracking string for analytics
	
	*****************************************
	
	FUNCTION NAME:
	
		fl_setProfileEnabled
	
	DESCRIPTION:
	
		Enables or disables the 'view profile' button in the videoPlayer controls
	
	PARAMETERS:
	
		enabled : Boolean - true = enabled, false = disabled
	
	*****************************************
	
	js_ functions are written and commented individually.
*/

var VIDEO_PLAYER_ID = "videoPlayer";
var videoPlayerInitialised = false;
var CAROUSEL_ID = "carousel";
var carouselInitialised = false;

var currentTeamID = '0';

var addThisActive = false;

var ADD_THIS_ID = "addThis";

var addThisCreationTimer = 0;
var addThisRollOverTimer = 0;
var addthis_pub = "hollersydney";
var addthis_options = 'twitter, myspace, google, facebook, stumbleupon, digg, delicious,  reddit, live, more';

var storedVideo = null;

var preRoll = false;

/*
	FUNCTION NAME:
	
		fl_setProfileEnabled
	
	DESCRIPTION:
	
		All flash components will call this function when they have successfully loaded and initialsed
		with the remoting services. This indicates that the flash componets are ready to begin recieving
		calls from and making call to javascript.
	
	PARAMETERS:
	
		id : String - Value identifying which component is calling the function. This value with match
		the id property set on the flashvars object in the components swfobject embed code.
*/
function js_initialiseFlashComponent( id )
{
	//alert(id);
	
	if( id == VIDEO_PLAYER_ID && videoPlayerInitialised == false )
	{
		videoPlayerInitialised = true;
		
		getObject(VIDEO_PLAYER_ID).fl_initialiseFlashComponent();
		
		//Automatically set the preroll video video
		
		if(preRoll)
		{
			var video = {};
						
			video.id = 0;
			//video.uri = "http://www.spiritofgoodtaste.com.au/videos/TVCHAH45.flv";
			video.uri = "http://tasteforsport.hollersydney.com.au/videos/hsd_intro.mp4";

			video.trackingID = "video_0";
			
			
			getObject(VIDEO_PLAYER_ID).fl_setVideo( video );
		}
		else
		{
			playInitialVideo();
		}
		
		

	}
	else if( id == CAROUSEL_ID && carouselInitialised == false )
	{
		carouselInitialised = true;
		
		getObject(CAROUSEL_ID).fl_initialiseFlashComponent();
		
		//demo_setViewProfile(true);
	}
}

function playInitialVideo()
{
	playIntro();
}

function js_videoChange(video)
{
	currentTeamID = video.id;


	
	if(preRoll)
	{
		preRoll = false;
		storedVideo = video;
	}
	else
	{
		if( videoPlayerInitialised == true ) getObject(VIDEO_PLAYER_ID).fl_setVideo( video );
		if(currentTeamID > 0){
			setViewProfileEnabled(true);
		}else{
			setViewProfileEnabled(false);
		}
	}
}

/*
	FUNCTION NAME:
	
		js_videoComplete
	
	DESCRIPTION:
	
		Called by the videoPlayer when it has completed playback of its current video.
	
	PARAMETERS:
	
		none
*/
function js_videoComplete()
{
		
	if(currentTeamID > 0){
			setViewProfileEnabled(true);
		}else{
			setViewProfileEnabled(false);
		}
	
	if(storedVideo != null)
	{
		js_videoChange(storedVideo);
		storedVideo = null;
	}
	else
	if( carouselInitialised == true ) getObject(CAROUSEL_ID).fl_incrementVideo( 1 );
	
}

/*
	FUNCTION NAME:
	
		js_viewProfile
	
	DESCRIPTION:
	
		Called by the videoPlayer when the 'view profile' button is clicked
	
	PARAMETERS:
	
		none
*/
function js_viewProfile()
{
	window.location = "/profile?teamid="+currentTeamID;
}

/*
	FUNCTION NAME:
	
		js_positionAddThis
	
	DESCRIPTION:
	
		Called by the videoPlayer to manually position the addThis div over the flash content.
		The location of the videoPlayer component must also be taken into account when calculating
		the voerall position.
	
	PARAMETERS:
	
		left : int - Number of pixels the addThis button is from the left edge of the flash movie. 
		top : int - Number of pixels the addThis button is from the top edge of the flash movie. 
*/
function js_positionAddThis(left, top)
{
	getObject(ADD_THIS_ID).style.top = (getObject(VIDEO_PLAYER_ID).offsetTop + top)+"px";
	getObject(ADD_THIS_ID).style.left = (getObject(VIDEO_PLAYER_ID).offsetLeft + left)+"px";
	
	
}

/*
	FUNCTION NAME:
	
		js_addThisRollOver
	
	DESCRIPTION:
	
		Called by the videoPlayer when the addThis button is rolled over
	
	PARAMETERS:
	
		none
*/
function js_addThisRollOver()
{
	
	if(addThisActive == false){
	//addthis_open(getObject(ADD_THIS_ID), '', '[URL]', '[TITLE]');		
		addThisActive = true;
		
	}
	
	
	
}

/*
	FUNCTION NAME:
	
		js_addThisClick
	
	DESCRIPTION:
	
		Called by the videoPlayer when the addThis button is clicked
	
	PARAMETERS:
	
		none
*/
function js_addThisClick()
{
	
	addthis_open(getObject(ADD_THIS_ID), '', '[URL]', '[TITLE]');		
	addthis_sendto();
}

/*
	FUNCTION NAME:
	
		js_addThisRollOut
	
	DESCRIPTION:
	
		Called by the videoPlayer when the addThis button is rolled out. Once the user has rolled
		off the addThis button we need to remove the dropdown if the user user doesn't roll over
		it within a certain time. The dropDown doesn't exist immediately so the checkDropDownExists()
		and addThisDropDownMouseOver() functions are necessary.
	
	PARAMETERS:
	
		none
*/
function js_addThisRollOut()
{
	checkDropDownExists();
}

/*
	FUNCTION NAME:
	
		setViewProfileEnabled
	
	DESCRIPTION:
	
		Enables or disables the View Profile button on the videoplayer
	
	PARAMETERS:
	
		enabled : Boolean
*/
function setViewProfileEnabled(enabled)
{
	if(videoPlayerInitialised == true)
	{
		getObject(VIDEO_PLAYER_ID).fl_setProfileEnabled( enabled );
	}
}

function checkDropDownExists()
{
	var addThisDropDown = getObject("at20mc");
	if( addThisDropDown == null )
	{
		addThisCreationTimer = setTimeout("checkDropDownExists()", 5);
	}
	else
	{
		addThisDropDown.onmouseover = addThisDropDownMouseOver;
		addThisRollOverTimer = setTimeout ( "addthis_close()", 1000 );
	}
}

function addThisDropDownMouseOver()
{
	clearTimeout ( addThisRollOverTimer );
}

function getObject(id)
{
	var object = null;
	if( document.layers )
	{   
		object = document.layers[id];
	}
	else if( document.all )
	{
		object = document.all[id];
	}
	else if( document.getElementById )
	{
		object = document.getElementById(id);
	}
	
	return object;
}