;(function(){
  var i=this;
  this.tgaf_ua=navigator.userAgent;

  this.max_height   = 250;
  this.banWidth     = 640;
  this.banHeight    = 100;
  this.max_height_h = 35;
  this.hideTime     = 300;

  this.scaleScreenBannerPositionToBody=function(max,p){
    var Ratio = window.screen.width/p.width;
    var a={};
    a.width  = Math.ceil(max.width/Ratio);
    a.height = Math.ceil(max.height/Ratio);
    return a;
  };

  this.maxBanScreenPosition=function(){
    var a={};
    switch(window.orientation){
      case 0:
      case 180:
        a.width  = window.screen.width;
        a.height = i.banHeight * a.width / i.banWidth;
        break;
      case 90:
      case-90:
        if(i.app=="android"){
          a.height = i.max_height_h;
        }else{
          a.height = Math.ceil(window.screen.height/10);
        }
        if(a.height > i.max_height_h){
          a.height = i.max_height_h;
        }
        var temp_height=window.screen.width*i.banHeight/i.banWidth;
        if(temp_height>a.height){
          a.width=i.banWidth*a.height/i.banHeight;
        }else{
          a.height=temp_height;
          a.width=window.screen.width;
        }
        break;
    }
    return a;
  };

  this.showBanner=function(){
    if(i.ban){
      if(i.app=='android'){
        if(window.innerHeight>i.max_height||(window.orientation==90||window.orientation==-90)){
          i.ban.style.visibility='visible';
          i.ban.style.display='block'
        } else {
          i.ban.style.visibility='hidden';
          i.ban.style.display='none'
        }
      }else{
        i.ban.style.visibility='visible';
        i.ban.style.display='block'
      }
    }
  };

  this.hideBanner=function(){
    if(i.ban){
      i.ban.style.visibility='hidden';
      i.ban.style.display='none';
    }
  };

  this.appendBanner=function(){
    if(i.ban){
      document.body.appendChild(i.ban)
    }
  };

  this.getPosition=function(){
    var p = {};
    p.left   = window.scrollX;
    p.top    = window.scrollY;
    p.width  = window.innerWidth;
    p.height = window.innerHeight;

    var max = i.maxBanScreenPosition();
    var s   = i.scaleScreenBannerPositionToBody(max,p);

    var a = {};
    a.left = Math.ceil(p.left+(p.width-s.width)/2);
    a.top = p.top + p.height - s.height
    a.width  = s.width;
    a.height = s.height;
    return a;

  };

  this.setPosition=function(div,p){
    div.style.width  = p.width  + "px";
    div.style.height = p.height + "px";
    div.style.left   = p.left   + "px";
    div.style.top    = p.top    + "px";
  };

  this.makeBanner=function(){
    var p = i.getPosition();

    var ban=document.createElement("div");
    ban.style.position="absolute";
    ban.style.zIndex=999999;

    var img = document.createElement("img");
    img.setAttribute("src", window.tgaf_bn_url);
    img.width          = p.width;
    img.height         = p.height;
    img.style.position = "absolute";
    img.style.left     = "0px";
    img.style.top      = "0px";
    var a=document.createElement("A");
    a.setAttribute("href", window.tgaf_rd_url);
    a.appendChild(img);

    i.ban=ban;
    i.setPosition(i.ban,p);

    i.ban.appendChild(a);
    return i.ban;
  };

  this.setTimeout=function(f,time){
    var id=setTimeout(f,time);
    i.timeoutId=id
  };

  this.clearTimeout=function(){
    if(i.timeoutId){
      clearTimeout(i.timeoutId);
      i.timeoutId=null
    }
    if(i.intervalId){
      clearInterval(i.intervalId);
      i.intervalId=null
    }
  };

  this.touchHandlerStart=function(){
    i.clearTimeout();
  };

  this.touchHandlerMove=function(){
    i.hideBanner();
  };

  this.touchHandlerEnd=function(){

    if(i.app=='android'){
      i.hideTime = 3000;
    }

    i.setTimeout(function(){
      i.hideBanner();
      i.makeBanner();
      i.appendBanner();
      i.showBanner();
    },i.hideTime);
  };

  this.winHandlerResize=function(){
    i.hideBanner();
    i.makeBanner();
    i.appendBanner();
    i.showBanner();
  };

  this.winHandlerOrChange=function(){
    i.hideBanner();
    i.makeBanner();
    i.appendBanner();
    i.showBanner();
  };


  this.init=function(){
    document.addEventListener("touchstart", i.touchHandlerStart, false);
    document.addEventListener("touchmove",  i.touchHandlerMove, false);
    document.addEventListener("touchend"  , i.touchHandlerEnd,   false);

    if(i.app=='android'){
      window.addEventListener("resize"    , i.winHandlerResize,  false);
    }else if(i.app=='apple'){
      window.addEventListener("orientationchange",i.winHandlerOrChange,false);
    }

    i.makeBanner();
    i.appendBanner();
    i.showBanner();

  };

  this.preInit=function(){
    if(i.tgaf_ua.search(/Android/)!=-1){
      i.app='android';
    }else if(i.tgaf_ua.search(/iPhone/)!=-1){
      i.app='apple';
    }

    if(navigator.cookieEnabled&&i.app){
      var s=document.getElementsByTagName('script')[0];
      var bn_url=window.tgaf_bn_url;

      var img = document.createElement("img");
      img.setAttribute("src", window.tgaf_bn_url);
      if( img ){
        i.init();
      }
    }
  };
  window.addEventListener("load",this.preInit,false)
}).call({});

