/*
SWFObjectУ΢IEм˿ؼ(ĿǰѾȥ)
ͨⲿűɲFlashHTMLȴܱܿơ
ͨʹSWFObjectFlashѾúܼ
mooSwiffĴSWFObjectmooԴPrototype
ͨSwiff࣬ѧװUIҪṩĻ
1.¼֧(ʹcallbackEventsʵ֣һʹEventsĽӿʵ)
2.ÿѡ(OptionssetOptionsĽӿʵ)
3.ӵDOMеķ
4.ṩUIҪHTML Element
ʹSwiff࣬ԿmooĹٷSwiff̳
*/
var Swiff = new Class({

 //setOptionsӿʵ֣ΪԣûʹEvents¼ӿʵ
 Implements: [Options],

 options: {
  id: null, //object/embedid
  height: 1, //߶
  width: 1, //
  container: null, //
  properties: {}, //Լ
  params: { //
   quality: 'high',//
   allowscrptAccess: 'always',
   wMode: 'transparent',//͸ģʽ
   swLiveConnect: true//ǷJava֧fscommand
  },
  callBacks: {}, //ص
  vars: {} //
 },

 //ȡָHTML Element
 toElement: function(){
  return this.object;
 },

 /*
 pathΪswfļ·
 */
 initialize: function(path, options){
  //Ψһʵ
  this.instance = 'Swiff_' + $time();

  this.setOptions(options);
  options = this.options;

  //õΨһid
  var id = this.id = options.id || this.instance;
  //
  var container = $(options.container);
 
  //ʹΨһʵʼǰĻص
  Swiff.CallBacks[this.instance] = {};

  var params = options.params, vars = options.vars, callBacks = options.callBacks;
  var properties = $extend({height: options.height, width: options.width}, options.properties);

  //ֲıհе
  var self = this;

  //ƻص
  for (var callBack in callBacks){
   Swiff.CallBacks[this.instance][callBack] = (function(option){
    return function(){
     return option.apply(self.object, arguments);
    };
   })(callBacks[callBack]);
   //ͨflashVarsswf
   vars[callBack] = 'Swiff.CallBacks.' + this.instance + '.' + callBack;
  }

  params.flashVars = Hash.toQueryString(vars);
  //IEʹclassidcontentTypeעļ·Ӧ
  if (Browser.Engine.trident){
   properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
   params.movie = path;
  } else {
   properties.type = 'application/x-shockwave-flash';
   properties.data = path;
  }
  /*
  ַƴӳHTML
  ҪЧʣӦ飬ʹַsubstitute
  */
  var build = '<object id="' + id + '"';
  //ƴ
  for (var property in properties) build += ' ' + property + '="' + properties[property] + '"';
  build += '>';
  //ƴӲ
  for (var param in params){
   if (params[param]) build += '<param name="' + param + '" value="' + params[param] + '" />';
  }
  build += '</object>';
  //ΪԺͲֱ̫࣬new ElementĻȽϷֱnewһʱdivHTMLٷӽڵ
  this.object =  ((container) ? container.empty() : new Element('div')).set('html', build).firstChild;
 },

 //ǰ滻element,ҲΪ˱ģź̫ûֱʹElementreplaces
 replaces: function(element){
  element = $(element, true);
  element.parentNode.replaceChild(this.toElement(), element);
  return this;
 },

 //ӵelementУҲûֱʹElementinject
 inject: function(element){
  $(element, true).appendChild(this.toElement());
  return this;
 },

 //
 remote: function(){
  return Swiff.remote.apply(Swiff, [this.toElement()].extend(arguments));
 }

});

//ص
Swiff.CallBacks = {};

//swfjsķ
Swiff.remote = function(obj, fn){
 var rs = obj.CallFunction('<invoke name="' + fn + '" returntype="javascrpt">' + __flash__argumentsToXML(arguments, 2) + '</invoke>');
 return eval(rs);
};

