dojo.require("dojo.io.cookie");

document.onmousemove = mouseCoords;
document.onclick = mouseCoordsClick;
//document.onmouseup = mouseCoordsClick;
var playlistPopActive = false;
var contentIdToAdd = "";
var contentTypeToAdd = "";
var protectAddActions = false;
var albumArtPopup = true;

// if(dojo.render.html.ie60) { albumArtPopup = false; }

if (window.Node && Node.prototype && !Node.prototype.contains) {
	Node.prototype.contains = function (arg) {
		return !!(this.compareDocumentPosition(arg) & 16)
	}
}

function mouseCoords(ev){
	if(typeof(ciHeight) != "undefined") {
		ev = ev || window.event;
		
		if(ev.pageX || ev.pageY){
			xPos = ev.pageX;
			yPos = ev.pageY;
		} else {
			xPos = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
			yPos = ev.clientY + document.body.scrollTop  - document.body.clientTop;
		};
		
		xPos = xPos + 25;
		yPos = yPos - 25;
		
		
		
		yOffset = document.getElementById("popupContainer") != null ?
			document.getElementById("popupContainer").offsetHeight : 0;
		
		//ie7 fix
		if(dojo.render.html.ie70) {
			yOffset += 10;
		}
		//dojo.debug("mm yOffset: " + yOffset);
		
		if((yPos + yOffset) > ciHeight) {
			//yPos = ciHeight - document.getElementById("popupContainer").offsetHeight;
			yPos = ciHeight - yOffset;
		}
		if (document.getElementById("popupContainer") != null) {
			document.getElementById("popupContainer").style.left = xPos+"px";
			document.getElementById("popupContainer").style.top = yPos+"px";
		}
	}
	
	// console.log("x: " + xPos + " y: " + yPos);
}

function mouseCoordsClick(ev){
	// if(typeof(console) != "undefined") { console.log("click"); }
	if(typeof(ciHeight) != "undefined") {
		ev = ev || window.event;
		if(!playlistPopActive) { // We only want to move the playlist pop-up if it isn't showing
			if(ev.pageX || ev.pageY){
				xPos = ev.pageX;
				yPos = ev.pageY;
			} else {
				xPos = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
				yPos = ev.clientY + document.body.scrollTop  - document.body.clientTop;
			}
			
			xClickPos = xPos;
			yClickPos = yPos - 25;
			yOffset = document.getElementById("popupContainerClick") != null ? document.getElementById("popupContainerClick").offsetHeight : 0;
			
			if((yPos + yOffset) > ciHeight) {
				yPos = ciHeight - yOffset;
			}
			
			if((xPos + document.getElementById("popupContainerClick").offsetWidth) > ciWidth) {
				xPos = ciWidth - document.getElementById("popupContainerClick").offsetWidth;
			}
			document.getElementById("popupContainerClick").style.left = xPos+"px";
			document.getElementById("popupContainerClick").style.top = yPos+"px";
		}
		// console.log("xClickPos: " + xClickPos + " yClickPos: " + yClickPos);
		hideAlbumArtPop();
		destroyAddActions();
	}
}

function repositionDiv(divID, xPosition, yPosition) {
	var paddingVal = 25;
	// if(typeof(console) != "undefined") { console.log("xPosition: " + xPosition + " yPosition: " + yPosition); }
	xOffset = document.getElementById(divID).offsetWidth;
	yOffset = document.getElementById(divID).offsetHeight; 
	
	// if(typeof(console) != "undefined") { console.log("xOffset: " + xOffset + " yOffset: " + yOffset); }
	if((xPosition + xOffset + paddingVal) > ciWidth) {
		xPosition = ciWidth - xOffset - paddingVal;
	}
	
	/* if((xPosition + xOffset + paddingVal) > document.body.offsetWidth) {
		xPosition = document.body.offsetWidth - xOffset - paddingVal;
	} */
	
	if((yPosition + yOffset + paddingVal) > ciHeight) {
		yPosition = ciHeight - yOffset - paddingVal;
	}
	
	/* if((yPosition + yOffset + paddingVal) > document.body.offsetHeight) {
		yPosition = document.body.offsetHeight - yOffset - paddingVal;
	} */
	
	// if(typeof(console) != "undefined") {  console.log("xPos: " + xPosition + " yPos: " + yPosition); }
	document.getElementById(divID).style.left = xPosition+"px";
	document.getElementById(divID).style.top = yPosition+"px";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			if(obj.offsetLeft) { if(typeof(console) != "undefined") { console.log("curleft: " + curleft + " obj.id: " + obj.id); } else { /* alert("curleft: " + curleft + " obj.id: " + obj.id); */ } }
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}


