// It's only for the Moli view player of the Small Biz Center Home Page and Business News pages only - Devi.
VideoArray = new Array();
AudioArray = new Array();
var vidCnt;
var audCnt;
var indx;
var aud0;
var vid0;
var current_type;
var domain;
var audli=0;
var so;
var prevPlayer = "";


function updateTrackingPixel()
{

  var url = "/a/business_resource_center//updatemem" ; 
   var ajaxobj = new Ajax.Request (
     url, 
     {    
       method: 'post',
       asynchronous: false,
       onSuccess: function(transport) {
		   updateAllAds (transport.responseText ); 
       }
     }    
    );     
}


function updateAllAds( info ) 
{
  var adinfo = info.split("," ) ; 

  prerollad =   adinfo[1]; 
  trackingPixel = adinfo[2]; 
  document.getElementById( 'iframe_2' ).src = adinfo[0]; 
return;
 writeTrackPixel(); 

}

function writeTrackPixel() 
{

    if ( trackingPixel.length ) { 
      // parent element
      var prnt = document.getElementById('masthead')
      var img = ""; 

      if ( document.getElementById('trckpixel' )  ) { 
        img = document.getElementById('trckpixel' ) ;  
        img.src = trackingPixel; 
        img.style.visibility  = "hidden"; 

      } else { 
        img = new Image(); 
        img.src = trackingPixel; 
        img.style.visibility  = "hidden"; 
        img.setAttribute('id', 'trckpixel' ) ; 
        prnt.appendChild( img ) ; 

      }
    }



}

function sendfiles (videos,audios,domain1) {
  VideoArray =videos;
  AudioArray =audios;
  vidCnt = VideoArray.length-1;
  audCnt = AudioArray.length-1;
  domain=domain1;
}

function toggleTabs(activediv, inactivediv,file) {


  if (document.getElementById(activediv).style.visibility == "hidden") {
   
   
    document.getElementById(activediv).style.visibility = "visible";
	
	
  }
  document.getElementById(activediv).style.display = "";
  document.getElementById(inactivediv).style.display = "none";
   
}

