/************************

Header Javascript


*************************/

var theButtons; //channel buttons reference
var theAnchors; //channel button anchor reference
var thePage;	//the body tag (live, music,news)
//var currentPlayerSetting; //player type pulled from cookie (real,qt,wm)
//var playlistDiv = new Object();
//var settingsDiv = new Object();
var isStream;


function initChannels()	{
	isStream = hasElementClass('channelSetting','media_player');

	if(isStream)	{
//		initScrollLayer();//initializes the scrollbar javascript(separate file)
		var theChannel = getChannel("channel");
//	getCookie(); // gets the current settings from the cookie
		theButtons = document.getElementById('channel_buttons');
		theAnchors = theButtons.getElementsByTagName("a");
		changeBody(theChannel); // sets the body to the proper channel (live,music, news... live is also used for myKCRW
		jQuery(window).unload(turnButtonOff);
	}
	return theChannel;

}
function preloadTheImages(x){
    if (document.images)
    {
	
	
	if (x==2){theLetter="m";}
	else if(x==3){theLetter="n";}
	else{theLetter="b";}
	
      preload_image_object = new Image();
      // set image url
      image_url = new Array();
      image_url[0] = "../images/mp_" + theLetter + "_logo_ro.gif";
      image_url[1] = "../images/mp_" + theLetter + "_live_ro.gif";
      image_url[2] = "../images/mp_" + theLetter + "_music_ro.gif";
      image_url[3] = "../images/mp_" + theLetter + "_news_ro.gif";
	  image_url[4] = "../images/mp_" + theLetter + "_mykcrw_ro.gif";
	  image_url[5] = "../images/mp_" + theLetter + "_close_ro.gif";

       var i = 0;
       for(i=0; i<=image_url.length; i++) {
         preload_image_object.src = image_url[i];
		 //alert(preload_image_object.src);
		 }
	}
   }
/*
function getCookie(){
// get setting
currentPlayerSetting="real";
}
*/

function getChannel(channelName){
//This gets the channel number from the parent's html request (xxx.html?channel=1)

  var qs = window.location.search;
  qs = qs.substr(1);	// Strip off the ?
/*
  var qparts = url.split("?");
  if (qparts.length == 0)
  {
    return 0;
  }
*/

	if(qs)
	{
		//	var query = qparts[1];
		//  var parts = qparts.split("=");
    
		queryvars = parseQueryString(qs);
		
	    // Check if the correct variable
   		//if (parts[0] == channelName)
		if(queryvars[channelName])
	    {
    	  // Load value into variable
	      value = queryvars[channelName];
		}
    }	else
	{
		value = 0;
	}
//	alert(value);
  return value;
}

function changeBody(tchannel){ 
// This changes the body tag to the appropriate id name and sets up the buttons
// live, music, news, mykcrw
	thePage = document.getElementsByTagName("body");

	switch(tchannel){
		case "0":

		thePage[0].className = "live";
		setButtonMP(tchannel);
		preloadTheImages(1);
		tellParentChange(tchannel);
		break;
		
		case "1":
		thePage[0].className = "music";
		setButtonMP(tchannel);
		preloadTheImages(2);
		tellParentChange(tchannel);
		break;

		case "2":

		thePage[0].className = "news";
		setButtonMP(tchannel);
		preloadTheImages(3);
		tellParentChange(tchannel);
		break;

		/*
		case "3":

		thePage[0].className = "mykcrw";
		setButtonMP(tchannel);
		preloadTheImages(1);
		tellParentChange(tchannel);
		break;
		*/
	}
}

function changeButtonMP(channel){

	window.location = "/media_player_channel?channel="+ channel;
}

// this tells the Parent window the media player is closing
function closeWindow(){
	turnButtonOff();
	this.close();
}

function turnButtonOff()	{
	try{
		top.opener.window.changeButton("off");
	}
	catch(err){	}
}


function homeButton(){
if(top.opener){
	top.opener.window.location.href = "http://www.kcrw.com";
	top.opener.window.focus();
	}else{
		window.open("http://www.kcrw.com");
	}
}

// this is the function you can use to change pages on the opener file
function openPage(theFile){

if(top.opener){
	top.opener.window.location.href = theFile;
	}else{
	window.open(theFile);
	}
}


function setButtonMP(channel){ 
	// Increment channel to account for Home button
	channel++;
	for (i = 0; i < theAnchors.length; i++){
		if (i==(channel)){
			theAnchors[i].className = "on";
		//mediaPopup(i);

		}else{
			theAnchors[i].className = "off";
		
		}
	}
}


function tellParentChange(channel){
// this tells the parent window to change the channel
	try{
	top.opener.window.changeButton(channel)
	}
	catch(err){
	
	}
}


function openStandalone()	{
	window.location.href = thestream;
}


// For NP Popup

var popWin = '';

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

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

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

	}
	popWin.focus();
	return false;

}

