
/**
 * The Flash Player embedded component does not go away with the modal popup.
 * Logic here will hide the component.  It is only created once on the page. 
 */
var currentPlayer;
function toggleHidePlayer(){
	
	if (learningCenterPanel!=null){
		learningCenterPanel.setBody(''); //remove player completely to stop it from playing
	}
	return true;
}
/**
 * This is the modal popup (aka lightbox) for the learning center.
 * It is a YUI Panel set to modal:true for the effect.
 * The YUI Panel is only created once, and the videoId is passed to this
 * function for the video/assets to load.
 */
var learningCenterPanel;
function displayLearningCenter(e, vidId){

	if (learningCenterPanel == null){
		learningCenterPanel = new YAHOO.widget.Panel("learningCenterPanel",  
                {
            	  y: 50,
  	  			  x: 200,
        		  width: "826px",
                  height: "536px", 
                  fixedcenter: false, 
                  close: true, 
                  draggable: false, 
                  zindex:999,
                  modal: true,
                  visible: false,
                  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
                } 
        );
	}
	learningCenterPanel.setBody('<div class="lrnctrlightboxbody"><div id="lrnctr"><br><br><br><br><br><img src="/img/ajaxloading.gif"/><br/><br/>Loading...</div></div>');
	learningCenterPanel.render(document.body);
	learningCenterPanel.beforeHideEvent.subscribe(toggleHidePlayer);
	learningCenterPanel.show();

	// Define the callback object for Connection Manager that will set the body of our content area when the content has loaded
    var callback = {
        success : function(o) {
    		if (isSessionValid(o.responseText)){
	    		learningCenterPanel.setBody(o.responseText);
  	  		}else{
  	  			self.location.href="/wclogin";
	  	  	}
    	
        },
        failure : function(o) {
        	learningCenterPanel.setBody(o.responseText);
        }
    };
    
    var videoPage = YAHOO.util.Dom.get("videoPage");
    var pageId = "4";
    var old = true;
    if (videoPage != null){
    	pageId = videoPage.value;
    }
   
	// Connect to our data source and load the data
    var conn = YAHOO.util.Connect.asyncRequest("GET", "/learningcenter/main/lightbox/"+vidId+"/"+pageId+"/"+old, callback);
    
}

function displayLMVideo(vidId){
	if (learningCenterPanel == null){
		learningCenterPanel = new YAHOO.widget.Panel("learningCenterPanel",  
                {
            	  width: "600px",
                  height: "350px", 
                  fixedcenter: true, 
                  close: true, 
                  draggable: false, 
                  zindex:999,
                  modal: true,
                  visible: false,
                  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
                } 
        );
	}
	learningCenterPanel.setBody('<br><br><br><br><br><img src="/img/ajaxloading.gif"/><br/><br/>Loading...');
	learningCenterPanel.render(document.body);
	learningCenterPanel.beforeHideEvent.subscribe(toggleHidePlayer);
	learningCenterPanel.show();

	// Define the callback object for Connection Manager that will set the body of our content area when the content has loaded
    var callback = {
        success : function(o) {
    		if (isSessionValid(o.responseText)){
	    		learningCenterPanel.setBody(o.responseText);
  	  		}else{
  	  			self.location.href="/wclogin";
	  	  	}
        },
        failure : function(o) {
        	learningCenterPanel.setBody(o.responseText);
        }
    };
       
	// Connect to our data source and load the data
    var conn = YAHOO.util.Connect.asyncRequest("GET", "/wc_home/lightboxAnonymous/"+vidId, callback);
    
}

/**
 * ajax calls for changing video category in the tabs and rating videos
 */
function changeVideoCategory(category,isNotAnonymous){
	if(isNotAnonymous=="")
		isNotAnonymous = true;
	// Define the callback object for Connection Manager
    var callback = {
        success : function(o) {    	
    		if (isSessionValid(o.responseText)){
    			if (o.responseText==1&&isNotAnonymous == true){
    				top.location.href="/wclogin";
    			}
			    var videoPreviewEl = YAHOO.util.Dom.get("videopreviewcontainer");
			    videoPreviewEl.src="/learningcenter/main/getVideosAJAX/"+category;
			
				//set tab css to change selected tab
			    var tabE1 = YAHOO.util.Dom.get("popmenu");
			    if (tabE1!=null)
			    	tabE1.className=(category=="POP")?'pop-selected':'pop';
				var tabE2 = YAHOO.util.Dom.get("admmenu");
				tabE2.className=(category=="ADM")?'adm-selected':'adm';
				var tabE3 = YAHOO.util.Dom.get("appmenu");
				tabE3.className=(category=="APP")?'app-selected':'app';
				var tabE4 = YAHOO.util.Dom.get("finmenu");
				tabE4.className=(category=="FIN")?'fin-selected':'fin';
				var tabE5 = YAHOO.util.Dom.get("paymenu");
				tabE5.className=(category=="PAY")?'pay-selected':'pay';
				var tabE6 = YAHOO.util.Dom.get("vismenu");
				tabE6.className=(category=="VIS")?'vis-selected':'vis';
				var tabE7 = YAHOO.util.Dom.get("knomenu");
				tabE7.className=(category=="KNO")?'kno-selected':'kno';
				var tabE8 = YAHOO.util.Dom.get("watmenu");
				if (tabE8!=null)
					tabE8.className=(category=="WAT")?'wat-selected':'wat';		   		
        
    			
    		}//end if else    		
    	},//end success
    	failure : function(o) {
    		//alert(o.responseText);
    		top.location.href="/wclogin";
    	}//end failure
    };//end callback 	
   
    //dummy ajax call
    var ts = new Date(); 
	var conn = YAHOO.util.Connect.asyncRequest("GET", "/basecontroller/resetTimeoutAjax/"+ts,callback);
}

