if(typeof(SayRadio) == "undefined") {
  var SayRadio = {
    sessId : null,
    listenUrl : null,
    
    factory : null,
    radio : null,
    debug : false,
    startTryCnt : 0,
    createTryCnt : 0,
    
    isCreated : false,

    defaultCallback : {
      BeginStage : function(name, desc) {
        SayRadio.log("BeginStage(" + name + " . " + desc + ")");    
      },
      Progress : function(progress, desc) {
        SayRadio.log("Progress(" + name + " . " + desc + ")");
      },
      EndStage : function(name, result) {
        SayRadio.log("EndStage(" + name + " . " + result + ")");
      },
      Completed : function(result, pIUnk) {
        SayRadio.log("Complete(" + result + ")");
      }   
    },
    
    init : function(sessId, listenUrl, classid, codebase, commonConfigUrl, radioConfigUrl, debug) { 
      this.sessId = sessId;
      this.listenUrl = listenUrl;
      
      this.debug = debug;
      if(this.debug) this.initDebug();

      this.log("--- Initialize ---");
      
      //----- check factory installed -----------------------------------------
      this.factory = document.getElementById("SaycastFactory");
      if(!this.factory) {
        var obj = document.createElement("<object id=\"SaycastFactory\" classid=\"CLSID:" + classid + "\" codebase=\"" + codebase + "\" width=\"0\" height=\"0\"></object>");
        document.body.appendChild(obj);
        
        //----- execute application after download complete -------------------
        var iv = setInterval(function() {
          var e = document.getElementById("SaycastFactory");
          if(typeof(e.CreateExtension) != 'undefined') {
            clearInterval(iv);
            
            SayRadio.init(sessId, listenUrl, classid, codebase, commonConfigUrl, radioConfigUrl, debug);
          }
        }, 100);          
        //---------------------------------------------------------------------
        
        return;
      }
      //alert(typeof(this.factory));
      //return;
      /*
      this.factory = $("#SaycastFactory")[0];
      if(typeof(this.factory.CreateExtension) == 'undefined') {
        $("#SaycastFactory").remove();
        
        var obj = document.createElement("<object id=\"SaycastFactory\" classid=\"CLSID:" + classid + "\" codebase=\"" + codebase + "\" width=\"0\" height=\"0\"></object>");
        document.body.appendChild(obj);       
        
        //----- execute application after download complete -------------------
        var iv = setInterval(function() {
          var e = document.getElementById("SaycastFactory");
          if(typeof(e.CreateExtension) != 'undefined') {
            clearInterval(iv);
            
            SayRadio.init(sessId, listenUrl, classid, codebase, commonConfigUrl, radioConfigUrl, debug);
          }
        }, 100);          
        //---------------------------------------------------------------------

        return;
      }
      */
      //-----------------------------------------------------------------------
      this.log("--- Initialize ---");
      
      //this.factory.UpdateCommon(commonConfigUrl, false, this.defaultCallback);
      this.log("--- Create Common Extension ---");
      
      var common = this.factory.CreateExtension(commonConfigUrl, "", this.defaultCallback, this.factory);
      
      this.log("--- Create Common Extension ---");

      this.create(radioConfigUrl, common);
    },

    initDebug : function() {
      $("#sayradioDebug").remove();
      $("<textarea></textarea>").attr("id", "sayradioDebug").css("width", 800).css("height", 400).appendTo("body");
    },
    
    log : function(str) {
      if(this.debug)
        $("#sayradioDebug").append((new Date()).toString() + " " + str + "<br/>");
    },
    
    create : function(radioConfigUrl, common) {
      this.log("--- Create Radio Extension ---");

      try {
        this.radio = this.factory.CreateExtension(radioConfigUrl, "common/factory/SAYRADIO.PNG", $.extend($.extend(this.defaultCallback), {
          Completed : function(result, pIUnk) {
            SayRadio.log("Complete(" + result + ")");
            
            if(result != 0) {
              alert("SayRadio CreateExtension Error : " + result);
            }
          }
        }), common);
      }
      catch(e) {
        alert(e);
        return;
      }

      SayRadio.start();
      
      this.log("--- Create Radio Extension ---");
    },
    
    start : function() {
      //----- check radio object created. have to check -----------------------
      if(!this.radio) {
        this.startTryCnt++;
        if(this.startTryCnt < 15) {
          setTimeout(function() {
            SayRadio.start();
          }, 100);
        }
        else {
          alert("ERROR : CANNOT ACCESS RADIO START METHOD");
        }
        return;
      }
      //-----------------------------------------------------------------------

      if(SayRadio.radio.status & 0x800) { // running
        SayRadio.execute();
      }
      else {
        if(!(SayRadio.radio.status & 0x80)) {
          this.log("--- Verify ---");
          try {
            this.radio.Verify(SayRadio.defaultCallback);
          }
          catch(e) {}
          this.log("--- Verify ---");
        }

        this.log("--- Startup Radio Extension ---");
        this.radio.Start(false, $.extend($.extend(this.defaultCallback), {
          Completed : function(result, pIUnk) {
            SayRadio.log("Complete(" + result + ")");
            
            if(result == 0 || result == 1) {
              SayRadio.execute();
            }
          }
        }));
        this.log("--- Startup Radio Extension ---");
      }
    },
    
    execute : function() {
      this.log("--- Execute ---");
      try {
        var url = SayRadio.listenUrl;
        if(SayRadio.sessId) {
          if(url) url += "?";
          url += "SessionID=" + SayRadio.sessId;
        }

        SayRadio.radio.Execute(url, SayRadio.defaultCallback);
      }
      catch(e) {}
      this.log("--- Execute --- ");
    },
    
    showRadioProperty : function() {
      this.log("--- Radio Extension Property ---");
      try {
        this.log("Extension Name = " + this.radio.name);
        this.log("Extension Link = " + this.radio.link);
        
        var status = radio.status;
        if(status == 0)
          this.log("Extension is NOT available.");
        else {
          if(status & 0x02)
            this.log("Extension is NOT installed.");
          if(status & 0x08)
            this.log("Extension installed.");
          if(status & 0x20)
            this.log("Extension needs update.");
          if(status & 0x80)
            this.log("Extension verified.");
          if(status & 0x400)
            this.log("Extension is running.");
        }
      }
      catch (e) {alert(e);}

      this.log("--- Radio Extension Property ---");
    }
  };
}

function openPlayer(id) 
{
  var url = "http://" + id + ".saycast.com/listen.pls";
    
  SayRadio.init(radioSessId, url, saycastFactoryClassid, saycastFactoryCodebase, saycastFactoryCommonConfig, saycastFactoryRadioConfig, false);
}

function openPlayerPrivate(pCjId, pChannel)
{
  var url = "http://www.saycast.com/" + pCjId + ".pls";

  SayRadio.init(radioSessId, url, saycastFactoryClassid, saycastFactoryCodebase, saycastFactoryCommonConfig, saycastFactoryRadioConfig, false);
}