function destroyAddActions() {
	if (document.getElementById("addDialog") != null && !protectAddActions) {
		document.getElementById("addDialog").parentNode.removeChild(document.getElementById("addDialog"));
	} else {
		protectAddActions = false;
	}
}

function AddListActions(btnEl, itemType) {
	var itemArray = getAllListReturnTracks();
	if(itemArray) {
		items = itemArray.join(",");
		AddActions(btnEl, items, 0);
	}
}

function AddActions(btnEl, itemID, itemType, itemRights, inLibrary) {
	// itemRights = {can_burn: 1, can_stream: 1, album_for_sale: 1};
	var pluralString = "";
	itemID = new String(itemID);
	var itemIDs = itemID.split(",");
	if(itemIDs.length > 1) {
		pluralString = "s";
		itemRights = {'can_purchase': 1, 'afs': 1};
	}
	itemID = itemID.replace(/ /g,''); //removing spaces - fix for share function
	/* if(typeof(inLibrary) == "undefined") {
		if(itemType == 0 && itemIDs.length == 1) {
			if(collection.store.getTrackFromColl(itemIDs[0]) == null) {
				inLibrary = 0;
			} else {
				inLibrary = 1;
			}
		} else {
			inLibrary = 0;
		}
	} */
	itemType = new Number(itemType);
	if(typeof(itemRights) != "object") {
		try {
			// itemRights = eval(itemRights);
			itemRights = dojo.json.evalJson(itemRights);
			if(itemRights == 0) { throw "empty rights"; }
		} catch(e) {
			itemRights = {'can_purchase': 1, 'afs': 1};
		}
	}


	// if all else fails, we need default values for itemRights
	if (!itemRights) {
		itemRights = {'can_purchase': 1, 'afs': 1};
	}
	
	destroyAddActions();
	protectAddActions = true;
	var addDialog = document.createElement("div");
	addDialog.id = "addDialog";
	addDialog.innerHTML = "";
	addDialog.innerHTML += '<a href="#" onClick=\'Queue("' + itemID + '","' + itemType + '"); return false;\'>Zur Wiedergabeliste hinzufügen</a>';
	if(inLibrary) {
		// addDialog.innerHTML += '<span>In Library</span>'
	} else {

		addDialog.innerHTML += '<a href="#" onClick=\'AddToLibrary("' + itemID + '","' + itemType + '"); return false;\'>Zum Archiv hinzufügen</a>'
	}
	addDialog.innerHTML += '<a href="#" onClick=\'playlistPopActive = true; showPlaylistPop("' + itemID + '","' + itemType + '", 1); return false;\'>Zur Playlist hinzufügen</a>'; // Setting playlistPopActive and the 1 at the end of showPlaylistPop are to work around an IE6 event bubbling bug that was crashing the browser (some strange collision of the add to playlist pop-up and the large album art pop-up
	addDialog.innerHTML += '<a href="#" class="divider" onClick=\'Share("' + itemID + '","' + itemType + '"); return false;\'>' +itemMap[itemType].substring(0,1).toUpperCase() + itemMap[itemType].substring(1,itemMap[itemType].length) + ' weiterempfehlen</a>';
	if(itemType == 1 && (itemRights.afs == 'Y' ))  { addDialog.innerHTML += '<a href="#" onClick=\'Buy("' + itemID + '","' + itemType + '"); return false;\'> ' + mp3Map[itemType] + ' kaufen</a>'; }
	else if  (itemType == 1 && (itemRights.afs == 'N' ))  { } 
	else if ((itemType == 0 && itemRights.can_purchase) || ((itemType == 0) && (itemIDs.length > 1)) || (itemType != 0)) { addDialog.innerHTML += '<a href="#" onClick=\'Buy("' + itemID + '","' + itemType + '"); return false;\'> ' + mp3Map[itemType] + ' kaufen</a>'; }
	else if(itemType == 0 && !itemRights.can_purchase && itemRights.afs) { addDialog.innerHTML += '<a href="#" onClick=\'Buy("' + itemRights.album_id + '","1"); return false;\'>Album kaufen (Nur als Album erh&auml;ltlich)</a>'; }
	else { addDialog.innerHTML += '<span>' + mp3Map[itemType] +  'kaufen (no AddAction)</span>'; }
	// btnEl.parentNode.style.position = "relative";
	
	// btnEl.parentNode.appendChild(addDialog);
	
	
	var objLocation = findPos(btnEl);
	xPos = objLocation[0];
	yPos = objLocation[1];
	// yPos = btnEl.parentNode.offsetTop;
	// xPos = btnEl.parentNode.offsetLeft;
	
	// var xPad = 27;
	// var yPad = 10;
	
	var xPad = 5;
	var yPad = 5;
	
	xPos += xPad;
	yPos += yPad;
	
	var scrollPosX = 0;
	var scrollPosY = 0;
	
	var cbOffsetTop = 0;
	var cbOffsetLeft = 0;
	
	/* if(dojo.byId("content_head").contains(btnEl)) {
		// chOffsetTop = 0;
		cbOffsetTop = dojo.html.getBorderBox(dojo.byId("header")).height + dojo.html.getBorderBox(dojo.byId("breadcrumb_bar")).height + dojo.html.getBorderBox(dojo.byId("vmode_undercarriage")).height;
		// cbOffsetLeft = 0;
		cbOffsetLeft = dojo.html.getBorderBox(dojo.byId("sidebar")).width;
		
		document.getElementById("content_head").appendChild(addDialog);
		scrollPos = dojo.byId("content_head").scrollTop;
		document.getElementById("addDialog").style.left = xPos - cbOffsetLeft + "px";
		if(dojo.render.html.ie70) {
			document.getElementById("addDialog").style.left = xPos - (cbOffsetLeft*2) + "px"; // IE7 bug
		}
		logMsg = "xPos: " + xPos + " cbOffsetLeft: " + cbOffsetLeft + " document.getElementById(\"addDialog\").style.left: " + document.getElementById("addDialog").style.left;
		if(typeof(console) != "undefined") { console.log(logMsg); }
		document.getElementById("addDialog").style.top = yPos - scrollPos - cbOffsetTop + "px";
		if(typeof(console) != "undefined") { console.log("yPos: " + yPos);}
	} */
	if(dojo.byId("content_head").contains(btnEl)) {
		scrollPosX = dojo.byId("content_head").scrollLeft;
		if(dojo.render.html.ie70) {
			scrollPosX += dojo.html.getBorderBox(dojo.byId("sidebar")).width; // IE7 bug
		}
		scrollPosY = dojo.byId("content_head").scrollTop;
		
		document.getElementsByTagName("body")[0].appendChild(addDialog);
		document.getElementById("addDialog").style.left = xPos - scrollPosX + "px";
		document.getElementById("addDialog").style.top = yPos - scrollPosY + "px";
	}
	
	if(dojo.byId("content_body").contains(btnEl)) {
		scrollPosX = dojo.byId("content_body").scrollLeft;
		scrollPosY = dojo.byId("content_body").scrollTop;
		cbOffsetTop = dojo.html.getBorderBox(dojo.byId("header")).height + dojo.html.getBorderBox(dojo.byId("breadcrumb_bar")).height + dojo.html.getBorderBox(dojo.byId("vmode_undercarriage")).height + dojo.html.getBorderBox(dojo.byId("content_head")).height;
		cbOffsetLeft = dojo.html.getBorderBox(dojo.byId("sidebar")).width;
		
		document.getElementById("content_body").appendChild(addDialog);
		// document.getElementsByTagName("body")[0].appendChild(addDialog);
		document.getElementById("addDialog").style.left = xPos - cbOffsetLeft + "px";
		// document.getElementById("addDialog").style.left = xPos + "px";
		// if(typeof(console) != "undefined") { console.log("xPos: " + xPos); }
		document.getElementById("addDialog").style.top = yPos - cbOffsetTop + "px";
		// document.getElementById("addDialog").style.top = yPos - scrollPos + "px";
		// if(typeof(console) != "undefined") { console.log("yPos: " + yPos);}
	}
	
	if(dojo.render.html.ie60) {
		document.getElementById("addDialog").style.zoom = 1; // Fix for menu not appearing on some list returns
	}
	
	/* need to reposition if we're off the screen */
	
	yOffset = document.getElementById("addDialog") != null ? document.getElementById("addDialog").offsetHeight : 0;
	xOffset = document.getElementById("addDialog") != null ? document.getElementById("addDialog").offsetWidth : 0;
	
	
	if(yPos + yOffset + yPad - scrollPosY > ciHeight) {
		// if(typeof(console) != "undefined") { console.log("repositioning AddActions yPos - yPos: " + yPos + " yOffset: " + yOffset + " ciHeight: " + ciHeight); }
		if(dojo.byId("content_body").contains(btnEl)) {
			document.getElementById("addDialog").style.top = yPos - cbOffsetTop - yOffset + "px";
		} else {
			document.getElementById("addDialog").style.top = yPos - scrollPosY - yOffset + "px";
		}
		// if(typeof(console) != "undefined") { console.log(document.getElementById("addDialog").style.bottom); }
		if(dojo.render.html.ie) {
			initalizePageHeight();
		}
	} /* else {
		if(dojo.byId("content_head").contains(btnEl)) {
			if(yPos + yOffset + yPad - scrollPos > cbOffsetTop + dojo.html.getBorderBox(dojo.byId("content_head")).height) {
				// document.getElementById("addDialog").style.top = yPos - cbOffsetTop - yOffset + "px";
				document.getElementById("addDialog").style.top = yPos - scrollPos - yOffset + "px";
			}
		}
		if(typeof(console) != "undefined") { console.log("AddActions yPos - yPos: " + yPos + " yOffset: " + yOffset + " ciHeight: " + ciHeight); }
	} */
	
	
	if(xPos + xOffset + xPad > ciWidth) {
		if(typeof(console) != "undefined") { console.log("repositioning AddActions xPos - xPos: " + xPos + " xOffset: " + xOffset + " ciWidth: " + ciWidth); }
		document.getElementById("addDialog").style.left = "auto";
		document.getElementById("addDialog").style.right = "5px";
		if(dojo.render.html.ie) {
			initalizePageHeight();
		}
	} else {
		if(typeof(console) != "undefined") { console.log("AddActions xPos - xPos: " + xPos + " xOffset: " + xOffset + " ciWidth: " + ciWidth); }
	}
}

