package tdh.adsys
{
   import flash.display.DisplayObjectContainer;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.net.SharedObject;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import flash.text.TextField;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFormat;
   import flash.utils.Dictionary;
   import flash.utils.getTimer;
   
   public class AdBox extends Object
   {
      
      public function AdBox(id:String = null, dpoc:DisplayObjectContainer = null, bottom:Boolean = false, url:String = "http://www.9ria.com/m")
      {
         this.sprite = new Sprite();
         this.tf = new TextField();
         this.depot = new Dictionary();
         this.list = new Vector.<AdMsg>();
         super();
         this.projId = id;
         this.bottom = bottom;
         this.viewInit(dpoc);
         this.connInit(url);
         this.listenerInit();
      }
      
      private var projId:String;
      
      private var sprite:Sprite;
      
      private var tf:TextField;
      
      private var bottom:Boolean;
      
      private var width:int;
      
      private const height:int = 50;
      
      private function viewInit(dpoc:DisplayObjectContainer) : void
      {
         this.tf.autoSize = TextFieldAutoSize.CENTER;
         this.tf.selectable = this.sprite.visible = false;
         this.tf.defaultTextFormat = new TextFormat(null,32,16777215);
         this.sprite.addEventListener(MouseEvent.CLICK,this.toUrl);
         if(dpoc != null)
         {
            this.showAs(dpoc);
         }
      }
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         dpoc.addChild(this.sprite);
         if(dpoc.stage != null)
         {
            this.sprite.graphics.clear();
            this.width = dpoc.stage.stageWidth;
            this.sprite.graphics.beginFill(0,0.5);
            this.sprite.graphics.drawRect(0,0,this.width,this.height);
            this.sprite.addChild(this.tf);
            if(this.bottom)
            {
               this.sprite.y = dpoc.stage.stageHeight - this.height;
            }
         }
      }
      
      public function set text(value:String) : void
      {
         this.tf.htmlText = value;
         this.tf.x = this.width - this.tf.width >> 1;
         this.tf.y = this.height - this.tf.height >> 1;
      }
      
      private const urlLoader:URLLoader = new URLLoader();
      
      private const urlRequest:URLRequest = new URLRequest();
      
      private const local:SharedObject = SharedObject.getLocal("AdBox_v1.0");
      
      private var _sysInfo:String;
      
      private const _serverDate:Date = new Date();
      
      private var _startAppTime:Number;
      
      private function connInit(url:String) : void
      {
        /* this.urlRequest.url = url + "?r=" + Math.random() + "&";
         var macId:String = MacInfo._get(this.projId).id;
         var decodeSrc:String = !(macId == null)?"macId=" + macId:"gId=" + this.projId + "&" + Capabilities.serverString;
         this.urlRequest.data = new URLVariables(decodeSrc);
         this.urlRequest.method = URLRequestMethod.GET;
         this.urlLoader.addEventListener(Event.COMPLETE,this.connComplete);
         this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR,trace);
         this.urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,trace);
         this.urlLoader.load(this.urlRequest);*/
      }
      
      private function connComplete(e:Event) : void
      {
         var msg:AdMsg = null;
         var id:String = null;
         var start:* = NaN;
         var past:* = NaN;
         var delay:* = 0;
         var loop:* = 0;
         var time:* = 0;
         var content:String = null;
         var url:String = null;
         var xml:XML = new XML(this.urlLoader.data);
         this._startAppTime = Number(xml.@date) * 1000 - getTimer();
         var macId:String = xml.@macId;
         var len:int = xml.txt.length();
         MacInfo._get(this.projId).id = macId;
         var i:int = 0;
         while(i < len)
         {
            id = String(xml.txt[i].@id);
            msg = this.depot[id] = this.depot[id] || new AdMsg();
            msg.id = id;
            if(msg.id != "")
            {
               start = Number(xml.txt[i].@date) * 1000;
               if(start != 0)
               {
                  msg.start = new Date(start);
               }
               else if(msg.start == null)
               {
                  msg.start = new Date(this._startAppTime);
               }
               
               past = Number(xml.txt[i].@past) * 1000;
               if(past != 0)
               {
                  msg.past = new Date(past);
               }
               delay = int(xml.txt[i].@delay) * 1000;
               if(delay > 0)
               {
                  msg.delay = delay;
               }
               loop = int(xml.txt[i].@loop);
               if(loop != 0)
               {
                  msg.loop = loop;
               }
               time = int(xml.txt[i].@time) * 1000;
               if(time != 0)
               {
                  msg.time = time;
               }
               content = xml.txt[i];
               if(content != "")
               {
                  msg.text = content;
               }
               url = String(xml.txt[i].@url);
               if(url != "")
               {
                  msg.url = url;
               }
            }
            i++;
         }
      }
      
      private function toUrl(e:Event) : void
      {
         if((!(this.currMsg == null)) && (!(this.currMsg.url.indexOf("http://") == -1)))
         {
            this.urlRequest.data = null;
            this.urlRequest.url = this.currMsg.url;
            this.urlRequest;
            ;
         }
      }
      
      public function get serverDate() : Date
      {
         return this._serverDate;
      }
      
      private var depot:Dictionary;
      
      private var list:Vector.<AdMsg>;
      
      private var currMsg:AdMsg;
      
      private function listenerInit() : void
      {
         this.sprite.addEventListener(Event.ENTER_FRAME,this.listener);
      }
      
      private function listener(e:Event) : void
      {
         var msg:AdMsg = null;
         this._serverDate.time = this._startAppTime + getTimer();
         for each(msg in this.depot)
         {
            if(msg.start < this._serverDate)
            {
               delete this.depot[msg.id];
               true;
               this.list.unshift(msg);
               this.currMsg = msg;
               this.text = this.currMsg.text;
               this.sprite.visible = true;
            }
         }
         if(this.currMsg == null)
         {
            if((this.list.length > 0) && (this.list[0].start < this._serverDate))
            {
               this.currMsg = this.list[0];
               this.text = this.currMsg.text;
               this.sprite.visible = true;
            }
         }
         else if(this.currMsg.start.time + this.currMsg.time < this._serverDate.time)
         {
            this.sprite.visible = false;
            if(--this.currMsg.loop > 0)
            {
               this.list.push(this.currMsg);
            }
            this.list.shift();
            if(this.list.length > 0)
            {
               this.list[0].start.time = this._serverDate.time + this.list[0].delay;
            }
            this.currMsg = null;
         }
         
      }
   }
}
