// Copyright (C) 2006 Morose Media

dojo.require('dojo.event');
dojo.require('dojo.html');

function doFlash(id, content) {
  document.getElementById(id).innerHTML = content;
}

function xmlconn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }
      };
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function getHeight() {
  if (window.innerHeight)  {
  	return window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
  	return document.documentElement.clientHeight;
  } else if (document.body) {
  	return document.body.clientHeight;
  }
}

function getWidth() {
  if (window.innerWidth)  {
  	return window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
  	return document.documentElement.clientWidth;
  } else if (document.body) {
  	return document.body.clientWidth;
  }
}

function elm(name) {
  return document.getElementById(name);
}

Fader = function(_target, _duration, _fadedout, _fadedin) {
  // Public variables
  this.target = null;
  this.fadedin = 1;
  this.fadedout = 0;
  this.running = false;
  this.duration = 1000;
  
  // Private variables
  var _this = this;
  var _startopacity = 0;
  var _endopacity = 1;
  var _starttime = 0;
  var _interval = null;
  
  if (_target) { this.target = _target; }
  if (_duration) { this.duration = _duration; }
  if (_fadedin) { this.fadedin = _fadedin; }
  if (_fadedout) { this.fadedout = _fadedout; }
   
  // Public functions
  this.fadeIn = function(absolute) {    
    if (this.running) { this._stop(); }
    this.running = true;
    this._startopacity = dojo.html.getOpacity(this.target);
    if (absolute) { this._startopacity = this.fadedout; }
    this._endopacity = this.fadedin;    
    this._start();
  }
  
  this.fadeOut = function(absolute) {
    if (this.running) { this._stop(); }    
    this.running = true;      
    this._startopacity = dojo.html.getOpacity(this.target);
    if (absolute) { this._startopacity = this.fadedin; }
    this._endopacity = this.fadedout;    
    this._start();   
  }
   
  // Private functions
  this._start = function() {
    var date = new Date().getTime();
    this._starttime = date;
    
    dojo.html.setOpacity(this.target, this._startopacity, false);
    this.target.style.visibility = 'visible';
    this._interval = window.setInterval(this._ontimer, 5);    
  }
   
  this._stop = function() {
    if (this.running) {
      window.clearInterval(this._interval);

      this.running = false;   
      dojo.html.setOpacity(this.target, this._endopacity, false);      
    }
  }

  this._ontimer = function() {
    if (_this.running) {
      var date = new Date().getTime();
      var deltatime = date - _this._starttime;
      var perc = (deltatime / _this.duration);
      var opacity = _this._startopacity + ((_this._endopacity - _this._startopacity) * perc);
      
      if (deltatime < _this.duration) {
        dojo.html.setOpacity(_this.target, opacity, false);
      } else { 
        _this._stop(); 
      }
    }
  } 
}

