/* SWFObject wrapper class */
var RenderSwfobject = Class.create();

RenderSwfobject.prototype = {
   initialize: function(swf, w, h) { 
      this.id = swf;
      this.div = $(this.id);
      Element.extend(this.div);
      this.img = this.div.firstDescendant();
      Element.extend(this.img);
      if(this.img != null) this.img.setStyle({'visibility':'hidden'});
      this.swf = swf;
      this.width = w;
      this.height = h;
      this.loader = '/portal/wcmobject/document/swf/stubloader.swf';
      this.so = new SWFObject(this.loader, "swf_"+this.id, this.width, this.height, 8);
      this.addParam('menu', false);
      this.addVariable("f", this.swf);
      this.clickTag = '';
	  this.instadiaTag = '';
      this.useMap = null;
      this.isIE = navigator.appName.toLowerCase().indexOf("internet explorer") != -1 ? true : false;
   },
   
   setClickTag : function(str, tag) {
      this.clickTag = str;
	  if(typeof tag == "string" && tag.isLength()) this.instadiaTag = tag;
      this.addVariable("clickTag", this.getFlashUrl(this.clickTag, this.instadiaTag));
   },
   
   setInstadiaTag : function(str) {
      this.instadiaTag = str;  
   },
   
   getFlashUrl : function(url, tag) {
      if(typeof tag == "string" && tag.isLength()) {
         return "javascript:try{Instadia_sendEvent('011kr', '"+tag+"', '');}catch(e){;}finally{location.href='"+url.replace(/&/g, '%26')+"';}";
	  } else {
         return url.replace(/&/g, '%26');  
	  }
   },
   
   getImageUrl : function(url, tag) {
      if(typeof tag == "string" && tag.isLength()) {
         return "javascript:try{Instadia_sendEvent('011kr', '"+tag+"', '');}catch(e){;}finally{location.href='"+url.replace(/%26/g, '&')+"';}";
	  } else {
         return url.replace(/%26/g, '&');  
	  }
   },

   setImageClickTag : function() {
      if(this.img == null) return;
      if(!this.clickTag.isLength()) return;
      var a = document.createElement('a');
      Element.extend(a);
      a.href = this.getImageUrl(this.clickTag, this.instadiaTag);
      this.div.removeChild(this.img);
      a.appendChild(this.img);
      this.div.appendChild(a);
   },
   
   setMap : function(arr) {
      this.useMap = arr;
      if(!isArray(this.useMap)) return;
      for(var i=0; i<this.useMap.length; i++) {
         this.addVariable("clickTag"+(i+1), this.getFlashUrl(this.useMap[i].href.replace(/&/g, '%26'), this.useMap[i].tag));
      }
   },
   
   setImageMap : function() {
      if(this.img == null) return;
      if(!isArray(this.useMap)) return;

      /* Ugly IE hack: can't set name attribute on elements */
      if(this.isIE) var map = document.createElement('<map name="map_'+this.id+'">');
      else var map = document.createElement('map');
      
      Element.extend(map);
      map.setAttribute('id', 'map_'+this.id);
      map.setAttribute('name', 'map_'+this.id);
      for(var i=0; i<this.useMap.length; i++) {
         var area = document.createElement('area');
         area.setAttribute('shape', 'rect');
         area.setAttribute('coords', this.useMap[i].coords);
         area.setAttribute('href', this.getImageUrl(this.useMap[i].href.replace(/%26/g, '&'), this.useMap[i].tag));
         map.appendChild(area);
      }
      this.div.appendChild(map);
      this.img.setAttribute('useMap', '#map_'+this.id); 
   },
   
   addParam: function(key, value) {
      this.so.addParam(key, value.toString().replace(/&/g, '%26'));
   },
   
   addVariable: function(key, value) {
      this.so.addVariable(key, value.toString().replace(/&/g, '%26'));
   },
   
   render: function() {
      new PeriodicalExecuter(this.doRender.bindAsEventListener(this), 0.1);
   },
   
   doRender: function(obj) {
      if(this.img == null || (this.img.width != "undefined" && this.img.width > 0)) {
         obj.stop();
         this.setImageClickTag();
         this.setImageMap();
         this.img.setStyle({'visibility' : 'visible'});
         this.so.write(this.id);
      }
   }
   
};