/*
It's also likely better to use abstracted Javascript variables here for picDir 
and pictype and whatever else: the goal ultimately being to have this portion of the
Javascript in a separate file and free of ASP code blocks
*/

/*
var pg = new PageQuery(location.search);
var mtype = pg.getValue("mtype");
if (mtype == -1) mtype = "iewm9";
*/

//var mtype = jMtype;
//alert(mtype);

var flTitle;

// This function should fire immediately
function acSet() {
	if (mtype == "iewm6") {
		// anything to do?
	} else if (mtype == "iewm9") {
		// Player.network.maxBandwidth = 62000;
		var p;
	} else if (mtype == "iewr" || mtype == "mr") {
		// Something similar
	}
}
acSet();


// ****---- Player agnostic javascript

// This function just became no longer agnostic...
var hitMax = false;
jMaxdeselect = jMaxdeselect + 5;
function ArtistStatus(index, ignore) {
	// I'm going to ignore the limits for the time being, in order to get a working model
	// of the new asx/deselect working first
	
	//alert (jds);
	// Strange mozilla safeguard
	if (mtype == "abacast") {
		var badstring = jChannel + " is a live radio station!  For that reason, we cannot offer many of the same features found on other AccuRadio channels, such as skip, pause, or artist deselect.";
		alert(badstring);
	} else {
		if (!hitMax) {
			if (document.images[index].src == null) {
				document.images[index].src = jPicDir + "box_checked.gif";
			} else if (document.images[index].src.search(jPicDir + "box_unchecked.gif") != -1) {
				document.images[index].src = jPicDir + "box_checked.gif";
				jds = jds - 1;
			} else {
				// This seems to be causing problems... let's set a safety
				if ((jds >= jMaxdeselect) && !hitMax) {
					alert("In order to comply with federal copyright laws which permit only \"limited personalization\" of Internet radio stations, we are currently only allowing you to deselect " + jMaxdeselect + " artists.  For stations with more streamlined artist selections, try an AccuRadio subchannel!");
					hitMax = true;
				} else {
					document.images[index].src = jPicDir + "box_unchecked.gif";
					jds++;
				}
			}
			if (ignore <= 0) SetCookie(0);
			ds = 1;
			// This is not player-agnostic
			if (mtype == "fl") {
				if (ftype == "swf7") {
					try {
						flashProxy.call("player_setds");
					} 
					catch (e) {
					}
				}
				else {
					try {
						document.AccuPlayer.asSetNew();
					} 
					catch (e) {
					}
				}
			}
		}
	}
}

function showHideLayers() {
	// far less fault-tolerance than the dreamweaver version...
	// no ie4 compatibility...
	var obj;
	var args = showHideLayers.arguments;
	for (var i = 0; i < args.length; i += 2) {
		// This is not an error, despite Aptana's saying it is
		if (obj = document.getElementById(args[i])) {
			obj.style.visibility = args[i + 1];
		}
	}
}

function swapImages() {
	// swapImages('fromname', to.gif', 'from', 'to');
	var obj;
	var args = swapImages.arguments;
	for (var i = 0; i < args.length; i += 2) {
		if (document.images[args[i]]) {
			document.images[args[i]].src = args[i + 1];
		}
	}
}

function showArtistLayer() {
	// Perhaps these should go inline...
	swapImages('tab_artist_focused.gif', jPicDir + 'tab_artist_focused.gif', 
	           'tab_subchannel_unfocused.gif', jPicDir + 'tab_subchannel_unfocused.gif', 
			   'tab_otherchannels_unfocused.gif', jPicDir + 'tab_otherchannels_unfocused.gif')
	if (mtype == "abacast") {
		swapImages('text_artist_left.gif', jPicDir + 'text_artist_left_aba.gif',
				   'text_artist_right.gif', jPicDir + 'text_artist_right_aba.gif');
	}
	else {
		swapImages('text_artist_left.gif', jPicDir + 'text_artist_left.gif',
			   	   'text_artist_right.gif', jPicDir + 'text_artist_right.gif');
	}
	showHideLayers('ArtistList', 'visible', 'SubchannelMenu', 'hidden', 'ChannelMenu', 'hidden');
}

