/* Javascript document */
var activePresentationStatus = null;

function init(){
Presentation = PresentationList.Current();
Presentation.Open();

//CreatePresentationList("hoverDivContents");

}
function ajaxSetProperty(t,id,p,v){
	agent.call('_ajax.php','usersetvalue',null,t,id,p,v);
	return(false);
}
function displayCompatibilityNotification(){
$("#notifications").append('<div style="padding:10px; font-weight:normal;"><p>Possible compatibility issues were found. <br>Please click <a style="color:#ffffff; font-weight:bold;" href="javascript:" onclick="$(\'#notifications\').hide(); displayCompatibilityResults(); return(false);">here</a> to view the compatibility test information.</p><a style="color:#ffffff" href="javascript:" onclick="$(\'#notifications\').slideUp(); return(false);">Close the notification [x]</a></div>').slideDown();	
}
function displayCompatibilityResults(){
	var testResult = '<p>Detected browser: '+ SystemEnvironment.getProperty('browsername')+'</p>'+
	  '<p>Browser version:'+SystemEnvironment.getProperty('browserversion')+'</p>'+
	 '<p>ActiveX supported:'+ ((SystemEnvironment.getProperty('activeXSupport')) ? "Yes" :"No") +'</p>'+
	 '<p>Media Player version:'+SystemEnvironment.getProperty('wmpversion')+' or later</p>';
if(!SystemEnvironment.getProperty('browsername').toLowerCase().match('explorer'))	 
	testResult += '<p>Media Player Plugin supported:'+SystemEnvironment.getProperty('wmppluginexists')+'</p>';
	 if(SystemEnvironment.getProperty('browsername').toLowerCase().match('firefox')) 
	 	testResult += '<p>Firefoxin Media Player Plugin installed: '+ ((SystemEnvironment.getProperty('firefoxWMPPluginSupport'))? "Yes" :"No")+'</p>';
	$("#resultText").append(testResult);
	//alert(testResult);
	var comment = '';
	$("#compatibilityResult").fadeIn();
	$("#compatibilityResult").css("background-position","right");
	$("#compatibilityResult").css("background-repeat","no-repeat");
	$("#compatibilityResult").css("text-align","left");
	if(SystemEnvironment.getProperty('isCompatible')==true){
		 comment += "<p><b>Your system meets the requirements.</p><p>You can continue with the presentations.";
	//	setTimeout("hideResults()",10000);
		}
	else {
				 comment +="<p><b>Possible problems were found:</b></p>";
				if(SystemEnvironment.getProperty('browsername').toLowerCase().match('firefox'))
				 comment +="<p>Please install the new MediaPlayer Plugin for Firefox browser to enjoy the best webcast experience! Click <a target=\"_blank\" href=\"http://magcast.magneetto.net/support/\">here</a> to open the download page.</p>";
				else  comment +="<p>We recommend Microsoft Windows operating system with Internet Explorer, Netscape (version 7 or later), or Mozilla Firefox (with Media Player Plugin version 11) -browser for the best webcast viewing experience.</p>";

		}
						comment +="<p>If you are experiencing problems, please contact <a href=\"mailto:support@magneetto.com\">support@magneetto.com</a> for technical support.</p>";
	$('#hoverDivContents').empty().append().load("formattedContent/hover.html",function(){setHoverContents.apply(this,['System compatibility check results',testResult+comment]);});
	return(false);

	}
	function hideResults(){
		$("#compatibilityResult").fadeOut();
		}
function getWebcastList(){
agent.call('_ajax.php','loadwebcastlist','setwebcastlist',categoryid,userid);
}
	function setwebcastlist(resultval){
		var Event = null;
		var b = 0;
		Events.loaded = true;
		//alert('list loaded:'+resultval);
		if(resultval && resultval != 'false'){
			for(b=0; b<resultval.length;b++){
				Event = Events.Insert();
				Event.ajaxSetProperties(resultval[b]);
				Event.setProperty('index',b+1);
				//alert(b +'<'+resultval.length);
				}
			Events.First();
			statusmessages.Add('Selected channel has '+Events.Count()+' events.');
			
		return(true);
			}//if
	}

function getCurrentWebcast(oID){
	if(oID!= null && oID != 'undefined') webcastid = oID;
//if(webcastid != null){
	agent.call('_ajax.php','loadwebcast','setcurrentwebcast',webcastid,categoryid,userid);
	statusmessages.Add('Loading current webcast information');
/*	}
else {
	//ChannelsView.Display();
CreateEventList('containercontent'); 
PresentationListView.Display();
	}
	*/
//setInterval('getUserCount()',30000);
//setTimeout('DisplayCompatibilityResults()',3000);
}
	function setcurrentwebcast(resultval){
	if(resultval && resultval != 'false'){
		Webcast.ajaxSetProperties(resultval);
		//alert(Webcast.getProperty('id'));
	statusmessages.Add('Webcast information loaded for event '+Webcast.getProperty('name')+'.');
	CreateEventList('leftCDiv');
	
	getPresentations();
	if(presentationToOpen && presentationToOpen != null)
	Webcast.setProperty('currentspeaker',presentationToOpen);
	Webcast.displayProperty('name','eventName');
		}
		
		}