function positionPlaylistPop() {
	yPos = document.getElementById("popupContainerClick").style.top.replace(/\D/g,'');
	yOffset = document.getElementById("popupContainerClick") != null ? document.getElementById("popupContainerClick").offsetHeight : 0;
	
	if((parseInt(yPos) + parseInt(yOffset)) > parseInt(ciHeight)) {
		yPos = parseInt(ciHeight) - parseInt(yOffset);
	}
	
	document.getElementById("popupContainerClick").style.top = yPos+"px";
}

function showAlbumArtPop(albumArtURL) {
	if(albumArtPopup) {
		displayPopup("<img src='" + albumArtURL + "' class='albumArtPop' alt='' width='150' height='150' />");
	}
}

function hideAlbumArtPop() {
	if(albumArtPopup) {
		hidePopup();
	}
}

function displayPopup(contents) {
	//ie7&6 fix
	if(dojo.render.html.ie) {
		yOffset = 204;
		if((yPos + yOffset) > ciHeight) {
			yPos = ciHeight - yOffset;
		}
		dojo.byId("popupContainer").style.top = yPos + "px";
	}
	
	contents = "<div class='main'>" + contents + "</div><div class='bottom'></div>";
	dojo.byId("popupContainer").innerHTML = contents;
	dojo.byId('popupContainer').style.opacity = 0;
	dojo.byId('popupContainer').style.display = "block";
	dojo.lfx.html.fadeIn('popupContainer', 700).play();
}

