/************************
Header Functions
*************************/
var theButtons;
var theAnchors;
var theDivs;

function initChannelButtons(){

theButtons = document.getElementById('channel_buttons');
theAnchors = theButtons.getElementsByTagName("a");

}

function checkForCookie(){}
// Checks to see if there is a cookie. 
// IF cookie, select the proper button for the on state
// IF not, do nothing

function setButton(channel){ 
// Turns the proper button to the "on" state, makes sure the others are on "off"
// also turns all buttons "off" if commanded from popup window
//var theButton = document.getElementById('live_button');
//alert (theButton.name);
//var theAnchor = theButton.getElementsByTagName("a");
//theAnchor[0].className = "on"

var channel_map = {0: "kcrwlive",
                   1: "kcrwmusic",
                   2: "kcrwnews"}
for (i = 0; i < theAnchors.length; i++){
	if (i== (channel)){
		theAnchors[i].className = "on";
		//mediaPopup(i);
        launchPlayer("?type=live&id="+channel_map[channel]);
	}else{
		theAnchors[i].className = "off";
		
	}
}
}

function changeButton(channel){ 
// This changes the main page channel only... called from the media player
if (channel=="off"){
//	alert('caught it!');

	for (i = 0; i < theAnchors.length; i++){
		theAnchors[i].className = "off";
	}
}else{
for (i = 0; i < theAnchors.length; i++){
	if (i== (channel)){
		theAnchors[i].className = "on";
	}else{
		theAnchors[i].className = "off";
	}
}
}
}



function writeCookie(){}
// writes a new cookie



var newwindow = '';
function mediaPopup(channel){

	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = "/media_player_channel?channel="+ channel;
	}
	else
	{
		newwindow = window.open("/media_player_channel?channel="+ channel ,"player","toolbar=no,menubar=no,width=700px,height=465px,resizable");
		if (!newwindow.opener) newwindow.opener = self;
        // There seems to be a race condition of some sort load the url twice
        window.setTimeout(function () {
                newwindow.location.href = "/media_player_channel?channel="+ channel;
            }, 100);
	}
//if (window.focus) {newwindow.focus()}
	newwindow.focus();
	return false;

}
// this calls the media popup

function mediaPopupArchive(action,url){
	if (!newwindow.closed && newwindow.location)
	{
//		newwindow.location.href = url + "/media_player_archives";
		newwindow.location.href = url + "/media_player_archives?action=" + action;
	}
	else
	{
//		newwindow = window.open(url + "/media_player_archives", "player","toolbar=no,menubar=no,width=700px,height=375px,resizable");
		newwindow = window.open(url + "/media_player_archives?action=" + action, "player","toolbar=no,menubar=no,width=700px,height=465px,resizable");
 
		if (!newwindow.opener) newwindow.opener = self;
	}
//	if (window.focus) {newwindow.focus()}
	newwindow.focus();
	return false;

}

var popWin = '';

function liveNowPlaying()	{
	otherPopup('/nowPlayPopLive');
	return false;
}

function musicNowPlaying()	{
	otherPopup('/nowPlayPopMusic');
	return false;
}

function otherPopup(url){
	if (!popWin.closed && popWin.location)
	{
		popWin.location.href = url;
	}  else 	{
		popWin = window.open(url,"nowplaypop","toolbar=no,menubar=no,width=254px,height=561px,resizable");

	}
	popWin.focus();
	return false;
}

function dc_load(url, selector, callback) {
    if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
    url = url + ';ord=' + ord + '?';
    if ((document.images  || navigator.userAgent.indexOf('Mozilla/2.') < 0) && navigator.userAgent.indexOf("WebTV") < 0) {
        // Replace document.write with a special version that writes into our selector
        var orig_doc_write = document.write;
        document.write = function (text) {jQuery(selector).html(text)};
        
        jQuery.getScript(url,
                         function () {
                             document.write = orig_doc_write;
                             if (callback) { callback(); }
                         });
    } else {
        // Here we try to construct the doubleclick ad url formats
        var jump_url = url.replace('/adj/','/jump/');
        jump_url = jump_url.replace(';dcopt=ist','');
        var img_url = jump_url.replace('/jump/', '/ad/');
        // Allow the browser to interpret the image size
        jQuery(selector).html('<a href="'+jump_url+'" target="_blank">'+
                              '<img src="'+img_url+'" alt="" border="0" /></a>');
        if (callback) { callback(); }
    }
}