

// file: PandoraAPIv2.js

if (typeof(Pandora) == "undefined") {
	Pandora = {
		// --------------------------
		//        public API
		// --------------------------
		HOMEPAGE_WINDOW_TARGET: "PandoraTuner",
		BACKSTAGE_WINDOW_TARGET: "pandoraContent",
		PERFORM_HOMEPAGE_LAUNCH: true,
		PING: true,
        eiWorking : null,
		
		setEventHandler: function(event, handler) {
			this.eventHandlers[event] = handler;
			this.registerForEventsIfNecessary();
		},

        pauseMusic: function(blnPause) {
            if (blnPause){
                this.sendTunerCommand("ext_pause", "");
            }else{
                this.sendTunerCommand("ext_play", "");
            }
        },

        launchFriendsPanel: function() {
            this.sendTunerCommand("ext_friends", "");
        },
        
        launchGenrePanel: function(/*string*/genre) {
            this.sendTunerCommand("ext_genre", genre);
        },

        launchCreateStation: function() {
            this.sendTunerCommand("ext_sh_sc", "");
        },

        launchShare: function(shareType) {
            this.sendTunerCommand("ext_share", shareType);
        },

		launchStationFromId: function(/*string*/ id) {
			this.sendTunerCommand("ext_lsfi", id);
		},
		
		launchStationFromMusicId: function(/*string*/ musicId) {
			this.sendTunerCommand("ext_lsfmi", musicId);
		},
		
		launchStationFromSearchText: function(/*string*/ searchText) {
			this.sendTunerCommand("ext_lsfst", searchText);
		},

		launchQuickMixFromId: function(/*string*/ id) {
			this.sendTunerCommand("ext_lqmfi", id);
		},
		
		launchRegistration: function() {
			this.sendTunerCommand("ext_reg", "1");
		},
		
		launchSubscription: function() {
			this.sendTunerCommand("ext_sub", "1");
		},

        launchVideoAd: function(/* string */ key, /* string */ value) {
            this.sendTunerCommand("ext_vid", key + "=" + value);
        },

        launchForcedVideoAd: function(/* string */ key, /* string */ value) {
            this.sendTunerCommand("ext_fvid", key + "=" + value);
        },
        
        launchAddVariety: function() {
            this.sendTunerCommand("ext_add_vrty");
        },
        
        launchRenewalPanel: function() {
            this.sendTunerCommand("ext_renew");
        },
        
        declineRenewal: function() {
            this.sendTunerCommand("ext_no_renew");
        },

        restart: function() {
            this.sendTunerCommand("ext_rst", "1");  
        },

        /*
         an ad calls this when it needs to display an ad that uses demog info,  the tuner calls a call back hook with the info
         */
		requestAdDemogCallback: function(function_callback) {
			window['ad_demog_callback'] = function_callback;

			this.sendTunerCommand("ext_demog_ad", "");
		},

		launchShareCurrentStation: function() {
			this.sendTunerCommand("ext_lss", "");
		},
		
		getBaseUrl: function() {
			return 'http://www.pandora.com/';
		},
		
		// ---------------------------
		//   private utility methods
		// ---------------------------

		// the callback will be passed a boolean value indicating whether
		// or not the LocalConnection was successful.
		// This is used by custom radio ads.  Change those if you change this.
		verifyLocalConnection: function(/*string*/ callbackHandlerName) {
			this.sendTunerCommand("ext_ping", "", callbackHandlerName);
		},

		// NOTE: the Yahoo Messenger plugin references this API.  If you
		// change the name of this method, you'll need to update the
		// Yahoo plugin code too.
		// This fixes RADIO-2264.
		sidebarWindow: null,
		sidebarLaunchStation: function(/*string*/ type, /*string*/ id) {
			if (this.sidebarWindow && !this.sidebarWindow.closed) {
				// We've already opened a window, so use Messenger to communicate with it
				this.sendTunerCommand(type, id);
			} else {
				// Open a new window
				this.sidebarWindow = window.open(this.getBaseUrl() + '?' + type + '=' + escape(id), 'PandoraTuner');
			}
		},
		
		eventHandlers: {},
		registeredForEvents: false,
		
		registerForEventsIfNecessary: function() {
			if (this.registeredForEvents)
				return;
			
			var div = this.getDiv("tunerEventListenerDiv");
			if( div == null ) {
				div = document.createElement("DIV");
				div.id = "tunerEventListenerDiv";
				document.documentElement.appendChild(div);
			}
			this.embedFlash("PandoraEventsv2.swf", div, { doPing: this.PING });
			this.registeredForEvents = true;
		},
		
		fireEvent: function(eventName, /*Array*/ args) {
			if (this.eventHandlers[eventName] != null) {
				this.eventHandlers[eventName](args);
			}
		},
		
		sendTunerCommand: function(/*string*/ tunerVarName, /*string*/ param, /*string|null*/ callbackHandlerName) {
            // if the externalInterface is working then we prefer to use it, as
            // is is more reliable than the Messenger. If it's not working (maybe
            // we're 
            var sec = this.getSecretary();
            if (sec != null && this.eiWorking) {
                sec.callTuner(tunerVarName, param, callbackHandlerName);
                return;
            }
			var div = this.getDiv("tunerMessengerDiv");
			if( div == null ) {
				div = document.createElement("DIV");
				div.id = "tunerMessengerDiv";
				document.documentElement.appendChild(div);
			}
			this.setEventHandler("MessengerStatus", function(data) {
				Pandora.onTunerCommandAttempt(data.success, data.method, data.param);
			});
			this.embedFlash("Messenger.swf",
							div, 
							{ 
								method: tunerVarName, 
								param: param, 
								callback: callbackHandlerName
							});
		},
		
		onTunerCommandAttempt: function(success, method, param) {
			if (!success && Pandora.PERFORM_HOMEPAGE_LAUNCH) {
				var args = {};
				args[method] = param;
				this.openHomepageWithArgs(args);
			}
		},
        
        getTrackingCodeCookie: function() {
            var cookies = document.cookie.split(";");
            for(var i = 0; i < cookies.length; i++) {
                var crumb = cookies[i].split("=");
                if (crumb[0].replace(/^\s\s*/, '') == "tc") {
                    return crumb[1];
                }
            }
            return null;
        },
		
		// Yahoo plugin doesn't support POST, so the plugin will override
		// this value to be GET.
		homepageLaunchMethod: "POST",
		openHomepageWithArgs: function(args) {
			var form = document.createElement("FORM");
			form.style.display = "none";
			form.setAttribute("method", this.homepageLaunchMethod);
            var tc = this.getTrackingCodeCookie();
            if (tc != null) {
                form.setAttribute("action", this.getBaseUrl() + "?tc=" + tc);
            } else {
			    form.setAttribute("action", this.getBaseUrl());
            }
			form.setAttribute("target", this.HOMEPAGE_WINDOW_TARGET);
			for (var n in args) {
				var input = document.createElement("INPUT");
				input.setAttribute("type", "hidden");
				input.setAttribute("name", n);
				input.setAttribute("value", args[n]);
				form.appendChild(input);
			}
			document.body.appendChild(form);
			form.submit();
		},
	
		embedFlash: function(/*string*/ swf, /*dom element*/ elem, /*hash*/ params) {
			var flashVarsStr = "";
			if (params != null) {
				for (var n in params) {
					if (params[n] == null)
						continue;

					var param = String(params[n]);
										
					if (flashVarsStr != "") 
						flashVarsStr += "&";
					
					// lame but extremely backward compatible mechanism for
					// escaping plus signs, since the escape method doesn't always do that.
					while (param.indexOf("+") != -1) {
						param = param.replace("+", "%20");
					}
					flashVarsStr += n + "=" + escape(param);
				}
			}
			
			var swfUrl = this.getBaseUrl() + "radio/" + swf;
			elem.innerHTML = [
				'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"',
				'		codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"',
				'		WIDTH="0"', 
				'		HEIGHT="0"',
				'       id="Messenger">',
				'	<PARAM NAME=movie VALUE="' + swfUrl + '">',
				'	<PARAM NAME=quality VALUE=high>',
				'	<PARAM NAME=bgcolor VALUE=#FFFFFF>',
				'	<PARAM NAME=allowscriptaccess value=always>',
				'	<PARAM NAME=menu VALUE=false>',
				' <PARAM NAME="FlashVars" VALUE="' + flashVarsStr + '">',
				'<EMBED src="' + swfUrl + '"',
				'       quality=high',
				'       bgcolor=#FFFFFF',
				'       allowscriptaccess=always',
				'       WIDTH="0"',
				'       HEIGHT="0"',
				'       MENU="false"',
				'       NAME="Messenger" ALIGN="" TYPE="application/x-shockwave-flash"',
				'       PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"',
				'       FlashVars="' + flashVarsStr + '">',
				'</EMBED>',
				'</OBJECT>'
			].join("");
		},
			
		getDiv: function(divID) {
			if( document.getElementById ) {
				// standard (IE 5+)
				return document.getElementById(divID);
			} else {
				// IE 4
				return document.all[divID];
			}
		},
        
        getSecretary: function() {
            if (navigator.appName != null && navigator.appName.indexOf("Microsoft") != -1) {
                return window['Secretary'];
            }
            return document['Secretary'];
        },

        // ------------------------
		//   private helper methods
		// ------------------------

        setFacebookShareUserTokens: function(/* String */ uid, /*String*/ sessionKey, /*String*/ sessionSecret) {
            var userTokens = isEmpty(sessionKey)
                    ? null
                    : uid + "|" + sessionKey + "|" + sessionSecret;

            this.sendTunerCommand("ext_fb_ssut", userTokens);
        },

        setTwitterShareUserTokens: function(/*String*/ sessionKey, /*String*/ sessionSecret){
            var userTokens = isEmpty(sessionKey)
                    ? null
                    : sessionKey + "|" + sessionSecret;

            this.sendTunerCommand("ext_tw_ssut", userTokens);
        },

        markTwitterFacebookConnectorAvailable: function(callback) {
            this.sendTunerCommand("ext_tw_fb_con_avail", callback);
        },

        onFacebookPublishSuccess: function(){
            this.sendTunerCommand("ext_fb_ps");
        },

        onFacebookPublishCanceled: function() {
            this.sendTunerCommand("ext_fb_pc", "");
        },

        onFacebookPublishFailed: function(info) {
            if (info){
                info = info.split("'").join("");
            }

            this.sendTunerCommand("ext_fb_pf", info);
        },

        onTwitterPublishSuccess: function(){
            this.sendTunerCommand("ext_tw_ps", "");
        },

        onTwitterPublishFailed: function(info) {
            this.sendTunerCommand("ext_tw_pf", info);
        },

        showFacebookTwitterShareConfirmation: function() {
            this.sendTunerCommand("ext_ft_sf", "");
        },

        showFacebookTwitterShareWaitLightbox: function() {
            this.sendTunerCommand("ext_ft_slw", "SHOW");
        },

        hideFacebookTwitterShareWaitLightbox: function() {
            this.sendTunerCommand("ext_ft_slw", "HIDE");
        },
        
        setFacebookSession: function(uid, sessionKey, secret) {
            this.sendTunerCommand("ext_fb_sess", 
                  uid + "," + sessionKey + "," + secret);  
        },

		setFacebookConnectState: function(/*String*/ state) {
			this.sendTunerCommand("ext_fb_st", state);
		},

		facebookDisconnect: function() {
			this.sendTunerCommand("ext_fb_dc");
		},

        showCreateStation: function(){
            this.sendTunerCommand("ext_sh_sc", "");
        },

        checkExternalInterface: function() {
            if (!this.eiWorking) {
                this.eiWorking = false;
                var sec = this.getSecretary();
                if (sec != null) {
                    sec.eiPing("eiPong");
                }
            }
        },

        sendMaxUrlLimit: function(/* int */ limit){
            this.sendTunerCommand("ext_smul", limit);
        }
	};
    
    // check if the external interface is working
    Pandora.checkExternalInterface();
    
    function eiPong() {
        Pandora.eiWorking = true;
    }
    
    Pandora.secretaryRetries = 10;
    
    // wait for up to 5s for the secretary to appear and start working
    // on the external interface. This is used for integrations on our
    // homepage that need to pass information into the tuner, but where the
    // secretary and tuner may not be ready for the data when it is ready for
    // them.
    //
    // The callback will be called with true if the secretary is ready and the
    // external interface is working, false if it is not. Getting a false doesn't
    // necessarily mean that local connection is broken, as the user could be on 
    // flash 6 or 7, so publicapi calls may still work through the messenger
    Pandora.waitForSecretary = function(callback) {
        if (Pandora.secretaryRetries <= 0) {
            callback(false);
            return;
        }
        
        if (Pandora.eiWorking) {
            callback(true);
        } else {
            Pandora.secretaryRetries--;
            setTimeout(function() {
                Pandora.waitForSecretary(callback);
            }, 1000);
        }
    }
    
}