function hidePopup() {
	dojo.byId('popupContainer').style.opacity = 0;
	dojo.byId('popupContainer').style.display = "none";
	dojo.byId("popupContainer").innerHTML = "";
}

function showNewPlaylistPop(contentId, contentType) {
	if (!$SCData('r') || ($SCData('r') == '0')) {
		showVisitorPop();
		return;
	}
	if(!playlistPopActive) { // Make sure we don't already have our playlist pop-up showing so we don't overwrite the contentIdToAdd
		navigate("http://home.de.napster.com/info/new_playlist.html");
		dojo.byId('popupContainer').style.opacity = 0;
		dojo.byId('popupContainerClick').style.display = "block";
		dojo.byId('addPL').focus();
		setTimeout("_showPlaylistPop(1, " + contentId + "," + contentType + ")", 50);
	}
}

function showPlaylistPop(contentId, contentType, force) {
	if (!$SCData('r') || ($SCData('r') == '0')) {
		playlistPopActive = false;
		showVisitorPop();
		return;
	}
	if(!playlistPopActive || force) { // Make sure we don't already have our playlist pop-up showing so we don't overwrite the contentIdToAdd
		playlistPopActive = true;
		dojo.byId('popupContainer').style.opacity = 0;
		dojo.byId('popupContainerClick').style.display = "block";
		typeof(contentId) == "array"?contentIdToAdd = contentId:contentIdToAdd = new Array(new String(contentId));
		contentTypeToAdd = new Number(contentType);
		// setTimeout("_showPlaylistPop(0, " + contentId + "," + contentType + ")", 50);
		setTimeout("_showPlaylistPop(0)", 50);
	}
}

