// download box java script object

function dwbox() {
  this.enBkgId="dwbox-bkg"
  this.enBkgObj=null
  this.enBoxId="dwbox-box"
  this.enBoxObj=null
  this.enTitleBarId="dwbox-titlebar"
  this.enTitleBarObj=null
  this.enTitleBarCloseObj=null
  this.enContentId="dwbox-content"
  this.enContentObj=null
  this.enTitleObj=null
  this.enTextObj=null
  this.enButtonObj=null
  this.enButtonImgObj=null
  this.enButtonAnchObj=null
  this.enCaptionId="dwbox-caption"
  this.enCaptionObj=null
  this.enTimerId=null
  this.enInterval=50
  this.enDegree=0
  this.id=0
  this.guid=""
  this.ext=""  
}

dwbox.prototype={
  init:function(link,domain) {
    var _self=this

    //man data box
    this.enBoxObj=document.createElement('div')
    this.enBoxObj.setAttribute("id",this.enBoxId)

    // top title bar
    this.enTitleBarObj=document.createElement('div')
    this.enTitleBarObj.setAttribute("id",this.enTitleBarId)
    this.enTitleBarCloseObj=document.createElement('a')
    this.enTitleBarCloseObj.setAttribute("href","javascript:void(0)")
    this.enTitleBarCloseObj.onclick=function() { _self.close(); return false }
    this.enTitleBarObj.appendChild(this.enTitleBarCloseObj)
    this.enBoxObj.appendChild(this.enTitleBarObj)

    // main content
    this.enContentObj=document.createElement('div')
    this.enContentObj.setAttribute("id",this.enContentId)
    
    this.enTitleObj=document.createElement('h2')
    this.enTitleObj.innerHTML="POBIERANIE PLIKU"
    this.enContentObj.appendChild(this.enTitleObj)
    
    this.enTextObj=document.createElement('p')
    this.enTextObj.innerHTML="<input type='checkbox' id='dwbox-chkbox'/> Oświadczam, że zapoznałem się z <a href='" + link + "'>Warunkami korzystania</a> z zasobów serwisu internetowego " + domain
    this.enContentObj.appendChild(this.enTextObj)

    this.enButtonObj=document.createElement('div')
    this.enButtonObj.setAttribute("class","text-button")

    this.enButtonImgObj=document.createElement('img')
    this.enButtonImgObj.src="/img/compare-off-btn.png"
    this.enButtonObj.appendChild(this.enButtonImgObj)

    this.enButtonAnchObj=document.createElement('a')
    this.enButtonAnchObj.setAttribute("href","javascript:void(0)")

    this.enButtonAnchObj.innerHTML="pobierz"
    this.enButtonObj.appendChild(this.enButtonAnchObj) 
    
    this.enContentObj.appendChild(this.enButtonObj)
    
    // adding content
    this.enBoxObj.appendChild(this.enContentObj)

    // bottom caption
    this.enCaptionObj=document.createElement('div')
    this.enCaptionObj.setAttribute("id",this.enCaptionId)
    this.enBoxObj.appendChild(this.enCaptionObj)
    document.body.appendChild(this.enBoxObj)
  
    this.enCheckBoxObj=document.getElementById("dwbox-chkbox")
    this.enCheckBoxObj.onclick=function() {
      if(this.checked) {
        _self.enButtonAnchObj.setAttribute("href","/_get/"+_self.id+","+_self.guid+"."+_self.ext)
        _self.enButtonImgObj.src="/img/compare-on-btn.png"
        _self.enButtonAnchObj.onclick=function() { _self.close() }
      }
      else {
        _self.enButtonAnchObj.setAttribute("href","javascript:void(0)")
        _self.enButtonImgObj.src="/img/compare-off-btn.png"
        _self.enButtonAnchObj.onclick=null
      }
      this.blur()
    }
  
    // shadow background
    this.enBkgObj=document.createElement('div')
    this.enBkgObj.setAttribute("id",this.enBkgId)
    document.body.appendChild(this.enBkgObj)
  },
   
  chkclick:function() {
    
  },
  
  close:function() {
    document.onkeyup=null
    this.enBoxObj.style.display="none"
    this.enBkgObj.style.display="none"
  },
    
  // background fading
  fade:function() {
    this.enDegree+=10
    with(this.enBkgObj.style) {
      opacity=(this.enDegree/100.0)
      MozOpacity=(this.enDegree/100.0)
      KhtmlOpacity=(this.enDegree/100.0)
      filter="alpha(opacity="+this.enDegree+")"
    }
    if(this.enDegree >= 70) {
      clearTimeout(this.enTimerId)

      var wt=390; var ht=210
      var ScreenSize=getScreenSize()
          
      // calculating top left corner position
      var arrayPageScroll = document.viewport.getScrollOffsets();
      var boxTop = arrayPageScroll[1]
      var boxLeft = arrayPageScroll[0]
    
      with(this.enBoxObj.style) {
        top=(document.viewport.getHeight()/2)-(ht/2)+boxTop+'px'
        left=(document.viewport.getWidth()/2)-(wt/2)+boxLeft+'px'
        width=wt+'px'
        height=ht+'px'
        zIndex=2
        display="block"
      }
      with(this.enTitleBarObj.style) {
        top=3+'px'
        left=366+'px'
      }    
      with(this.enContentObj.style) {
        height=166+'px'
      }  
      
    }
    else {
      var _self = this;
      this.enTimerId = setTimeout(function() { _self.fade() }, this.enInterval)
    }
  },

  show:function(id,guid,ext) {
    this.id=id
    this.guid=guid
    this.ext=ext
    var wt=0; var ht=1
    var ScreenSize=getScreenSize()
    var arrayPageScroll = document.viewport.getScrollOffsets();
    var boxTop = arrayPageScroll[1]
    var boxLeft = arrayPageScroll[0]
    with(this.enBkgObj.style) {
      position="absolute"
      top=boxTop+'px'
      left=boxLeft+'px'
      width=ScreenSize[wt]+'px'
      height=ScreenSize[ht]+'px'
      zIndex=1
      opacity=0
      MozOpacity=0
      KhtmlOpacity=0
      filter="alpha(opacity=0)"
      display="block"
    }
    this.enCheckBoxObj.checked=false   
    this.enButtonImgObj.src="/img/compare-off-btn.png"
    this.enButtonAnchObj.setAttribute("href","javascript:void(0)")
    var _self = this;
    this.enBkgObj.onclick=function() { _self.close() }
    this.enBkgObj.onclick=function() { _self.close() }
    this.enDegree=0
    this.enTimerId = setTimeout(function() { _self.fade() }, this.enInterval)
  }
}

