 var pUrl;
 var videoid;
 //code to gracefully handle errors
 //todo: add logging
 
 
 function blockError(){return true;}
//window.onerror=blockError;

 var URL='';
 var state=0;
 var position=0.0;
 var player=null; //wmplayer
 var p=null; //wmplayer.player
 var width = 356;
 var height = 339;
 var playcount=0;

 function checkDownloadTime(url){
 starttime = new Date().getTime();
 speedtestImage.onload = ImageLoaded;
 speedtestImage.src = url;
 }

 function ImageLoaded(){
 endtime = new Date().getTime();
 usedtime = endtime-starttime;
 }

 function PluginCheck(name){

 for ( var i in navigator.plugins ) {
 var n = navigator.plugins[i].name;
 //alert(n);
 if ( n != null && n.indexOf(name) >= 0 ) {
 return true;
 }

 }
 return false;
 }


function Video_Footer(fname,id)
{
  var VidFootHtml='';
  var vname="'"+fname+"'";
  var vid="'"+id+"'";

  VidFootHtml+='<table align="center"><tr><td><a onclick="playeronly('+vname+')">Player Only</a> | <a onclick="download('+vname+','+vid+')">Download</a> | <a onclick="sharevideo('+vname+','+id+')">Share</a></td></tr></table>';
  $('#video-footer').html(VidFootHtml);
  pUrl=vname;
}