function clearPlayer() {

  document.getElementById("flashPlayer").style.display = "none";
  document.getElementById("quickPlayer").style.display = "none";
  document.getElementById("realPlayer").style.display = "none";
  document.getElementById("mediaPlayer").style.display = "none";
 
   if (document.getElementById("main")) {
	
	document.getElementById("flashPlayer").innerHTML = "";

  }   

  if (document.getElementById("objMediaPlayer")  ) {
	document.getElementById("mediaPlayer").innerHTML = "";

  } 
}
function playAd(adSrc,ind,type,status, load ) {


  if ( $('DESC_POPUP') ) { 
    hide_balloon($('DESC_POPUP'),true); 
  }

  var load = (load ) ? load : null;
  var nextImg; 

  indx=ind;
  current_type=type;
  //need to comment when adPlayer is implemented  --- Start
  medianum = indx; 
if(current_type=='video') {
  swapStyle('video'+indx, 'videolist'); 
  $('dynamic_title').innerHTML = stripslashes(VideoArray[indx][3]); 
 

  if (load  == null) { 
    var nextone = ( VideoArray.length == indx +1 ) ? indx : indx + 1   ; 
    playlist = "playAd(\'ext_images/mvvideo/scooter.flv\'," + nextone  + ",\'video\',\'true\'); holder=" + VideoArray[nextone][4] +  ";";
    //$('media_comment_title').innerHTML = VideoArray[indx][3]; 

    mediatype = 'video'; 
    nextImg =  $('up_next_conent').getElementsByTagName('img'); 
    

    //$('up_next_title').innerHTML = stripslashes(VideoArray[nextone][3]);

    $('up_next_title').innerHTML = stripslashes(VideoArray[nextone][4]);

    $('up_next_date').innerHTML  = VideoArray[nextone][5] ;   

	if (document.getElementById("up_next_byline")  ) {
      $('up_next_byline').innerHTML  = stripslashes(VideoArray[nextone][6]) ;
	}




    //$('media_comment_title_href').href= VideoArray[indx][7] ;
    //$('media_comment_title_href').innerHTML= VideoArray[indx][6] ;

     if ( nextImg[0] ) { nextImg[0].style.background =   "url(" + VideoArray[nextone][0] + ")"  ;  }  
  }


  if ( prerollad.length ) { 

    writeTrackPixel(); 

    document.getElementById("flashPlayer").style.display = ""
    var imgSrc = "http://"+VideoArray[0][2]+ VideoArray[0][8];
    //adSrc = "http://"+VideoArray[0][2] + prerollad; 
    adSrc = prerollad; 
    var so = new SWFObject("/moliview/player/MoliViewAdPlayer.swf", "main", 300, 219, "8", "#000000");
    so.useExpressInstall("/moliview/player/javascript/swfobject/expressinstall.swf");
    so.addParam("allowScriptAccess", "always");
    so.addParam("allowFullscreen", "true");
    so.addParam("flashVars",  "image="+imgSrc+ "&" + streaminglist + "&adSrc="+adSrc+"&status="+status+"");
    so.write("flashPlayer");

  } else { 

    if(status == 'false')
      buildPlayer(false, VideoArray[indx][8], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);
    else
    buildPlayer(true, VideoArray[indx][8], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);
  }

}
	
	else {	

    //$('dynamic_title').innerHTML = AudioArray[indx][3]; 

    /*if ( load == null) { 
    mediatype = 'audio'; 
    var nextone = ( AudioArray.length == indx +1 ) ? indx : indx + 1   ; 
    playlist = "playAd(\'ext_images/mvvideo/scooter.flv\'," + nextone  + ",\'audio',\'true\'); holder=" + AudioArray[nextone][4] +  ";";
    //$('media_comment_title').innerHTML = AudioArray[indx][3]; 
    nextImg =  $('up_next_conent').getElementsByTagName('img'); 
    
    $('up_next_title').innerHTML = AudioArray[nextone][3];
    $('up_next_date').innerHTML  = AudioArray[nextone][5] ;   
    $('up_next_byline').innerHTML  = AudioArray[nextone][6] ;   

    //$('media_comment_title_href').href= AudioArray[indx][7] ;
    //$('media_comment_title_href').innerHTML= AudioArray[indx][6] ;

     if ( nextImg[0] ) { nextImg[0].style.background = "url(" +  AudioArray[nextone][0] + ")";  }  }

	  swapStyle('audio'+indx, 'audiolist'); 
 	  if(status == 'false')
	  buildPlayer(false, AudioArray[indx][8], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
    else
    buildPlayer(true, AudioArray[indx][8], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); */
	
	}

	//need to comment when adPlayer is implemented	--- End
         
/*  var imgSrc = "http://"+VideoArray[0][2]+ VideoArray[0][0];
  adSrc = "http://"+VideoArray[0][2] + "/ext_images/mvvideo/scooter.flv";
  alert(indx + "   " + current_type + "    " + "image="+imgSrc+"&adSrc="+adSrc+"&status=false")
  document.getElementById("flashPlayer").style.display = "";
  
  var so = new SWFObject("http://www0.semantic1.covibe.com/moliview/player/MoliViewAdPlayer.swf", "main", 300, 219, "8", "#000000");
  so.useExpressInstall("http://www0.semantic1.covibe.com/moliview/player/javascript/swfobject/expressinstall.swf");
  so.addParam("allowScriptAccess", "always");
  so.addParam("allowFullscreen", "true");
  so.addParam("flashVars",  "image="+imgSrc+"&adSrc="+adSrc+"&status="+status+"");
  so.write("flashPlayer"); */
}

/*function adComplete() {
	alert("ad completed");
	if(current_type=='video')
   	buildPlayer(true, VideoArray[indx][0], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);
	else	
    buildPlayer(true, AudioArray[indx][0], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
}*/


function buildPlayer(status, imageSrc, fileSrc, domain,index,count) {
  
  
  var file = fileSrc.split("/");
  var fileName = file[file.length - 1 ];
  var fileType = fileName.split(".");
  var mimeType = fileType[fileType.length - 1 ];
   var fileFlv = fileName.split("."); 
      fileFlv = fileFlv[0];  
 
  indx = index;
  
  switch (mimeType.toLowerCase()) {

    case "swf":
    case "flv":

      clearPlayer();
      initializeVidFlv(status, imageSrc, fileSrc, domain,index,count);
      break;
    
	case "mp3":
	    clearPlayer();
      initializeAudFlv(status, imageSrc, fileSrc, domain,index,count);
      break;
    
	case "mov":
  case "aiff":
      clearPlayer();
      initializeQuick(status, fileSrc);
      break;

    case "rm":
    case "ram":
    case "rpm":

      clearPlayer();
      initializeReal(status, fileSrc);
      break;

    case "mpeg":
    case "dat":
    case "mpg":
    case "MPG":
    case "avi":
    case "wma":
    case "wav":

      clearPlayer();
      initializeMedia(status, fileSrc);
      break;

    default:

      clearPlayer();
      initializeVidFlv(status, imageSrc, fileSrc , domain,index,count);

  //  close for switch
  }

}

