/**********************************************************
 **********************************************************
 * vxWidgets.js
 * 
 * Various functions and processes used by widgets
 *
 * THIS IS IMPLEMENTATION
 **********************************************************
 **********************************************************/

 
 
addEventListener(vxEvents.GET_USER_STATUS, 			onGetUserStatus);
addEventListener(vxEvents.COMPONENT_READY, 			onComponentReady); //EventManager.js > onComponentReady()
addEventListener(vxEvents.APPLICATION_INITIALIZED, 	onAppInit);
addEventListener(vxEvents.URL_CLICK,				onUrlClick);
//addEventListener(vxEvents.URL_OVER,					onUrlOver);

function onGetUserStatus(eventType, payload, source)
{
	debug("vxWidgets: onGetUserStatus");
	var userInfo = 
	{
		status:		getCookie("UID") && getCookie("username") ? 1 : 0,
		uid: 		getCookie("UID"),
		username: 	getCookie("username")
	}
	
	vxBroadcast(vxEvents.USER_STATUS, userInfo, "vxWidgets.js")
}

function onAppInit(eventType, payload, source)
{
	//Load default Program
	if(getQueryStringParam("fr_story"))
	{
		var sProgramUid = getQueryStringParam("fr_story");
		vxBroadcast(vxEvents.LOAD_PROGRAM, { programUid: sProgramUid }, "vxWidgets.js");
	}
}

function onUrlClick(eventType, payload, source)
{
	
	// Find Out What is in Payload looking for a StoryID seems it does not have one
	// We Should add the ProgramID to the payload and the Channel perhaps.
	var temp = "";
	
	for (x in flashvars)
        	temp += x + ": " + flashvars[x] + "\n";
	
	//alert("FlashVars Object " + temp);

	// Check if the URL is the LIVE SITE POPOUT will change URL to string LIVEPOPOUT
	if ( payload.url == "http://www.tribecafilm.com/virtual/live/" )
	{
		// Stop Current Video Playing in the base window to stop double audio
		getObjectByName("player"). pbPauseVideo();

		// Open Live Window POPOUT URL
		popupPlayerWithChat(payload.programUid);	
	
	}
	else {	
		window.open(payload.url);
	}
}


function popupPlayerWithChat(story)
{

	// *******************************************************
   	// Using the FlashVars the initial local variables.
	// *******************************************************

	var url = "http://www.tribecafilm.com/virtual/live/?";
  
   	//var channel = "fr_chl="+flashvars.ChannelID;
   	var story = "fr_story="+story

   	var site = flashvars.siteType;
   	var sk = flashvars.SkinName;
 
  	try {

   		//var newUrl = url+channel+"&"+story+"&"+site+"&"+sk;
   		var newUrl = url+story;//+"&"+site+"&"+sk;

   		//console.log("Popup URL: " + newUrl );
   		window.open(newUrl, "Tribeca Film Festival", 'height=1600,width=880,status=no,toolbar=no,menubar=no,location=0,scrollbars=0');

   	}
   	catch(err){
      		console.log("Error in popupPlayerWithChat: "+err);
   	}

}

 function setTheStoryID(strStoryID)
 {
  	var oComponent;
  	var strStoryID;
  	
  	oComponent = getObjectByName("player");
  	if (oComponent != null)
  	{
  		//strStoryID = document.getElementById ('txtStoryID').value;
  		//console.log("sending storyID to flash: " + strStoryID+ "typeof: "+typeof strStoryID);
  		oComponent.pbSetStoryID (strStoryID);
  	}
        else{
           //console.log("flash object not found (player)");
        }
 
}

function EmbedLiveChatWindow(programID,isMod, width, height)
{

	//alert(isMod);

	var userInfo = 
	{
		status:		getCookie("UID") && getCookie("username") ? 1 : 0,
		uid: 		getCookie("UID"),
		username: 	getCookie("username")
	}
	
	if ( userInfo.uid == false ) {
	}

	// Use the UID and The username to SSO into the Live Chat.
	// Replace SID with Story ID.
	// Default Size is 854 x 200

	var chatUrl;

	if ( userInfo.uid == false || userInfo.username == false )
	{
		// Provide without the User ID
		userInfo.uid = 0;
		userInfo.username = "Guest";
		chatUrl = '<scr'+'ipt type="text/javascript" charset="utf-8" src="http://static.juzou.tv/andy/chat_js.php?sname=tff-integration&width='
			+ width +'&height=' + height +'&sid=' + programID + '&uid='+ userInfo.uid + '&uname='+ userInfo.username + '&ismod=' + isMod
			+'&ulink=http://www.tribecafilm.com&upic="></scr'+'ipt>';
	}
	else
	{
		chatUrl = '<scr'+'ipt type="text/javascript" charset="utf-8" src="http://static.juzou.tv/andy/chat_js.php?sname=tff-integration&width=' 
			+ width + '&height=' + height + '&sid=' + programID + '&uid='+ userInfo.uid + '&uname='+ userInfo.username + '&ismod=' + isMod
			+'&ulink=http://www.tribecafilm.com&upic="></scr'+'ipt>';
	}

	//alert(chatUrl);

	document.write(chatUrl);

}

/*
function onUrlOver(eventType, payload, source)
{
	debug("onUrlOver: " + payload.url);
	window.status = payload.url;
}
*/