//onclick="sharefriend('+vname+','+vid+')"
 function fullscreen(){
 if (p && p.playState==3) p.fullScreen=true;
 }


 function beforeunload()
 {

 if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1){

 if (p) {
 state=p.PlayState;
 position=p.controls.currentposition;

 }


 }

 else {

 newvideo('','');
 window.onbeforeunload=null;
 }
 }

 function resume(){
 if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1){
 if (p && state && state>2) {
 p.controls.currentposition = position;
 p.controls.play();
 }
 }
 }

 window.onbeforeunload = beforeunload;



 //reloads video and data for selected date/file
 function newvideo(filename,id){

	if (p) {
 player.stop();
 }

 if (filename==null || filename=="") return;

 //hiorlow = (high!="false"?"H":"L");

 var pp = document.getElementById("playerparent");
 pp.innerHTML="";
 if (filename!=null && filename!="") {
 videosource=filename;
 URL=filename;
 ID = id;
 player = new wmplayer(pp,URL,'wmplayer',width,height,ID);
 $.post("hitcounter.php",{video_id:id},function(data,status){});

 p = player.wmplayer;
 
 $('#CurrentVideoId').val(ID);
 
/* Last Blocked 
var divTag=$('#InfoWindow').val();
 
 show_info(divTag);      End*/
 
 /*
 if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1)
 {

 p.width = width;
 p.height = height;
 p.uiMode = "full";
 p.stretchToFit = "true";
 p.settings.setMode("autoRewind", "true");
 p.settings.setMode("showStatusBar", "true");
 p.settings.setMode("showFrame", "true");
 p.settings.autoStart="true";
 p.settings.enableErrorDialogs="false";
 player.load(URL);


 }
 //else {
 // player = new wmplayer(pp,URL,'wmplayer',width,height);
 //}
 */
 

 
 } else {
//if (p && p.close)
 //p.close();
 }
 
 }



 function wmplayer(p, filename, name, width, height,id) {
 if (!p) return;

 this.id = (name != undefined) ? name : "wmplayer";
 this.className = this.id;
 this.document = p.ownerDocument || p.document;
 this.container = p;
 this.container.player = this;
 this.ended = false;
 this.container.unselectable = "on";
 var html='';

 if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('MSIE') != -1)
 //IE on Windows
 {

 html = '<object width="' + width + '" height="' + height + '" id="' + this.id + 'control" ' +
 'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,1,52,701" ' +
 'standby="Loading..." ' +
 'type="application/x-oleobject"> ' +
 '<param name="url" value="' + filename + '">' +
 '<param name="FileName" value="' + filename + '">' +
 '<param name="stretchtofit" value="true">' +
 '<param name="autoStart" value="true">' +
 '<param name="EnableContextMenu" value="true">' +
 '<param name="ShowStatusBar" value="true">' +
 '<param name="showFrame" value="true">' +
 '<param name="uiMode" value="full">' +
 '</object> ';
 Video_Footer(filename,id);


 }
 else if (navigator.userAgent.indexOf('Macintosh') !=-1 && navigator.userAgent.indexOf('Safari')!=-1)
 {
 if (playcount==0) {
 html='<br><br><p class="linkFeatureText">Some features may not function as intended in Safari.<br><br>' +
 'The free <a href="http://www.mozilla.com" target="_blank" class="linkHelpText">Firefox</a> browser <br>(available from <a href="http://www.mozilla.com" target="_blank" class="linkHelpText">http://www.mozilla.com</a>)<br>' +
 '<br><br>and the free <a href="http://www.flip4mac.com" target="_blank" class="linkHelpText">Flip4Mac component for QuickTime</a><br> (available from <a href="http://www.flip4mac.com" target="_blank" class="linkHelpText">http://www.flip4mac.com</a>)<br><br>are recommended.</p>';
 }
 else{
 html = '<embed type="video/x-ms-wmx" ' +
 'pluginspage="http://www.microsoft.com/windows/windowsmedia/player/wmcomponents.mspx" ' +
 'width="' + width + '" height="' + height + '" ' +
 'src="' + filename + '" filename="' + filename + '" ' +
 'allowembedtagoverrides="True"' +
 'bgcolor="#000000"' +
 'enablejavascript="True" ' +
 'id="' + this.id + 'control" ' +
 'autoplay="True" scale="ASPECT"></embed>';
 }

 }
 else if (navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('Firefox') != -1)
 //Windows-FireFox
 {

 if (PluginCheck('Windows Media Player Firefox')) {
 html = '<object type="application/x-ms-wmp" ' +
 'codebase="http://port25.technet.com/videos/downloads/wmpfirefoxplugin.exe" ' +
 'data="' + filename +
 '" width="' + width + '" height="' + height + '">';
 '<param name="URL" value="' + filename + '">' +
 '<param name="stretchtofit" value="true">' +
 '<param name="autoStart" value="true">' +
 '<param name="EnableContextMenu" value="true">' +
 '<param name="ShowStatusBar" value="true">' +
 '<param name="showFrame" value="true">' +
 '<param name="uiMode" value="full">' +
 '</object> ';
 
 Video_Footer(filename,id);
 }
 else {

 html='<br><br><p class="linkFeatureText">Please <a href="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx" target="_blank" class="linkHelpText">' +
 'download the latest Windows Media Player<br> Plugin for FireFox.</a><br>' +
 '(You will need to close and restart <br> Firefox after installing the plugin)';
 }

 }
 else
 {
 html = '<embed type="application/x-mplayer2" ' +
 'pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
 'width="' + width + '" height="' + height + '" ' +
 'src="' + filename + '" filename="' + filename + '" ' +
 'enablejavascript="true" ' +
 'id="' + this.id + 'control" ' +
 'name="' + this.id + 'control" ' +
 'autostart="True" showcontrols="True" showstatusbar="1" showstatusbar="True" showdisplay="False" autorewind="True" scale="ASPECT"></embed>';
 
  Video_Footer(filename,id);
 
 }

 this.container.innerHTML = html;



 this.wmplayer = this.document.getElementById(this.id+"control");


 /*
 this.ponlybutton = this.document.createElement("DIV");
 this.ponlybutton.className = "playeronlybutton";
 this.ponlybutton.unselectable = "on";
 this.container.appendChild(this.ponlybutton);
 */
 if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('MSIE') != -1)
 {
 this.wmplayer.attachEvent("playstatechange",this.eventHandlers.playStateChanged);
 }
 playcount++;

 }

 //Prototype code
 player = wmplayer.prototype;

 player.className = "player_control";


 //Methods

 player.load =function(v){

 alert("loading");
 player.wmplayer.URL = v;



 }

 player.stop =function(){


 if (navigator.userAgent.indexOf('Macintosh') !=-1 ){

 //wmplayercontrol.Stop();


 }
 else if (player.wmplayer.controls) {
 player.wmplayer.controls.stop();
 }




 }

 player.eventHandlers = {
 playStateChanged: function (v) {


 var fullscreenbutton = document.getElementById('fullscreenimage');
 if (!fullscreenbutton){
 var fullscreenbutton = document.getElementById('fullscreenimage_disabled');
 }


 if (fullscreenbutton){
 if (v==3 ){

 fullscreenbutton.id = 'fullscreenimage';

 }
 else {

 fullscreenbutton.id = 'fullscreenimage_disabled';

 }
 }
 }
 
}