function _showPlaylistPop(newPlaylist, contentId, contentType) {
	if(newPlaylist) {
		dojo.byId('popupListContainer').style.display = "none";
		dojo.byId('popupContainerClick').getElementsByTagName("h3")[0].style.display = "none";
	} else {
		dojo.byId('popupListContainer').style.display = "block";
		dojo.byId('popupContainerClick').getElementsByTagName("h3")[0].style.display = "block";
	}
	positionPlaylistPop();
	if(typeof(contentId) != "undefined") { contentIdToAdd = contentId; }
	// alert(contentIdToAdd + " || " + contentTypeToAdd);
	if(typeof(contentId) != "undefined") { contentTypeToAdd = contentType; }
	playlistPopActive = true;
	if(typeof(contentIdToAdd) != "array") {
		if(contentIdToAdd != "") {
			contentIdToAdd = new String(contentIdToAdd);
			contentIdToAdd = [contentIdToAdd];
		} else {
			contentTypeToAdd = new String(contentTypeToAdd);
		}
	}
	dojo.byId('popupContainer').style.opacity = 100;
}

function reinitVisitorPop() {
	//reset the content of playlistPop to use this popup for the visitor mode messaging instead
	var mDiv = dojo.byId('popupContainerClick').getElementsByTagName('div')[0];
	
	mDiv.parentNode.className = "wide";
	
	mDiv.innerHTML = '<a href="#" class="closeBtn" onclick="hidePlaylistPop(); return false;">X</a>';
	mDiv.innerHTML += '<h3 id="visitor_hdr">Please Sign In or Sign Up to access this feature</h3>';
	
	mDiv.innerHTML += '<div class="visitor_d1"><p>Members</p><a href="#" onClick="top.location.href=\'https://sms.napster.com/DE/de/ns/login/\'; return false;">Sign In</a></div>';
	mDiv.innerHTML += '<div class="visitor_d2"><p>New to Napster?</p><a href="#" onClick="top.location.href=\'https://sms.napster.com/DE/de/ns/registration/standard/create_account.html\'; return false;">Sign up</a></div>';
}

