
/* Common JavaScript functions for sirius.com */
/* **********************************
 * Opens the Media Player window 
 * DEPRECATED:  Use launchPlayer()
 * **********************************/
function openMediaPlayer(winName, genre, channel, source, target) {

	var sourceString = "";
	
	//setup source if exists
	if(source != null && source.length >  0) {
		sourceString = "source=" + source;	
	}
	
	//setup url
	var url = "http://mediaplayer.sirius.com/servlet/MediaPlayer?activity=expand&streamNumber=" + channel + "&" + sourceString;
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=575,width=650,screenX=10,screenY=10,left=10,top=10";
	
	//determine target media player page if requested - default is selection screen
	if(target == "register") {
		url = "http://mediaplayer.sirius.com/servlet/MediaPlayerRegistration?" + sourceString;
	} else if(target == "guestlogin") {
		url = "http://mediaplayer.sirius.com/servlet/MediaPlayer?activity=selectLoginType&type=guest&" + sourceString;
	} else if(target == "sublogin") {
		url = "http://mediaplayer.sirius.com/servlet/MediaPlayer?activity=selectLoginType&type=subscriber&" + sourceString;
	}

	var newWin = window.open(url, "FullMediaPlayer", features);
	return;
}

/* ********************************** 
* Opens the Media Player window  
* **********************************/ 
function launchPlayer(genreKey, channelKey, source, target) { 
      
     //setup source if exists 
     /*alert("genreKey:"+genreKey); 
          alert("channelKey:"+channelKey); 
               alert("source:"+source); 
                    alert("target:"+target);*/ 
     var sourceString = ""; 
     if(source != null && source.length >  0) { 
          sourceString = "source=" + source;      
     } 
      
     var domain = "http://www.sirius.com";  
      
     var url = domain + "/sirius/servlet/MediaPlayer?stream=" + channelKey + "&" + sourceString; 
      
     if(target == "register") { 
          url = domain + "/sirius/servlet/MediaPlayerRegistration?" + sourceString; 
     } else if(target == "guestlogin") { 
          url = domain + "/sirius/servlet/MediaPlayer?activity=selectLoginType&type=guest&" + sourceString; 
     } else if(target == "sublogin") { 
          url = domain + "/sirius/servlet/MediaPlayer?activity=selectLoginType&type=subscriber&" + sourceString; 
     } 
      
     var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=575,width=650,screenX=10,screenY=10,left=10,top=10"; 
     var newWin = window.open(url, "FullMediaPlayer", features); 
 
     return; 
}

/* *********************************************** 
 *Opens the Email Update window with the input form 
 * ***********************************************/
function openEmailSignup(target) {
	var win = window.open(target, 
												"email_updates", 
												"menubar=no,locationbar=no,status=no,resizable=yes,height=200,width=400");
	return;
}

/** ***********************************************************
 * Pops open a new window for email to a friend.
 * This method adds the additional decrypt flag to indicate whether or not the
 * URL for the email is encrypted or not.
 *
 * decrypt = true for encryption/decryption
 * ************************************************************/
function popEmailWindow(szTitle,szURL,szPageName, decrypt) {
	var nWidth = 552;
	var nHeight = 400;
		
	var szFeatures = "menubar=no,resizable=no,scrollbars=no,toolbar=no,height="+nHeight+",width="+nWidth;
	var szBaseURL = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/EmailToFriend&c=Article&cid=1026142020450";
	var szWinURL = szBaseURL + "&p1=" + escape(szTitle) + "&p2=" + escape(szURL) + "&p3=" + escape(szPageName) + "&decrypt=" + decrypt;
	wEmailWin = window.open(szWinURL,"EmailWin",szFeatures);
	wEmailWin.focus();
	
	return;
		
}

/* ******************************************************
 * Opens a new window for the Single Stream Media Player 
 * ******************************************************/
function openSingleStreamPlayer(stream) {

	var width = 552;
	var height = 350;

	var url = "http://mediaplayer.sirius.com/servlet/SingleMediaPlayer?stream=" + escape(stream);
	var windowName = "SIRIUS_Single_Stream_Player";
	var features="menubar=no,locationbar=no,status=no,resizable=no,height=" + height + ",width=" + width;
	var newWin = window.open(url,windowName,features);
	return;
}

/* *********************************************************
 * View BackStageAsset window 
 * *********************************************************/
function viewBackstageAsset(assetId) {

	var url = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Page&c=BackStageAsset&cid=" + assetId;
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=400,width=400";
	var newWin = window.open(url,"BackStageAsset",features);

	return;
}

/* *********************************************************
 * View Slideshow window 
 * *********************************************************/