var highorlow = 'm';
cookieval = getCookie("PlayerSettings");
if (cookieval) {
 highorlow=cookieval.replace('bandwidth=',''); //replace to handle legacy format
}

function changebandwidth(sSpeed){
 // sSpeed = h or l
 highorlow=sSpeed; //highorlow=='h'?'l':'h');
 videoURL = pUrl;
 // are we viewing a live channel?
 //if (videoURL.indexOf("/iptv") >=0) {
 // we are viewing a live channel
 // 1. remove any existing bitrate parameter
 // 2. add the new bitrate parameter
 
 
 videoURL= URL.replace(/(\?WMBitrate=\d+)+/, '');
 switch (sSpeed) {
 case "m":
 videoURL = videoURL + '?WMBitrate=500000';
 break;
 case "l":
 videoURL = videoURL + '?WMBitrate=100000';
 break;
 case 'h':
 default:
 // leave as is without a parameter
 }
 //} else if (videoURL.indexOf("/cbn_live1") >=0) {
 // we are viewing Limelight's 
 //remove any existing bitrate parameter
 
 /*videoURL= URL.replace(/(\?wmbitrate=\d+)+/, '');
 switch (sSpeed) {
 case "m":
 videoURL = videoURL + '?wmbitrate=400000';
 break;
 case "l":
 videoURL = videoURL + '?wmbitrate=200000';
 break;
 case 'h':
 default:
 // leave as is without a parameter
 }*/
 /*} else {*/
 // we are viewing a video on demand
 if (sSpeed=='m'){
 videoURL = URL.replace(/_h?l?\.wmv(\?WMBitrate=500000)?/, '_h.wmv?WMBitrate=500000');
 } else{
 videoURL = URL.replace(/_h?l?\.wmv(\?WMBitrate=500000)?/, '_' + highorlow + '.wmv');
 }
 //}
// do we need to add the prefix to the video
 //if (videoURL.indexOf("http://61.246.140.9:90/ncn/index.php?VideoPath=") < 0) {
 //videoURL = 'http://61.246.140.9:90/ncn/index.php?VideoPath=' + videoURL;
// }
 newvideo(videoURL);
 setCookie("PlayerSettings",highorlow,365 );

}