function showSubchannelLayer() {
	swapImages('tab_artist_focused.gif', jPicDir + 'tab_artist_unfocused.gif', 
			   'tab_subchannel_unfocused.gif', jPicDir + 'tab_subchannel_focused.gif', 
			   'tab_otherchannels_unfocused.gif', jPicDir + 'tab_otherchannels_unfocused.gif',
			   'text_artist_left.gif', jPicDir + 'text_subchannels_left.gif',
			   'text_artist_right.gif', jPicDir + 'text_subchannels_right.gif');
	showHideLayers('ArtistList', 'hidden', 'SubchannelMenu', 'visible', 'ChannelMenu', 'hidden');
}

function showOtherchannelLayer() {
	swapImages('tab_artist_focused.gif', jPicDir + 'tab_artist_unfocused.gif', 
	           'tab_subchannel_unfocused.gif', jPicDir + 'tab_subchannel_unfocused.gif', 
			   'tab_otherchannels_unfocused.gif', jPicDir + 'tab_otherchannels_focused.gif',
			   'text_artist_left.gif', jPicDir + 'text_otherchannels_left.gif',
			   'text_artist_right.gif', jPicDir + 'text_otherchannels_right.gif');
	showHideLayers('ArtistList', 'hidden', 'SubchannelMenu', 'hidden', 'ChannelMenu', 'visible');
}

function swapBtn(obj, state) {
	var state = state ? state : "out";
	var re = /(over|out|down)/;
	var s = obj.src;
	s = s.replace(re, state);
	obj.src = s;
}

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
function getCookie(name) { // use: getCookie("name");
var bikky = document.cookie;
var index = bikky.indexOf(name + "=");
if (index == -1) return null;
index = bikky.indexOf("=", index) + 1; // first character
var endstr = bikky.indexOf(";", index);
if (endstr == -1) endstr = bikky.length; // last character
return unescape(bikky.substring(index, endstr));
}

function SetCookie(isalert) {
	var ds = new Array();
	var c = 0;
	var tm;
	//alert(top.mainframe.document.images.length);
	for (var i = 0; i <= top.mainframe.document.images.length; i++) {
		if (top.mainframe.document.images[i]) {
			if (top.mainframe.document.images[i].name != null) {
				if (top.mainframe.document.images[i].name.search("al_") != -1) {
					if (top.mainframe.document.images[i].src.search("box_unchecked.gif") != -1) {
						tm = top.mainframe.document.images[i].name;
						tm = tm.slice(3, tm.length);
						ds[c] = tm;
						c++;
					}
				}
			}
		}
	}
	var ds_w = ds.join("|");
	var qs = "/cookies.aspx?ds=" + escape(ds_w) + "&channel=" + jChannel;
	var gc = getCookie("Unregistered");
	if (gc != null) gc = gc.replace('"', '');
	//alert(qs);
	top.cookie.location.href = qs;
	if (isalert == 1) alert("Your selections have been saved for future use on this computer. (" + gc + ")");
}

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
// modified slightly by us (accuradio.com)
function jsSetCookie(name, value, expiryDays) { // use: setCookie("name", value);
	var today = new Date();
	var expiry = new Date(today.getTime() + expiryDays * 24 * 60 * 60 * 1000)
	if (value != null && value != "") {
		document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/";
	}
	bites = document.cookie.split("; "); // update cookie bites
}

