// SETUP VARIABLES #############################################################
	lastURL = "";

// the base URL 
	baseUrl = "http://" + document.domain + "/";

// the full URL for proxit.asp
	proxyUrl = baseUrl+"proxit.asp";

// the emusic Affiliate URL to prepend to all links
//	emuAffUrl = "http://www.tkqlhce.com/click-1553336-10364977?cjsku=10895059&url=";
	emuAffUrl = "";

// the emusic FREF to append to all emusic links
//	emuFREF = "%3FFREF%3D701087";
	emuFREF = "?fref=703855";
	emuFREF2 = "&fref=703855";

// the album URL string to search google results for
	albumURLSearchString = "http://www.emusic.com/album";

// emusic similar artists search URL
	emusSimArtSrchURL = "http://www.emusic.com/search.html?mode=r&off=0&QT=";

// emusic Track search URL
	emusTrkSrchURL = "http://www.emusic.com/search.html?mode=s&QT=";

// emusic Artist search URL
	emusArtSrchURL = "http://www.emusic.com/search.html?mode=a&QT=";

// set station
	station = "pandora";
	
// SEARCH FUNCTIONS ##########################################

function lookupNowPlaying (){
	//alert('lookupNowPlaying fired');
	try {
		if (station == "classicfm"){
			queryString = escape(artist) + '%20' + escape(song);
		}else{
			queryString = '%22' + escape(artist) + '%22%20%22' + escape(song) + '%22';
		}
	} catch (e){
		alert(e);
		//artist = "Johnny Cash";
		//song = "Ring of";
		//queryString = '%22' + escape(artist) + '%22%20%22' + escape(song) + '%22';
	}

	// Update UI
	updateSimilar(escape(artist));
	updateStatus("Searching eMusic for track using google.com");
	
	var parsGoogle = "u="+escape("http://www.google.com/search?q=site%3Aemusic.com%2Falbum+" + queryString);
	var ajaxGoogle = new Ajax.Request( proxyUrl, {	method: 'get',	parameters: parsGoogle, onComplete: parseGoogle });
}	
	
function parseGoogle (originalRequest){	// Look through google search results for the first emusic album URL we can find
		gString = originalRequest.responseText;
		//updateDebug(gString);
		updateStatus("Now Playing: "+song+" by "+artist);
		eMusAlbFound = gString.indexOf(albumURLSearchString);
		newString =  gString.substring(eMusAlbFound);

		if (eMusAlbFound > 1){ // If we found an emusic album URL
				// Extract the emusic album URL
				albURL = newString.substring(0,(newString.indexOf('.html'))+5);
//				albURL = emuAffUrl+escape(albURL)+emuFREF;
				albURL = emuAffUrl+albURL+emuFREF;
				// Update UI 
				updateAlbum(albURL);
				//document.getElementById('albuminfo').src = albURL;
				findEmusArtist(artist);
		
		}else{ // If we didn't find an emusic album URL
				// Update UI
				updateAlbum(false);
				
				// Execute an eMusic TRACK search	
				updateStatus("Searching eMusic for track using emusic.com");
				//alert("executing emus search");				
				var parsEmusTrk = "u=" + escape(emusTrkSrchURL) + "%22" + escape(song) + "%22";
				//alert("parsEmusTrk = ");				
				var emusTrkAjax = new Ajax.Request( proxyUrl, {	method: 'get',	parameters: parsEmusTrk, onComplete: parseEmusTrk });
		}
}

function parseEmusTrk (originalRequest){
	//alert("parsing emus search");				
	eString = originalRequest.responseText;
	//myeString = new String(eString)
	//mynewString = new String ("<head><base href='http://www.emusic.com'>")
	//results = myeString.replace("<head>", mynewString)
	//updateDebug(eString);
	updateStatus("Now Playing: "+song+" by "+artist);
	// Look through eMusic search results for the first eMusic album URL we can find
	eMusArtistFound = eString.indexOf(artist);
	
		if (eMusArtistFound > 1){ // If the artist name is found within the track search results, parse the album URL for that track
			newString =  eString.substring((eMusArtistFound - 526),(eMusArtistFound));
			eMusAlbFound = newString.indexOf('/album/');
			if (eMusAlbFound > 1){ // If we found an eMusic album URL
				// Extract the eMusic album URL
				albURL = "http://www.emusic.com" + newString.substring((eMusAlbFound),(newString.indexOf('.html'))+5);
//				albURL = emuAffUrl+escape(albURL)+emuFREF;
				albURL = emuAffUrl+albURL+emuFREF;
				// Update UI
				updateAlbum(albURL);
				//document.getElementById('albuminfo').src = albURL;
				findEmusArtist(artist);
			}else{ // If we didn't find an eMusic album URL
				// Update UI
				updateAlbum(false);
				findEmusArtist(artist);
			}
		}else{ // The artist name WAS NOT found within the track search results
				// Update UI
				updateAlbum(false);
				findEmusArtist(artist);
		}
}

