/** 
* return a reference to a flash movie
*/
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


/**
* add to my street
* - open dialog
*/
function flash_addToMyStreet(param)
{
	
//	param = "/web/street/mystreet?p_p_lifecycle=0&p_p_id=MY_STREET&p_p_action=0&p_p_state=exclusive&_MY_STREET_struts_action=%2Fext%2FmyStreet%2Fadd_my_street&p_p_assetType=GAMES&p_p_uid=940298ee-14e4-11dd-83fd-1ff2199def8e&p_p_source=PLAYLISTOMATIC";
	
	// calling urching tracking code 

	flash_sendStatsUrl(lastStatsUrl + "/addtomystreet");
	 var userid =0;
	 day = new Date();
	 id = day.getTime();
	
	 if(document.playerForm.userId!=null)
	{
		 userid = document.playerForm.userId.value;
		 redirect = document.playerForm.redirect.value;
		 param = param.replace("maximized","normal");
		if(param.indexOf("source=PLAYLISTOMATIC")!=-1)
		{
			param = param + "&cmd=addPrefPlaylist";
			var message = Liferay.Popup(
				{
					width: 300,
					height: 150,
					modal: false,
					title: "Please name your playlist"
					
				});

			AjaxUtil.update(param, message, {onComplete: function() {Liferay.Popup.center()}});
		}
		else
		{
			param = param + "&cmd=addPref";
			if(userid==0)
			{
				param = param.replace("exclusive","maximized");
				param = param+"&redirect="+redirect;
				document.location.href=param;
			
			}
			else
			{
				param = param+"&redirect="+redirect;
				AjaxUtil.update(param);
			}
		}
		// calling urching tracking code  - success
		flash_sendStatsUrl(lastStatsUrl + "/addtomystreet-success");


	}




}


/**
* back to my street
*/
function flash_backToMyStreet(){
	alert("back to my street...");
}

/**
* play safe dialog
*/

function showPlaySafePopupMessage(){
	//GB_showCenter('Playsafe security prompt', '../../../../html_street/images/my_street/playsafe_prompt.gif', 260, 590)
	var message = Liferay.Popup(
	{
	width: 650,
	height:300,
	modal: true,
	title: "Playsafe security prompt"
	});

	url = "/sesameportlets4369/playsafe_securitywarning.jsp";

	AjaxUtil.update(url, message, {onComplete: function(){Liferay.Popup.center()}});
}

function flash_openPlaySafeDialog(param){
	//GB_showCenter("PlaySafe", "../../../../html_street/playsafe_dialog.html", 300, 350);

	
	//alert(param);

	var message = Liferay.Popup(
	{
	width: 350,
	height:300,
	modal: true,
	title: "PlaySafe"
	});


	redirect = "/playsafeInstallation";
	
	if(param!=null)
	{
		var paramData = param.split(";");

		if(paramData[0]=="playlist")
		{
			redirect = "/sesameportlets4369/playsafe_playlistplayer.jsp?uid=" + paramData[1];
	
		}else if(paramData[0]=="gameplayer")
		{
			redirect = "/sesameportlets4369/playsafe_gameplayer.jsp?uid=" + paramData[1];
	
		}
		
	}
	

	url = "/sesameportlets4369/playsafe_dialog.jsp?param="+escape(param)+ "&lastStatsUrl=" + lastStatsUrl ;

	AjaxUtil.update(url, message, {onComplete: function(){Liferay.Popup.center()}});
}



/**
* Share dialog
*/
function flash_openShareDialog(param){
	var param = p; 
//	GB_showCenter("Share", "/sesame-portlets-4.3.6.9/share_dialog.jsp", 300, 350);
	var displayUrl = "/sesame-portlets-4.3.6.9/share_dialog.jsp";
	if(param!="test")
	{
		var redirect = document.playerForm.redirect.value;
		displayUrl = "/sesame-portlets-4.3.6.9/share_dialog.jsp?sf_url="+redirect
	}
	var message = Liferay.Popup(
		{
			width: 350,
			height:300,
			modal: true,
		title: "Share"
				});

	url = displayUrl;

AjaxUtil.update(url, message, {onComplete: function(){Liferay.Popup.center()}});
}
/**
* Save Playlist dialog
*/
function flash_savePlaylistDialog(param){
	GB_showCenter("Save a Playlist", "../../../../html_street/add_mystreet_dialog.html", 200, 350);
}

/**
* download video dialog/functionality
*/
function flash_downloadAsset(param){
	alert("download..." + param);
}

/**
* create your own playlist (playlist landing page)
*/
function flash_createPlaylist(muppets,subject,theme,mediatype){
	alert("this is the flash_createPlaylist method.\n\nYou selected\nmuppets: "+muppets+"\nsubject: "+subject+"\ntheme:"+theme+"\nmedia type: "+mediatype);	
}


/**
* handle urchin stats calls from flash
* /**
* handle urchin stats calls from flash
* @param context - the player which called this method (PlaylistPlayer,GamePlayer,VideoPlayer + other variants)
* @param eventType - the event which occured
	"start" indicates asset has begun playback 
	"finish" means asset has completed playback normally. 
	"abort" indicates asset was terminated early)
* @param assetId - ID of affected asset (from XML)
* @param assetUrl - directLink URL (from XML)
* @param assetDuration - duration of playback in milliseconds (zero for a "start" event)
* @param assetTitle - title of asset (from XML)
*/
var statsWindow;
var playlistData={};
var lastStatsUrl="";

