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" );

// 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 writeWindowsMediaPlayer( year, id, premium ) {
  var path = "";
  var width = "176";
  var height = "211";
  
 
  switch ( premium.toUpperCase() ) {
    case "NORMAL":	path = "/swimsuit/collection/video/"; break;
    default:		path = "/pr/subs/swimsuit/video/meta/"; break;
  }
  switch ( speed.toUpperCase() ) {
    case "LOW": path += year + "/" + id + ".080k.asx"; width = "176"; height = "211"; break;
    default:    path += year + "/" + id + ".300k.asx"; width = "320"; height = "273"; 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><br><br>\n';
  return newPage;
}

function showVideoPage( year, id, premium ) {
  document.write( writeWindowsMediaPlayer( year, id, premium ) );
}

function showPreferences( year, id ) {
  var newPage = "";
  switch( year ) {
    case "diary":
      newPage = '<div><img src="http://i.a.cnn.net/si/swimsuit/collection/' + year + '/video_' + id + '.jpg"></div>';
      break;
    default:
      newPage = '<div><img src="http://i.a.cnn.net/si/swimsuit/collection/video/' + year + '/video_' + id + '.jpg"></div>';
      break;
  }
  
  document.write( newPage );
}

function writePage( year, id, premium ) {
  if( speed == null || player == null || speed == "" || player == "" || ( player.toUpperCase() != "REAL" && player.toUpperCase() != "WINDOWS MEDIA" ) ){
    showPreferences( year, id );
  } else {
    if( player.toUpperCase() == "REAL" ) { killOldCookies();location.reload(); }
    showVideoPage( year, id, premium );
  }
}
