// Copyright 2006-2008 ClickTale Ltd.
var P2CurIndex,P2SIDs;

function Player2(PID,UID,SID,SIDStr)
{
	P2SIDs=SIDStr.split(","); // get SIDs
	var l=P2SIDs.length,i;
	// get id of current rec
	for(i=0;i<l && P2SIDs[i]!=SID;i++);
	if(i==l)
	{
		alert("Error: SID not in SIDs");
		return;
	}

	P2CurIndex=i;
	PHSetText("P2TotalRec",l);
	// set rec list
	var sel=$('P2CurRecSel');
	sel.options.length=l;
	for(i=0;i<l;i++)
	{
		sel.options[i].text=i+1;
	}
	// make sure browser doesn't reset select on load
	YUIEvent.addListener(window,"load", P2UpdateCurRecSel );
	// update next/prev buttons
	P2OnRecChanged();
}

var P2bAutoPlayNext=false;
function P2SetCurRec(index)
{
	P2bAutoPlayNext=false;
	P2CurIndex=index;
	WebPlayer(PID,UID,P2SIDs[index]);	
	P2OnRecChanged();
}

function P2Prev()
{
	P2SetCurRec(P2CurIndex-1);
}

function P2Next(bAutoPlay)
{
	P2bAutoPlayNext=bAutoPlay; // save value to next OnPlaybackReadyEvent
	//alert(P2bAutoPlayNext+'a'+bAutoPlay);
	
	WebPlayer(PID,UID,P2SIDs[++P2CurIndex]);	
	P2OnRecChanged();
}

function P2OnRecChanged()
{
	// update next/prev buttons
	ButtonDisable("P2PrevBut", P2CurIndex==0 );
	ButtonDisable("P2NextBut", P2CurIndex==P2SIDs.length-1 );
	// update index
	PHSetText("P2CurRec",P2CurIndex+1);
	P2UpdateCurRecSel();
	// update read status
	if(!OFFLINE_PLAYER)
		YUIConnect.asyncRequest('GET', 'RecordingsMarkRead.ashx?PID='+PID+'&UID='+UID+'&SID='+P2SIDs[P2CurIndex] /*, {failure: function() {alert('RecordingsMarkRead failed');} }*/ );
}

function P2UpdateCurRecSel()
{
	$('P2CurRecSel').selectedIndex=P2CurIndex;
}

// show gantt overlay
function ShowGantt()
{
	/*
	Firefox:
	If putting a gantt on top of an iframe (playback frame), then the mouse input is blocked.
	To over come this, put the flash in an iframe of its own.
	*/
	$("P2GanttOverlay").style.display="block";

	/*
	if(!$("P2FlashGantt")) // if already there, keep it (note that then we have to kill it on next/prev recording event
	{
		var so = new SWFObject("ganttChart.swf", "P2FlashGantt", "800", "300", "7", "#FFFFFF");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque");
		//so.addParam("scale", "noscale");
		so.addParam("salign", "LT");
		so.addVariable("PID", PID);
		so.addVariable("UID", UID);
		so.write("P2GanttFrame");
	}
	*/
	if(!$("P2FlashGanttIframe")) // if already there, keep it (note that then we have to kill it on next/prev recording event
	{
		// note, that FireFox seems to reload the iframe anyway whitout going through this part
		$("P2GanttFrame").appendChild( h.IFRAME({src: "GanttIframe.htm?PID="+PID+"&UID="+UID, id: "P2FlashGanttIframe", width: 800, height: 300, scrolling: "no", frameBorder: "no", marginHeight: 0, marginWidth: 0 }) );
	}
}

// show gantt overlay
var GanttPanel;
function ShowGantt2()
{
	if(!GanttPanel)
	{
		GanttPanel=new YAHOO.widget.Panel("P2GanttPanel", 
								{ width:"810px", height:"340px", x: 20, y: 120,
								iframe: true,
								  /*fixedcenter:false, 
								  underlay:"shadow", */
								  close:true, 
								  draggable:true, 
								  modal:false/*, 
								  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.2} */
								  } 
								 );
		GanttPanel.setHeader("Time-line");
		GanttPanel.setBody("<div id='P2PanelContent'></div>");
		GanttPanel.render(document.body);

	/*
	Firefox:
	If putting a gantt on top of an iframe (playback frame), then the mouse input is blocked.
	To over come this, put the flash in an iframe of its own.
	*/
		/*
		var so = new SWFObject("ganttChart.swf", "P2FlashGantt", "800", "300", "7", "#FFFFFF");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque");
		//so.addParam("scale", "noscale");
		so.addParam("salign", "LT");
		so.addVariable("PID", PID);
		so.addVariable("UID", UID);
		so.write("P2PanelContent");
		*/
		
		$("P2PanelContent").appendChild( h.IFRAME({src: "GanttIframe.htm?PID="+PID+"&UID="+UID, id: "P2FlashGanttIframe", width: 800, height: 300, scrolling: "no", frameBorder: "no", marginHeight: 0, marginWidth: 0 }) );
	}

	GanttPanel.show();
}

// hide gantt overlay
function HideGantt()
{
	$("P2GanttOverlay").style.display="none";
}

// subscribe to events
PlaybackEndedEvent.subscribe(OnPlaybackEnded);
ReadyToPlayEvent.subscribe(OnReadyToPlay);

// event handler
function OnPlaybackEnded()
{
	if(myLogReader) YAHOO.log("OnPlaybackEnded: "+$('P2AutoAdvanceNextRec').checked);
	if($('P2AutoAdvanceNextRec').checked && P2CurIndex<P2SIDs.length-1)
		P2Next(true/*set for autoplay*/);		
}

function OnReadyToPlay()
{
	if(myLogReader) YAHOO.log("OnReadyToPlay: "+P2bAutoPlayNext);
	//alert('a');
	// first check that we can playback
	if(P2bAutoPlayNext && IsButtonEnabled($('PHPlayBut')))
		WPPlay();
}

// preferences
function P2SavePreferences()
{
	// collect data
	var o={
		AutoSkip: $('PHAutoSkip').value,
		Zoom: $('PHZoom').value,
		Speed: $('PHSpeed').value,
		AutoAdvance: $('P2AutoAdvanceNextRec').checked
		};
	// save
	createCookie('PlayPrefs',JSON.stringify(o),3650);

	alert('Done!');
}

function P2LoadPreferences()
{
	var o=readCookie('PlayPrefs');
	if(o)
	{
		try
		{
			o=JSON.parse(o); // can throw
			if(!o) throw "False";
			// read
			$('P2AutoAdvanceNextRec').checked=o.AutoAdvance;
			$('PHSpeed').value=o.Speed;
			$('PHAutoSkip').value=o.AutoSkip;
			$('PHZoom').value=o.Zoom;
		}
		catch(err)
		{}
	}
}