function flash_urchinStats(context, eventType, assetId, assetType, assetUrl, assetDuration, assetTitle, assetOrder){
	var statsUrl = flash_generateStatsUrl(context, eventType, assetId, assetType, assetUrl, assetDuration, assetTitle, assetOrder);
	lastStatsUrl = flash_generateStatsUrl(context, null, assetId, assetType, assetUrl, assetDuration, assetTitle);
	flash_sendStatsUrl(statsUrl);
}

function playsafeLaunchTrack(lastStatsUrl_new)
{
	//alert(lastStatsUrl_new);
	flash_sendStatsUrl(lastStatsUrl_new + "/playsafe")
}

function shareToAFriendTrack(lastStatsUrl_new)
{
	//alert(lastStatsUrl);
	flash_sendStatsUrl(lastStatsUrl + "/share-complete");

}


function flash_sendStatsUrl(statsUrl){
	pageTracker._trackPageview(statsUrl);
	flash_debugStats(statsUrl);
}



/**
* context is the player, eventType is either "impression" or "click", asset title is the PT title, url is the PT link URL
*/
function flash_parentTipUrchinStats(context, eventType, assetTitle, assetUrl){	
	var statsUrl = "/parenttip/" + eventType + "/" + flash_escapeTitle(assetTitle);
	flash_sendStatsUrl(statsUrl);
}

function flash_generateStatsUrl(context, eventType, assetId, assetType, assetUrl, assetDuration, assetTitle, assetOrder){	
	var topLevel=context;
	switch(context){
		case "FavoritesPlayer" : 
			topLevel="mystreet"; 
			break;
		case "PlaylistPlayer" : 
			topLevel="playlist"; 
			break;
		case "GamePlayer" : 
			topLevel="games"; 
			break;
		default :
			topLevel = "videos/"+context;
			break
	}
	
	// TEST -- TITLE REWRITE
	// just for kicks
	var titleArray = ["Sesame Street"];
	titleArray.push(topLevel);
	if(playlistData && playlistData.playlistName){
		titleArray.push(playlistData.playlistName);
	}
	titleArray.push(assetTitle);
	document.title = titleArray.join(" - ");
	// END TEST

	var statsArray = [topLevel];
	var statsUrl;	

	// if this event comes from playlist just store its name and abort
	if(assetType=="playlist" ){		
		playlistData.playlistName=assetTitle;
		statsArray.push(flash_escapeTitle(playlistData.playlistName));
		if(eventType!=null) statsArray.push(eventType);
		statsUrl = "/"+statsArray.join("/");		
		return statsUrl;
	}

	if(playlistData && playlistData.playlistName){
		statsArray.push(flash_escapeTitle(playlistData.playlistName));
	}
	if(assetOrder>=0) statsArray.push("p"+flash_zeroPad(assetOrder,2));
	statsArray.push(assetType);	
	statsArray.push(assetId + "--" + flash_escapeTitle(assetTitle));
	if(eventType!=null) statsArray.push(eventType);
	statsUrl = "/"+statsArray.join("/");	

	return statsUrl;
}




function flash_debugStats(str){
	try{
		document.getElementById("last-stats-call").value += ("\n"+str)
	}catch(e){}
}
function flash_escapeTitle(title){
	
	//alert(title);
	if(title==null)
	{
		title="noTitle";
		return title;
	}
	var rex = /[^a-zA-Z0-9_-]/g;
	var rex2 = /_{2,}/g
	var newTitle = title.split(" ").join("_");
	newTitle = newTitle.replace(rex,"").replace(rex2,"_");
	
	return newTitle;
}


function flash_zeroPad(num, digits) {
	var str = num+"";
	while(str.length < digits){
		str = "0"+str;	
	}
	return str;
}

/**
* handle a vote from the voting app
* @param assetId - ID of affected asset (from XML)
* @param assetTitle - title of asset (from XML)
*/
function flash_submitVote(assetId, assetTitle){

			var voteURL = "/home?p_p_id=votevideo_WAR_votevideoportlet&p_p_lifecycle=2&action=vote_video&videoId=" + assetId;
			
			AjaxUtil.update(voteURL);

	
			var message = Liferay.Popup(
				{
					width: 400,
					height: 450,
					modal: true,
					title: "Your submission was successful!"
				});
			url = "/html/voting_dialog.html";

			AjaxUtil.update(url, message, {onComplete: function(setAttributes) {Liferay.Popup.center()}});	
}

/**
* handle a vote again from the voting results app
*/
function flash_voteAgain(){
	parent.parent.document.location="/onair/history/vote";
}

function flash_viewResults(){
	parent.parent.document.location="/onair/history/voteresults";
}

/**
* handle a explore from the voting results app
*/
function flash_explore(){
	parent.parent.document.location="/browseallvideos";
}

/**
* handle a share from the voting results app
* @param assetId - ID of affected asset (from XML)
* @param assetTitle - title of asset (from XML)
*/
function flash_share(assetId, assetTitle){
	alert("Share!");
	var str = "SEND TO FRIEND: id=" + assetId + "  title=" + assetTitle;	
	flash_debugStats(str);
}

/**
* handle a "more like this" from the voting results app
* @param assetId - ID of affected asset (from XML)
* @param assetTitle - title of asset (from XML)
*/
function flash_more(assetId, assetTitle){
	parent.parent.document.location="/video_player?p_p_lifecycle=0&p_p_id=videoPlayer_WAR_sesameportlets4369&p_p_uid=" + assetId;
}

/**
* handle a week menu being selected from the voting results app
* @param week - week that is active
*/
function flash_weekMenu(week){

}
