var flashversion = 0;
var thevid;
var stop;
var _fms;
var gplus = 0;
var _autop = 1;

function loadslide() {}

function selectvideo( val, franchise, vid_id, stopimg, plus, fms, newt, autop ) {
  if( plus ) gplus = plus;
  if( flashversion < 8 ) return;
  if( val == 'none' ) return;
  stop = stopimg;
  thevid = val;
  _fms = fms;
  if( newt ) _autop = autop;
  loadfile( "/template/cgi-bin/wcm/wcm_video.pl?pop=getads&loc="+curstation+"&v="+vid_id+'&f='+franchise, gotads );
}

function randomvid( stopimg ) {
  var num = arguments.length - 1;
  var pick = Math.floor(Math.random()*num);
  var vid = arguments[pick+1];
  selectvideo2( vid, 0,0, stopimg, 1 );
}
function gotads( xml ) {
  //alert( text);
  //var text = xml.firstChild.firstChild.nodeValue;
  var xmlo = xml2obj( xml );
  
  //if( !xmlo.xml || xml.o.xml == ',' ) {
  if( !xmlo.xml ) {//|| xml.o.xml == ',' ) {
    selectvideo2( thevid, '', '', stop );
  }
  else {
  
    if( xmlo.xml.newtype ) {
      var text = xmlo.xml.val ? xmlo.xml.val.value : ',';
      var ads = text.split(',');
      createvideo2( thevid, ads[0], ads[1], stop, 0, xmlo.xml.beforet.value, xmlo.xml.aftert.value, _autop );
    }
    else {
      var text = xmlo.xml ? xmlo.xml.value : ',';
      var ads = text.split(',');
      selectvideo2( thevid, ads[0], ads[1], stop, 0, '', '', _autop );
    }
  
  }
}
function selectvideo2( val, ad1, ad2, stopimg, nocontrols ) {
  delold();
  
  var filestart = val.indexOf( ',' ) + 1;
  var file      = val.substr( filestart );
  var size      = val.substring( 0, filestart - 1 );
  var xstart    = size.indexOf( 'x' );
  var width     = 320;//size.substring( 0, xstart );
  var height    = 260;//size.substr( xstart + 1 ) * 1 + 20;
  
  var ad1b;
  var ad2b;
  
  var curstation2 = '';
  try {
  	curstation2 = curstation;
  }
  catch (err) {};
  
  ad1b = newplayer( ad1, curstation2 );
  ad2b = newplayer( ad2, curstation2 );
  
  ad1b = ad1b.substr( 0, ad1b.length - 4 );
  ad2b = ad2b.substr( 0, ad2b.length - 4 );
  
  var pad1       = ( ad1 != '' ) ? ( '&ad1='+ad1b ) : '';
  var pad2       = ( ad2 != '' ) ? ( '&ad2='+ad2b ) : '';
  var stopt = stopimg ? ( '&stopimg=' + stopimg ) : '';
  var noc = nocontrols ? '&showcontrols=false': '';
  var fmst = _fms ? ( '&fms=' + _fms ) : '';
  
  var audioo = ( file.indexOf('.mp3') != -1 );
  var audio = '';
  var noheight = 0;
  if( audioo ) {
    audio = '&audioonly=true';
    pad1 = '';
    pad2 = '';
    stopt = '';
    height = '20';
    noheight = 1;
  }
  
  var path;
  //if( file.indexOf('plus') != 0 ) file = newplayer( file );
  //else file = 'plus/'+file;
  if( !gplus ) file = newplayer( file, curstation2 );
  else file = 'plus/'+file;
    
  path = '/template/flashplayers/streamplayer.swf?stream=' + file +fmst +audio +pad1 +pad2 + noc + stopt;
      
  addvid( path, width, height, noheight );
}

function createvideo( vid, ops ) {
  createvideo2( vid, ops['preroll']||'', ops['postroll']||'', ops['stopimg']||'', ops['nocontrols']||0, ops['pretype']||'',ops['posttype']||'', ops['autoplay']||0, ops['size']||0, 1 );
}

