domainArray = location.hostname.split( '.' );
thisdomain = ( domainArray.length > 1 ) ?
             '.' + domainArray[domainArray.length-2] + '.' + domainArray[domainArray.length-1] :
             '';

var speed = WM_readCookie( "SIVideoBitrate" );
var player = WM_readCookie( "SIVideoPlayer" );

var prefTable = '<div style="padding-bottom:7px;border-bottom:1px solid #000;">';
prefTable += '<div style="font-family:verdana;font-size:11px;font-weight:bold;text-align:center;padding-bottom:2px;">Choose a speed.</div>';
prefTable += '<table align="center" border="0" cellpadding="0" cellspacing="0"><tr><td style="background-color:#ccc;padding:2px;"><table border="0" cellpadding="0" cellspacing="0" width="130">';
prefTable += '<tr bgcolor="#ffffff"><td style="border-bottom:1px solid #ccc;text-align:center;" colspan="2"><a href="http://microsoft.com/windows/mediaplayer/download/" target="new"><img src="http://premium.si.cnn.com/video/popup/images/netshow.gif"width="50" height="18" vspace="6" border="0"></a></td></tr>';
prefTable += '<tr bgcolor="#cccccc"><td><input name="videoPref" type="radio" value="media_high" onclick="onSetClicked( \'Windows Media\', \'cnn300\' );"></td><td style="font-family:verdana;font-size:11px;">Broadband</td></tr>';
prefTable += '<tr bgcolor="#e7e7e7"><td><input name="videoPref" type="radio" value="real_high" onclick="onSetClicked( \'Windows Media\', \'low\' );"></td><td style="font-family:verdana;font-size:11px;">Dialup</td></tr>';
prefTable += '</table></td></tr></table>';
prefTable += '</div>';

// Cookie stuff
function WM_browserAcceptsCookies() {
	var WM_acceptsCookies = false;
	if ( document.cookie == '' ) {
		document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
		if ( document.cookie.indexOf( 'WM_acceptsCookies=yes' ) != -1 ) {
			WM_acceptsCookies = true;
		} // If it succeeds, set variable
	} else { // there was already a cookie
		WM_acceptsCookies = true;
	}

	return ( WM_acceptsCookies );
}

function WM_setCookie( name, value, hours, path, domain, secure ) {
	if ( WM_browserAcceptsCookies() ) { // Don't waste your time if the browser doesn't accept cookies.
		var numHours = 0;
		var not_NN2 = ( navigator && navigator.appName
					&& (navigator.appName == 'Netscape')
					&& navigator.appVersion
					&& (parseInt(navigator.appVersion) == 2) ) ? false : true;

		if ( hours && not_NN2 ) { // NN2 cannot handle Dates, so skip this part
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
				numHours = hours;
			} else if ( typeof(hours) == 'number' ) { // calculate Date from number of hours
				numHours = ( new Date((new Date()).getTime() + hours*3600000) ).toGMTString();
			}
		}

		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
	}
} // WM_setCookie

function WM_readCookie( name ) {
	if ( document.cookie == '' ) { // there's no cookie, so go no further
		return false;
	} else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if ( (firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=') ) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if (lastChar == -1) lastChar = theBigCookie.length;
			return unescape( theBigCookie.substring(firstChar, lastChar) );
		} else { // If there was no cookie of that name, return false.
			return false;
		}
	}
} // WM_readCookie

function WM_killCookie( name, path, domain ) {
	var theValue = WM_readCookie( name ); // We need the value to kill the cookie
	if ( theValue ) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
} // WM_killCookie

function setCookies( player, bitrate ) {
  WM_setCookie( "SIVideoBitrate", bitrate, ( 2 * 365 * 24 ), "/", thisdomain );
  WM_setCookie( "SIVideoPlayer", player, ( 2 * 365 * 24 ), "/", thisdomain );
}

function killOldCookies() {
  WM_killCookie( "SIVideoPlayer", "/", thisdomain );
  WM_killCookie( "SIVideoBitrate", "/", thisdomain );
}

function onSetClicked( player, bitrate) {
  setCookies( player, bitrate );
  location.reload();
}

function genLinksTable( id ) {
  var linksTable = "<div style=\"padding-top:7px;font-size:9px;\">";
  switch( id ) {
    case "hoop_thoughts": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/seth_davis/archive/\">Columns</a> | <a href=\"/video/seth_davis/\">Video</a>"; break; }
    case "josh_elliott": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/" + id + "/archive/\">Columns</a> | <a href=\"/video/" + id + "/\">Video</a>"; break; }
    case "kings_corner": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/peter_king/archive/\">Columns</a> | <a href=\"/video/peter_king/\">Video</a>"; break; }
    case "peter_king": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/" + id + "/archive/\">Columns</a> | <a href=\"/video/" + id + "/\">Video</a>"; break; }
    case "seth_davis": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/" + id + "/archive/\">Columns</a> | <a href=\"/video/" + id + "/\">Video</a>"; break; }
    case "tom_verducci": { linksTable += "&#149; <b>Archive :</b> <a href=\"/writers/" + id + "/archive/\">Columns</a> | <a href=\"/video/" + id + "/\">Video</a>"; break; }
    default: { linksTable += "&#149; <b>Archive :</b> <a href=\"/video/" + id + "/\">Video</a>"; break; };
  }
  linksTable += "</div>";
  return linksTable;
}