function VolCookie(needVolCookie){
	if (needVolCookie > 0) {
		if (document.cookie) {
			jsSetCookie("volume", needVolCookie, 355);
		}
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// ****---- In-stream interstitial

function playerAreaSwap(t, strPage) {
	jTabs = document.getElementById("tabs");
	jMainarea = document.getElementById("mainarea");
	jDivLists = document.getElementById("DivLists");
	jArtistList = document.getElementById("ArtistList");
	jSubchannelMenu = document.getElementById("SubchannelMenu");
	jChannelMenu = document.getElementById("ChannelMenu");
	jPlayerDiv = document.getElementById("PlayerDiv");
	jInterstitial = document.getElementById("divinterstitial");
	jIfinterstitial = document.getElementById("ifinterstitial");
	if (t > 0) {
		// to interstitial
		jTabs.style.visibility = "hidden";
		jMainarea.style.visibility = "hidden";
		jDivLists.style.visibility = "hidden";
		jArtistList.style.visibility = "hidden";
		jSubchannelMenu.style.visibility = "hidden";
		jChannelMenu.style.visibility = "hidden";
		jPlayerDiv.style.visibility = "hidden";
		jInterstitial.style.visibility = "visible";
		jIfinterstitial.src = "/" + strPage + ".aspx?bgcolor=" + jBgColor + "&channel=" + jChannel;
		top.money.location.href = "/money_interstitial.aspx?bgcolor=" + jBgColor;
	} else {
		// to playerarea
		// wipe out iframes so that the advertisement is not loaded anymore
		jInterstitial.style.visibility = "hidden";
		jIfinterstitial.src = "/blank.html";
		jTabs.style.visibility = "visible";
		jMainarea.style.visibility = "visible";
		jDivLists.style.visibility = "visible";
		jArtistList.style.visibility = "visible";
		jSubchannelMenu.style.visibility = "visible";
		jChannelMenu.style.visibility = "visible";
		jPlayerDiv.style.visibility = "visible";
	}
}

// ****---- Amazon stuff

function popUp(URL,NAME) {
	amznwin=window.open(URL,NAME,'location=yes,scrollbars=yes,status=yes,toolbar=yes,resizable=yes,width=380,height=450,screenX=10,screenY=10,top=10,left=10');
	amznwin.focus();
}


// ****---- Player-discriminating javascript

fnSkipOver = function() { swapImages('control_skip_out.gif', jPicDir + 'control_skip_over.gif'); }
fnSkipOut = function() { swapImages('control_skip_out.gif', jPicDir + 'control_skip_out.gif'); }
fnSkipDown = function() { swapImages('control_skip_over.gif', jPicDir + 'control_skip_down.gif'); }
fnSkipUp = function() { swapImages('control_skip_down.gif', jPicDir + 'control_skip_over.gif');  }

function greyskip(mouse) {
	// The actual greying of the skip button happens in the scripts in 
	// mediaplayer.ascx
	if (mtype == "iewm6") {
		var gCurr = document.Player.GetCurrentEntry();
		var gTitle = document.Player.GetMediaParameter(gCurr, "Title");
		if (gTitle.indexOf("sweeper") < 0) {
			if (mouse == "over") {
				swapImages("control_skip_out.gif", jPicDir + "control_skip_over.gif");
			} else if (mouse == "out") {
				swapImages("control_skip_out.gif", jPicDir + "control_skip_out.gif");
			}
		}
	} else if (mtype == "iewm9") {
		if (!(document.Player.currentMedia.getItemInfo("Title") == "sweeper")) {
			if (mouse == "over") {
				swapImages('control_skip_out.gif', jPicDir + 'control_skip_over.gif');
			} else if (mouse == "out") {
				swapImages('control_skip_out.gif', jPicDir + 'control_skip_out.gif');
			} else if (mouse == "down") {
				swapImages('control_skip_over.gif', jPicDir + 'control_skip_down.gif');
			} else if (mouse == "up") {
				swapImages('control_skip_down.gif', jPicDir + 'control_skip_over.gif');
			}
		}
	} else if (mtype == "fl") {
		// This can't be done in the same was as the rest, owing to the limitations
		// of the Flash-Javascript gateway.
		if (ftype == "swf7") {
			if (mouse == "over") {
				flashProxy.call("greySkip", 'fnSkipOver');
			} else if (mouse == "out") {
				flashProxy.call("greySkip", 'fnSkipOut');
			} else if (mouse == "down") {
				flashProxy.call("greySkip", 'fnSkipDown');
			} else if (mouse == "up") {
				flashProxy.call("greySkip", 'fnSkipUp');
			}	
		}
		
	}
}

function alertStream() {
	if (mtype == "iewm6") {
		alert(document.Player.SourceLink);
	} else if (mtype == "iewm9") {
		alert(document.Player.currentMedia.sourceURL);
	}
}

function acPlay() {
	// alert(mtype);
	if (mtype == "iewm6") {
		// .PlayState == 0 == Stopped
		if (document.Player.PlayState == 0) {
			document.Player.Next();
		} else {
			document.Player.Play();
		}
	} else if (mtype == "iewm9") {
		// .playState == 1 == Stopped
		//alert(Player.controls.playState);
		if (document.Player.controls.playState == 1) {
			document.Player.controls.next();
		} else {
			document.Player.controls.play();
		}
	} else if (mtype == "iewr" || mtype == "mr") { 
		document.Player.DoPlay();
	} else if (mtype == "qtmac") {
		document.getElementById("qt").trackStart();
	} else if (mtype == "abacast") {
		document.aba_MediaPlayer.Play();
	} else if (mtype == "fl") {
		if (ftype == "swf7") flashProxy.call("player_start");
		else document.AccuPlayer.asResume();
	}
}

function acSkip() {
	// alert("you've selected skip!");
	if (mtype == "iewm6") {
		document.Player.Next();
	} else if (mtype == "iewm9") {
		if ((document.Player.playState == 3) || (document.Player.playState == 6)) {
			document.Player.controls.next();
		}
	} else if (mtype == "iewr" || mtype == "mr") {
		// there's no way to indicate not to skip on the clip inherently, I think
		// so this method is likely going to have to rely on metadata
		document.Player.DoNextClip();
	} else if (mtype == "qtmac") {
		document.getElementById("qt").trackSkip();
	} else if (mtype == "fl") {
		if (ftype == "swf7") flashProxy.call("player_skip");
		else document.AccuPlayer.asSkip();
	}
	// the trackSkip() function should come from the 
	// media-player specific javascript
	trackSkip();
}

function acPause() {
	if (mtype == "iewm6") {
		document.Player.Pause();
	} else if (mtype == "iewm9") {
		if (document.Player.playState == 3) {
			document.Player.controls.pause();
		} else {
			acPlay();
		}
	} else if (mtype == "iewr" || mtype == "mr") {
		document.Player.DoPause();
	} else if (mtype == "qtmac") {
		document.getElementById("qt").trackPause();
	} else if (mtype == "fl") {
		if (ftype == "swf7") flashProxy.call("player_pause");
		else document.AccuPlayer.asPause();
	}
}

function acStop() {
	if (mtype == "iewm6") {
		// Not verified...
		document.Player.Stop();
	} else if (mtype == "iewm9") {
		// alert ("you've selected stop!");
		document.Player.controls.stop();
	} else if (mtype == "iewr" || mtype == "mr") {
		document.Player.DoStop();
	} else if (mtype == "qtmac") {
		document.getElementById("qt").trackPause();
	} else if (mtype == "abacast") {
		document.aba_MediaPlayer.Stop();
	} else if (mtype == "fl") {
		if (ftype == "swf7") flashProxy.call("player_stop");
		else document.AccuPlayer.asPause();
	}
}

function acVolume(num){
	if (mtype == "iewm6") {
		document.Player.Volume =- 3900 + num * 650;
	} else if (mtype == "iewm9") {
		document.Player.settings.volume = (16 * num) + 1;
	} else if (mtype == "abacast") {
		document.aba_MediaPlayer.Volume =- 3900 + num * 650;
	}  else if (mtype == "qtmac") { 
		document.getElementById("qt").trackSetVolume(num);
	} else if (mtype == "fl") {
		if (ftype == "swf7") flashProxy.call("player_setvolume", num);
		else document.AccuPlayer.asSetVolume(parseInt((16.5 * num)) + 1);
	}
	if (typeof(tsVolume) != "undefined") { tsVolume(num) }
	for (var i = 1; i <= 6; i++) {
		if (i <= num) { 
			document.images["volume_" + i + ".gif"].src = jPicDir + "volume_" + i + ".gif"; 
		} else { 
			document.images["volume_" + i + ".gif"].src = jPicDir + "volume_" + i + "_out.gif"; 
		}
	}
}
