/*
ARIAwork.com, Professional Script creating Group

ARIAfx.js   2009/04/21
ARIAfxQ.js  2009/04/21

desc    : ARIAfx  Class / UTF-8
desc    : ARIAfxQClass Class / UTF-8

history : 2009/04/21, squall ARIAfx v1.0, ARIAfxQClass v1.0
          2009/04/23, squall ARIAfxQ playQueueFn, requestQueueFn bug patch queue array pop change

Copyright (c) 2000-2009 NAMUsoft, Inc. All Rights Reserved.

This software is the proprietary information of NAMUsoft, Inc.
Use is subject to license terms.

===============================================================
*/
Error.prototype.print = function(){
  return "자바 스크립트 오류번호 : "+(this.number&0xFFFF)+" \n < "+this+" >";
}

var ARIAfxQClass = Class.create({
  version: "1.0",
  nTime: function(){
   var d, s = "UTC:";
   var c = ":";
   d = new Date();
   s += d.getUTCHours() + c;
   s += d.getUTCMinutes() + c;
   s += d.getUTCSeconds() + c;
   s += d.getUTCMilliseconds();
   return(s);
	},
  initialize: function(){
    this.fxQueue      = [];
    this.playQueue    = [];
    this.connectQueue = [];
    this.config       = {showError:false};
    this.delayTime    = {play:200, rePlay:500, requestFn:300, reRequestFn:500};
    this.requestQueueLive = false; // requestFn 
    this.reqErrCnt = 0;
  },
  add: function(fxName, fxOptions){
    // Object test
    var hasObj = this.fxQueue.any(function(n){
      return n.objName == fxName;
    });
    if(hasObj == false){
      var myOPT = fxOptions;

      if(!myOPT.src)                 alert("'src' option is required");
      if(!myOPT.width)               myOPT.width = "100%";
      if(!myOPT.height)              myOPT.height = "100%";
      if(!myOPT.allowScriptAccess)   myOPT.allowScriptAccess = "always";
      if(!myOPT.menu)                myOPT.menu = "false";
      if(!myOPT.quality)             myOPT.quality = "best";
      if(!myOPT.wmode)               myOPT.wmode = "opaque";
      if(!myOPT.bgcolor)             myOPT.bgcolor = "";
      if(!myOPT.align)               myOPT.align = "middle";
      if(!myOPT.version)             myOPT.version = "9,0,0,0";
      if(!myOPT.allowFullScreen)     myOPT.allowFullScreen = "false";
      
      var myName = fxName + "@ARIAfxQ";
      var addObj = new ARIAfx({
        fxName: fxName, 
        src:myOPT.src+"?dummy="+this.nTime(), width:myOPT.width, height:myOPT.height, name:myName, menu:myOPT.menu, 
        allowScriptAccess:myOPT.allowScriptAccess, 
        quality:myOPT.quality, wmode:myOPT.wmode, bgcolor:myOPT.bgcolor, align:myOPT.align, version:myOPT.version, 
        allowFullScreen:myOPT.allowFullScreen
      });
      
      this.fxQueue.push({objName:fxName, fxObject:addObj});
      this.playQueue.push(fxName);
    }
  },
  setParam: function(fxName, params){
    var catchIndex  = this.getIndex(fxName);
    if(catchIndex > -1) this.fxQueue[catchIndex].fxObject.setParam(params);
    else alert("Was calling the wrong name !");
  },
  getIndex: function(fxName){
    var catchIndex  = -1;
    this.fxQueue.each(function(n, index){
      if(n.objName == fxName) catchIndex = index;
    });
    return catchIndex;
  },
  play: function(fxName){
    var myTime = this.delayTime;
    var rePlay = this.playQueueFn.bind(this);
    setTimeout(rePlay, myTime.play);
  },
  playQueueFn: function(){
    var myTime = this.delayTime;
    // queue control
    var rePlay = this.playQueueFn.bind(this);
    var errs = [];
    var failQueue = [];
    for(var a=this.playQueue.length-1;a>-1;a--){
      try{
        var catchName     = this.playQueue[a];
        var catchIndex    = this.getIndex(catchName);
        if(catchIndex > -1) this.fxQueue[catchIndex].fxObject.play();
      }catch(e){
        failQueue.push(this.playQueue[a]);  
        errs.push(catchName + ":play실패 \n" + e.print().toString() + "\n");
      }
    }
    this.playQueue = failQueue; //실행 실패된 큐 다시 지정
    
    if(errs.length > 0){
      if(this.config.showError){
        alert(errs.join(''));
      }else{
        setTimeout(rePlay, myTime.rePlay);
      }
    }
  },
  requestFn: function(){
    var myTime = this.delayTime;
    this.connectQueue.push(arguments);
    var rePlay = this.requestQueueFn.bind(this);
    if(!this.requestQueueLive){
      setTimeout(rePlay, myTime.requestFn); 
      this.requestQueueLive = true;
    } // queue live then call
  },
  requestQueueFn: function(){
    
    var myTime = this.delayTime;
    this.requestQueueLive = true;
    // queue control
    var rePlay = this.requestQueueFn.bind(this);
    var errs = [];
    
    var failQueue = [];
    for(var a=this.connectQueue.length-1;a>-1;a--){
      try{
        if(this.connectQueue[a][0]){
          var catchName     = this.connectQueue[a][0];
          var catchFn       = this.connectQueue[a][1];
          var catchIndex    = this.getIndex(catchName);
          if(catchIndex > -1){
            eval("this.fxQueue[catchIndex].fxObject.getMovie."+catchFn+"(this.connectQueue[a][2])");
          }          
        }
      }catch(e){
        failQueue.push(this.connectQueue[a]);  
        errs.push(catchName + ":호출실패 \n" + e.print().toString() + "\n");
      }
    }
    this.connectQueue = failQueue; //실행 실패된 큐 다시 지정

    if(errs.length > 0){
      if(this.config.showError){
        if(this.reqErrCnt < 3){
          alert(errs.join(''));
          this.reqErrCnt++;
          setTimeout(rePlay, 1000);
        }else{
          alert("에러가 3번이상 반복 되어 프로세스를 중지합니다.");
        }
      }else{
        setTimeout(rePlay, myTime.reRequestFn);
      }
    }else{
      this.requestQueueLive = false;
    }
  },
  returnFn: function(fxName, fxOptions){
    
  }
}); // ARIAfxQ