/**
 * Called from iframe to change the video playing via AJAX
 */
function changeVideo(learningModuleGk){

	//show the user ajax loading symbol while calling via ajax
	var el1 = YAHOO.util.Dom.get("videoplayer");
	var el2 = YAHOO.util.Dom.get("moduleName");
	var el3 = YAHOO.util.Dom.get("moduleSubName");
	var el4 = YAHOO.util.Dom.get("publicDescription");
	var el5 = YAHOO.util.Dom.get("thumbNail");
	
	var thumbsDivText = YAHOO.util.Dom.get("thumbsDiv");
	var thumbsUpElement = YAHOO.util.Dom.get("thumbsUpElement");
	var thumbsDownElement = YAHOO.util.Dom.get("thumbsDownElement");
	var facediv = document.getElementById("facebook");
                        
	var pageTabViewed = YAHOO.util.Dom.get("watmenu");
	
	var pdflink= YAHOO.util.Dom.get("pdflink");
	
	var forumlinks = YAHOO.util.Dom.get("forumlinks");
		
	el1.innerHTML = "<center><img src='/img/ajaxloadingsmall.gif' /> Loading Video, Please Wait...</center>";
	el2.innerHTML = "";
	el3.innerHTML = "";
	el4.innerHTML = "";	
	
	if(forumlinks!=null)
	forumlinks.innerHTML="";
		
	if(thumbsDivText!=null){
		thumbsDivText = thumbsDivText.firstChild;
		thumbsDivText.nodeValue="";
	}
	
	if(thumbsUpElement != null)
	{
		thumbsUpElement.innerHTML = "";
	}
	
	if(thumbsDownElement != null)
	{
		thumbsDownElement.innerHTML = "";
	}
	
	pdflink.innerHTML="";
	if(facediv != null){
            facediv.innerHTML='';
        }
	
	// Define the callback object for Connection Manager
    var callback = {
        success : function(o) {    	
    		if (isSessionValid(o.responseText)){   
    			
    			var CSVresponse = o.responseText;
	    		var CSVarray = CSVresponse.split("|");		    		
	    		var linkLocation = CSVarray[0];
	    		var learningAssetGk = CSVarray[1];
	    		var moduleName = CSVarray[2];
	    		var moduleSubName = CSVarray[3];
	    		var publicDescription = CSVarray[4];
	    		var linkHardcopyLocation = CSVarray[5];
	    		var linkHardcopyDesc = CSVarray[6];
	    		var closeCaptionAsset = CSVarray[7];
	    		var streamer = CSVarray[8];	
	    		var rating = CSVarray[9];
	    		var viewed = CSVarray[10];
	    		var webpath =CSVarray[11] ;
	    		var backgroundImage =CSVarray[12] ;
	    		var forumLink=CSVarray[13] ;
	    		var length = CSVarray[14] ;
	    			    	   		
	    		loadVideoPlayer(linkLocation, streamer, closeCaptionAsset,webpath,backgroundImage,length);
    			
	    		el2.innerHTML = moduleName;
    			el3.innerHTML = moduleSubName;
    			el4.innerHTML = publicDescription;    			
				
    			if (rating!= 'anonymous'){
    			if (rating == 'N'){	
					thumbsUpElement.innerHTML = "<a href=\"#\" onclick=\"rateVideo("+learningModuleGk+",'1');return false;\"><img src=\"/img/gfx/thumbs_up.gif\" /></a>";
			   		thumbsDownElement.innerHTML = "<a href=\"#\" onclick=\"rateVideo("+learningModuleGk+",'1');return false;\"><img src=\"/img/gfx/thumbs_down.gif\" /></a>";
				   	}else{ 
				   	thumbsUpElement.innerHTML = "<i>You have already voted</i>";				   	
				   	}
				 }//end rating if/else.
    			    			
    			if (pageTabViewed!=null){
    				pageTabViewed.innerHTML="ALREADY VIEWED("+viewed+")";
    			}
    			
    			if(linkHardcopyLocation!=""){    				
    				//if a Video has more than one PDF associated with it
    				 var pdfArray = linkHardcopyLocation.split(",");
    				 var pdfDescArray = linkHardcopyDesc.split("   ");
    				 var allPDF = "<br><br>";
    				 for (var i = 0;i<pdfArray.length;i++)
    				 {    					
    					 allPDF +="<a href=\""+webpath+"/pdf/"+pdfArray[i]+"\" target=\"_blank\"> "+pdfDescArray[i]+"</a><br><br>";
    				 }//end for loop    				
    				 	    				
    				 pdflink.innerHTML=allPDF;    				
    			}else{
    				pdflink.innerHTML="";
    			}
    			
    			if(moduleName=="Privacy"){
    				pdflink.innerHTML="<a href=\"/privacy\" target=\"_blank\">View the privacy policy.</a>";
    			}
    			if(forumlinks!=null){
	    			forumlinks.innerHTML = "Did you find this helpful?<br/>Discuss it in the<a href=\""+forumLink+"\" target=\"_blank\"> community forums</a>";
    			}
    			else if(thumbsDivText!=null&&rating!="anonymous"){
    			thumbsDivText.nodeValue="Did You find this helpful?";
    			}
                        

                        //update the facebook meta-tags in the <head>
                        metatags = document.getElementsByTagName("meta");
                        for (cnt = 0; cnt < metatags.length; cnt++){
                            // Update the Title meta tag
                            if (metatags[cnt].getAttribute("property") == "og:title"){
                                  metatags[cnt].setAttribute("content", moduleName +": " + +moduleSubName);
                            }else if (metatags[cnt].getAttribute("property") == "og:description"){
                                  metatags[cnt].setAttribute("content", publicDescription);
                            }else if (metatags[cnt].getAttribute("property") == "og:video"){
                                fbvid=baseurl+ "flash/lm/player-licensed.swf?file=" +linkLocation+"&duration=" +lengthToSeconds(length)+ "&streamer=" +streamer+ "/cfx/st&captions.file=" +baseurl+ "caption/" +closeCaptionAsset+ "&captions.state=false&plugins=captions-1&image=" +webpath+ "/img/lmvideobackground/" +backgroundImage;
                                  metatags[cnt].setAttribute("content", "Better document title");
                            }
                        }
                       
                        if(facediv != null){
                            facediv.innerHTML='<iframe src="http://www.facebook.com/plugins/like.php?href='+baseurl+'college-learning-center/college-learning-videos/'+learningAssetGk+'&layout=standard&show_faces=true&width=450&action=recommend&font=arial&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>';
                        }
                        
    		}else{
      	  		self.location.href="/wclogin";
      	  	}//end session valid if/else
        	
        },//end success case
        failure : function(o) {
        	helpPopupPanel.setBody(o.responseText);
        }//end failure case
    };//end callback function
    
	// Connect to our data source and load the data
    var conn = YAHOO.util.Connect.asyncRequest("GET", "/learningcenter/main/getVideoAJAX/" + learningModuleGk, callback);
}//end change video function.

