//
//
var currentArchiveURL = "";
//
// Show comvu archive
// in player
//
function ShowArchiveStream( itemLink)
{
	content="";
	content += '<OBJECT id="player" name="player" height=294 width=300 border=2 classid=clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6 	borderColor="#cccccc" VIEWASTEXT>';
	content += '<PARAM NAME="URL" VALUE="'+itemLink+'">';
	content += '<PARAM NAME="autoStart" VALUE="-1">'
	content += '<embed type="application/x-mplayer2" id="player" name="player" src="'+itemLink+'" width="300" height="294" showcontrols="1" 	autostart="true"></embed>';
	content += '</OBJECT>';
	document.getElementById("videodiv").innerHTML = content;
//	HideArchiveStreamSrouce();
}

function HideArchiveStreamSrouce()
{
	document.getElementById("linkmanagerdiv").style.display="none";
}

function ShowArchiveStreamSource( itemLink)
{
	content  ='Select all the content below, copy/paste to embedd in a page:<BR>';
	content +='<form><textarea class="codecontainer" rows="10" name="S1" cols="35" wrap="virtual">';
	content += '<OBJECT id="player" name="player" height=294 width=300 border=2 classid=clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6 	borderColor="#cccccc" VIEWASTEXT>';
	content += '<PARAM NAME="URL" VALUE="'+itemLink+'">';
	content += '<PARAM NAME="autoStart" VALUE="-1">'
	content += '<embed type="application/x-mplayer2" id="player" name="player" src="'+itemLink+'" width="300" height="294" showcontrols="1" 	autostart="true"></embed>';
	content += '</OBJECT>';
	content += '</textarea></form>';
	content += '<a href="javascript:HideArchiveStreamSrouce()">Close</a>';
	document.getElementById("linkmanagerdiv").innerHTML = content;
	document.getElementById("linkmanagerdiv").style.display="";
}
//
//
function addTagArchive( linkid, tagDate )
{
	createArchiveTagForm( linkid, tagDate );
}
//
function createArchiveTagCancel()
{
	document.getElementById("tagmanagerdiv").innerHTML = "";
}
//
function createArchiveTagSubmit()
{
	tagDescription=document.newarchiveform.tagdescription.value;	
	linkid=document.newarchiveform.linkid.value;	
	url = DAOAPI_ADDARCHIVETAG+"&url="+currentArchiveURL+"&linkid="+linkid+"&tag="+escape(tagDescription);
	DAO( url, "OnArchiveTagged", null );
}
//
function deleteTagArchive( id )
{
	url = DAOAPI_DELETEARCHIVETAG+id+"&url="+currentArchiveURL;
	DAO( url, "OnArchiveTagged", null );	
}
//
//
var bAllCached 	= false;
var oCached 	= null;
//
var oSelectedDate=null;
//
//
function OnArchiveLoadStarting()
{
	document['button_archive'].src = "images/stream_archive_loading.png";
}
function OnArchiveLoadinfFinished()
{
	document['button_archive'].src = "images/stream_archive2.png";
}
//
//
function OnArchiveTagged( )
{
	GetAllArchivesByDate();
}
//
//
function OnAllArchiveLoaded( jsonArray )
{
		oCached 	= jsonArray;
		bAllCached 	= true;
		OnArchiveLoaded( jsonArray )
}
//
//
function SearchArchiveSelectedDate()
{
	//
	if ( oSelectedDate == null )
		oSelectedDate = new Date();
	oSelectedDate.setDate( 		document.dateselectionform.day.selectedIndex+1 		);
	oSelectedDate.setMonth( 	document.dateselectionform.month.selectedIndex	 	);
	oSelectedDate.setFullYear( 	document.dateselectionform.year.selectedIndex+2008 	);
	//
	GetAllArchivesByDate( );
}
//
//
function GenerateCalendarSelector()
{
	content  ='';
	content +='<form name="dateselectionform">';
	content +='<table width="300" border="0" cellspacing="0" cellpadding="0">';
	content +='  <tr>';
	content +='    <td><select name="month" size="1" id="month">';
	content +='      <option selected>January</option>';
	content +='      <option>February</option>';
	content +='      <option>March</option>';
	content +='      <option>April</option>';
	content +='      <option>May</option>';
	content +='      <option>June</option>';
	content +='      <option>July</option>';
	content +='      <option>August</option>';
	content +='      <option>September</option>';
	content +='      <option>October</option>';
	content +='      <option>November</option>';
	content +='      <option>December</option>';
	content +='    </select>';
	content +='    <select name="day" id="day">';
	content +='      <option selected>1</option>';
	content +='      <option>2</option>';
	content +='      <option>3</option>';
	content +='      <option>4</option>';
	content +='      <option>5</option>';
	content +='      <option>6</option>';
	content +='      <option>7</option>';
	content +='      <option>8</option>';
	content +='      <option>9</option>';
	content +='      <option>10</option>';
	content +='      <option>11</option>';
	content +='      <option>12</option>';
	content +='      <option>13</option>';
	content +='      <option>14</option>';
	content +='      <option>15</option>';
	content +='      <option>16</option>';
	content +='      <option>17</option>';
	content +='      <option>18</option>';
	content +='      <option>19</option>';
	content +='      <option>20</option>';
	content +='      <option>21</option>';
	content +='      <option>22</option>';
	content +='      <option>23</option>';
	content +='      <option>24</option>';
	content +='      <option>25</option>';
	content +='      <option>26</option>';
	content +='      <option>27</option>';
	content +='      <option>28</option>';
	content +='      <option>29</option>';
	content +='      <option>30</option>';
	content +='      <option>31</option>';
	content +='    </select>';
	content +='    <select name="year" id="year">';
	content +='      <option selected>2008</option>';
	content +='      <option>2009</option>';
	content +='      <option>2010</option>';
	content +='	  </select>';
	content +='	  &nbsp;<a  class="archiveheader" href="javascript:SearchArchiveSelectedDate()"><img border=0 align="middle" src="images/button_search.png"></a>';
	content +='	  </td>';
	content +='  </tr>';
	content +='</table>';
	content +='</form>';
	return content;
}
//
//
function OnArchiveLoaded( jsonArray )
{
	//document.getElementById("videodiv").innerHTML = "Rendering. Please Wait...";
	htmlheader="<div style='width:320px; height:400px; background-color:#ffffff; overflow:auto;' class='archiveheader'>";
	htmlheader+="<div id='linkmanagerdiv'></div>";
	htmlheader+="<div id='tagmanagerdiv'></div>";
	html="";
	
	if ( jsonArray != null )
	{
		//
		var oObjects = eval(jsonArray);
		var iCount = 0;
		//
		if ( !HasErrorProperty( oObjects) )
		{
			//
			for ( var oId in oObjects)
			{
				var oObject = oObjects[oId];	
//alert( oObject.m_date +" " + oObject.m_title );
				
				var oDate = new Date( oObject.m_date );
				//
				
				//
				if ( oSelectedDate != null )
				{					
					if ( 
						oDate.getMonth() != oSelectedDate.getMonth() ||
						oDate.getDate()  != oSelectedDate.getDate() ||
						oDate.getYear() != oSelectedDate.getYear() 
						)
							continue;
				}
				
				//
				//							
				title = oObject.m_title;
				if ( title.length > 20 )
				  title=title.substring(0,20);
				//
				tagHtml="";
				//
				tagHtml+="<a href='javascript:ShowArchiveStreamSource(\""+oObject.m_link+"\")'>[LNK]</a>"
				//
				if ( canTagArchive() )
				{				
					try
					{
					//alert( oObject.m_link );
						linkid = oObject.m_link.substr( oObject.m_link.indexOf('?')+1 );
						tagHtml+="&nbsp;<a href='javascript:addTagArchive("+linkid+",\""+oObject.m_date+"\")'>[TAG]</a>";
					}
					catch(Err){					
					}
				}
				//
				if ( oObject.m_tags != null )
				{
				//alert( oObject.m_tags );
					//
					for ( var oIdTag in oObject.m_tags)
					{
						tagHtml += "<li>"+ oObject.m_tags[oIdTag] +"</li>";
						
						if ( canDeleteTagArchive() )
						tagHtml+="&nbsp;<a href='javascript:deleteTagArchive("+oIdTag+")'>[DELETE TAG]</a>";
					}
				}
				//
				if ( iCount % 2 )
					TDH="<Td bgcolor='#BBDDFF' class='archiveheader'>";
				else
					TDH="<Td bgcolor='#AABBFF' class='archiveheader'>";	
				iCount++;				
				//	
				html="<table width=310><tr>"+TDH+
						"<a href='javascript:ShowArchiveStream(\""+oObject.m_link+"\")'>"+title+"("+oObject.m_date+")</a>"+tagHtml+"<BR>"+
						"</td></tr></table>"+
						html;			
				

			
			}// end for each archive entry
			
		}// end if no error in data access
	}
	//
	html+="</span>";
	//
	// document.getElementById("videodiv").innerHTML = htmlheader+html;	
	//
	//
	var inlinewin=
			dhtmlwindow.open("Archive", "inline", htmlheader+GenerateCalendarSelector()+html, "Archive Window", 
				"width=350px,height=420px,left=300px,top=340px,resize=0,scrolling=0", 
				"recal");
	//
	document.dateselectionform.day.selectedIndex 	= oSelectedDate.getDate() - 1;
	document.dateselectionform.month.selectedIndex 	= oSelectedDate.getMonth();
	document.dateselectionform.year.selectedIndex	= oSelectedDate.getFullYear()-2008;
	//
	OnArchiveLoadinfFinished();
}
//
//
function createArchiveTagForm( linkid, tagDate )
{
	html="<form name='newarchiveform' method='POST'><table>";
	html+="<input type='hidden' name='linkid' value="+linkid+">";

	html+="<tr>";
	html+="<td>Tag Date</td><td>"+tagDate+"</td>";
	html+="</tr>";

	html+="<tr>";
	html+="<td>Description</td><td><input type='text' name='tagdescription' value='Tag Description'></td>";
	html+="</tr>";

	html+="<tr>";
	html+="<td colspan=2><a href='javascript:createArchiveTagSubmit()'>SAVE</a> <a href='javascript:createArchiveTagCancel()'>CANCEL</a> </td>";
	html+="</tr>";

	html+="</table></form>";
	
	document.getElementById("tagmanagerdiv").innerHTML = html;
}
//
//
function GetArchiveByUrl( url )
{
	document.getElementById("videodiv").innerHTML = "Loading. Please Wait...";
	currentArchiveURL = url;
	DAO( DAOAPI_GETARCHIVEBYURL+url, "OnArchiveLoaded", null );
}
function GetAllArchives()
{
	//document.getElementById("videodiv").innerHTML = "Loading. Please Wait...";

	oSelectedDate=new Date();
	
	if ( bAllCached == false )
	{		
		DAO( DAOAPI_GETALLARCHIVES, "OnAllArchiveLoaded", null );		
	}
	else
	{
		OnArchiveLoaded( oCached )
	}
	currentArchiveURL = null;	
}
function GetAllArchivesByDate()
{
	OnArchiveLoadStarting();
	
	//document.getElementById("videodiv").innerHTML = "Loading. Please Wait...";

	if ( oSelectedDate == null )
		oSelectedDate=new Date();
	
	url = DAOAPI_GETALLARCHIVESBYDATE+"&day="+oSelectedDate.getDate()+"&month="+ (1+oSelectedDate.getMonth())+"&year="+oSelectedDate.getFullYear();

	DAO( url, "OnAllArchiveLoaded", null );
	
	currentArchiveURL = null;	
}
//
//
//