function isNullOrEmpty(myVar) {
	return (typeof(myVar) != 'undefined') && (myVar != null) && (myVar != "") ? false : true;
}

function getPANDA() {
	if (window.external) {
		return (window.external.application != null) ? window.external.application : null;
	}
	return null;
}

function getUI() {
	if (window.external) {
		return (window.external.ui != null) ? window.external.ui : null;
	}
	return null;
}

function getCore() {
	if (window.external) {
		return (window.external.core != null) ? window.external.core : null;
	}
	return null;
}

function isClient() {
	return (getPANDA() != null) ? true : false;
}

function genericPop(url, width, height, scrolling) {
	var myWidth = (width != null) ? width : 800;
	var myHeight = (height != null) ? height : 600; 
	var isExternalLink = (url.indexOf("http:") > -1 || url.indexOf("https:") > -1) && url.indexOf("napster.com/") < 0 ? 1 : 0;
	if (isClient()) {
		myWidth = parseInt(myWidth) + 6;
		myHeight = parseInt(myHeight) + 43;
	}
	if (isClient() && !isExternalLink) {
		//top.location.href = "http://opcode.napster.com/?op=pop_url&width=" + myWidth + "&height=" + myHeight + "&url=" + encodeURI(url);
		window.external.executeOpcode("op=pop_url&width=" + myWidth + "&height=" + myHeight + "&url=" + encodeURIComponent(url));
	}
	else {
		//alert("scrolling is: " + scrolling);
		window.open(decodeURI(url), "popWin", "width=" + myWidth + ",height=" + myHeight + ",menubar=no,location=no,resizable=yes,scrollbars=" + (typeof scrolling != 'undefined' ? (scrolling ? 'yes' : 'no') : 'yes') + ",status=no");
	}
}

function helpPop(num, width, height) {
	//default to map ID of 2 when none is specified
	/*
	if (isNullOrEmpty(num)) {
		num = 2;
	}
	*/

    if (isClient()) {
        //top.location.href = "http://opcode.napster.com/?op=userguide&mapid=" + num;
		window.external.executeOpcode("op=userguide&mapid=" + num);
    }
    else {
		if (!isNullOrEmpty(num)) {
	    	genericPop('http://home.gb.napster.com/help/user_guide/Napster_User_Guide_CSH.htm#' + num, width, height);
		}
		else {
	    	genericPop('http://home.gb.napster.com/help/user_guide/Napster_User_Guide.htm', width, height);
		}
	}
}

function cancel(inFrames) {
    if (isClient()) {
		// window.external currently doesn't support close opcode per S. Huynh
        window.external.close("");
		//window.external.executeOpcode("op=close");
    }
    else {
		if(inFrames) {
			top.window.close();
		} else {
			self.window.close();
		}
    }
}



function getCookieRaw() {
	var results = new Array();

	if (document.cookie.length > 0) {
		var cookie  = document.cookie;
		var cookies = cookie.split("\; ");

		for (var i = 0; i < cookies.length; i++) {
			var namevalue = cookies[i].split("=");
			results[namevalue[0]] = namevalue[1];
		}
	}

	return results;
}

function getCookieHash(cookieName) {
	var results = new Array();
	var cookies = getCookieRaw();
	var cookie = cookies[cookieName];
	if (cookie != null && cookie.length > 0) {
		var names = cookie.split("&");

		for (var i = 0; i < names.length; i += 2) {
			results[decodeURI(names[i])] = decodeURI(names[i + 1]);
		}
	}

	return results;
}

function updateCookieHash(cookieName,key,value) {
	var existingCookie = getCookieHash(cookieName);
	var newValues = new Array();
	var set = 0;
	for (var i in existingCookie) {
		if(typeof existingCookie[i] == 'function') { // do this to get around prototyping issue
			continue;
		}
		if(i == key) {
			// don't put in array if value is empty (i.e. delete the key from the hash)
			if(value) {
				newValues[newValues.length] = encodeURI(key);
				newValues[newValues.length] = encodeURI(value);
			}
			set++;
		}
		else {
			newValues[newValues.length] = encodeURI(i);
			newValues[newValues.length] = encodeURI(existingCookie[i]);
		}
	}
	// maybe this is a new value, so see if we updated
	if(set < 1) {
		newValues[newValues.length] = encodeURI(key);
		newValues[newValues.length] = encodeURI(value);
	}
	var newVal = newValues.join("&");
	document.cookie = cookieName + "=" + newVal + "; path=/; domain=.napster.com";
}

function checkForClient() {
	// see if we've already set value
	var SDAT = getCookieHash('SDAT');
	if(SDAT['HAS_CLIENT']) {
		return SDAT['HAS_CLIENT'];
	}
	else {
		var objPlugin;
		var myWinRef = (self.window.name && self.window.name != 'napsterMain' ? self.window.opener : self);
		//HACK: this is necessary if we're called from a page that is part of a frameset
		if (myWinRef == null) myWinRef = self;
		if( dojo.isMozilla ) {
			objPlugin = myWinRef.document.embeds['objNapsterPlugin'];
		} else {
			try {
				objPlugin = myWinRef.dojo.byId( 'objNapsterPlugin' );
			} catch(e) {
				try {
					objPlugin = self.dojo.byId( 'objNapsterPlugin' ); // Exception code for reg. path - we're not in a pop-up and our window is not named "napsterMain"
				} catch(e1) {}
			}
		}
		
		var hasClient = false;
		if(typeof(objPlugin) != "undefined" && objPlugin != null) {
			/*
			if(typeof(objPlugin.NCOMAuth2) != "undefined") {
				hasClient = (objPlugin.NCOMAuth2 ? true : false);
			}*/
			hasClient = typeof(objPlugin.NCOMAuth2) != "undefined" ? true : false;
//alert(objPlugin.Version); //this is IE
//alert(objPlugin.clientversion); //this is mozilla
		}
		if (hasClient) updateCookieHash('SDAT','HAS_CLIENT','1');
		return hasClient;
	}
}

function reloadHome() {
	var homeWin = null;
	var homeURL = "http://home.gb.napster.com/";

	//NOTE: proceed only if we're *not* inside the client
	if (isClient()) return homeWin;

	//NOTE: do we need to make sure the parent window is "home"?
	if (window.opener != null && !window.opener.closed) {
		homeWin = window.opener;
		homeWin.location = homeURL;
		homeWin.focus();
	}
	else {
		homeWin = window.open(homeURL, "home", "top=0,location=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,status=yes");
		homeWin.focus();
	}

	return homeWin;
}

function displayElements(className, element) {
	elementList = dojo.query("." + className, element);
	for(i=0;i<elementList.length;i++) {
		elementList[i].style.display = "block";
	}
}

function hideElements(className, element) {
	elementList = dojo.query("." + className, element);
	for(i=0;i<elementList.length;i++) {
		elementList[i].style.display = "none";
	}
}

function showTracks(element) {
	displayElements('additionalTrack',element.parentNode.parentNode); 
	hideElements('showTracks',element.parentNode);
	displayElements('hideTracks',element.parentNode);
}

function hideTracks(element) {
	hideElements('additionalTrack',element.parentNode.parentNode); 
	hideElements('hideTracks',element.parentNode);
	displayElements('showTracks',element.parentNode);
}