var mframe;
function showVisitorPop() {
	//alert("xPos: " + xPos + " yPos: " + yPos);
	if(!playlistPopActive) { // Make sure we don't already have our playlist pop-up showing
		dojo.byId('popupContainer').style.opacity = 0;
		dojo.byId('popupContainerClick').style.display = "block";
		
		positionVisitorPop();
		playlistPopActive = true;
	}
}

function positionVisitorPop() {
	//
	var pcHt = dojo.html.getBorderBox(dojo.byId("popupContainerClick")).height;
	var pcWd = dojo.html.getBorderBox(dojo.byId("popupContainerClick")).width;
	
	var yvOffset = parseInt(pcHt) + 5;
	var xvOffset = parseInt(pcWd) + 18;
	
	if((yPos + yvOffset) > ciHeight) {
		dojo.byId('popupContainerClick').style.top = (ciHeight - yvOffset) + "px";
	} else if (yPos < 0) {
		dojo.byId('popupContainerClick').style.top = "0px";
	} else {
		dojo.byId('popupContainerClick').style.top = yPos + "px";
	}
	
	if((xPos + xvOffset) > ciWidth) {
		dojo.byId('popupContainerClick').style.left = (ciWidth - xvOffset) + "px";
	} else {
		dojo.byId('popupContainerClick').style.left = xPos + "px";
	}
	
	if(dojo.render.html.ie60) {
			//alert("test3");
			//position:absolute; left:-9999px; top:-9999px; width:0; height:0; margin:0; padding:0;
			//dojo.byId('popupContainerClick').innerHTML += "<iframe style='position:absolute; left: 0; top: 0; width: 50px; height: 50px; z-index: 2;'></iframe>";
			//+ dojo.byId('popupContainerClick').innerHTML;
			//Increase default zIndex of div by 1, so that DIV appears before IFrame
			var divPopup = dojo.byId('popupContainerClick');
			
            if ( (navigator.appName != "Microsoft Internet Explorer") || !document.documentMode ) {
                // non-IE8
                divPopup.style.zIndex=divPopup.style.zIndex+1;
            }
			
			mframe = document.createElement("IFRAME");
			mframe.setAttribute("src", "");
			
			//Match IFrame position with divPopup
			mframe.style.position= "absolute";
			mframe.style.left = divPopup.offsetLeft + 'px';
			mframe.style.top = divPopup.offsetTop + 'px';
			//can't use exact offsets because of drop shadow on box
			mframe.style.width = (divPopup.offsetWidth - 7) + 'px';
			mframe.style.height = (divPopup.offsetHeight - 7)+ 'px';
			
			document.body.appendChild(mframe);


		}
	
}

function hidePlaylistPop() {
	playlistPopActive = false;
	contentIdToAdd = "";
	contentTypeToAdd = "";
	dojo.byId('popupContainerClick').style.display = "none";
	if(dojo.render.html.ie60) {
		if(typeof(mframe) != "undefined") { document.body.removeChild(mframe) };
	}
}

function videoplayerPop(vidID) {
	//check for visitor mode
	//if (dojo.io.cookie.get('SL') == 'VIS') {
	if (!$SCData('r') || ($SCData('r') == '0')) {
		showVisitorPop();
		return;
	}

	//check if we're allowed to play videos
	//if ($C('LFM') == 'Y' || $C('SL') == 'LFM') {
	if (dojo.io.cookie.get('LFM') == 'Y' || dojo.io.cookie.get('SL') == 'LFM') {
		var success = confirm('This feature is only available to Napster subscribers, click \'OK\' to upgrade your membership.');
		if (success) myAccountPop(3); //change membership node
		return;
	}

	vidID = encodeURIComponent(vidID);
	var vidplay = window.open("http://home.de.napster.com/videos/popup_player.html?vidID=" + vidID,"videoplayerWin", "width=320,height=315,menubar=no,location=no,resizable=yes,scrollbars=no,status=no");
}


