function metaLink(theLink) {
	opener.location.href = theLink;
}

var NOWPLAYING = {
	getCurrentMediaObj : function(){
		try {
			return RadioPlayerObj.currentMedia;
		} catch(e) {
			WriteToLog("error calling getCurrentMediaObj");
		}
	},
	getNowPlayingArtist : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("Author");
		} catch(e) {
			WriteToLog("error calling getNowPlayingArtist");
		}
	},
	getNowPlayingIsAmgArtist : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("AmgArtist");
		} catch(e) {
			WriteToLog("error calling getNowPlayingIsAmgArtist");
		}
	},
	getNowPlayingArtistId : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("ArtistId");
		} catch(e) {
			WriteToLog("error calling getNowPlayingArtistId");
		}
	},
	getNowPlayingArtistUrlAlias : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("ArtistUrlAlias");
		} catch(e) {
			WriteToLog("error calling getNowPlayingArtistUrlAlias");
		}
	},
	getNowPlayingTitle : function(){
		try {
			return RadioPlayerObj.currentMedia.name;
		} catch(e) {
			WriteToLog("error calling getNowPlayingTitle");
		}
	},
	getTrackId : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("CPTrackID");
		} catch(e) {
			WriteToLog("error calling getTrackId");
		}
	},	
	getNowPlayingAlbum : function(){
		try {
		return RadioPlayerObj.currentMedia.getItemInfo("AlbumTitle");
		} catch(e) {
			WriteToLog("error calling getNowPlayingAlbum");
		}
	},
	getNowPlayingIsAmgAlbum : function(){
		try {
		return RadioPlayerObj.currentMedia.getItemInfo("AmgAlbum");
		} catch(e) {
			WriteToLog("error calling getNowPlayingIsAmgAlbum");
		}
	},
	getNowPlayingAlbumId : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("AlbumId");
		} catch(e) {
			WriteToLog("error calling getNowPlayingAlbumId");
		}
	},
	getNextUpArtist : function() {
		try {
			for(var i=0; i < (RadioPlayerObj.currentPlaylist.count); i++){
				if (RadioPlayerObj.currentmedia.isIdentical(RadioPlayerObj.currentPlaylist.item(i))){
					if (i+1 < (RadioPlayerObj.currentPlaylist.count)) {
					return RadioPlayerObj.currentPlaylist.item(i+1).getItemInfo("Author");
					} else {
					return "NONE";
					}
				}
			}	
		} catch(e) {
			WriteToLog("error calling getNextUpArtist");
		}
	},
	getNextUpArtistId : function() {
		try {
			for(var i=0; i < (RadioPlayerObj.currentPlaylist.count); i++){
			  	if (RadioPlayerObj.currentmedia.isIdentical(RadioPlayerObj.currentPlaylist.item(i))){
					if (i+1 < RadioPlayerObj.currentPlaylist.count) {
					return RadioPlayerObj.currentPlaylist.item(i+1).getItemInfo("ArtistId");
					} 
				}
			}	
		} catch(e) {
			WriteToLog("error calling getNextUpArtistId");
		}
	},
	getNextUpTitle : function() {
		try {
			for(var i=0; i < (RadioPlayerObj.currentPlaylist.count); i++){
				if (RadioPlayerObj.currentmedia.isIdentical(RadioPlayerObj.currentPlaylist.item(i))){
					if (i+1 < RadioPlayerObj.currentPlaylist.count) {
					return RadioPlayerObj.currentPlaylist.item(i+1).getItemInfo("Title");
					} else {
					return "NONE";
					}
				}
			}	
		} catch(e) {
			WriteToLog("error calling getNextUpTitle");
		}
	},
	playlistCount : function() {
		try {
			return RadioPlayerObj.currentPlaylist.count;
		} catch(e) {
			WriteToLog("error calling playlistCount");
		}
	},
	playlistCurrentItemCount : function() {
		for (var i = 0; i < RadioPlayerObj.currentPlaylist.count; i++){
		   	if (RadioPlayerObj.currentmedia.isIdentical(RadioPlayerObj.currentPlaylist.item(i))){
		  		return (i + 1); 
		     }  
		}
		return "";
	},
	getNextEntrytype : function() {
		try {
			for(var i=0; i < (RadioPlayerObj.currentPlaylist.count); i++){
				if (RadioPlayerObj.currentmedia.isIdentical(RadioPlayerObj.currentPlaylist.item(i))){
					if (i+1 < RadioPlayerObj.currentPlaylist.count) {
					return RadioPlayerObj.currentPlaylist.item(i+1).getItemInfo("entrytype");
					} else {
					return "NONE";
					}
				}
			}	
		} catch(e) {
			WriteToLog("error calling getNextEntrytype");
		}
	},
	getNextCanSkip : function() {
		try {
			return RadioPlayerObj.controls.isAvailable("next");
		} catch(e) {
			WriteToLog("error calling getNextCanSkip");
		}
	},
	playNextUp : function() {
		try {
			if(RadioPlayerObj.controls.isAvailable('Next') && (TrackSkips < 10)){
				// send reporting call
				RadioReporting.Send(true);
				
				RadioPlayerObj.controls.next();
				WriteToLog("NEXT method called on player");
				TrackSkips++;
			}
			if(TrackSkips>=10){ // disable next button
				$("BtnNext").DisableOrEnableImg(false);
			}
			if (RadioPlayerObj.controls.isAvailable('Play')){ RadioPlayerObj.controls.play();}
		} catch(e) {
			WriteToLog("error calling playNextUp");
		}
	}, 
	getStationObj : function(){
		try {
			return RadioPlayerObj.currentPlaylist;
		} catch(e) {
			WriteToLog("error calling getStationObj");
		}
	},
	getStationName : function(){
		try {
			return RadioPlayerObj.currentPlaylist.name;
		} catch(e) {
			WriteToLog("error calling getStationName");
		}
	},
	getStationId : function(){
		try {
			return RadioPlayerObj.currentPlaylist.getItemInfo("CPRadioID");
		} catch(e) {
			WriteToLog("error calling getStationId, Error" + e.number + " :" + e.message + " :" + e.description)
		}
	},
	currentStationId : "",
	getTrackAvailable : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("Available");
		} catch(e) {
			WriteToLog("error calling getTrackAvailable");
		}
	},
	getArtistAvailable : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("ArtistAvailable");
		} catch(e) {
			WriteToLog("error calling ArtistAvailable");
		}
	},
	init : function(){
		try {
			this.currentStationId = NOWPLAYING.getStationId();
		} catch(e) {
			WriteToLog("error calling init");
		}
	},	
	getPlayState : function(){
		try {
			return RadioPlayerObj.playState
;
		} catch(e) {
			WriteToLog("error calling getPlayState");
		}
	},	
	entrytype : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("entrytype");
		} catch(e) {
			WriteToLog("error calling entrytype");
		}
	},	
	getStartUrl : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("startUrl");
		} catch(e) {
			WriteToLog("error calling getStartUrl");
		}
	},	
	getEndUrl : function(){
		try {
			return RadioPlayerObj.currentMedia.getItemInfo("endUrl");
		} catch(e) {
			WriteToLog("error calling getStartUrl");
		}
	},	
	hasCoAd : function(){
		try {
			if ((RadioPlayerObj.currentMedia.getItemInfo("coAd"))  && (RadioPlayerObj.currentMedia.getItemInfo("coAd")!= "undefined")){
				return true;
			} else {
				return false;
			}
		} catch(e) {
			WriteToLog("error calling hasCoAd");
		}
	},	
	displayCoAd : function(displayType){
		try {
			var adTag = RadioPlayerObj.currentMedia.getItemInfo("coAd");
			WriteToLog("coAd : " + adTag.substr(0,20));
			var adFrame = document.all.adFrame;
			if (displayType == 'load'){
				adFrame.src = adTag;
			} else {
				//adFrame.src = "/sitewide/img/spacer.gif";
			}
		} catch (e) { WriteToLog("error in display_coAd: " + e.description); }
	},	
	display : function() {
		var charlimit = characterLimit;
		try {
			var trackTitle = this.getNowPlayingTitle();
			if(this.entrytype()=="promo"){ trackTitle = ""; }
			if (trackTitle.length > charlimit) { 
				trackTitle = trackTitle.substr(0,charlimit); 
				trackTitle = trackTitle.concat("...");
			}
			var artistName = this.getNowPlayingArtist();
			var artistNameTitle = artistName;
			if (this.entrytype()=="promo"){ artistName = " "; }
			if (artistName.length > charlimit) { 
				artistName = artistName.substr(0,charlimit); 
				artistName = artistName.concat("...");
			}
			var albumTitle = this.getNowPlayingAlbum();
			if (albumTitle.length > charlimit) { 
				albumTitle = albumTitle.substr(0,charlimit); 
				albumTitle = albumTitle.concat("...");
			}
			var nextUpArtist = this.getNextUpArtist();
			var nextUpArtistTitle = nextUpArtist;
			if (nextUpArtist.length > 30) { 
				nextUpArtist = albumTitle.substr(0,charlimit); 
				nextUpArtist = albumTitle.concat("...");
			}		
			
			var stationName = this.getStationName();
			if (stationName.length > charlimit) { 
				stationName = stationName.substr(0,charlimit); 
				stationName = stationName.concat("...");
			}
			
			// this will have to be replaced with the actual image url
			var AlbumImgSrc = "images/album_placeholder_75.gif";
			var ThisAlbumId = this.getNowPlayingAlbumId();
			if(ThisAlbumId!=""){
				AlbumImgSrc = "http://" + urgeStage + "/sitewide/dataservices/images/?type=album&id=" + this.getNowPlayingAlbumId();
			}
			var errorImg = "http://" + urgeStage + "/sitewide/img/spacer.gif";
			
			// get links for artist and album
			var npArtistLink = artistLink;
			var artistUrlAlias = this.getNowPlayingArtistUrlAlias();
			npArtistLink = npArtistLink.replace(/{artistUrlAlias}/gi, artistUrlAlias);
			var npAlbumLink = albumLink;
			npAlbumLink = npAlbumLink.replace(/{artistUrlAlias}/gi, artistUrlAlias);	
			npAlbumLink = npAlbumLink.replace(/{albumId}/gi, this.getNowPlayingAlbumId());	
			var np = "<div id='NowPlayingHeading'>"+CopyStrings.NowPlaying.Heading+"</div>";
			np += "<div id='NowPlayingStation'><span class='left'>"+CopyStrings.NowPlaying.Station+"</span><span class='right'>" + stationName + "</span></div>";
			if (npAlbumLink != ""){
			np += "<div id='NowPlayingAlbumArtImg'><a href='" + npAlbumLink + "' target='_blank' title='" + NOWPLAYING.getNowPlayingTitle() + "'><img class='AlbumArtImg' src='"+AlbumImgSrc+"' border='0' width='75' height='75' alt='" + albumTitle + "' onError=this.src='" + errorImg + "'; /></a></div>";	
			} else {
			np += "<div id='NowPlayingAlbumArtImg'></div><img class='AlbumArtImg' src='"+AlbumImgSrc+"' border='0' width='75' height='75' alt='" + albumTitle + "' onError=this.src='" + errorImg + "'; /></div>";			
			}
			np += "<div id='NowPlayingSong'><span class='left'>"+CopyStrings.NowPlaying.Song+"</span><span class='right'>";
		
			// don't display track title if the current track is a promo
			if (NOWPLAYING.entrytype()=="promo"){
				np += "";
				} else {
				np += trackTitle;
				}
			np += "</span></div></br>";
			if (this.getNowPlayingIsAmgArtist() == "true" && npArtistLink != ""){
				np += "<div id='NowPlayingArtist'><span class='left'>"+CopyStrings.NowPlaying.Artist+"</span><span class='right'><a href=\"" + npArtistLink + "\" target=\"_blank\" title=\"" + NOWPLAYING.getNowPlayingArtist() + "\">" + artistName + "</a></span></div></br>";
			} else {
				np += "<div id='NowPlayingArtist'><span class='left'>"+CopyStrings.NowPlaying.Artist+"</span><span class='right'>" + artistName + "</span></div></br>";
			}
			if (this.getNowPlayingIsAmgAlbum() == "true" && npAlbumLink != ""){
				np += "<div id='NowPlayingAlbum'><span class='left'>"+CopyStrings.NowPlaying.Album+"</span><span class='right'><a href=\"" + npAlbumLink + "\" target=\"_blank\" title=\"" + NOWPLAYING.getNowPlayingAlbum() + "\">" + albumTitle + "</a></span></div></br>";
			} else {
				np += "<div id='NowPlayingAlbum'><span class='left'>"+CopyStrings.NowPlaying.Album+"</span><span class='right'>" + albumTitle + "</span></div></br>";
			}
			// 
			if (NOWPLAYING.getNextEntrytype()!="NONE"){
				np += "<div id='NowPlayingUpNext'><b>"+CopyStrings.NowPlaying.UpNext+"</b></div>";
				if (NOWPLAYING.getNextEntrytype()!="promo"){
					// disable skipping
					if (!NOWPLAYING.getNextCanSkip()){
						np += "<div id='NowPlayingNextUp'>" + nextUpArtist + "</div>"; 	
					}else{
						np += "<div id='NowPlayingNextUp'><a href='javascript:NOWPLAYING.playNextUp();'>" + nextUpArtist + "</a></div>"; 
					}
				} else {
					np += "<div id='NowPlayingNextUp'><a href='javascript:NOWPLAYING.playNextUp();'>" + "Urge Radio" + "</a></div>"; 
				}
				np += "<br />";
			}
			return np;
		}catch(e){
			WriteToLog("error calling display: " + e);
		}			
	}
}


