/* Javascript document */

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

//CreatePresentationList("hoverDivContents");


}
function displayCompatibilityNotification(){
$("#notifications").append('<div style="padding:10px; font-weight:normal;"><p>Possible compatibility issues detected. <br>Click <a style="color:#ffffff; font-weight:bold;" href="javascript:" onclick="$(\'#notifications\').hide(); displayCompatibilityResults(); return(false);">here</a> to view more information.</p><a href="javascript:" onclick="$(\'#notifications\').slideUp(); return(false);">Close[x]</a></div>').slideDown();	
}
function displayCompatibilityResults(){
	var comment = '';
	var generalInfo = '<p><strong>System requirements</strong></p><p>The webcasts are best viewed with Internet Explorer (version 5.5 or later), Netscape (version 7 or later) or Mozilla Firefox browser running on Microsoft Windows (2000 or later, XP or Vista recommended).</p>'+
	'<p>Other operating systems and browsers may work, but we cannot guarantee 100% compatibility.</p>'+
	'<p>Your system configration is as follows:</p>';
	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.</b></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>";
	comment = generalInfo+testResult+comment;
	$('#hoverDivContents').empty().append().load("formattedContent/hover.html",function(){setHoverContents.apply(this,['Technical help',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.Clear();
		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
		//else alert(resultval);
	}

function getCurrentWebcast(oID){
	if(oID!= null && oID != 'undefined') webcastid = oID;
	
	agent.call('_ajax.php','loadwebcast','setcurrentwebcast',webcastid,categoryid,userid);
	statusmessages.Add('Loading current webcast information');
}
	function setcurrentwebcast(resultval){
	
	if(resultval && resultval != 'false'){
		Webcast.ajaxSetProperties(resultval);
		//alert(Webcast.getProperty('id'));
		statusmessages.Add('Webcast information loaded for event '+Webcast.getProperty('name')+'.');
		
		// Archive view not in use
		//CreateEventList('leftCDiv');
		// Event select changed to channel select
		//CreateEventSelect();
//		alert('Webcast information loaded for Webcast ID '+Webcast.getProperty('id'));
		getPresentations();
		//if(menuLoaded!=true) getPresentationsDisplayList();
		
		if(presentationToOpen && presentationToOpen != null)
		Webcast.setProperty('currentspeaker',presentationToOpen);
		Webcast.displayProperty('name','eventName');
		}
	}
function getPresentationsDisplayList(){
		agent.call('_ajax.php','loadpresentationsdisplaylist','setpresentationsdisplaylist',Webcast.getProperty('category'));
	}
	function setpresentationsdisplaylist(resultval){
	var current = null;
	if(resultval && resultval != 'false'){
		var i = 0;
		PresentationDisplayList.Clear();
		for(i=0; resultval[i];i++){
			current = PresentationDisplayList.Insert();
			current.ajaxSetProperties(resultval[i]);
			}//for
		PresentationDisplayList.First();
		CreateShortPresentationList("presentationlistcontainer");
		}//if
	else alert('unable to load presentation list for the selected webcast.' );
	}
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;
			Presentation.getIndexes = getIndexes;
			Presentation.addListProperty('indexlist');
			 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();

		CreateShortPresentationList("presentationlistcontainer");
			init();			
		}//if
	else alert('unable to load presentation list for the selected webcast.' );
	}
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';
			}
	$("#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 getIndexes(){
	if(!this.getProperty('indexlist').Count()){
				statusmessages.Add('loading index list for presentation ' +this.getProperty('id'));
			agent.call('_ajax.php','loadindexlist','setindexes',this.getProperty('id'));
			}
	else displayIndexSelect();
}
	function setindexes(resultval){
		PresentationList.Current().getProperty('indexlist').Clear();
		if(resultval && resultval!= 'false'){
			for(index in resultval){
				currentIndex =  PresentationList.Current().getProperty('indexlist').Insert();
				currentIndex.ajaxSetProperties(resultval[index]);
				}
		DisplayIndexSelect();
		}
	else $('#indexSelect').html('');
	}
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){
			$('#askResult').html('Message received.');
			jobqueue.addJob('$(\'#askResult\').html(\'\');',false,1,5000);
		}
	else $('#askResult').html('An error occurred while sending the message.');
	}
function chatGetLines(){
	agent.call('_ajax.php','chatgetlines','Chat.AddLines');
	}

function getstatus(){
if(PresentationList.IsEmpty()) alert('The presentationlist for current event 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').hide();
	if(Webcast.getProperty('filecategory') == -1 || Webcast.getProperty('filecategory')== 0) {

		$("#"+MaterialListView.getProperty('objectID')).load('formattedContent/materials.php',{content:'No materials found related to this event.'});
	//	$('#materialsContent').html();
		$('#hoverDiv').show();
		}
	else agent.call('_ajax.php','loadFileArchiveHTML','setmateriallist',Webcast.getProperty('filecategory'));
}
	function setmateriallist(resultval){
		if(resultval && resultval != 'false'){
			if(MaterialListView){
				$("#"+MaterialListView.getProperty('objectID')).load('formattedContent/materials.php',function(){setMListContents.apply(this,[resultval]);});

				$('#hoverDiv').show();
				statusmessages.Add('List of related materials loaded.');
				}
				else statusmessages.Add('Loading related materials list returned an error.');
			}
	}
		function setMListContents(resultval){$('#materialsContent').html(resultval);}
function openSlideList(){
	if((Presentation.getProperty('status')==4 )|| (Webcast.getProperty('status')>2))
		displayslidelist();
	}
	function setListContents(resultval){$('#slideContent').html(resultval);
//	$("#hoverDivContents").height($('#wrapper').height());
	//alert($('#wrapper').height());
		}
	function displayslidelist(resultval){

			$("#hoverDivContents").load('formattedContent/slidelist.php',{id:PresentationList.Current().getProperty('id')});
			$('#hoverDiv').show();
			$('#hoverDiv').corner({
          tl: { radius: 5 },
          tr: { radius: 5 },
          bl: { radius: 5 },
          br: { radius: 5 }
		  });
			statusmessages.Add('Slide thumbnail list loaded.');
		}

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 ajaxSetProperty(t,id,p,v){
	agent.call('_ajax.php','usersetvalue',null,t,id,p,v);
	return(false);
}
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 the 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);
	}
	