var prev_link = null;

function hide_msg(video_id)
{
  $("#msg_div_"+video_id).html("");
}

function close2(content_div)
{
  if(dojo.byId(content_div))
  {
    var n = dojo.byId(content_div);
	
    if(n.firstChild)
	{  
      n.removeChild(n.firstChild);
	}
  }
  

  prev_link = null; 
}

function close(content_div)
{
  dojo.byId(content_div).innerHTML = "";
  
  prev_link = null;
}

function loadPlayer(video_id, content_div) {

  dojo.xhrGet( { 
        // The following URL must match that used to test the server.
        url: "http://www.roch.edu/library/streaming/get_path2.php?video_id=" + video_id, 
        handleAs: "json",
        timeout: 10000, // Time in milliseconds
        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { 
              display_video(content_div, response.path, response.title, video_id); 
          return 1; 
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { 
          console.error("HTTP status code: ", ioArgs.xhr.status); 
          return 0; 
          }
        });
}

function display_video(content_div, path, ti, video_id)
{
  var div = dojo.byId(content_div);	  
  
  var main_div = document.createElement("div");	
	  
  div.appendChild(main_div);	  
  var video_div = document.createElement("div");	 
  var control_div = document.createElement("div");
	  
  main_div.appendChild(video_div);
  main_div.appendChild(control_div);
  document.title = ti;
  //dojo.query("title").value = "test";
 // alert("here");
	 
	  //var so = new SWFObject('http://www.roch.edu/facultylectureseries/resources/flvplayer.swf','mpl','320','260','7');
	  var so = new SWFObject('flvplayer.swf','mpl','320','260','7');
	
	  so.addParam("allowfullscreen","true");
	  so.addVariable("file",path);
	  so.addVariable("autostart","true");
	  so.addVariable("streamscript", "lighttpd");
	
	  so.write(video_div);
	
	 control_div.innerHTML = ("<a href='javascript:close(\""+ content_div+ "\")';>close</a>");
	 //control_div.innerHTML = ("<a href='javascript:close(\"content_div_" + video_id + "\")';>close</a>");
	
}

function get_video(video_id)
{
    var content_div = 'content_div_' + video_id;
  
    if(prev_link)
	{  
	  if(dojo.byId(content_div))
      {
        var n = dojo.byId(content_div);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 
	}
	
	if(prev_link == content_div)
	{
	  if(dojo.byId(content_div))
      {
        var n = dojo.byId(content_div);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 	
	  prev_link = null;
	}
	else
	{	
	   if(dojo.byId(prev_link))
      {
        var n = dojo.byId(prev_link);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 
	  
	  prev_link = content_div;
	  
	  if(on_campus == 1)
	  {
	    loadPlayer(video_id, content_div);
	  }
	  else
	  {
	    
	    var div = dojo.byId(content_div);	  
        var main_div = document.createElement("div");	
		
			 var str = "<div id='msg_div_"+video_id+"'><hr><center><font color='#FF0000'>This item is currently available on the UCR campus and from off-campus for RCTC students. The login is the same as your RCTC network/D2L account. WSU-R off-campus access is coming soon.</font></center><hr>";
     str += "<table><tr><th>User Name:</th><td><input id='uid' type='text' autocomplete='off'></td></tr><tr><th>Password:</th><td><input id='pwd' type='password' autocomplete='off'></td></tr><tr><td><button onclick='authenticate("+video_id+","+content_div+");'>Submit</button></td><td><button>Cancel</button></td></tr><tr><td colspan=2 id='auth_response'> </td></tr></table></div>";
	
		
	    div.innerHTML = str;
	   
	  }
	}
	
}

function authenticate(video_id)
{  

  var uid = ($('#uid').val()).toLowerCase();
  var pwd = $('#pwd').val();

  if(pwd)
  {
	var content_div_s = '';
    var url = "https://www.roch.edu/apps/authentication/ad_auth_jsonp.php?uid="+uid+"&pwd="+escape(pwd)+"&video_id="+video_id+"&content_div="+content_div_s;


    //When AJAX request returns, it will call 'handle_authentication_response(response)'
    $.ajax({  
      url: url,
	  cache: false,
      type: "GET",
      async: false,
      dataType: 'jsonp' 
    });
  }
  else
    alert("you failed to enter a password!");
    
  return false;
} 


function authenticate_subj(video_id,content_div_s)
{  

  var uid = ($('#uid').val()).toLowerCase();
  var pwd = $('#pwd').val();

  if(pwd)
  {
	//var content_div_s = 'content_div_' + video_id;
    var url = "https://www.roch.edu/apps/authentication/ad_auth_jsonp.php?uid="+uid+"&pwd="+escape(pwd)+"&video_id="+video_id+"&content_div="+content_div_s;


    //When AJAX request returns, it will call 'handle_authentication_response(response)'
    $.ajax({  
      url: url,
	  cache: false,
      type: "GET",
      async: false,
      dataType: 'jsonp' 
    });
  }
  else
    alert("you failed to enter a password!");
    
  return false;
} 




function handle_authentication_response2(response)
{
	alert(3)
  
  
} 


function handle_authentication_response(response)
{
	
   //alert(response.video_id+" "+response.content_div)
  if(response.result == '1')
  {
	var d = "content_div_"+response.video_id;
	
	if(response.content_div)
	  d = response.content_div;
	  
    loadPlayer(response.video_id, d);
	//loadPlayer(49, "content_div_49");
	hide_msg(response.video_id);
  
  }
  else
    $("#auth_response").html("<font color='#FF0000'><b>You entered invalid credentials</b></font>");
  
} 


function authenticate2()
{
	//var s =$("#username").val();

  //alert($("#password").val());
   var valid=0;
   if(valid)
    loadPlayer(video_id, content_div);
  else
  	$("#auth_response").html("You entered invalid credentials");
}

function get_video_subj(video_id, content_div)
{
   // var content_div = 'content_div_' + video_id;
  
    if(prev_link)
	{  
	  if(dojo.byId(content_div))
      {
        var n = dojo.byId(content_div);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 
	}
	
	if(prev_link == content_div)
	{
	  if(dojo.byId(content_div))
      {
        var n = dojo.byId(content_div);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 	
	  prev_link = null;
	}
	else
	{	
	   if(dojo.byId(prev_link))
      {
        var n = dojo.byId(prev_link);
	
        if(n.firstChild)
	    {  
          n.removeChild(n.firstChild);
	    }
      } 
	  
	  prev_link = content_div;
	  
	  if(on_campus == 1)
	  {
	    loadPlayer(video_id, content_div);
	  }
	  else
	  {
	   // var content_div = 'content_div_' + video_id;
	    var div = dojo.byId(content_div);	  
        var main_div = document.createElement("div");	
	     var str = "<div id='msg_div_"+video_id+"'><hr><center><font color='#FF0000'>This item is currently available on the UCR campus and from off-campus for RCTC students. The login is the same as your RCTC network/D2L account. WSU-R off-campus access is coming soon.</font></center><hr>";
     str += "<table><tr><th>User Name:</th><td><input id='uid' type='text' autocomplete='off'></td></tr><tr><th>Password:</th><td><input id='pwd' type='password' autocomplete='off'></td></tr><tr><td><button onclick='authenticate_subj("+video_id+",&#39;"+content_div+"&#39;);'>Submit</button></td><td><button>Cancel</button></td></tr><tr><td colspan=2 id='auth_response'> </td></tr></table></div>";
	    div.innerHTML = str;
	   
	  }
	  //loadPlayer(video_id, content_div);	   
	}
}