function getPresentations(){
	if(Webcast.getProperty('id') && Webcast.getProperty('id') != -1){
		statusmessages.Add('loading presentations for the event..');
		agent.call('_ajax.php','loadpresentations','setpresentationlist',Webcast.getProperty('id'));
		}
	else alert('no Webcast selected');
	}
	function setpresentationlist(resultval){
	var current = null;
	if(resultval && resultval != 'false'){
		var i = 0;
		var Slidelist = new Array();
		PresentationList.Clear();
		for(i=0; resultval[i];i++){
			Presentation = PresentationList.Insert();
			Presentation.ajaxSetProperties(resultval[i]);
		//	Presentation.printProperties();
			Presentation.addListProperty('mediasources');
			Presentation.addListProperty('slidechanges');
			Presentation.getSlideChanges = getSlideChanges;
			Presentation.Open = openPresentation;
			 Slidelist[i] = Presentation.addListProperty('slides');
			Slidelist[i].parent = Presentation;
			Slidelist[i].getSlideList = getSlideList;
			Slidelist[i].setslidelist = setslidelist;
//			Presentation.getSlidelist();
//			Presentation.getMediaList();
			}//for
			PresentationList.First();
			PresentationList.GetItem(Webcast.getProperty('currentspeaker'));
			
			statusmessages.Add('Presentation list loaded ('+PresentationList.Count()+' presentations).');
			Presentation = PresentationList.Current();

			//alert('loaded '+PresentationList.Count()+ '('+i+') presentations');
			/*if(Presentation.getProperty('slides').IsEmpty())
				Presentation.getProperty('slides').getSlideList();*/
			init();			
		}//if
	else alert('unable to load presentation list for the selected webcast ' +Webcast.getProperty('name') );
	}
function getSlideList(){
	if(this.parent.getProperty('id')){
		agent.call('_ajax.php','loadslides','PresentationList.Current().getProperty("slides").setslidelist',this.parent.getProperty('id'));
	}
}
	function setslidelist(resultval){
		var currentSlide = null;
		if(resultval && resultval!= 'false'){
			for(slide in resultval){
				if(resultval[slide]['id']){
					currentSlide = this.Insert();
					currentSlide.ajaxSetProperties(resultval[slide]);
					}
				}
			if(getElement('slideControlsTable')) getElement('slideControlsTable').style.display='block';
			SlideControl.init();
			this.parent.getSlideChanges();	
			}
		else{
			statusmessages.Add('current presentations has no slide material.');
			if(getElement('slideControlsTable')) getElement('slideControlsTable').style.display='none';
			}
	//if(beta != true)	
	//if(getElement('statusmessageContainer'))getElement('statusmessageContainer').style.display='none';
	$("#statusmessageContainer:visible").hide();
		}//end function

	
	
function getSlideChanges(){
			if(this.getProperty('id')){
				statusmessages.Add('loading slide changes for presentation ' +this.getProperty('id'));
			agent.call('_ajax.php','loadslidechanges','slidesetchanges',this.getProperty('id'));
			}
			
		}
	function slidesetchanges(resultval){
		var currentSlide = null;
		if(resultval && resultval!= 'false'){
			
			for(slide in resultval){
				if(resultval[slide]['time']){
					//statusmessages.Add(resultval[slide]['time']);
					currentSlide = PresentationList.Current().getProperty('slidechanges').Insert();
					currentSlide.ajaxSetProperties(resultval[slide]);
					}//endif
				}//endfor
				statusmessages.Add('slide change information loaded, '+PresentationList.Current().getProperty('slidechanges').Count()+' changes defined ');
			}//endif
			else statusmessages.Add('no slide changes defined for this presentation')
		}//end function
		
function getMediasources(){
	if(PresentationList.Current().getProperty('id') != -1){
		if(PresentationList.Current().getProperty('mediasources').IsEmpty()){
			var sourceType = 'live';
			if((PresentationList.Current().getProperty('status')==4 )|| (Webcast.getProperty('status')>2)){
				sourceType = 'ondemand';
				statusmessages.Add('The event is available for on-demand viewing.');
				}
			else statusmessages.Add('Opening live feed.');
			agent.call('_ajax.php','loadmediasources','setmediasourcelist',Webcast.getProperty('id'),PresentationList.Current().getProperty('id'),sourceType,mediatype);
			} 
		}
	}
	function setmediasourcelist(resultval){
		if(resultval && resultval != 'false'){
		
		statusmessages.Add('Mediasourcelist loaded for current presentation');
			var i = 0;
			var MediaSourceList = PresentationList.Current().getProperty('mediasources');
			audioSourceID = null;
			videoSourceID = null;
			for(i=0; resultval[i];i++){
	
				MediaSource = MediaSourceList.Insert();
				MediaSource.ajaxSetProperties(resultval[i]);
				MediaSource.parent = PresentationList.Current();
				if(MediaSource.getProperty('mediatype')=='video'){
					if (videoSourceID == null)
						videoSourceID = MediaSource.getProperty('id');
					}
				else if(MediaSource.getProperty('mediatype')=='audio'){
					//alert('audio feed found');
					if(audioSourceID == null)
						audioSourceID = MediaSource.getProperty('id');
					}
				}
			MediaSourceList.First();
			PresentationList.Current().Open();
			}//if
		else statusmessages.Add('Could not load media sources for presentation '+PresentationList.Current().getProperty('showtitle'),4);
		}
