var queuedIds = null;
var queuedName = null;
var queuedMaxSongsPerId = null;
var queuedAppendToPlaylist = false;

function checkCookiesPlayList(ids, name, node) {
    conditionallySetDefaultListeningCookies()
    playList(ids, name, null, node);
}

function checkCookiesAddToPlayList(ids, name) {
    conditionallySetDefaultListeningCookies()
    addToPlaylist(ids, name, null);
}



function conditionallySetDefaultListeningCookies() {
    if (!checkCookie('mdl_streaming_bandwidth')) {
      writeCookie('mdl_streaming_bandwidth', 128, 1);
    }

    if (!checkCookie('mdl_streaming_explicit')) {
      writeCookie('mdl_streaming_explicit', 1, 1);
    }
}


function getCenter (width, height) {
	if(navigator.appName == "Microsoft Internet Explorer") {
		screenY = document.body.offsetHeight;
		screenX = window.screen.availWidth;
	}
	else {
		screenY = window.outerHeight
		screenX = window.outerWidth
	}
	leftvar = (screenX - width) / 2;
	topvar = (screenY - height) / 2;
	if(navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Netscape") {
		leftprop = leftvar;
		topprop = topvar;
	}
	else {
		leftprop = (leftvar - pageXOffset);
		topprop = (topvar - pageYOffset);
  }
	var properties = ",left = " + leftprop;
	properties += ", top = " + topprop;
	return (properties);
}

function sendQueuedPlaylist() {
  if ( queuedIds != null )
    playList(queuedIds, queuedName, queuedMaxSongsPerId, queuedAppendToPlaylist );
}

function communicate( variables ) {

  var divcontainer = "flash_setvariables_CommunicationMovie";
  if(!document.getElementById(divcontainer)) {
    var divholder = document.createElement("div");
    divholder.id = divcontainer;
    document.body.appendChild(divholder);
  }
  document.getElementById(divcontainer).innerHTML = "";
  var params = '';
  for ( var x in variables ) if ( variables[x] != null )
    params += (params != '' ? '&' : '') + escape(x) + '=' + escape( variables[x] );

  var divinfo = "<embed src='/html/mdl/player/2006/CommunicationMovie.swf' FlashVars='" + params + "' width='0' height='0' type='application/x-shockwave-flash'></embed>";

  document.getElementById(divcontainer).innerHTML = divinfo;
}

function playList(ids, name, maxSongsPerId, node) {
  queuedIds = ids;
  queuedName = name;
  queuedMaxSongsPerId = maxSongsPerId;
  queuedAppendToPlaylist = false;
  communicate( { playlistIds:ids, playlistName:name, maxSongsPerId:maxSongsPerId, appendToPlaylist:false, node:node } );
}

function addToPlaylist( ids, name, maxSongsPerId ) {
  queuedIds = ids;
  queuedName = name;
  queuedMaxSongsPerId = maxSongsPerId;
  queuedAppendToPlaylist = true;
  communicate( { playlistIds:ids, playlistName:name, maxSongsPerId:maxSongsPerId, appendToPlaylist:true } );
}

function showOtherPlaylists() {
  communicate( { openOtherPlaylists:'true' } );
}

//get user explicit settings
 var MDLExpCookieName = "mdl_streaming_explicit";
 var cookiePatternExp = new RegExp( MDLExpCookieName + "=([^;]+)(;|$)" );
 function MDLGetExp() {
    var matchExp = document.cookie.match( cookiePatternExp );
    return ( ( matchExp ) && ( matchExp[1] ) ? matchExp[1] : '0' );
 }


var MDLConnSpeedCookieName = "mdl_streaming_bandwidth";
var MDLShowHelpCookieName = "mdl_disable_help";
var cookiePattern = new RegExp( MDLConnSpeedCookieName + "=([^;]+)(;|$)" );
var showHelpPattern = new RegExp( MDLShowHelpCookieName + "=([^;]+)(;|$)" );

function MDLPlayerListening()
{
  opener.sendQueuedPlaylist();
}

function MDLPlayerSetConnectionSpeed( speed )
{
  // get domain name.
  var cookieDomain = "";
  var domainName = document.domain;
  if(domainName) {
   var domainNameComponents = domainName.split('.');
   var count = domainNameComponents.length;
   var added = 0;
   for(var i=count-1;i>=0;i--) {
       if(added > 0) {
  	 cookieDomain = "." + cookieDomain;
       }
       cookieDomain = domainNameComponents[i] + cookieDomain;
       added++;
       if(added == 2) {
  	 break;
       }
   }
  } else {
   cookieDomain = "download.com";
  }
  document.cookie= MDLConnSpeedCookieName + "=" + escape(speed) + ";domain=" + cookieDomain + ";expires=Thu,31-Dec-2020 00:00:00 GMT";
}

function MDLGetConnectionSpeed()
{
  var match = document.cookie.match( cookiePattern );
  return ( ( match ) && ( match[1] ) ? match[1] : '32' );
}

function MDLPlayerDisableHelp()
{
  document.cookie= MDLShowHelpCookieName + "=true;domain=download.com;expires=Thu,31-Dec-2020 00:00:00 GMT";
}

function MDLGetDisableHelp()
{
  var match = document.cookie.match( showHelpPattern );
  return ( ( match != null ) && ( match[1] == 'true' ) );
}

function MDLPlayerTakeFocus()
{
  window.focus();
}

function MDLLoadInParent( escapedURL )
{
  var url = unescape( escapedURL );
  if (window.opener != null) {
    window.opener.location = escapedURL;
    window.opener.focus();
  } else {
    window.open(url,'mdlPlayerInfo');
  }
}

function openMDLPlayer( ids, escapedName, node )
{
  queuedIds = ids;
  openPlayerWindow(node);
  if ( escapedName )
    queuedName = unescape(escapedName);
}

function openPlayerWindow(node)
{
	if (node == null) {
		node = '5';
	}
	var playerURL = '/3612-' + node + '_32-0.html';
	var popupWarning = '<div id=\"popupWarning\"><a href=\"javascript:nd();void(0);\">close</a></div><a href="javascript:nd();void(0);"><img src="http://music.download.com/i/mdl/global/launch_header.gif" width="402" height="74" border="0" style="border:3px solid #34374C"></a>';
	//overlib(popupWarning,MIDX,0,MIDY,0,TIMEOUT,6000,SHADOW);
	var center = getCenter(402, 340);
	var playerwindow = window.open(playerURL, 'mdlplayer', 'width=780,height=530,resizable=no,scrollbars=no,location=no,status=no,directories=no');
  playerwindow.focus();
}


function openOtherPlaylists( ids, escapedName )
{
  queuedIds = null;
  openPlayerWindow();
}