function createvideo2( val, ad1, ad2, stopimg, nocontrols, prerolltype, postrolltype, autoplay, sizeo, cust ) {
  delold();
  
  var filestart = val.indexOf( ',' ) + 1;
  var file      = val.substr( filestart );
  var size      = val.substring( 0, filestart - 1 );
  if( sizeo ) size = sizeo;
  if( !size ) size = '320x240';
  var xstart    = size.indexOf( 'x' );
  var width     = size.substring( 0, xstart );
  var height    = size.substr( xstart + 1 ) * 1 + 20;
  
  var ad1b;
  var ad2b;
  
  var curstation2 = '';
  try {
  	curstation2 = curstation;
  }
  catch (err) {};
  
  var ads_before = ad1.split('^');
  var ads_after = ad2.split('^');
  var before_string = '';
  var after_string = '';
  for( var i = 0; i < ads_before.length; i++ ) {
    var val = ads_before[i];
    if( val != 'noad' ) {
      var str = newplayer( ads_before[ i ], curstation2 );
      before_string += str.substr( 0, str.length - 4 );
    }
    else before_string += 'noad';
    if( val || prerolltype == 'linked' )
      if( i != ( ads_before.length - 1 ) ) before_string += ',';
  }
  if( before_string == 'noad' ) before_string = '';
  for( var i = 0; i < ads_after.length; i++ ) {
    var val = ads_after[i];
    if( val != 'noad' ) {
      var str = newplayer( ads_after[ i ], curstation2 );
      after_string += str.substr( 0, str.length - 4 );
    }
    else after_string += 'noad';
    if( val || postrolltype == 'linked' )
      if( i != ( ads_after.length - 1 ) ) after_string += ',';
  }
  if( after_string == 'noad' ) after_string = '';
  
  ad1b = before_string; //ad1b.substr( 0, ad1b.length - 4 );
  ad2b = after_string;//ad2b.substr( 0, ad2b.length - 4 );
  
  var pad1       = ( ad1 != '' ) ? ( '&preroll='+ad1b ) : '';
  var pad2       = ( ad2 != '' ) ? ( '&postroll='+ad2b ) : '';
  var pretype = '&prerollstyle=' + prerolltype;
  var posttype = '&postrollstyle=' + postrolltype;
  var stopt = stopimg ? ( '&stopimg=' + stopimg ) : '';
  var autop;
  if( cust ) {
    autop = autoplay ? '&autostart=true' : '&autostart=false';
  }
  else {
    autop = autoplay ? '&autostart=true' : '&autostart=false';
  }
  var noc = nocontrols ? '&showcontrols=false': '';
  var fmst = _fms ? ( '&fms=' + _fms ) : '';
  
  var audioo = ( file.indexOf('.mp3') != -1 );
  var audio = '';
  var noheight = 0;
  if( audioo ) {
    audio = '&audioonly=true';
    pad1 = '';
    pad2 = '';
    stopt = '';
    height = '20';
    noheight = 1;
  }
  
  var path;
  //if( file.indexOf('plus') != 0 ) file = newplayer( file );
  //else file = 'plus/'+file;
  if( !gplus ) file = newplayer( file, curstation2 );
  else file = 'plus/'+file;
    
  path = '/template/flashplayers/streamplayer.swf?stream=' + file +fmst +autop +audio +pad1 +pad2 + noc + stopt + pretype + posttype;
      
  addvid( path, width, height, noheight );
}
function newplayer( file, curstation ) {
   //if( file.substring( 0, 7 ) == "/flash/" ) file = file.substring( 7, file.length - 4 );
   //else file = file.substring( 0, file.length - 4 );
   return curstation + '/' + file;
}
function addvid( path, width, height, noheight ) {

  // create the video
  var vidwin    = _newel('div');
  vidwin.id     = 'vidwin';
  var newob     = _newel( 'object' );
  newob.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
  newob.data    = path;
  newob.width   = width;
  newob.height  = height;
  newob.wmode   = 'opaque';
  newob.id      = 'movie';
  var newpm1    = _newel( 'param' );
  newpm1.name   = 'wmode';
  newpm1.value  = 'opaque';
  if( noheight ) vidwin.style.height = '30px !important';
  _append( _getel( 'content' ), _append( vidwin, _append( newob, newpm1 ) ) );
  newob.movie   = path;
}
function delold() {
  var ovid = _getel( 'vidwin' );
  if( ovid ) {
    _clear( ovid );
    _del( ovid );
  }
}