function Share( strIds, intType ) {
	strIds = new String( strIds );
	intType = parseInt( intType );
	var types = new Array();
	types[0] = 'tracks';
	types[1] = 'album';
	types[2] = 'admin_playlist';
	types[3] = 'playlist';
	types[4] = 'artist';
	var scShareVarsStr = 's.eVar1="Share"; s.eVar2="Button";s.eVar3=s.pageName;s.eVar5="' + types[intType] + '";';
	$SCEG( 'ContentAction',scShareVarsStr);
	var shareURL = "http://home.de.napster.com/DE/de/ns/share/index.html?type=" + types[intType] + "&ids=" + strIds;
	var sharePopUp = window.open(shareURL,"shareWin", "width=700,height=550,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
}

function myAccountPop( node ) {
	if (!$SCData('r') || ($SCData('r') == '0')) {
		showVisitorPop();
		return;
	}
	if(node == 7) { toolsPop(1); return; }
	var myaccount = window.open("https://sms.napster.com/account/de/?url=" + node, "accountWin", "width=815,height=600,menubar=no,location=no,resizable=yes,scrollbars=no,status=no");
	myaccount.focus();
}

function purchasePop() {
	var purchaseFlowJump =  dojo.io.cookie.get('POPUP');
	var purchaseURL = "https://sms.napster.com/DE/de/ns/purchase/clear/" + (purchaseFlowJump ? purchaseFlowJump : '');
	if(isClient()) {
		window.external.close("op=pop_url&height=600&width=815&url=" + purchaseURL); 
	}
	else {
		var purchase = window.open(purchaseURL, "purchaseWin", "width=815,height=600,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
		if(purchase) {
			if(purchase.focus) {
				purchase.focus();
			}
		}
	}
}

function toolsPop( node ) {
	if (!$SCData('r') || ($SCData('r') == '0')) {
		showVisitorPop();
		return;
	}
	var myaccount = window.open("https://sms.napster.com/account/de/tools.html?url=" + node, "accountWin", "width=800,height=600,menubar=no,location=no,resizable=yes,scrollbars=no,status=no");
	myaccount.focus();
}

function bundleRegPop() {
	//var bundlereg = window.open("http://sms.napster.com/duet/account/ac_bundle.html?type=upsell&country_code=DE&src=global_nav", "bundleWin", "width=800,height=600,menubar=no,location=no,resizable=yes,scrollbars=no,status=no");
	if(dojo.io.cookie.get('SL') == 'VIS') {
		var bundlereg = window.open("https://sms.napster.com/DE/de/ns/registration/bundle/?type=reg&country_code=DE&promo_id=BUNDLE_CREATIVEZV1GB_1Y'"); 
	}
	else {
	var bundlereg = window.open("https://sms.napster.com/DE/de/ns/registration/bundle/agree_terms.html", "bundleWin", "width=800,height=600,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes");
	}
	bundlereg.focus();
}

function renPlaylistPop() {
	//alert("xPos: " + xPos + " yPos: " + yPos + " ciWidth: " + ciWidth);

	//making this all in script - in case we can port this to the client later
	if(!document.getElementById("renPopup")) {
		var renpop = document.createElement('div');
		renpop.id = "renPopup";
		
		//can't get width so setting it by hand.
		var tWidth = 202;
		var oWidth = ciWidth - tWidth;
		var cxPos = (xPos > oWidth) ? oWidth : xPos;
		
		renpop.style.top = yPos + "px";
		renpop.style.left = cxPos + "px";
		
		var renpopMain = document.createElement('div');
		renpopMain.className = "main";
		
		var renform = document.createElement('form');
		renform.id = "renPForm";
		renform.onsubmit = renamePlaylist;
		
		renform.innerHTML += '<a href="#" class="closeBtn" onclick="ripRenPlaylistPop(); return false;">X</a>';
		renform.innerHTML += '<h3>Rename Playlist</h3>';
		renform.innerHTML += '<input type="text" id="renPopup_text" name="rename_text" size="20" />';
		renform.innerHTML += '<a href="#" onclick="renamePlaylist() ; return false;"><button class="btn_short"><span>Rename</span></button></a>';
		
		renpopMain.appendChild(renform);
		renpop.appendChild(renpopMain);
		
		renpop.innerHTML += '<div class="bottom"></div>';
		
		//insert
		var cntr = document.getElementById('container');
		cntr.appendChild(renpop);
		
		var rt = document.getElementById("renPopup_text");
		rt.value = currentPlaylistName;
		rt.focus();	
	}
}

function renamePlaylist() {
	//rename playlist
	//alert(currentPlaylistName);
	var newPlaylistName = document.getElementById("renPopup_text").value;
	if (newPlaylistName == "") {
		alert("Your new name contains no characters please re-enter");
		return;
	}
	//alert(newPlaylistName);
	currentPlaylistName = collection.store.uniquePlaylistName(newPlaylistName);
	//collection.store.uniquePlaylistName(playlistName);
	
	
	var pl = document.getElementById('playList');
	var plh2 = pl.getElementsByTagName('div')[0].getElementsByTagName('h2')[0];
	plh2.innerHTML = currentPlaylistName;
	
	var bbr = document.getElementById('breadcrumb_bar');
	var bbrp = bbr.getElementsByTagName('p')[0];
	
	bbrp.innerHTML = bbrp.innerHTML.replace(/Playlists - .+$/, "Playlists - " + currentPlaylistName);
	
	collection.store.updateCurrentPlaylist(0);
	
	allPlaylists = "";
	libraryDisplay.writeAllPlaylists();
	
	ripRenPlaylistPop();
	
	displayUMessage("Your playlist has been renamed", 1);
}

function ripRenPlaylistPop() {
	//remove ren playlist pop
	
	var renpop = document.getElementById("renPopup");
	//if(typeof(renpop) != "undefined") {
	if (renpop) {
		var parentDiv = renpop.parentNode;
		parentDiv.removeChild(renpop);
	}
}

//I'm being bad - this not strictly a popup but a link sorry
function linkNStuffH() {
	//var dli = dojo.byId('link_napster_stuff');
	//dli.className+=" sfhover";
	var dul = dojo.byId('napster_stuff_list');
	dul.style.left = '121px';
}

function linkNStuffO() {
	//var dli = dojo.byId('link_napster_stuff');
	//dli.className = dli.className.replace(new RegExp(" sfhover\\b"), "");
	var dul = dojo.byId('napster_stuff_list');
	dul.style.left =  '-999px';
	
}

//my hack footer link
function webFooterInclude() {
	var wfinc = document.getElementById("WFinc");
	
	var webfoot = document.createElement('div');
	webfoot.id = "GenWebFooter";
	
	webfoot.innerHTML += '<a href="#" onclick="genericPop(\'http://home.de.napster.com/info/terms.html\'); return false;">Terms and Conditions</a> | <a href="#" onclick="genericPop(\'http://home.de.napster.com/info/privacypolicy.html\'); return false;">Privacy Policy</a> | &copy; 2003-2007 Napster, LLC';
	
	var parentDiv = wfinc.parentNode;
	
	parentDiv.appendChild(webfoot);
	parentDiv.removeChild(wfinc);
}

function gatedLink(url,isPopUp) {
	//check for visitormode - if not go to link
	if (!$SCData('r') || ($SCData('r') == '0')) {
		showVisitorPop();
	} else {
		if(isPopUp) {
			genericPop(url);
		} else {
			var fUrl = "http://home.de.napster.com/" + url;
			navigate(fUrl);
		}
	}
}

/*
function genericPop(url) {
	var generic = window.open(decodeURIComponent(url), "genericWin", "width=800,height=600,menubar=no,location=no,resizable=yes,scrollbars=no,status=no");
}
*/
