

// call to select a genre by it's ID

function mc_g(genre)
{
	writeCookie("genre_id", genre, 1);
	writeCookie("ignore_genre", 'no', 1);
	writeCookie("search_keyword", '', 1);
	//alert('selecting genre ' + genre);
	setIDToSelected(genre);
	mc_refresh_artist_head();
	mc_refresh_artist();
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

// selects an artist by id

function mc_a(artist)
{
	writeCookie("artist_id", artist, 1);
	writeCookie("ignore_genre", 'no', 1);
	writeCookie("search_keyword", '', 1);
	setIDToSelected(artist);
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

// selects an artist by ID when called from artist search results

function mc_as(artist)
{
	writeCookie("artist_id", artist, 1);
	writeCookie("ignore_genre", 'yes', 1);
	writeCookie("search_keyword", '', 1);
	setIDToSelected(artist);
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

function mc_ss(song)
{
	// in demo mode, do nothing
	
	if (mc_demomode()) {alert('demo mode'); return};
	
	writeCookie("song_id", song, 1);
	parent.frames['playlist'].location = "playlist-add.php";
	mc_refresh_info();
}

function mc_sr(song)
{
	writeCookie("song_id", song, 1);
	parent.frames['playlist'].location = "playlist-remove.php";
	mc_refresh_info();
}

function mc_clear_genre()
{
	writeCookie("ignore_genre", 'yes', 1);
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

function mc_search(name)
{
	writeCookie("search_keyword", name, 1);
	writeCookie("artist_id", '',1);
	mc_refresh_artist_head();
	mc_refresh_artist();
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

function mc_clear_search()
{
	writeCookie("search_keyword", name, 1);
	mc_refresh_artist_head();
	mc_refresh_artist();
	mc_refresh_songlist_head();
	mc_refresh_songlist();
}

function mc_reqdel(title)
{
	parent.frames['playlist'].location = "request-remove.php?title=" + title;
}


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


function mc_refresh_artist()
{
	keyword = readCookie("search_keyword");
	if (keyword == '')
	{
		// regular artist display	
		artist_id = readCookie("artist_id");
		mc_reloadFrame('artist', "artist.php#" + artist_id);
	}
	else
	{
		// do a keyword search
		mc_reloadFrame('artist', 'artist-search.php');
	}
}

function mc_refresh_artist_head()
{
	mc_reloadFrame('artist-head', "artist-head.php");
}

function mc_refresh_genre()
{
	genre_id = readCookie("genre_id");
	mc_reloadFrame('genre', "genre.php#" + genre_id);
}

function mc_refresh_songlist_head()
{
	mc_reloadFrame('song-head', "songlist-head.php");
}

function mc_refresh_songlist()
{
	var demo = ''
	
	if (mc_demomode())
	{
		demo = "?demo=yes";
	}
	
	keyword = readCookie("search_keyword");
	if (keyword =='')
	{
		mc_reloadFrame('songlist', "songlist.php" + demo);
	}
	else
	{
		mc_reloadFrame('songlist', "songlist-search.php" + demo);
	}
}

function mc_refresh_info()
{
	mc_reloadFrame('playlist-head', 'playlist-head.php');
	//parent.frames['info'].location = 'loading.php?location=stats.php';
}

function mc_refresh_playlist()
{
	mc_reloadFrame('playlist', 'playlist.php');
	//parent.frames['playlist'].location = 'loading.php?location=playlist.php';
}


function mc_goto_anchor(name)
{
	location.hash = name;
}

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + "; path=/music_list/;";
}



function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}


function setIDToSelected(id)
{
	// first, get the songlist object
	
	var theList = document.getElementById('songlist');
	
	// now get all thd 'a' elements under it
	
	var anchors = theList.getElementsByTagName("a");
	
	// if they have class="selected", set them to class=""
	
	for (i = 0; i< anchors.length; i++)
	{
		theItem = anchors[i];
		if (theItem.className == "selected") theItem.className = ""; 
	}
	
	// get the one to highlight and change it's class
	
	theObj = document.getElementById(id);
	theObj.className="selected";
}


function mc_setFrameTo(frame, location)
{
	parent.frames[frame].location = location;
}


function mc_reloadFrame(frame, location)
{
	final_location = "loading.php?location=" + escape(location);
	mc_setFrameTo(frame, final_location);
}

function mc_demomode()
{
	if (parent.frames['playlist']) return false;
	
	return true;
}