//function _newtext( text )  { return document.createTextNode( text );      }
function _newel( name )    { return document.createElement( name );       }
//function _newbr()          { return document.createElement( 'br' );       }
function _getel( id )      { return document.getElementById( id );        }
function _del( domObject ) { domObject.parentNode.removeChild(domObject); }
function _append( loc ) {
  var len = arguments.length;
  if (! loc) { return; }
  for( var i = 1; i < len; i++ ) { loc.appendChild( arguments[ i ] ); }
  return loc;
}
function _clear( domObject ) {
  while( domObject.firstChild ) { domObject.removeChild( domObject.firstChild ); }
}

function checkflash() {
   if( navigator.plugins && navigator.plugins.length ) {
      if( x = navigator.plugins["Shockwave Flash"] ) {
       desc = x.description;
         flashversion = desc ? desc.match(/\s(\d{1,2})\./)[1] : -1;
     }
   }
   else if( navigator.mimeTypes && navigator.mimeTypes.length ) {
      x = navigator.mimeTypes[ 'application/x-shockwave-flash' ];
      flashversion = ( x && x.enabledPlugin ) ? -1 : 0;
   }
   else if( window.ActiveXObject ) { //IE
     i = 9;
     while( ( !flashversion ) && i ) {
       try {
            var flash = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
            flashversion = i;
       } catch( e ) {}
       i--;
      }
   }
   if( flashversion < 8 ) {
      delold();
      var content = _getel('content');
      var newdiv  = _newel('div');
      newdiv.id   = 'vidwin';
      _append( content, newdiv );
      newdiv.innerHTML = "<a href='http://www.macromedia.com/go/getflash/'><img border=0 src='/template/players/imgs/pleaseupgrade.gif'></a>";
   }
   setuphover();
}

function setuphover( ob ) {
  var els = _getbyclass('videoitem');
  var nosize = ob ? ( ob.nosize ? 1 : 0 ) : 0;
  for( var i in els ) {
    var el = els[i];
    //var as = el.getElementsByTagName('a');
    
    el.onmouseover = iover.bind( {fixed:nosize}, el );
    el.onmouseout = iout.bind( el );
    //el.onclick = visit.bind( as[0].href );
  }
}

//function visit() {
//  document.location.href = this;
//}

function iover( el ) {
  el.className='videoitemover';
  if( !this.fixed ) {
    var link = _getbyclass('videolink',el)[0];
    var desth = el.offsetHeight*1 - 10;
    if( link.offsetHeight < desth ) {
      link.style.height = desth + 'px';
    }
    this.fixed = 1;
  }
  
}

function iout(el) {
  this.className='videoitem';
}

function loadfile( filename, callback, server ) {
  var rand_tmp = new String( Math.random() );
  var id       = rand_tmp.substring( 2, 11 );
  var XMLobj;
  
  XMLobj = createxml( callback );
  
  if( !XMLobj ) return 0;

  //if( server ) XMLobj.documentURI = server;

  filename += ( filename.indexOf( '?' ) != -1 ) ? '&' : '?'; //allow for parameters in fname
  filename += id;
  if( !XMLobj.diff ) XMLobj.load( filename );
  else {
    XMLobj.open( 'GET', filename );
    XMLobj.send();
  }
  return 1;
}