function findEmusArtist (artistNm){
		updateStatus("Searching eMusic for artist using emusic.com");
		var parsEmusArt = "u=" + escape(emusArtSrchURL) + "%22" + escape(artist) + "%22";
		var emusArtAjax = new Ajax.Request( proxyUrl, {	method: 'get',	parameters: parsEmusArt, onComplete: parseEmusArt });
}

function parseEmusArt (originalRequest){
		eArtString = originalRequest.responseText;
		//updateDebug(eArtString);
		updateStatus("Now Playing: "+song+" by "+artist);
		// Look through eMusic search results for the first eMusic artist URL we can find
		eMusArtFound = eArtString.indexOf('/artist/');
		if (eMusArtFound > 1){ // If we found an eMusic artist URL
			// Extract the eMusic album URL
			myNewStr = eArtString.substring(eMusArtFound);
			artURL = "http://www.emusic.com" + myNewStr.substring(0,(myNewStr.indexOf('.html'))+5);
//			artURL = emuAffUrl+escape(artURL)+emuFREF;
			artURL = emuAffUrl+artURL+emuFREF;
			// Update UI
			updateArtist(artURL);
			//document.getElementById('albuminfo').src = artURL;
		}else{ // If we didn't find an eMusic artist URL
			// Update UI
			updateArtist(false);
		}

		if (albURL !=""){
			albumLink = "<a href='"+albURL+"' target=_blank>";
		}
		
		if (artURL !=""){
			artistLink = "<a href='"+artURL+"' target=_blank>";
		}

		rpTable = document.getElementById('recentlyPlayedTable');
		//rpTable.style.display = "inline";
		$('recentlyPlayed').innerHTML = "<li>"+albumLink+song+"</a> by "+artistLink+artist+"</a></li>"+$('recentlyPlayed').innerHTML;
		updateNowPlaying();	
}

function updateAlbum (albURL){
	if (albURL){
		$('TD_DL').innerHTML = "<a href='"+albURL+"' target='_blank'><img src='/img/but_download.gif' width=146 height=27 alt='Download this track from emusic.com' border=0></a>";
		albumFound=1;
		updateNowPlaying();
	}else{
		$('TD_DL').innerHTML = "<img src='/img/but_download_off.gif' width=146 height=27 alt='This track is currently unavailable for download.' border=0>";
		albumFound=false;
	}
}

function updateArtist (artURL){
	if (artURL){
		$('TD_ARTIST').innerHTML = "<a href='"+artURL+"' target='_blank'><img src='/img/but_artist.gif' width=123 height=27 alt='View other albums from the artist at emusic.com' border=0></a>";
		artistFound=1;
		updateNowPlaying();
	}else{
		$('TD_ARTIST').innerHTML = "<img src='/img/but_artist_off.gif' width=123 height=27 alt='This artist is currently unavailable.' border=0>";
		artistFound=false;
	}
}

function updateSimilar (artName){
	simURL = emusSimArtSrchURL+artName;
//	simURL = emuAffUrl+escape(simURL)+emuFREF;
	simURL = emuAffUrl+simURL+emuFREF2;

	if (artName){
		$('TD_SIMILAR').innerHTML = "<a href='" +simURL+ "' target='_blank'><img src='/img/but_similar.gif' width=146 height=27 alt='Find similar artists at emusic.com' border=0></a>";
	}else{
		$('TD_SIMILAR').innerHTML = "<img src='/img/but_similar_off.gif' width=146 height=27 alt='There are no similar artists available.' border=0>";
	}
}

function updateStatus (strStatus){ 	
	$('statusText').innerHTML = strStatus;
	try {
		updatePlayerStatus(strStatus);
	} catch (e){
		//alert(e);
	}
}

function updatePlayerStatus(strStatus){ 	
	$('playerStatusText').innerHTML = strStatus;
}

function updateDebug (strStatus){ 	
	$('debugDiv').innerHTML = strStatus;
}

function updateNowPlaying (){
	npHeading = $('tab1');
	npTable = $('albuminfo');
	npLoading = $('nowPlayingLoading');

	if(albumFound){
		//alert("albumFound");
		npURL = albURL;	
		npHeading.innerHTML = '<img src="/img/download.png" alt="Track Available" align=absmiddle border=0> <b>Download "'+song+'" </b>by<b> '+artist+'</b> @ eMusic';
		//npLoading.style.display = "none";
		npTable.style.display = "inline";		
		//npTable.style.display = "inline";
	} else if(artistFound) {
		//alert("artistFound");
		npURL = artURL;		
		npHeading.innerHTML = '<b>'+artist+' Albums</b> @ eMusic';
		//npLoading.style.display = "none";
		npTable.style.display = "inline";		
		//npTable.style.display = "inline";
	} else {
		//alert("showSimilarArtists");
		npURL = simURL;		
		npHeading.innerHTML = "Artists <i><b>Like "+artist+"</b></i> @ eMusic";
		//npLoading.style.display = "none";
		npTable.style.display = "inline";		
		//npTable.style.display = "inline";
	}	
	//alert(npURL);
	//alert(lastURL);
	if (npURL != lastURL){
		npTable.src = npURL;
		lastURL = npURL;
	}
}

