// As of 01.23.2006, this script file is shared for SPLN, PGATOUR, and NCAASports. 
// Any changes must be tested across domains.

//-------------------------------------------
// Writes the intro text
function writeIntro() {
	document.write("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"7\" border=\"0\"><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td><font class=\"white\">A custom playlist lets you pick the CBS SportsLine.com videos you want to watch and plays them one right after the other in the order you've chosen.<br><br> To create your playlist, click on the \"Add To Playlist\" button next to the clips you want to see. When you are ready to start watching, click on the \"Play Playlist\" button that appears in the space above.</font></td></tr></table></td></tr></table>");
}

//-------------------------------------------
// Change the image header to this channel
function changeChannelHeader(name) 
{
	var img = document.getElementById('channelheader');
	if (img != null)
	img.src = cp.imageSrc+ "hdr_" + name + ".gif";
	document.images['channelheader'].src = cp.imageSrc+ "hdr_" + name + ".gif";
}

//------------------------------------------
// Plays a video
function playVideo(id, title) {
    if (cp.play_state != "stopped" && cp.cur_clip && cp.cur_clip.id == id)
		return;

	var vid = new cbsVideo(id, 'all');
	vid.displayTitle = title;

	cp.removeClip(id);
	cp.insertClip(cp.list_pos, vid);
	cp.Play(vid);
}

//------------------------------------------
// Adds a video to the playlist
function addVideo(id, title) {
	var vid = new cbsVideo(id, 'all');
	vid.displayTitle = title;

	cp.addClip(vid);
}

//------------------------------------------
// Removes a video from the playlist
function removeVideo(id) {
	cp.removeClip(id);
}
//------------------------------------------
// Emails a video
function emailVideo(id, title) {
	alert("email " + id);
}


//------------------------------------------
// Display playlist
function displayPlaylist() {
	cp.log("DISPLAY");
	var pl = cp.getPlaylist();
	var html = '<table width="100%" cellspacing="0" cellpadding="7" border="0"><tr><td><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td>';
	if ((cp.list_pos != 0) && (cp.list_pos <= cp.clips.length)) {
		var clip = pl[cp.list_pos-1];
		html += '<tr><td class="whitebold" height=18><b>' + clip.displayTitle + '</b></td>';
		html += '<TD class=whitesmall align=right>Playing</TD></TR>';
	}
	for (var i=cp.list_pos; i<pl.length; i++) {
		var clip = pl[i];
		html += '<tr><td class="white">';
		html += "<a href=\"javascript:void(0);\" onclick=\"javascript:parent.top.playVideo('" + clip.id + "', '" + clip.displayTitle.replace(/\'/g, "\\\'").replace(/\"/g, "\\\"") + "'); return false;\" class='white'>" + clip.displayTitle + "</a>";
		html += '</td><td align="right" class="whitesmall">';
		html += "<a href=\"javascript:void(0);\" onclick=\"javascript:parent.top.playVideo('" + clip.id + "', '" + clip.displayTitle.replace(/\'/g, "\\\'").replace(/\"/g, "\\\"") + "'); return false;\" class='whitesmall'><img src='" +cp.imageSrc +"button_play_small.gif' width='11' height='11' border='0' align='absmiddle'> Play</a>&nbsp;";
		html += "<a href=\"javascript:void(0);\" onclick=\"javascript:parent.top.removeVideo('" + clip.id + "'); return false;\" class='whitesmall'><img src='" +cp.imageSrc +"button_remove.gif' width='11' height='11' border='0' align='absmiddle'> Remove</a>";
		html += '</td></tr>';
	}
	html += '</td></tr></table></td></tr></table>';

	var div = document.getElementById('playList');
	div.innerHTML = html;

	displayPlaylistControls();
}


//------------------------------------------

// Display playlist controls

function displayPlaylistControls() {
        var html = "";
        if (cp.clips.length > 0) {
                var state = cp.getPlayState();
                if ((state == "paused") || (state == "seeking")) {
                        html = '';
                } else {
                        if (cp.isPlaying()) {
                                html = '<a href="javascript:void(0)" onclick="cp.Stop();return false" class="white"><img src="' +cp.imageSrc +'button_stop_small.gif" alt="Stop Playlist" width="11" height="11" border="0" align="absmiddle"> Stop Playlist </a>';
                        } else {
                                html = '<a href="javascript:void(0)" onclick="cp.Play();return false" class="white"><img src="' +cp.imageSrc +'button_play_small.gif" alt="Play Playlist" width="11" height="11" border="0" align="absmiddle"> Play Playlist </a>';
                        }
                        html += '<a href="javascript:void(0)" onclick="cp.Stop();cp.clearClips();return false" class="white"><img src="' +cp.imageSrc +'button_remove.gif" alt="Clear Playlist" width="11" height="11" border="0" align="absmiddle"> Clear Playlist </a>';
                }
        }
        var div = document.getElementById('playListStatus');
        div.innerHTML = html;
}

function onClipClosed(event, clip) {
	cp.log("REMOVE " + clip.id);
	cp.removeClip(clip.id);
	displayPlaylist();
}

//---------------------------------------------------
// Parse query string into array
function parseQueryString() {
        var params = new Array();
        var url = location.href.split('?', 2);
        if(url.length != 2) return params;
        var args = url[1].split('&');
        for(var i = 0; i<args.length; i++) {
       	        var arg = args[i].split('=', 2);
                params[arg[0]] = arg[1];
        }	
        return params;
}

//------------------------------------------
// Load a video
function loadVideo(id) {
	document.write("<iframe id='loadVideo' width=0 height=0 src='/video/player/load?id=" + id + "'>");
}

//-----------------------------------------
// Show the ad if there is one
function showAd(vp, bit) {
        cp.log("showAd " + bit.adUrl + ' ' + bit.adImg);
        if (bit.adShown){
                return;
        }
        if (bit.adUrl) {
                cp.log("bit.adURL exists");
                cp.log("root is:" + cp.root);
                var f = document.getElementById("banner");
                if (f) {
                        cp.log("Before attempt to access f.contentWindow.document at line 136 in control_test.js");
                        var d = f.contentWindow.document;
                        if (d) {
                                cp.log("location.href moving to bit.adURL:" + bit.adUrl + "?adClk=" + escape(bit.adClk));
                                //d.location.href = bit.adUrl + "?adClk=" + escape(bit.adClk);
                                d.location.href = "/video/player/html_ad?adUrl=" + escape(bit.adUrl) + "&adClk=" + escape(bit.adClk);
                        }
                }
        } else if (bit.adImg) {
                //var ad = document.getElementById("adDiv");
                //var html = '<a href="' + unescape(bit.adClk) + '" target="_new">';
                //html += '<img src="' + unescape(bit.adImg) + '" border="0">';
                //html += '</a>';
                //adDiv.innerHTML = html;
                //adDiv.style.display = '';
                var f = document.getElementById("banner");
                if (f) {
                        cp.log("Before attempt to access f.contentWindow.document at line 152 in control_test.js");
                        var d = f.contentWindow.document;
                        if (d) {
                                d.location.href = "/video/player/img_ad?adClk=" + escape(bit.adClk) + "&adImg=" + escape(bit.adImg);
                        }
                }
        } else {
                //var ad = document.getElementById("adDiv");
                //adDiv.style.display = 'none';
                //cp.log("adDiv no show");
        }
        bit.adShown = 1;
}