function initializeVidFlv(status, imgSrc, fileSrc, domain,index,count) {

  document.getElementById("flashPlayer").style.display = "";
  
	var imgSrc = "http://"+domain + "/" + imgSrc;
       var fileArcArray = fileSrc.split(".")
	var fileSrc = fileArcArray[0].replace("/ext_images/smallbiz/","");
    var shareSrc = "http://"+domain+"/p/business_resource_center//media/";
  
  var so = new SWFObject("http://"+domain+"/moliview/player/MoliViewVideoPlayer.swf", "main", 300, 219, "8", "#000000");
  so.useExpressInstall("http://"+domain+"/moliview/player/javascript/swfobject/expressinstall.swf");
  so.addParam("allowScriptAccess", "always");
  so.addParam("allowFullscreen", "true");
  so.addParam("data", "image="+imgSrc+"&"+streaminglist +"&files="+fileSrc+"&autostart="+status+"");
  so.addParam("flashVars",  "image="+imgSrc+"&"+streaminglist +"&files="+fileSrc+"&autostart="+status+"&share="+shareSrc+"&index="+index+"&filecount="+count+"");
  so.write("flashPlayer");
  prevPlayer = "flv";
 }

function initializeAudFlv(status, imageSrc, fileSrc, domain,index,count) {

  document.getElementById("flashPlayer").style.display = "";

  var imgSrc = "http://"+domain+"/" +imageSrc;
  var fileSrc = "http://"+domain + "/"+fileSrc;
  var shareSrc = "http://"+domain+"/p/business_resource_center//media/";
  var visualSrc="http://"+domain+"/moliview/player/"
  
	var so = new SWFObject("http://"+domain+"/moliview/player/MoliViewAudioPlayer.swf", "main", 300, 219, "8", "#000000");
  so.useExpressInstall("http://"+domain+"/moliview/player/javascript/swfobject/expressinstall.swf");
  so.addParam("allowScriptAccess", "always");
  so.addParam("allowFullscreen", "true");
  so.addParam("quality", "high");
  so.addParam("bgcolor", "#000000");
	so.addParam("autostart",status)
  so.addParam("flashVars",  "image="+imgSrc+"&files="+fileSrc+"&visualSrc="+visualSrc+"&autostart="+status+"&share="+shareSrc+"&index="+index+"&filecount="+count+"");
  so.write("flashPlayer");
  prevPlayer = "flv";
}

function initializeQuick(status, fileSrc) {

  var divContent = "";

  document.getElementById("quickPlayer").style.display = "";

  divContent = "<OBJECT id=\"objQuickPlayer\" name=\"objQuickPlayer\" CLASSID=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" CODEBASE=\"http://www.apple.com/qtactivex/qtplugin.cab\"  HEIGHT=\"219\" WIDTH=\"300\" > <PARAM NAME=\"src\" VALUE=\"" + fileSrc + "\" \/> <PARAM NAME=\"autoplay\" VALUE=\"" + status + "\" \/> <PARAM NAME=\"CONTROLLER\" VALUE=\"true\" \/ >";
  divContent = divContent + "<EMBED id=\"embedQuickPlayer\" src=\" " + fileSrc +" \" name=\"embedQuickPlayer\"	HEIGHT=\"219\" WIDTH=\"300\" TYPE=\"video/quicktime\" PLUGINSPAGE=\"http://www.apple.com/quicktime/download/\"  autoplay=\" " + status + "\" controller=\"true\"\/><\/OBJECT>";

  document.getElementById("quickPlayer").innerHTML=divContent;
  prevPlayer = "notflv";

}

function initializeReal(status, fileSrc) {

  var divContent = "";

  document.getElementById("realPlayer").style.display = "";

  divContent = "<object id=\"objrealPlayer\" name=\"objrealPlayer\" width=\"300\" height=\"219\" codebase=\"http://www.real.com\" classid=\"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\"> <param value=\"all\" name=\"controls\"\/> <param value=\"Clip1\" name=\"console\"\/> <param value=\""+status+ "\" name=\"autostart\" \/> <param value=\"" + fileSrc + "\" name=\"src\" \/> ";
  divContent = divContent + "<embed width=\"300\" id=\"embedRealPlayer\"  name=\"embedRealPlayer\" height=\"219\" controls=\"all\" autostart=\""+status+"\" console=\"Clip1\" type=\"audio/x-pn-realaudio-plugin\" src=\""+ fileSrc + "\"\/><\/object>";

  document.getElementById("realPlayer").innerHTML = divContent;
  prevPlayer = "notflv";

}

