/** 
 * @fileoverview Documentazione della classe base che avvolge il plugin 
 * Macromedia Flash e gestisce le funzionalitą base
 *
 * @author Salvi Andrea - Globo srl
 */


/**
 * @class Classe base che avvolge il plugin Macromedia Flash
 *
 * @constructor
 */
function FlashMXObject() {
}

/**
 * Esegue la rilevazione del plugin Macromedia Flash
 * @return true se ha rilevato il plugin o false in caso contrario
 * @type boolean
 */
FlashMXObject.prototype.detectPlugin = function() {
  var pluginlist;
  var agt=navigator.userAgent.toLowerCase();
  var ie  = (agt.indexOf("msie") != -1);
  var ns  = (navigator.appName.indexOf("Netscape") != -1);
  var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
  var mac = (agt.indexOf("mac")!=-1);
  
  if (ie && win) {    
    pluginlist = detectIE("Adobe.SVGCtl","SVG Viewer") + 
                 detectIE("SWCtl.SWCtl.1","Shockwave Director") + 
                 detectIE("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash") + 
                 detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + 
                 detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + 
                 detectIE("MediaPlayer.MediaPlayer.1","Windows Media Player") + 
                 detectIE("PDF.PdfCtrl.5","Acrobat Reader"); }
  if (ns || !win) {
    nse = ""; 
    for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
    pluginlist = detectNS("image/svg-xml","SVG Viewer") + 
                 detectNS("application/x-director","Shockwave Director") + 
                 detectNS("application/x-shockwave-flash","Shockwave Flash") + 
                 detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + 
                 detectNS("video/quicktime","QuickTime") + 
                 detectNS("application/x-mplayer2","Windows Media Player") + 
                 detectNS("application/pdf","Acrobat Reader");
  }
  
  function detectIE(ClassID,name) { 
    result = false; 
    document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID +                   '"))</SCRIPT>\n'); 
    if (result) return name+','; 
    else return ''; 
  }
  
  function detectNS(ClassID,name) { 
    n = ""; 
    if (nse.indexOf(ClassID) != -1) 
      if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; 
    return n; 
  }
  
  pluginlist += navigator.javaEnabled() ? "Java," : "";
  if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);
  
  if (pluginlist.indexOf("Flash")!=-1) return true;
  else return false;

}

/**
 * Esegue la rilevazione del plugin Macromedia Flash e associa l'applicazione 
 * Macromedia Flash specificata all'oggetto javascript di tipo FlashMXObject
 * creato in precedenza  
 * @return true se ha rilevato e associato il plugin o false in caso contrario
 * @type boolean
 */
FlashMXObject.prototype.detectAndBind = function(objId) {
  // rileva presenza flash plugin
  //alert(this.detectPlugin());
  if (this.detectPlugin()) {
    this.objId = objId;
    // nome del movieclip che viene chiamato nell'applicazione flash
    this.mcName = "applicationInterfaceJS";
    // cerco l'oggetto flash
    var IE = navigator.appName.indexOf("Microsoft") != -1;
    this.flashObj = IE ? window[objId] : window.document[objId];
    // salvo dimensioni oggetto
    this.objWidth = this.flashObj.style.width;
    this.objHeight = this.flashObj.style.height;
    return true;
  }
  else return false;
}

/**
 * Chiama un metodo generico all'interno dell'applicazione flash 
 * passando eventuali parametri
 * @param {String} methodName nome del metodo da chiamare
 * @param {Array} paramArray elenco dei valori di tipo String da passare al metodo 
 * @return esito dell'operazione
 * @type Numeric
 */
FlashMXObject.prototype.callMethod = function(methodName,paramArray) {
  //alert(methodName + "-" + paramArray);
  // se ci sono dei parametri
  if (typeof(this.flashObj.SetVariable) == "undefined") return;  
  if (paramArray != undefined) {
    for (var i=0; i < paramArray.length; i++) {
      this.flashObj.SetVariable(this.mcName+"."+methodName+"_par"+i,paramArray[i]);
    }
  }
  // chiamo il metodo
  this.flashObj.TCallLabel(this.mcName,methodName);
  // ottengo l'eventuale risultato
  var result = this.flashObj.GetVariable(this.mcName+"."+methodName+"_return");
  
  return result;
}

/**
 * Imposta una proprietą specifica all'interno dell'applicazione flash
 *
 * @param {String} propertyName nome della proprietą da impostare
 * @param {String} propertyValue valore da impstare alla proprietą
 */
FlashMXObject.prototype.setProperty = function(propertyName,propertyValue) {
  this.flashObj.SetVariable(this.mcName + "." + propertyName,propertyValue);
}

/**
 * Restituisce una proprietą specifica all'interno dell'applicazione flash
 *
 * @param {String} propertyName nome della proprietą da cui ottenere il valore 
 * @return il valore della proprietą specificata
 * @type String
 */
FlashMXObject.prototype.getProperty = function(propertyName) {
  return this.flashObj.GetVariable(this.mcName + "." + propertyName);
}

/**
 * Permette di mostrare o nascondere l'applicazione Macromedia Flash
 *
 * @param {boolean} state se true mostra l'applicazione in caso contrario la
 * nasconde
 */
FlashMXObject.prototype.show = function(state) {
  this.showed = state;
  if (state) {
    this.flashObj.style.width = this.objWidth;
    this.flashObj.style.height = this.objHeight;
  }
  else {
    this.flashObj.style.width = "0px";
    this.flashObj.style.height = "0px";    
  }
}

/**
 * Variabile che contiene lo stato di visualizzazione corrente
 * @type boolean
 */
FlashMXObject.prototype.showed = false;

/**
 * Permette di ridimensionare l'applicazione Macromedia Flash
 *
 * @param {Number} width larghezza
 * @param {Number} height altezza
 */
FlashMXObject.prototype.resize = function(width,height) {
    this.objWidth = 1*width;
    this.objHeight = 1*height;    
    this.flashObj.style.width = 1*width + "px";
    this.flashObj.style.height = 1*height + "px";
}