function viewSlideShow(collectionId) {

	var url = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Page&c=Collection&cid=" + collectionId;
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=390,width=390";
	var newWin = window.open(url,"Collection",features);

	return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//	setCookie() - Create a cookie with the specified name and value.
//  Args:
//      sName                       - name of the cookie
//      sValue                      - value of the cookie
//      sDomain[OPTIONAL]           - Setting the domain of the cookie allows pages on a domain made up
//                                      of more than one server to share cookie information
//      sPath[OPTIONAL]             - Setting a path for the cookie allows the current document to share cookie
//                                      information with other pages within the same domain
//      sExpires[OPTIONAL]          - Setting no expiration date on a cookie causes it to expire when
//                                      the browser closes. If you set an expiration date in the future, the cookie
//                                      is saved across browser sessions. If you set an expiration date in the past,
//                                      the cookie is deleted. Use GMT format to specify the date.
////////////////////////////////////////////////////////////////////////////////////////////////////////
function setCookie(sName, sValue, sDomain, sPath, sExpires) {
	if(sName) {
		var sCookie = sName + "=";
		if(sValue)
			sCookie += escape(sValue);
		if(sPath)
		   sCookie += "; path=" + sPath;
		if(sDomain)
			sCookie += "; domain=" + sDomain;
		if(sExpires)
			sCookie += "; expires=" + sExpires;
		document.cookie = sCookie;
	}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
//	getCookie() - Retrieve the value of the cookie with the specified name.
//  Args:
//  sName                       - name of the cookie
////////////////////////////////////////////////////////////////////////////////////////////////////////
function getCookie(cookieName) {
	var cookieArray = document.cookie.split("; ");
	for(k in cookieArray)
	{
		var cookie = cookieArray[k].split("=");
		if (cookieName == cookie[0])
			return unescape(cookie[1]);
	}
	return null;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
//	deleteCookie() - Deletes the cookie with the specified name.
//  Args:
//  sName                       - name of the cookie
////////////////////////////////////////////////////////////////////////////////////////////////////////
function deleteCookie(cookieName, path, domain)
{
	setCookie(cookieName, null, domain, path, "Thu, 01-Jan-70 00:00:01 GMT")
}

/** *************************************************
 * Appends the users source (from SIRIUS_CAMPAIGN cookie) to the input URL
 * **************************************************/
function getSourceUrl(instring) {
	var campaign_code = getCookie("SIRIUS_CAMPAIGN");	
	if (campaign_code != null) {
		if (instring.indexOf("?") == -1)
			instring += "?source=" + campaign_code;
		else
			instring += "&source=" + campaign_code;
	}
	return instring;
}

/** *************************************************
 * Appends the users sid (from SIRIUS_VISITOR_ID cookie) to the input URL
 * **************************************************/
function getSIDUrl(instring) {
	var sidVal = getCookie("SIRIUS_VISITOR_ID");	
	if (sidVal != null) {
		if (instring.indexOf("?") == -1)
			instring += "?sid=" + sidVal;
		else
			instring += "&sid=" + sidVal;
	}
	return instring;
}

/** *************************************************
 * Appends the users source and sid 
 * (from SIRIUS_CAMPAIGN and SIRIUS_VISITOR_ID cookie) to the input URL
 * **************************************************/
function getSourceAndSIDUrl(instring) {
	var campaign_code = getCookie("SIRIUS_CAMPAIGN");	
	if (campaign_code != null) {
		if (instring.indexOf("?") == -1)
			instring += "?source=" + campaign_code;
		else
			instring += "&source=" + campaign_code;
	}
	var sidVal = getCookie("SIRIUS_VISITOR_ID");	
	if (sidVal != null) {
		if (instring.indexOf("?") == -1)
			instring += "?sid=" + sidVal;
		else
			instring += "&sid=" + sidVal;
	}
	return instring;
}

/** ***********************************************************
 * opens the SIRIUS Direct site and passes in the users source code if present
 * ************************************************************/
function siriusDirect() {
	var url = getSourceUrl("https://home.sirius.com/webDUWI/HomePage.aspx");
	window.location = url;
}

/** **********************************************
 * Pre-load images for rollover effects
 * ***********************************************/
function preload(name,dir) {
	eval(
		name + "_on = new Image();" + 
		name + "_on.src = \"" + dir + name + "_on.gif\";" + 
		name + "_off = new Image();" + 
		name + "_off.src = \"" + dir + name + "_off.gif\";"
	);
}

/** **************************************************
 * load a new image
 * ***************************************************/
function loadImage(name, img) {
	if (document.images) {
		document.images[name].src = img.src;
	}
}

/* ****************************************************
 * Opens new window 
 * ****************************************************/
function pop(url,name,h,w,s,t,m,r){
	var wWin = null;
	if(uBN=="MSIE" && uOS=="Macintosh"){r = "no"};
	if(document.location.href.substring(0,5) == "https"){
		name="https";
	}
	if(aol){
		wWin = window.open(url,name,"height="+h+",width="+w+"scrollbars="+s+",toolbar="+t+",menubar="+m+",locationbar=no");

		//wWin.resizeTo(w,h);

		wWin.focus();
	} else {
		x=(screen.availWidth-w);
		y=(screen.availHeight-h);
		if (name.document) {
			name.document.location.href=url;
		} else {
			wWin=window.open(url,name,"height="+h+",width="+w+",scrollbars="+s+",toolbar="+t+",menubar="+m+",resizable="+r+",status=yes,location=no,screenX="+x/2+",screenY="+y/2+",left="+x/2+",top="+y/2);
			//wWin.resizeTo(w,h);
			wWin.focus();
		}
	}
}

/** ***************************************************
 * Returns the value of the input query string parameter
 * ****************************************************/
function getParaValue(paraName) {
	var searchString = location.search;
	
	if (searchString == null)
		return null;
		
	var fields = searchString.substring(1).split("&");
	for (var i = 0; i < fields.length; i++) {
		tmp = fields[i].split("=");
		if (tmp[0] == "source") {
			if (typeof(tmp[1]) == "undefined")
				return null;
			var sourceVal = unescape(tmp[1]);
			var validChars = "abcdefghijklmnopqrstuvwxyz0123456789";
			var letter = sourceVal.charAt(0).toLowerCase();
			if (validChars.indexOf(letter) == -1)
				return null;
			else
				return sourceVal;
		}		
	}
	return null;
}

/* ***********************************************************
 * Set cookie for SIRIUS_CAMPAIGN, if source is found on query string
 * This script runs every time this file is loaded
 * ***********************************************************/
var expires = new Date();
expires.setTime(expires.getTime() + (30*24*60*60*1000));
var campaignCookie = getParaValue("source");

if (campaignCookie) {
	setCookie("SIRIUS_CAMPAIGN", campaignCookie, null, "/", expires.toGMTString());
}

// Clear Field
function clearField(fldObj) {
	fldObj.value = "";
}
  
// Diplay Default
function populateField(fldObj,val) {
	if (fldObj.value == "") {
		fldObj.value = val;
	}
}
/** ***********************************************************
 * Talk To Us PopUp Window 
 *
 * Parameter - Channel Id
 * ************************************************************/
function popTalkToUsWindow(channelId) {
	var nWidth = 550;
	var nHeight = 374;
		
	var szFeatures = "menubar=no,resizable=no,scrollbars=no,toolbar=no,height="+nHeight+",width="+nWidth;
	var szBaseURL = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Channel/TalkToUs&cid="+channelId;
	wTalkToUsWin = window.open(szBaseURL,"TalkToUs",szFeatures);
	wTalkToUsWin.focus();
	
	return;
		
}
/** ***********************************************************
 * Request A Song PopUp Window 
 *
 * Parameter - Channel Id
 * ************************************************************/
function popRequestASongWindow(channelId) {
	var nWidth = 550;
	var nHeight = 374;
		
	var szFeatures = "menubar=no,resizable=no,scrollbars=no,toolbar=no,height="+nHeight+",width="+nWidth;
	var szBaseURL = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Channel/RequestASong&cid="+channelId;
	wRequestASongWin = window.open(szBaseURL,"RequestASong",szFeatures);
	wRequestASongWin.focus();
	
	return;	
}

/** **************************************************************
 * Open a basic new window.  Used for opening new windows from flash movies
 *
 *****************************************************************/
function openWindow(url) {
	var newWin = window.open(url,"");
	newWin.focus();
}

/** ****************************************************************
 * processes a link tracking call from flash movies
 *
 *******************************************************************/
function trackLink(linkName) {

	if(linkName != null && linkName.length > 0) {
		s_linkType = "o";
		s_eVar11 = linkName;
		s_linkName = linkName;
		s_prop5 = s_pageName;
		s_lnk = s_co(this);
		s_gs(s_account);
	}
	return;
}

/* **********************************
 * Opens the Radio Guide window 
 * **********************************/
function launchRadioGuide() {
	
	var url = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Common/RadioGuide";
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=515,width=795,screenX=10,screenY=10,left=10,top=10";
	var newWin = window.open(url, "RadioGuide", features);

	return;
}

/* **********************************
 * Opens the Flash Demo window 
 * **********************************/
function launchDemo() {
	
	var url = "http://mediaplayer.sirius.com/servlet/ContentServer?pagename=Sirius/Common/Demo";
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=275,width=605,screenX=10,screenY=10,left=10,top=10";
	var newWin = window.open(url, "SiriusFlashDemo", features);

	return;
}

/** ***********************************************************
 * opens the Music Mayhem site and passes in the users source code if present
 * ************************************************************/
function musicMayhem() {
	var url = getSourceUrl("http://mayhem.sirius.com/index.html");
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=600,width=800,screenX=10,screenY=10,left=10,top=10";
	var newWin = window.open(url, "MusicMayhem", features);
	return;
}

/** ***********************************************************
 * opens the Get Sirius page in the parent window - used for the DEMO pup-up : Organic's solution
 * ************************************************************/
function getSirius(){
    window.opener.location.href = 'http://mediaplayer.sirius.com/getsirius';
    window.close();
}

/* **********************************
 * Launch the Flash Video Player 
 * **********************************/
function launchVideoPlayer(pageName, flashMovie) {
	
	var url = "http://mediaplayer.sirius.com/common/flash/video_player.jsp?pageName=" + escape(pageName) + "&flashMovie=" + escape(flashMovie);
	var features = "menubar=no,locationbar=no,status=no,resizable=yes,height=260,width=320,screenX=10,screenY=10,left=10,top=10";
	var newWin = window.open(url, "SiriusVideoPlayer", features);

	return;
}