function initializeMedia(status, fileSrc) {

  var divContent = "";

  document.getElementById("mediaPlayer").style.display = "";

  if (status == "true" ) {

    divContent = "<object id=\"objMediaPlayer\" name=\"objMediaPlayer\" width=\"300\" height=\"219\" classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" type=\"application/x-oleobject\"\/><param name=\"src\" value=\""+ fileSrc + "\" \/><param name=\"AllowChangeDisplaySize\" value=\"-1\" \/><param name=\"AutoStart\" value=\"TRUE\" \/> <param name=\"ShowControls\" value=\"1\"\/>";
    divContent = divContent + "<embed type=\"application/x-mplayer2\" id=\"EmbedMediaPlayer\" width=\"300\" autostart=\"1\"  pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows\&sbp=mediaplayer\&ar=Media\&sba=Plugin\&\"   name=\"EmbedMediaPlayer\"  src=\"" + fileSrc + "\"  height=\"219\" /\> <\/object> ";

  }
  else {

    divContent = "<object id=\"objMediaPlayer\" name=\"objMediaPlayer\" width=\"300\" height=\"219\" classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" type=\"application/x-oleobject\"\/><param name=\"src\" value=\""+ fileSrc + "\" \/><param name=\"AllowChangeDisplaySize\" value=\"-1\" \/><param name=\"autostart\" value=\"false\" \/> <param name=\"ShowControls\" value=\"1\"\/>";
    divContent = divContent + "<embed type=\"application/x-mplayer2\" id=\"EmbedMediaPlayer\" width=\"300\" autostart=\"0\"  pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows\&sbp=mediaplayer\&ar=Media\&sba=Plugin\&\"   name=\"EmbedMediaPlayer\"  src=\"" + fileSrc + "\"  height=\"219\" /\> <\/object> ";
  
  }

  document.getElementById("mediaPlayer").innerHTML = divContent;
  prevPlayer = "notflv";

}