// Create XML Request Object
function createxml( callback ) {
  var xmlDoc;
  
  if ( document.implementation && document.implementation.createDocument ) {
    xmlDoc        = document.implementation.createDocument( "", "", null );
    //xmlDoc.onload = responsereg.bind( { callback: callback, xmlobj: xmlDoc } );
    xmlDoc.addEventListener("load", responsereg.bind( { callback: callback, xmlobj: xmlDoc } ), false );
    if( !xmlDoc.load ) xmlDoc = 0;
  }
  else if ( window.ActiveXObject ) {
    xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
    xmlDoc.onreadystatechange = responseie.bind( { callback: callback, xmlobj: xmlDoc } );
  }
  if( !xmlDoc && XMLHttpRequest ) {
    xmlDoc = new XMLHttpRequest;
    xmlDoc.onreadystatechange = responseie.bind( { callback: callback, xmlobj: xmlDoc } );
    xmlDoc.diff = 1;
  }
  /*else {
    alert( "Your browser can't handle this script" );
    return 0;
  }*/
  xmlDoc.async = "false";

  return xmlDoc;
}

function responseie() {
  if( this.xmlobj.readyState == 4 ) response( this.xmlobj, this.callback );
}
function responsereg() {
  response( this.xmlobj, this.callback );
}

function response( xmlobj, callback ) {
  if( xmlobj.responseXML ) xmlobj = xmlobj.responseXML;
  callback( xmlobj );
}

// [some function].bind( newthis )
// Creates a reference to [some function] with the scope of newthis
Function.prototype.bind = function() {
  var __method = this,
      args     = arg2A( arguments ),
      object   = args.shift();
  return function() {
    return __method.apply( object, args.concat( arg2A( arguments) ) );
  }
}

function arg2A( arg ) {
  var ar = new Array;
  for( var i = 0; i < arg.length; i++ ) {
    ar.push( arg[ i ] );
  }
  return ar;
}

function xml2obj(A) {
  var B={},C={},D=[];if(A.attributes){if(A.attributes.length){for(var k=0;k<A.attributes.length;
  k++){var E={};B[A.attributes[k].nodeName]=E;E.value=A.attributes[k].nodeValue;E.att=1;}}};if(
  allTextChildren(A)){B.value='';for(var i=0;i<A.childNodes.length;i++){B.value+=A.childNodes[i]
  .nodeValue;}}else if(A.childNodes.length){var F,G='';for(var i=0;i<A.childNodes.length;i++
  ){F=A.childNodes[i];if(F.nodeType==4)G=F.nodeValue;if(F.nodeType!=1)continue;if(!C[F.nodeName]
  ){C[F.nodeName]=[];D.push(F.nodeName);}C[F.nodeName].push(F);};if(G){B.value=G;}else for(var i
  =0;i<D.length;i++){var H=D[i];var I=C[H].length;if(I>1||C['multi_'+H]){B[H]=[];for(var i2=0;i2
  <I;i2++){B[H].push(xml2obj(C[H][i2]));}}else B[H]=xml2obj(C[H][0]);}}return B;
}
    
//Returns true iff all children of the node are text nodes
function allTextChildren( node ) {
  if( !node.childNodes        ) return 0;// Possibly not normal node
  if( !node.childNodes.length ) return 0;// No children
  for( var i = 0; i < node.childNodes.length; i++) {
    if( node.childNodes[ i ].nodeType != 3 ) return 0;// A non-text child
  }
  return 1;
}

function _getbyclass( find, rootel ) {
  rootel = rootel || document.body;
  find = ' ' + find + ' ';
  var els = rootel.getElementsByTagName('*'), found = [];
  for( var i = 0; i < els.length; i++ ) {
    var check = ' ' + els[i].className + ' ';
    if( check.indexOf( find ) != -1 ) found.push( els[ i ] );
  }
  return found;
}

function arg2A(arg){
var ar=new Array;
for(var i=0;i<arg.length;i++){
ar.push(arg[i]);
}
return ar;
}
Function.prototype.bind=function(){
var __method=this,
args=arg2A(arguments),
object=args.shift();
return function(){
return __method.apply(object,args.concat(arg2A(arguments)));
}
}
