// this is for Dan Patrick Audio
function CNN_DPAudio( url )
{	
    var openURL='/danpatrick/listenlive.player.html';
	var cnnAudioArgs='';
    url = unescape(url);
    if(url != '')
    {
    	cnnAudioArgs = '?file='+url;
    }
	//if(openURL.indexOf('http://')==-1) {openURL='http://sportsillustrated.cnn.com'+openURL;}	
    if (window.name == 'DPAudioPlayer')
    {
    	//window.location.href = '/danpatrick/listenlive.player.html?file=' + url;
        writeAudio(url);
	}
    else
    {
    	CNN_openPopup( openURL + cnnAudioArgs, 'DPAudioPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=650,height=480' );
	}
}

function pageQuery( q ) {
	if( q.length > 1 ) this.q = q.substring( 1, q.length );
	else this.q = null;
	this.keyValuePairs = new Array();
	if( q ) {
		for( var i = 0; i < this.q.split( "&" ).length; i++ ) {
			this.keyValuePairs[i] = this.q.split( "&" )[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function( s ) {
		for( var j = 0; j < this.keyValuePairs.length; j++ ) {
			if( this.keyValuePairs[j].split( "=" )[0] == s )
			return this.keyValuePairs[j].split( "=" )[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array( this.getLength() );
		for( var j = 0; j < this.keyValuePairs.length; j++ ) {
			a[j] = this.keyValuePairs[j].split( "=" )[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString( key ){
	var page = new pageQuery( window.location.search ); 
	return unescape( page.getValue( key ) ); 
}
var dpliveurl = 'http://sportsillustrated.cnn.com/danpatrick/live.asx';
var isStream = true;
if( queryString( 'file' ) != 'false' ) {
	dpliveurl = queryString( 'file' );
    isStream = false;
   
}

function writeAudio(url){
	$(Player).innerHTML = "";
	dpliveurl = url;
    if(url != '')
    {
    	isStream = false;
    }
	var a = new Audio({
		element: 'Player',
		url: dpliveurl,
		width: 283
	});
    writeNowPlaying()

}

var fileHostPath = 'http://ht.cdn.turner.com/si/danpatrick/audio';
var fileExt = '.mp3';

// this function logic is in sync with xslt/si/danpatrick/now_playing.xslt
function writeNowPlaying(){
	var urlStr = 'Listen Live Now';
    if(!isStream)
    {
        var pLength = fileHostPath.length;
        var numSlashes = dpliveurl.lastIndexOf('/') + 1;
     	var str1 = dpliveurl.substr(numSlashes);
        var str2 = str1.replace(fileExt,'');
        //alert('str1: ' + str1 + '\nstr2: ' + str2);
        var urlStr = '/.element/ssi/auto/4.0/danpatrick/nowplaying/' + str2 + '.txt';
   		updateNowPlaying(urlStr);
   }
   else{
   		$('dpAudioNowPlaying').innerHTML = urlStr;
   }
    

}

function updateNowPlaying(urlStr){
	$('dpAudioNowPlaying').innerHTML = '';
	new Ajax.Updater({success: $('dpAudioNowPlaying')}, urlStr ,{ method: 'get' });
    //TODO:write out text in case of error, now it will be blank if no text is found

}

function dpAudio_openLinkWindow( a ){
	if (opener && !opener.closed){
		self.opener.location = a.name;
	}
	else {
		open(a.name, "_blank", "resizable=yes, titlebar=yes, menubar=yes, toolbar=yes, location=yes");
	}
}
/*

// created a new feed for these links so added the onclick directly to links
// ff was not loading in the events correctly
function loadLink() {
	if (document.getElementById("cnnDPRadio")) {
    
		var moreLinks = document.getElementById("cnnDPRadio").getElementsByTagName("a");
		for  ( i = 0; i < moreLinks.length; i++) {
			if (moreLinks[i].className == "cnnDPMoreLinks") {
				moreLinks[i].onclick = function() {
					if (opener && !opener.closed){
						self.opener.location = this.name;
					}
					else {
						open(this.name, "_blank", "resizable=yes, titlebar=yes, menubar=yes, toolbar=yes, location=yes");
					}
				}
			}
		}
	}
}

if (document.getElementById("cnnDPRadio")) {
    if (window.attachEvent) {
    	window.attachEvent('onload', loadLink);
    }
    if (window.addEventListener) {
    	window.addEventListener('load', loadLink, false);
    }
}
*/