var ARIAfx = Class.create({
  version: "1.0",
  initialize: function(options){
    // 
    this.plugin     = "http://www.macromedia.com/go/getflashplayer"
    this.classid    = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
    this.embedType  = "application/x-shockwave-flash";
    this.OPT        = options;
    this.Params     = "";
    
    if(!this.OPT.src)                 alert("'src' option is required");
    if(!this.OPT.width)               this.OPT.width = 100;
    if(!this.OPT.height)              this.OPT.height = 100;
    if(!this.OPT.name)                this.OPT.name = "noname";
    if(!this.OPT.id)                  this.OPT.id = this.OPT.name;
    if(!this.OPT.allowScriptAccess)   this.OPT.allowScriptAccess = "sameDomain";
    if(!this.OPT.menu)                this.OPT.menu = "true";
    if(!this.OPT.quality)             this.OPT.quality = "best";
    if(!this.OPT.wmode)               this.OPT.wmode = "opaque";
    if(!this.OPT.bgcolor)             this.OPT.bgcolor = "";
    if(!this.OPT.align)               this.OPT.align = "middle";
    if(!this.OPT.version)             this.OPT.version = "9,0,0,0";
    if(!this.OPT.allowFullScreen)     this.OPT.allowFullScreen = "false";
    this.codebase = "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+this.OPT.version;
    
    this.fxName = this.OPT.fxName;
  },
  play: function(playTG){
    if(!playTG) playTG = this.fxName;
    var myParam = this.getParam();
    var p_out = [];
    
    if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
        p_out.push("<embed src=\""+this.OPT.src+"\" ");
        p_out.push(" menu=\""+this.OPT.menu+"\" quality=\""+this.OPT.quality+"\" wmode=\""+this.OPT.wmode+"\" bgcolor=\""+this.OPT.bgcolor+"\" width=\""+this.OPT.width+"\" height=\""+this.OPT.height+"\" align=\""+this.OPT.align+"\" ");
      if(myParam.length > 0)
        p_out.push(" flashvars='"+myParam.join("&")+"' ");
        p_out.push(" name=\""+this.OPT.name+"\" allowScriptAccess=\""+this.OPT.allowScriptAccess+"\" allowFullScreen=\""+this.OPT.allowFullScreen+"\" type=\""+this.embedType+"\" pluginspage=\""+this.plugin+"\" />");
    }else{
        p_out.push("<object classid=\""+this.classid+"\" codebase=\""+this.codebase+"\" ");
        p_out.push(" width=\""+this.OPT.width+"\" height=\""+this.OPT.height+"\" id=\""+this.OPT.id+"\" align=\""+this.OPT.align+"\">");
        p_out.push("<param name=\"allowScriptAccess\" value=\""+this.OPT.allowScriptAccess+"\" />");
        p_out.push("<param name=\"movie\" value=\""+this.OPT.src+"\" />");
        p_out.push("<param name=\"menu\" value=\""+this.OPT.menu+"\" /><param name=\"quality\" value=\""+this.OPT.quality+"\" />");
        p_out.push("<param name=\"wmode\" value=\""+this.OPT.wmode+"\" /><param name=\"bgcolor\" value=\""+this.OPT.bgcolor+"\" />");
        p_out.push("<param name=\"allowFullScreen\" value=\""+this.OPT.allowFullScreen+"\" />");
      if(myParam.length > 0)
        p_out.push("<param name='flashvars' value='"+myParam.join("&")+"' />");
        p_out.push("</object>");
    }
    
    $(playTG).update(p_out.join(''));
    var myObjects = null;
    (navigator.appName.indexOf("Microsoft") != -1) ? myObjects = window[this.OPT.name] : myObjects = document[this.OPT.name];
    this.getMovie = myObjects;
  },
  setParam: function(params){
    if(typeof(params) == "object"){
      this.Params = params;
    }else{
      alert("Param set up incorrectly");
    }
  },
  getParam: function(params){
    if(this.Params){
  	  var queryString = [];
  	  var params = this.Params
      for(var key in params){
        queryString.push(key+"="+params[key]);
      }
      return queryString;
  	}else{
  	  return [];
  	}
  }
}); // ARIAfx

var ARIAfxQ = new ARIAfxQClass();