function genLinks( archive, video ) {
  return "&#149; <b>Archive :</b> <a href=\"/writers/" + archive + "/archive/\">Columns</a> | <a href=\"/video/" + video + "/\">Video</a>";
}

function genRelateds( id, relatedsArray ) {
  var relatedsList = "<div style=\"color:#2a4863;font-family:verdana;font-size:10px;font-weight:bold;padding:7px 0px 7px 0px;\">";
  if( relatedsArray.length > 0 ) {
    switch( id ) {
      case "hoop_thoughts": { relatedsList += "SETH DAVIS\' LATEST COLUMNS"; break; }
      case "josh_elliott": { relatedsList += "JOSH ELLIOTT\'s LATEST COLUMNS"; break; }
      case "kings_corner": { relatedsList += "PETER KING\'s LATEST COLUMNS"; break; }
      case "peter_king": { relatedsList += "PETER KING\'s LATEST COLUMNS"; break; }
      case "seth_davis": { relatedsList += "SETH DAVIS\' LATEST COLUMNS"; break; }
      case "tom_verducci": { relatedsList += "TOM VERDUCCI\'S LATEST COLUMNS"; break; }
      default: { relatedsList += "RELATED"; break; }
    }
  }
  relatedsList += "</div>";
  for( var x = 0; x < relatedsArray.length; x++ ) {
    relatedsList += "<div class=\"cnnVideoRelateds\">";
    relatedsList += "&#149; <a href=\"" + relatedsArray[x][0] + "\">" + relatedsArray[x][1] + "</a>";
    relatedsList += "</div>";
  }
  return relatedsList;  
}

function genTease( title, date, tease ) {
  var retTease = '';
  retTease += '<div class="cnnVideoTitle">' + title + '</div>';
  retTease += '<div class="cnnVideoDate">' + date + '</div>';
  retTease += tease;
  return retTease;
}

function writeWindowsMediaPlayer( speed, inpath, id ) {
  var path = "";
  switch( id ) {
    default:
      if( inpath.charAt(0) != '/' ) path = "/";
      break;
  }
  var width = "240";
  var height = "230";

  switch( id ) {
    default:
      switch ( speed.toUpperCase() ) {
        case "LOW": path += inpath + ".080k.asx"; width = "176"; height = "211"; break;
        default:    path += inpath + ".300k.asx"; width = "320"; height = "288"; break;
      }
      break;
  }

  var newPage = '<OBJECT ID=Player WIDTH=' + width + ' HEIGHT=' + height + ' CLASSID=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=7,0,0,1954" standby="Loading Microsoft Media Player components..." type="application/x-oleobject">\n';
  newPage += '<PARAM name="URL" value="' + path + '"/>\n';
  newPage += '<PARAM name="animationatStart" value="true"/>\n';
  newPage += '<PARAM name="transparentatStart" value="true"/>\n';
  newPage += '<PARAM name="autoStart" value="true">\n';
  newPage += '<PARAM name="uiMode" value="full"/>\n';
  newPage += '<PARAM NAME=FileName VALUE=' + path + '>\n';
  newPage += '<PARAM NAME=ControlType VALUE=1>\n';
  newPage += '<EMBED Type=video/x-ms-asf-plugin pluginspage="http://www.microsoft.com/windows/mediaplayer/download/default.asp" src="' + path + '" name="nsplay" AutoStart=1 ShowControls=1 width=' + width + ' height=' + height + '>\n';
  newPage += '</OBJECT>\n';
  return newPage;
}

function showVideoPage( title, date, tease, credit, id, relateds, path ) {
  var newPage = '<table border="0" cellpadding="0" cellspacing="0" width="554" style="border:1px solid #000;">';
  newPage += '<tr valign="top">';
  newPage += '<td width="320" style="padding:7px;text-align:center;">' + writeWindowsMediaPlayer( speed, path, id ) + '</td>';
  newPage += '<td class="cnnVideoTease">';
  newPage += genTease( title, date, tease );
  newPage += '<div style="padding:7px 0px 7px 0px;border-bottom:1px solid #000;">&#149;  <a href="javascript:killOldCookies();location.reload();">Change Video Preferences</a></div>';
  newPage += genRelateds( id, relateds ) + genLinksTable( id );
  newPage += '</td>';
  newPage += '</tr>';
  newPage += '</table>';
  document.write( newPage );
}

function showPreferences( title, date, tease, credit, id, image ) {
  var newPage ='<table border="0" cellpadding="0" cellspacing="0" width="554" style="border:1px solid #000;">';
  newPage += '<tr valign="top">';
  if( image != "" ) {
  	newPage += '<td width="320" style="padding:7px 0px 7px 7px;text-align:center;"><img src="' + image + '" width="320" height="240" alt="" border="0"></td>';
  }
  newPage += '<td class="cnnVideoTease">';
  newPage += prefTable;
  newPage += '<div style="padding:7px;">' + genTease( title, date, tease ) + genLinksTable( id ) + '</div>';
  newPage += '</td>';
  newPage += '</tr>';
  newPage += '</table>';
  document.write( newPage );
}

function writePage( title, date, tease, credit, id, relateds, image, path ) {
  if( speed == null || player == null || speed == "" || player == "" || ( player.toUpperCase() != "REAL" && player.toUpperCase() != "WINDOWS MEDIA" ) ){
    showPreferences( title, date, tease, credit, id, image );
  } else {
    if( player.toUpperCase() == "REAL" ) { killOldCookies();location.reload(); }
    showVideoPage( title, date, tease, credit, id, relateds, path );
  }
}
