

// 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();
		},
		
		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");
		},

		launchExtra: function(id) {
			this.sendTunerCommand("ext_extra", id);
		},

        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");
        },

        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);
			}
		},
		
		// 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);
			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*/ sessionKey, /*String*/ sessionSecret) {
            var userTokens = isEmpty(sessionKey)
                    ? null
                    : 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");
        },


        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;
    }
    
}




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=27517314367159930\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lsfi\', \'27517314367159930\'); return false;\">Damfino Radio</a></div></div></div><div class=\"blogrollitemStations\"><div class=\"stationitemStations\"><div class=\"stationnameStations\"><a href=\"http://www.pandora.com/?ext_lqmfi=67567226\" onclick=\"Pandora.sidebarLaunchStation(\'ext_lqmfi\', \'67567226\'); return false;\">jfindlay9\'s QuickMix</a></div></div></div></div><div id=\"pandoraFooterStations\">powered by <a href=\"http://www.pandora.com\" target=\"pandoraFeedTarget\">PANDORA</a></div></div>');
function drawPandora(s){
document.write(s);
}