function swapStyle(ctrl, ullist) {
	
	/*if(ullist == "audiolist") {
		
   audli =  audli +1;
	 var vidul= document.getElementById("videolist")

		for (var itemi = 0; itemi < vidul.childNodes.length; itemi++) {
      var liitem1 = vidul.childNodes[itemi];
      if (liitem1.nodeName == "LI") {
         liitem1.className = "scrollContent_test";
		  }
		}
		
	} else if (audli>0) {
		
	 	for (var items = 0; items < document.getElementById("audiolist").childNodes.length; items++) {
    	var liitems = document.getElementById("audiolist").childNodes[items];
    	if (liitems.nodeName == "LI") {
		 		liitems.className = "scrollContent_test";
	  	}  
		}
		
	} 

  var ctrlli = document.getElementById(ctrl);
  var ctrlul = document.getElementById(ullist);

  for (var itemi = 0; itemi < ctrlul.childNodes.length; itemi++) {

    var liitem = ctrlul.childNodes[itemi];
    if (liitem.nodeName == "LI") {

      liitem.className = "scrollContent_test";

    }

  }

  ctrlli.className="scrollContent_selected";
*/
}
function repeat()
{
  
	
if(indx < vidCnt && current_type=="video")
		{
      indx += 1;
	
				buildPlayer(true, VideoArray[indx][0], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt); 
			  swapStyle('video'+indx, 'videolist'); 			 
		}
		else if(indx < audCnt && current_type=="audio") {
			indx += 1;
       buildPlayer(true, AudioArray[indx][0], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
		   swapStyle('audio'+indx, 'audiolist'); 
	 }
					
		
}

function playAgain() { 
 	indx = 0;
  if(current_type=="video") {
	buildPlayer(true, VideoArray[indx][0], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt); 
			  swapStyle('video'+indx, 'videolist'); 		
  }
  else {
	   buildPlayer(true, AudioArray[indx][0], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
	   swapStyle('audio'+indx, 'audiolist'); 
  }

	  
}

function playVideoPodcast(adSrc,ind,type,status, load ) {

  if ( $('DESC_POPUP') ) { 
    hide_balloon(); 
  }

  var load = (load ) ? load : null;
  var nextImg; 

  indx=ind;
  current_type=type;
  //need to comment when adPlayer is implemented  --- Start
  medianum = indx; 
if(current_type=='video') {
  swapStyle('video'+indx, 'videolist'); 
	
  $('dynamic_title').innerHTML = stripslashes(VideoArray[indx][3]); 


  if (load  == null) { 
    var nextone = ( VideoArray.length == indx +1 ) ? indx : indx + 1   ; 
    playlist = "playVideoPodcast(\'ext_images/mvvideo/scooter.flv\'," + nextone  + ",\'video\',\'true\'); holder=" + VideoArray[nextone][4] +  "; getComments(" +  VideoArray[nextone][4]  + ")";
    $('media_comment_title').innerHTML = stripslashes(VideoArray[indx][3]); 

    mediatype = 'video'; 
    nextImg =  $('up_next_conent').getElementsByTagName('img'); 
    
    $('up_next_title').innerHTML = stripslashes(VideoArray[nextone][3]);
    $('up_next_date').innerHTML  = VideoArray[nextone][5] ;  
    if (document.getElementById("up_next_byline")  ) {
      $('up_next_byline').innerHTML  = stripslashes(VideoArray[nextone][6]) ;
    }
    $('media_comment_title_href').href= VideoArray[indx][7] ;
    $('media_comment_title_href').innerHTML= VideoArray[indx][6] ;

     if ( nextImg[0] ) { nextImg[0].src  =   VideoArray[nextone][0]  ;  }  
  }


  // pre-roll ad 
  if ( prerollad.length ) { 
    status = true; 
    writeTrackPixel(); 
      document.getElementById("flashPlayer").style.display = ""
      var imgSrc = "http://"+VideoArray[0][2]+ VideoArray[0][8];
      //adSrc = "http://"+VideoArray[0][2] + prerollad; 
      adSrc =  prerollad; 
      var so = new SWFObject("/moliview/player/MoliViewAdPlayer.swf", "main", 300, 219, "8", "#000000");
      so.useExpressInstall("/moliview/player/javascript/swfobject/expressinstall.swf");
      so.addParam("allowScriptAccess", "always");
      so.addParam("allowFullscreen", "true");
      so.addParam("flashVars",  "image="+imgSrc+"&adSrc="+adSrc+"&" +  streaminglist + "&status="+status+"");
      so.write("flashPlayer"); 

    status = false; 
  } else { 

      if(status == 'false') { 
        buildPlayer(false, VideoArray[indx][8], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);

      }   else { 
        buildPlayer(true, VideoArray[indx][8], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);

      }
  }

}
	
	else {	

    $('dynamic_title').innerHTML = stripslashes(AudioArray[indx][3]); 

    if ( load == null) { 
    mediatype = 'audio'; 
    var nextone = ( AudioArray.length == indx +1 ) ? indx : indx + 1   ; 
    playlist = "playVideoPodcast(\'ext_images/mvvideo/scooter.flv\'," + nextone  + ",\'audio',\'true\'); holder=" + AudioArray[nextone][4] +  "; getComments(" +  AudioArray[nextone][4]  + ")";
    $('media_comment_title').innerHTML = stripslashes(AudioArray[indx][3]); 
    nextImg =  $('up_next_conent').getElementsByTagName('img'); 
    
    $('up_next_title').innerHTML = stripslashes(AudioArray[nextone][3]);
    $('up_next_date').innerHTML  = AudioArray[nextone][5] ;   
    if (document.getElementById("up_next_byline")  ) {
      $('up_next_byline').innerHTML  = stripslashes(AudioArray[nextone][6]) ;
    }
    $('media_comment_title_href').href= AudioArray[indx][7] ;
    $('media_comment_title_href').innerHTML= AudioArray[indx][6] ;

     if ( nextImg[0] ) { nextImg[0].src =   AudioArray[nextone][0] ;  }  }

	  swapStyle('audio'+indx, 'audiolist'); 
 	  if(status == 'false')
	  buildPlayer(false, AudioArray[indx][7], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
    else
    buildPlayer(true, AudioArray[indx][7], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
	
	}


}

function stripslashes(str) {
  str=str.replace(/\\'/g,'\'');
  str=str.replace(/\\"/g,'"');
  str=str.replace(/\\\\/g,'\\');
  str=str.replace(/\\0/g,'\0');
  return str;
}

function adComplete() {
	alert("ad completed");
	if(current_type=='video')
   	buildPlayer(true, VideoArray[indx][0], VideoArray[indx][1], VideoArray[indx][2],indx,vidCnt);
	else	
    buildPlayer(true, AudioArray[indx][0], AudioArray[indx][1], AudioArray[indx][2],indx,audCnt); 
}