drawPandora('<div id=\"pandoraPluginStations\"><div id=\"pandoraTitleStations\">MY STATIONS</div><div id=\"pandoraContentStations\"><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=22731015638387545\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'22731015638387545\'); return false;\">Baby Baby Radio</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=22623589916379993\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'22623589916379993\'); return false;\">Three Days Grace Radio</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=6052115734760281\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'6052115734760281\'); return false;\">Secret Garden Radio</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=110955802881330009\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'110955802881330009\'); return false;\">Celtic Chillout</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=110811586469464921\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'110811586469464921\'); return false;\">R&amp;B Smooth</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lsfi=110056750262160217\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'110056750262160217\'); return false;\">Rock it up</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lqmfi=8555353\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lqmfi\', \'8555353\'); return false;\">frezz1n\'s QuickMix</a></div></div></div></div><div id=\"pandoraFormDivStations\"><script>function createStation(formName) {var pForm = document.getElementById(formName);if (pForm.search.value != \"artist or song name\")pForm.submit();}function selectInput(formName) {var pForm = document.getElementById(formName);if (pForm.search.value == \"artist or song name\")pForm.search.value = \"\";}</script><form method=\"get\" id=\"pandoraForm1574170079\" target=\"pandoraFeedTarget\" action=\"http://www.pandora.com/\">Create a new station:<br><input onfocus=\"selectInput(\'pandoraForm1574170079\')\" value=\"artist or song name\" type=text name=search size=\"15\"></input> <input id=\"pandoraButton\" type=\"submit\" value=\"create station\" onclick=\"createStation(\'pandoraForm1574170079\')\"></form></div><div id=\"pandoraFooterStations\">powered by <a href=\"http://www.pandora.com\" target=\"pandoraFeedTarget\">PANDORA</a></div></div>');
function drawPandora(s){
document.write(s);
}