/**
 * resuable function to load the player into the videoplayer div
 */
function loadVideoPlayer(linkLocation, streamer, caption,webpath, backgroundImage,length){
	var expired =(linkLocation.substring(0,6));
	
	if(expired=="<html>"){
		self.location.href="/wclogin";
	}
		
	if (length==""){
		length=0;
	}
	else
	{
		var seconds = lengthToSeconds(length);
	}
	var el1 = YAHOO.util.Dom.get("videoplayer");
	var sa = new SWFObject('/flash/lm/player-licensed.swf','mp1','515','300','9');
	sa.addParam("allowfullscreen","true");
	sa.addParam("allowscriptaccess","always");
	if(caption!=""){
		sa.addParam("flashvars","file="+linkLocation+"&duration="+seconds+" &streamer="+streamer+"&captions.file=/caption/"+caption+"&captions.state=false&plugins=captions-1&image="+webpath+"/img/lmvideobackground/"+backgroundImage);//+"&plugins=viral-2"
	}else{
		sa.addParam("flashvars","file="+linkLocation+"&duration="+seconds+" &streamer="+streamer+"&image="+webpath+"/img/lmvideobackground/"+backgroundImage);
	}
	

	sa.write('videoplayer');	
}

function lengthToSeconds(length){
	
	var seconds;	
	var lengthArray = length.split(":");
	var splits = lengthArray.length;
	
	if (splits==2)
	{
		//MM:SS
		seconds =((parseInt(lengthArray['0'],10)*60)+ parseInt(lengthArray['1'],10));
	}
	
	if (splits==3)
	{
		//HH:MM:SS
		seconds =((parseInt(lengthArray['0'],10)*60*60)+ parseInt(lengthArray['1'],10)*60 + parseInt(lengthArray['2'],10));
	}

	return seconds;
}


/**
 * For video thumbnails, shows the video description using the little 'i' icon
 * @param elementId
 * @return
 */ 
function showVideoDescripion(elementId){
	var descriptionWrapperEl = YAHOO.util.Dom.get("descwrapper"+elementId);
	var descriptionEl = YAHOO.util.Dom.get("description"+elementId);
	descriptionEl.style.visibility="visible";
	descriptionWrapperEl.style.visibility="hidden";
}
function hideVideoDescripion(elementId){
	var descriptionWrapperEl = YAHOO.util.Dom.get("descwrapper"+elementId);
	var descriptionEl = YAHOO.util.Dom.get("description"+elementId);
	descriptionEl.style.visibility="hidden";
	descriptionWrapperEl.style.visibility="visible";
}