function getHTMLSlide(sId){
var slideId = sId ? sId : PresentationList.Current().getProperty('slides').Current().getProperty('id');
	agent.call('_ajax.php','openhtmlslide','SlideControl.setHTMLContent',slideId);
	}
function chatSendLine(nametosend,linetosend){
		agent.call('../_ajax.php','chatsendline','chatlinesent',nametosend,linetosend);
	}
function chatlinesent(resultstatus){
	if(resultstatus=='true' || resultstatus==1){
		//alert('line sent');
		if(getElement('chat_send_header')){
//			getElement('chat_send_header').innerHTML = 'Viesti vastaanotettu.';
			$('#askResult').html('Message received.');
			jobqueue.addJob('getElement(\'chat_send_header\').innerHTML = \'Send a comment.\'',false,1,5000);
			
			}
		}
	else alert(resultstatus);
	}
function chatGetLines(){
	agent.call('_ajax.php','chatgetlines','Chat.AddLines');
	}

function getstatus(){
if(PresentationList.IsEmpty()) alert('The presentation list is empty..');
else {
	globalRowCount++;
	statusmessages.Add(globalRowCount+'. Presentationlist has '+PresentationList.Count() +' presentations. The current presentation is '+PresentationList.Current().getProperty('showtitle') + ' by ' + PresentationList.Current().getProperty('speaker') +'. It has ' + PresentationList.Current().getProperty('slides').Count() + ' slides.');
	statusmessages.Print();
	statusmessages.Trim(5);
//	alert(PresentationList.Current().getProperty('slides').Current().getProperty('number'));
	//alert(PresentationList.Current().printProperties());
	}
}
function geterrors(){
	errors.Print();
	}
function getUserCount(){
	
	agent.call('_ajax.php','getLoggedInUserCount','setusercount');
	}
	function setusercount(resultval){
	 $("#usercount").text(''+resultval);
	}
function openMaterialList(){
		$('#hoverDiv:visible').fadeOut();
	if(Webcast.getProperty('filecategory') == -1 || Webcast.getProperty('filecategory')== 0) {

		$("#"+MaterialListView.getProperty('objectID')).html('<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="padding:10px; background-color:#d6d3d1; color:#cf0545; font-weight:bold;">Oheismateriaalit</td></tr><tr><td style=" vertical-align:top; text-align:left; padding:10px;">No material was found relating to this event.</td></tr></table>');
		$('#hoverDiv:hidden').fadeIn();
		}
	else agent.call('_ajax.php','loadFileArchiveHTML','setmateriallist',Webcast.getProperty('filecategory'));
}
	function setmateriallist(resultval){
	//	alert(resultval);
		if(resultval && resultval != 'false'){
			if(MaterialListView){
				$("#"+MaterialListView.getProperty('objectID')).html('<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="padding:10px; background-color:#d6d3d1; color:#cf0545; font-weight:bold;">Related materials</td></tr><tr><td style=" vertical-align:top; text-align:left; padding:10px;">'+resultval+'</td></tr></table>');
				$('#hoverDiv:hidden').fadeIn();
				statusmessages.Add('List of related materials loaded.');
				}
				statusmessages.Add('Loading related materials list returned an error.');
			}
	}
function openIndexList(){
	agent.call('_ajax.php','GetIndexListHTML','setindexlist',PresentationList.Current().getProperty('id'));
	}
	function setindexlist(resultval){
	//	alert(resultval);
		if(resultval && resultval != 'false'){
			if(IndexListView){
				if(getElement(IndexListView.getProperty('objectID'))){
					getElement(IndexListView.getProperty('objectID')).innerHTML = resultval;
				}
			}
		}
	}
function ajaxSendShowOpened(eventid,showid){
	agent.call('_ajax.php','updateviewstatistics',null,eventid,showid);
	}
function ajaxUpdateShowViewCounter(showid){
		agent.call('_ajax.php','updateshowviewcounter',null,showid);
	}
function ajaxSendFeedback(topic,sender,line){
//DEBUG	alert('calling agent to send message '+topic+'\n'+line+' from '+sender);
		agent.call('_ajax.php','sendfeedback','feedbacksent',topic,sender,line);
		}
		function feedbacksent(resultstatus){
			alert('Your feedback has been sent. Thank you for your feedback!');
			statusmessages.Add('Feedback sent.');
			getElement('fb').reset();
			$('#hoverDiv').fadeOut(); 
			}
			
function ajaxSearch(param){
	
	agent.call('_ajax.php','search','CreateSearchResults',param);
}
function ajaxLog(message){
	if((message) && (message !="undefined"))
	agent.call('_ajax.php','alog',null,message);
	else return(true);
	}
	