VerticalScrollbar = function(_target, _window, _up, _down, _outer, _inner, _box, _speed) {
  // Public variables
  this.target = null;
  this.window = null;
  this.up = null;
  this.down = null;
  this.outer = null;
  this.inner = null;
  this.box = null;
  this.speed = 1;
  
  // Private variables
  var _this = this;
  var _interval = null;
  var _curpad = 0;
  var _scrollspeed = 0;
  var _borderinwidth = false;
  var _following = false;
  var _startpos = 0;
  var _startpad = 0;
  var _last = 0;
 
  // Public functions
  this.init = function() {
    if ((_this.target) &&
        (_this.window) &&
        (_this.up) &&
        (_this.down) &&
        (_this.outer) &&
        (_this.inner) &&
        (_this.speed)) {
          
      if (dojo.html.getBorderBox(_this.target).height > dojo.html.getBorderBox(_this.window).height) {
        _this._borderinwidth = (dojo.html.isBorderBox(_this.outer));
        
        // Connect events
        dojo.event.connect(_this.up, "onmouseover", _this._onupover);
        dojo.event.connect(_this.up, "onmouseout", _this._onupout);
        dojo.event.connect(_this.up, "onmousedown", _this._onupdown);
        dojo.event.connect(_this.up, "onmouseup", _this._onupup);
      
        dojo.event.connect(_this.down, "onmouseover", _this._ondownover);
        dojo.event.connect(_this.down, "onmouseout", _this._ondownout);
        dojo.event.connect(_this.down, "onmousedown", _this._ondowndown);
        dojo.event.connect(_this.down, "onmouseup", _this._ondownup);
        
        dojo.event.connect(_this.inner, "onmousedown", _this._oninnerdown);
        dojo.event.connect(_this.inner, "onmouseup", _this._oninnerup);
        dojo.event.connect(_this.inner, "onmouseout", _this._oninnerout);
        dojo.event.connect(_this.inner, "onmouseover", _this._oninnerover);
        dojo.event.connect(_this.inner, "onmousemove", _this._oninnermove);
               
        dojo.event.connect(_this.outer, "onclick", _this._onouterclick);
                     
        // Fix styles
        _this._basestyle(_this.up);
        _this._basestyle(_this.down);
        _this._basestyle(_this.outer);
        _this._basestyle(_this.inner);
        
        _this.inner.style.position = 'absolute';
        if (!_this._borderinwidth) {
          _this.outer.style.width = (dojo.html.getBorderBox(_this.outer).width - (dojo.html.getBorder(_this.outer).width * 2)) + 'px';
          _this.outer.style.height = (dojo.html.getBorderBox(_this.outer).height - (dojo.html.getBorder(_this.outer).height * 2)) + 'px';
        }        
        _this.inner.style.width = (dojo.html.getBorderBox(_this.outer).width - dojo.html.getBorder(_this.outer).width) + 'px';
        if (!_this._borderinwidth) {
          _this.inner.style.width = (dojo.html.getBorderBox(_this.inner).width - (dojo.html.getBorder(_this.inner).width * 2)) + 'px';
          _this.inner.style.height = (dojo.html.getBorderBox(_this.inner).height - (dojo.html.getBorder(_this.inner).height * 2)) + 'px';
        }        
        _this.outer.style.zIndex = 90;
        _this.inner.style.zIndex = 91;
        
        _this._setPad(0);
        
        _this._show(_this.up);
        _this._show(_this.down);
        _this._show(_this.outer);
        _this._show(_this.inner);
        _this._show(_this.box);
        _this._show(_this.target);
        _this._show(_this.window);
      } else {
        _this._basestyle(_this.up);
        _this._basestyle(_this.down);
        _this._basestyle(_this.outer);
        _this._basestyle(_this.inner);
        _this._hide(_this.up);
        _this._hide(_this.down);
        _this._hide(_this.outer);
        _this._hide(_this.box);
        _this._hide(_this.inner); 
        _this._show(_this.target);
        _this._show(_this.window);
      }
    }
  }
  
  // Private functions
  this._basestyle = function(elm) {
    if (elm) {
      elm.style.cursor = 'pointer';
      elm.style.overflowX = 'hidden';
      elm.style.overflowY = 'hidden';
    }
  }
  
  this._show = function(elm) {
    if (elm) {
      elm.style.visibility = 'visible';
    }
  }
  
  this._hide = function(elm) {
    if (elm) {
      elm.style.visibility = 'hidden';
    }
  }
  
  this._onupover = function() { _this._startScroll(_this.speed); return false; }  
  this._onupout = function() { _this._stopScroll(); return false; }
  this._onupdown = function() { _this._startScroll(_this.speed * 3); return false; }
  this._onupup = function() { _this._stopScroll(); return false; }

  this._ondownover = function() { _this._startScroll(0 - _this.speed); return false; }
  this._ondownout = function() { _this._stopScroll(); return false; }
  this._ondowndown = function() { _this._startScroll(0 - _this.speed * 3); return false; }
  this._ondownup = function() { _this._stopScroll(); return false; }

  this._oninnerdown = function(e) { _this._startFollow(e); return false; }
  this._oninnerup = function() { _this._stopFollow(); return false; }
  this._oninnerout = function() { 
    if (_this._following) {
      if (_this.inner.setCapture) {
        _this.inner.setCapture();
      } else if (document.body.addEventListener) {
        document.body.addEventListener("mousemove", _this._oninnermove, true);
        document.body.addEventListener("mouseup", _this._oninnerup, true);
      }
    } 
    return false; 
  }
   
  this._oninnermove = function(e) {
    var d = new Date();
    if (d.getTime() - 40 > _last) {
      _this._last = d.getTime();
    
      if (_this._following) {
        var difY = e.clientY - _this._startpos;
        var height = dojo.html.getBorderBox(_this.outer).height - dojo.html.getBorder(_this.outer).height - dojo.html.getBorderBox(_this.inner).height;
        _this._setPad(Math.round(_startpad + (0 - ((difY / height) * (dojo.html.getBorderBox(_this.target).height - dojo.html.getBorderBox(_this.window).height)))));
      }
    }
       
    return false;
  }

  this._onouterclick = function(e) { 
    var pos = e.clientY - dojo.html.getAbsolutePosition(_this.outer).top;
    var comp = dojo.html.getAbsolutePosition(_this.inner).top - dojo.html.getAbsolutePosition(_this.outer).top + 5;
    if (pos < comp) {
      _this._setPad(_curpad + dojo.html.getBorderBox(_this.window).height);
    } else {
      _this._setPad(_curpad - dojo.html.getBorderBox(_this.window).height);
    }
    return false; 
  }
    
  this._startFollow = function(e) {
    _this._following = true;
    _this._startpos = e.clientY;
    _startpad = _curpad;
  }
  
  this._stopFollow = function() {
    if (_this.inner.releaseCapture) {
      _this.inner.releaseCapture();
    } else if (document.body.removeEventListener) {
      document.body.removeEventListener("mousemove", _this._oninnermove, true);
      document.body.removeEventListener("mouseup", _this._oninnerup, true);
    }
    _this._following = false;
  }
  
  this._startScroll = function(scrollspeed) {
    _this._stopFollow();
    _this._stopScroll();
    _this._scrollspeed = scrollspeed;
    _this._interval = window.setInterval(_this._oninterval, 40);    
  }
  
  this._stopScroll = function() {
    if (_this._interval) {
      window.clearInterval(_this._interval);
    }
    _this._scrollspeed = 0;
  }
  
  this._oninterval = function() {
    _this._setPad(_curpad + _this._scrollspeed);
  }
   
  this._setPad = function(pad) { 
    var maxpad = dojo.html.getBorderBox(_this.target).height - dojo.html.getBorderBox(_this.window).height;
    if (((0 - pad) <= maxpad) && (pad <= 0)) {
      _curpad = pad;
    } else if (pad > 0) {
      _curpad = 0;
    } else {
      _curpad = 0 - maxpad;
    }
    
    var thumbheight = Math.round((dojo.html.getBorderBox(_this.window).height / dojo.html.getBorderBox(_this.target).height) * (dojo.html.getBorderBox(_this.outer).height - dojo.html.getBorder(_this.outer).height));
    if (!_this.borderinwidth) {
      thumbheight = (thumbheight - (dojo.html.getBorder(_this.inner).height));
    }
    if (thumbheight < 10) { thumbheight = 10; }
    _this.inner.style.height = thumbheight + 'px';
    
    var posx = (dojo.html.getAbsolutePosition(_this.outer, true).left + Math.floor(dojo.html.getBorder(_this.outer).width / 2));
    var posy = dojo.html.getAbsolutePosition(_this.outer, false).top + Math.floor(dojo.html.getBorder(_this.outer).height / 2) + Math.round(((0 - _curpad) / maxpad) * (dojo.html.getBorderBox(_this.outer).height - dojo.html.getBorder(_this.outer).height - dojo.html.getBorderBox(_this.inner).height));
    _this.inner.style.left = posx - dojo.html.getAbsolutePosition(document.body).left + 'px';
    _this.inner.style.top = posy - dojo.html.getAbsolutePosition(document.body).top + 'px';
       
    _this.target.style.marginTop = _curpad + 'px';
  }
  
  // Initialization  
  if (_target)  { this.target = _target; }
  if (_window)  { this.window = _window; }
  if (_up)      { this.up = _up; }
  if (_down)    { this.down = _down; }
  if (_outer)   { this.outer = _outer; }
  if (_inner)   { this.inner = _inner; }
  if (_box)     { this.box = _box; }
  if (_speed)   { this.speed = _speed; }
  
  dojo.addOnLoad(this.init);
}

// Copyright (C) 2006 Morose Media