/*var InfoHtml;
function show_info(divname)
{

    InfoHtml="";
    var IHtml='';
    $('#'+divname).html(IHtml);
    VideoId = $('#CurrentVideoId').val();

    $.get("info.php",{id:VideoId,remove_cache:new Date().getTime()},function (xml)
    {
     $("startvid", xml).each(function () {
                    var startvid = $(this);

                        Infoname = $(this).find('name').text();
                        Is_Info = $(this).find('is_info').text();
                        Infotitle = $(this).find('title').text();
                        Infodesc = $(this).find('description').text();
                        Inforelativelink1 = $(this).find('relativelink1').text();
                        Infodescrelativelink1 = $(this).find('desc_relativelink1').text();
                        Inforelativelink2 = $(this).find('relativelink2').text();
                        Infodescrelativelink2 = $(this).find('desc_relativelink2').text();
                        Inforelativelink3 = $(this).find('relativelink3').text();
                        Infodescrelativelink3 = $(this).find('desc_relativelink3').text();
                        Inforelativelink4 = $(this).find('relativelink4').text();
                        Infodescrelativelink4 = $(this).find('desc_relativelink4').text();

                        if(Is_Info == 1)
                        {
                          InfoHtml +='<table cellpadding="0" cellspacing="0" width="505px" border="0" id="info"	><tr><td class="info-header" colspan="2" style="background-color:#F5F5EB;height:60px;width:100%;"><img src="images/logo-ncnnew.gif" hspace="5" /></td></tr><tr><td class="info_title">'+Infotitle+'</td></tr><tr><td class="info-description">'+Infodesc+'</td></tr><tr><td class="info-description">Related Links</td></tr><tr><td class="bdr-bottom" colspan="2">&nbsp;</td></tr><tr><td class="watch-new" align="left" valign="top"><ul><li><a href='+Inforelativelink1+' class="watch-new">'+Infodescrelativelink1+'</a></li><li><a href='+Inforelativelink2+' class="watch-new">'+Infodescrelativelink2+'</a></li></ul></td><td align="left" valign="top" class="watch-new"><ul><li><a href='+Inforelativelink3+' class="watch-new"> '+Infodescrelativelink3+'</a></li><li><a href='+Inforelativelink4+' class="watch-new"> '+Infodescrelativelink4+'</a></li></ul></td></tr></table>';
                        }
                        if(Is_Info == 0)
                        {
                          InfoHtml +='<table cellpadding="0" cellspacing="0" width="505px" border="0" id="info"><tr><td class="info-header" colspan="2" style="background-color:#F5F5EB;height:60px;width:100%;"><img src="images/logo-ncnnew.gif" hspace="5" /></td></tr><tr><td class="info_title">Featured Videos</td></tr><tr><td class="info-description">You are currently watching a selection of featured videos.</td></tr><tr><td class="info-description">View the current schedule of <a onclick="clickfeatured()" class="watch-new">features.</a></td></tr><tr><td class="info-description">Related Links</td></tr><tr><td class="bdr-bottom" colspan="2">&nbsp;</td></tr><tr><td class="watch-new" align="left" valign="top"><ul><li><a href='+Inforelativelink1+' class="watch-new">'+Infodescrelativelink1+'</a></li><li><a href='+Inforelativelink2+' class="watch-new">'+Infodescrelativelink2+'</a></li></ul></td><td class="info_title" valign="top"><ul><li><a href='+Inforelativelink3+' class="watch-new">'+Infodescrelativelink3+'</a></li><li><a href='+Inforelativelink4+' class="watch-new">'+Infodescrelativelink4+'</a></li></ul></td></tr></table>';
                        }


              });


					 $('#'+divname).removeClass("ui-tabs-panel ui-tabs-hide");
					  $('#'+divname).addClass("ui-tabs-panel");
					var LastClkTabUpper=$('#LastClktabUpper').val();		
					
					
					 $('#'+LastClkTabUpper).hide();
					$('#'+LastClkTabUpper).removeClass("ui-tabs-panel");
					 $('#'+LastClkTabUpper).addClass("ui-tabs-panel ui-tabs-hide");
					
					var UpperSelected=LastClkTabUpper+'_upper';
					
					
					$('#'+UpperSelected).removeClass("ui-tabs-nav-item ui-tabs-selected");
					$('#'+UpperSelected).addClass("ui-tabs-nav-item");
					$('#'+divname+'_upper').removeClass("ui-tabs-nav-item ");
					$('#'+divname+'_upper').addClass("ui-tabs-nav-item ui-tabs-selected");
					
					
                     $('#'+divname).html(InfoHtml);

            });


}*/

function clickfeatured()
{
	
	
				var LastClkTabUpper=$('#InfoWindow').val();		
					$('#'+LastClkTabUpper).removeClass("ui-tabs-panel");
					 $('#'+LastClkTabUpper).addClass("ui-tabs-panel ui-tabs-hide");
					 $('#'+LastClkTabUpper).hide();
					 showfeatured();
					LastClkTabUpper = $('#FeaturedTabs').val();
			  
			    $('#'+LastClkTabUpper).removeClass("ui-tabs-panel ui-tabs-hide");
			   $('#'+LastClkTabUpper).addClass("ui-tabs-panel");
			    $('#'+LastClkTabUpper+'_upper').addClass("ui-tabs-nav-item ui-tabs-selected");
					
					 
}

function playeronly(video_name)
{
	var temp = video_name.split("//");
	window.location.href = "mms://"+temp[1];
}