﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoPositioningLibrary.js
// 
// © 2007-2008 Wco iEnterprise Solutions. All Rights Reserved.
// This file is licensed as part of the DataPortal 2.0 Managed Web Presence Solution, for details look here: http://www.wco.com.au
//
///////////////////////////////////////////////////////////////////////////////

var positioning = {
  _sliderTarget: null,
  _sliderTargetWidth: -1000,
  _sliderStartX: 0,
  _sliderStartY: 0,
  _sliderEndX: 0,
  _sliderEndY: 0,
  _sliderDirection: -1,
  _sliderDistanceToTravel: 0,
  _sliderCurrentDistanceTravelled: 0,
  _sliderSnapFactor: 0.1,
  _sliderTimerId: null,
  _sliderTimerInterval: 0.5,
  _sliderTimerMoveOffset: 0,
  _sliderTimerFactor: 0.15,
  _sliderCallback: null,
  
  _sliderQueue: {
    id: null,
    x: 0,
    y: 0,
    speed: 'normal',
    w: 0,
    callback: null
  },
  
  _faderTarget: null,
  _faderDirection: 1,
  _faderDifferential: 2,
  _faderTimerId: null,
  _faderTimerInterval: 0.5,
  _faderAlpha: 0,
  _faderCallback: null,
  
  _fader2Target: null,
  _fader2Direction: 1,
  _fader2Differential: 2,
  _fader2TimerId: null,
  _fader2TimerInterval: 0.5,
  _fader2Alpha: 0,
  _fader2Callback: null

};

positioning.getObject = function(id) {
  if (typeof id == 'string') {
    if (document.getElementById) {
      return(document.getElementById(id));
    }
    else if (document.all) {
      return(document.all[id]);
    }
    else if (document.layers) {
      return(document.layers[id]);
    }
    else {
      return(null);
    }
  }
  else {
    return(id);
  }
}

positioning.showObject = function(id) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.style.display = 'block';
  }
}

positioning.hideObject = function(id) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.style.display = 'none';
  }
}

positioning.isObjectHidden = function(id) {
  var thisObject = this.getObject(id);
  
  if (thisObject.currentStyle != null) {
    return (thisObject.currentStyle.display == 'none');
  }
  else {
    return (thisObject.style.display == 'none');
  }
}

positioning.centerObjectInViewport = function(id) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    var left = (windowGeometry.getViewportWidth() - this.getObjectWidth(thisObject)) / 2;
    this.setObjectLeft(thisObject, left);
  }
}

positioning.scrollObjectToTop = function(id) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.scrollTop = 0;
  }
}

// retrieve the pixel width of the specified object
positioning.getObjectHeight = function(id) {
  var thisObject = this.getObject(id);
  var result = 0;
  
  if (thisObject != null) {
    if (thisObject.offsetHeight) {
      result = thisObject.offsetHeight;
    }
    else if (thisObject.clip && thisObject.clip.height) {
      result = thisObject.clip.height;
    }
    else if (thisObject.style && thisObject.style.height) {
      result = thisObject.style.height;
    }
  }
  
  return parseInt(result, 10);
}

// retrieve the pixel width of the specified object
positioning.getObjectWidth = function(id) {
  var thisObject = this.getObject(id);
  var result = 0;
  
  if (thisObject != null) {
    if (thisObject.offsetWidth) {
      result = thisObject.offsetWidth;
    }
    else if (thisObject.clip && thisObject.clip.width) {
      result = thisObject.clip.width;
    }
    else if (thisObject.style && thisObject.style.width) {
      result = thisObject.style.width;
    }
  }
  
  return parseInt(result, 10);
}

// set the pixel width of the specified object
positioning.setObjectWidth = function(id, w) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.style.width = w + 'px';;
  }
}

// retrieve the pixel top co-ordinate of the specified object
positioning.getObjectTop = function(id) {
  var thisObject = this.getObject(id);
  var result = 0;
  
  if (thisObject != null) {
    if (thisObject.offsetTop) {
      result = thisObject.offsetTop;
    }
    else if (thisObject.style && thisObject.style.top) {
      result = thisObject.style.top;
    }
  }
  
  return parseInt(result, 10);
}

// set the pixel top co-ordinate of the specified object
positioning.setObjectTop = function(id, y) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    if (thisObject.style && thisObject.style.top) {
      thisObject.style.top = y + 'px';
    }
  }
}

// retrieve the pixel left co-ordinate of the specified object
positioning.getObjectLeft = function(id) {
  var thisObject = this.getObject(id);
  var result = 0;
  
  if (thisObject != null) {
    if (thisObject.offsetLeft) {
      result = thisObject.offsetLeft;
    }
    else if (thisObject.style && thisObject.style.left) {
      result = thisObject.style.left;
    }
  }
  
  return parseInt(result, 10);
}

// set the pixel left co-ordinate of the specified object
positioning.setObjectLeft = function(id, x) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.style.left = x + 'px';
  }
}

// position the object at the specified pixel X,Y co-ordinate
positioning.shiftObjectTo = function(id, x, y) {
  var thisObject = this.getObject(id);
  
  if (thisObject != null) {
    thisObject.style.left = x + 'px';
    thisObject.style.top = y + 'px';
  }
}

positioning.fadeOut = function(id, callback, speed) {
  //alert('id = ' + id.id + '\ncallback =\n' + callback + '\nspeed = ' + speed);
  positioning._faderTarget = this.getObject(id);
  positioning._faderDirection = -1;
  positioning._faderDifferential = 5;
  if (speed != null) {
    if (speed == 'slow') {
      positioning._faderDifferential = 2;
    }
    else {
      positioning._faderDifferential = 10;
    }
  }
  positioning._faderAlpha = 100 - positioning._faderDifferential;
  //alert('positioning._faderAlpha = ' + positioning._faderAlpha);
  
  positioning._faderCallback = null;
  if (callback != null) {
    positioning._faderCallback = callback;
  }
  
  if (positioning._faderTarget.filters != null) {
    positioning._faderTarget.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
    positioning._faderTarget.style.display = 'block';
    //alert('positioning._faderAlpha = ' + positioning._faderAlpha + '\npositioning._faderCallback = \n' + positioning._faderCallback);
    positioning._faderTimerId = window.setTimeout(positioning._fade, positioning._faderTimerInterval);
  }
  else {
    positioning.hideObject(positioning._faderTarget);
    if (positioning._faderCallback != null) {positioning._faderCallback();};
  }
  
}

positioning.fadeIn = function(id, callback, speed) {
  positioning._faderTarget = this.getObject(id);
  positioning._faderDirection = 1;
  positioning._faderDifferential = 5;
  if (speed != null) {
    if (speed == 'slow') {
      positioning._faderDifferential = 2;
    }
    else {
      positioning._faderDifferential = 10;
    }
  }
  positioning._faderAlpha = positioning._faderDifferential;
  
  positioning._faderCallback = null;
  if (callback != null) {
    positioning._faderCallback = callback;
  }
  
  if (positioning._faderTarget.filters != null) {
    positioning._faderTarget.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
    positioning._faderTarget.style.display = 'block';
    positioning._faderTimerId = window.setTimeout(positioning._fade, positioning._faderTimerInterval);
  }
  else {
    positioning.showObject(positioning._faderTarget);
    if (positioning._faderCallback != null) {positioning._faderCallback();};
  }
  
}

positioning._fade = function() {
  // determine the end alpha value
  var endAlpha = 100;
  if (positioning._faderDirection == -1) {
    endAlpha = 0;
    //alert('positioning._faderAlpha = ' + positioning._faderAlpha + '\nendAlpha = ' + endAlpha + '\npositioning._faderCallback = \n' + positioning._faderCallback);
  }
  
  //alert('positioning._faderTarget = ' + positioning._faderTarget.id + '\npositioning._faderAlpha =' + positioning._faderAlpha);
  positioning._faderTarget.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + positioning._faderAlpha + ')';
  
  if (positioning._faderAlpha == endAlpha) {
    if (positioning._faderDirection == -1) {
      positioning.hideObject(positioning._faderTarget);
    }
    // stop the fade
    positioning._faderTarget = null;
    positioning._faderDirection = 1;
    positioning._faderTimerId = null;
    positioning._faderAlpha = 0;
    
    // if the calling client passed a pointer to a callback function, then
    // call that function now
    //alert(positioning._faderCallback);
    if (positioning._faderCallback != null) {positioning._faderCallback();};
    
  }
  else {
    // continue to fade
    if (positioning._faderDirection == -1) {
      positioning._faderAlpha = positioning._faderAlpha - positioning._faderDifferential;
    }
    else {
      positioning._faderAlpha += positioning._faderDifferential;
    }
//    if (positioning._faderDirection == -1) {
//      alert('positioning._faderAlpha = ' + positioning._faderAlpha + '\nendAlpha = ' + endAlpha + '\npositioning._faderCallback = \n' + positioning._faderCallback);
//    }
    
    positioning._faderTimerId = window.setTimeout(positioning._fade, positioning._faderTimerInterval);
  }
}

positioning.fadeOut2 = function(id, callback) {
  positioning._fader2Target = this.getObject(id);
  positioning._fader2Direction = -1;
  positioning._fader2Alpha = 90;
  
  positioning._fader2Callback = null;
  if (callback != null) {
    positioning._fader2Callback = callback;
  }
  
  if (positioning._fader2Target.filters != null) {
    positioning._fader2Target.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
    positioning._fader2Target.style.display = 'block';
    positioning._fader2TimerId = window.setTimeout(positioning._fade2, positioning._fader2TimerInterval);
  }
  else {
    positioning.hideObject(positioning._fader2Target);
    if (positioning._fader2Callback != null) {positioning._fader2Callback();};
  }
  
}

positioning.fadeIn2 = function(id, callback) {
  positioning._fader2Target = this.getObject(id);
  positioning._fader2Direction = 1;
  positioning._fader2Alpha = 10;
  
  positioning._fader2Callback = null;
  if (callback != null) {
    positioning._fader2Callback = callback;
  }
  
  if (positioning._fader2Target.filters != null) {
    positioning._fader2Target.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
    positioning._fader2Target.style.display = 'block';
    positioning._fader2TimerId = window.setTimeout(positioning._fade2, positioning._fader2TimerInterval);
  }
  else {
    positioning.showObject(positioning._fader2Target);
    if (positioning._fader2Callback != null) {positioning._fader2Callback();};
  }
  
}

positioning._fade2 = function() {
  // determine the end alpha value
  var endAlpha = 100;
  if (positioning._fader2Direction == -1) {
    endAlpha = 0;
  }
  
  //alert('positioning._fader2Target = ' + positioning._fader2Target.id + '\npositioning._fader2Alpha =' + positioning._fader2Alpha);
  positioning._fader2Target.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + positioning._fader2Alpha + ')';
  
  if (positioning._fader2Alpha == endAlpha) {
    if (positioning._fader2Direction == -1) {
      positioning.hideObject(positioning._fader2Target);
    }
    // stop the fade
    positioning._fader2Target = null;
    positioning._fader2Direction = 1;
    positioning._fader2TimerId = null;
    positioning._fader2Alpha = 0;
    
    // if the calling client passed a pointer to a callback function, then
    // call that function now
    //alert(positioning._fader2Callback);
    if (positioning._fader2Callback != null) {positioning._fader2Callback();};
    
  }
  else {
    // continue to fade
    if (positioning._fader2Direction == -1) {
      positioning._fader2Alpha -= 10;
    }
    else {
      positioning._fader2Alpha += 10;
    }
    
    positioning._fader2TimerId = window.setTimeout(positioning._fade2, positioning._fader2TimerInterval);
  }
}

positioning.slideObjectUp = function(id, toY) {
  //alert('id = ' + arguments[0] + '\ntoY = ' + arguments[1] + '\nsnapFactor = ' + arguments[2] + '\ncallBack = ' + arguments[3]);
  positioning.showObject(id);
  
  // retrieve a poiner to the target object of the slide animation
  positioning._sliderTarget = positioning.getObject(id);
  
  // configure the 'snap' factor applied to the slide motion
  if (arguments[2] != null) {
    if (arguments[2] == 'fast') {positioning._sliderSnapFactor = 0.5;}
    else if (arguments[2] == 'slow') {positioning._sliderSnapFactor = 0.0005;}
    else if (arguments[2] == 'very slow') {positioning._sliderSnapFactor = 0.000005;}
  }
  else {
    positioning._sliderSnapFactor = 0.1;
  }
  
  // configure the callback function, if supplied
  positioning._sliderCallback = null;
  if (arguments[3] != null) {
    positioning._sliderCallback = arguments[3];
    //alert(positioning._sliderCallback);
  }
  
  if (positioning._sliderTarget != null) {
    positioning._sliderStartY = positioning.getObjectTop(positioning._sliderTarget);
    positioning._sliderEndX = positioning.getObjectLeft(positioning._sliderTarget);
    positioning._sliderEndY = toY;
    positioning._sliderDistanceToTravel = Math.abs(positioning._sliderStartY - positioning._sliderEndY);
    //alert('positioning._sliderStartY = ' + positioning._sliderStartY + '\nthis._sliderEndY = ' + positioning._sliderEndY + '\nthis._sliderDistanceToTravel = ' + positioning._sliderDistanceToTravel);
    positioning._sliderDirection = -1;
    
    positioning._sliderTimerId = window.setTimeout(positioning._slide, positioning._sliderTimerInterval);
  }
}

positioning.slideObjectDown = function(id, toY) {
  //alert('id = ' + arguments[0] + '\ntoY = ' + arguments[1] + '\nsnapFactor = ' + arguments[2] + '\ncallBack = ' + arguments[3]);
  
  // retrieve a poiner to the target object of the slide animation
  positioning._sliderTarget = positioning.getObject(id);
  positioning.showObject(positioning._sliderTarget);
  
  // configure the 'snap' factor applied to the slide motion
  if (arguments[2] != null) {
    if (arguments[2] == 'fast') {positioning._sliderSnapFactor = 0.5;}
    else if (arguments[2] == 'slow') {positioning._sliderSnapFactor = 0.0005;}
    else if (arguments[2] == 'very slow') {positioning._sliderSnapFactor = 0.000005;}
  }
  else {
    positioning._sliderSnapFactor = 0.1;
  }
  
  // configure the callback function, if supplied
  positioning._sliderCallback = null;
  if (arguments[3] != null) {
    positioning._sliderCallback = arguments[3];
    //alert(positioning._sliderCallback);
  }
  
  //alert(positioning._sliderTarget.id);
  if (positioning._sliderTarget != null) {
    //alert('here');
    positioning._sliderStartY = positioning.getObjectTop(positioning._sliderTarget);
    positioning._sliderEndX = positioning.getObjectLeft(positioning._sliderTarget);
    positioning._sliderEndY = toY;
    positioning._sliderDistanceToTravel = Math.abs(positioning._sliderStartY - positioning._sliderEndY);
    positioning._sliderDirection = 1;
    //alert('positioning._sliderTimerId = ' + positioning._sliderTimerId + '\nthis._sliderStartY = ' + positioning._sliderStartY + '\nthis._sliderEndY = ' + positioning._sliderEndY + '\nthis._sliderDistanceToTravel = ' + positioning._sliderDistanceToTravel + '\nthis._sliderTimerInterval = ' + positioning._sliderTimerInterval);
    
    positioning._sliderTimerId = window.setTimeout(positioning._slide, positioning._sliderTimerInterval);
    
  }
}

positioning._slide = function() {
//  if (positioning._sliderDirection == 1)
//    alert('here');
  
  positioning._sliderTimerMoveOffset += 1;
  positioning._sliderTimerFactor += positioning._sliderSnapFactor;
  
  var yOffset = (positioning._sliderTimerFactor * positioning._sliderTimerMoveOffset) * positioning._sliderDirection;
  var x = positioning.getObjectLeft(positioning._sliderTarget);
  var y = positioning.getObjectTop(positioning._sliderTarget) + yOffset;

  positioning.shiftObjectTo(positioning._sliderTarget, x, y);
  positioning._sliderCurrentDistanceTravelled = Math.abs(y - positioning._sliderStartY);

//  if (positioning._sliderDirection == 1)
//   alert('positioning._sliderCurrentDistanceTravelled = ' + positioning._sliderCurrentDistanceTravelled + '\nthis._sliderDistanceToTravel = ' + positioning._sliderDistanceToTravel);
  if (positioning._sliderCurrentDistanceTravelled >= positioning._sliderDistanceToTravel) {
    // first, stop the timer
    window.clearTimeout(positioning._sliderTimerId);
    
    // and then position the object at its final coordinates
    positioning.shiftObjectTo(positioning._sliderTarget, positioning._sliderEndX, positioning._sliderEndY);
    
    // and, finally, reset the slide state properties
    positioning._sliderTarget = null;
    positioning._sliderTargetWidth = -1000;
    positioning._sliderEndX = 0;
    positioning._sliderEndY = 0;
    positioning._sliderDirection = -1;
    positioning._sliderDistanceToTravel = 0;
    positioning._sliderCurrentDistanceTravelled = 0;
    positioning._sliderSnapFactor = 0.1;
    
    positioning._sliderTimerId = null;
    positioning._sliderTimerInterval = 0.1;
    positioning._sliderTimerMoveOffset = 0;
    positioning._sliderTimerFactor = 0.15;
    
    // if the calling client passed a pointer to a callback function, then
    // call that function now
    if (positioning._sliderCallback != null) {positioning._sliderCallback();};
    
    positioning._sliderCallback = null;
    
    if (positioning._sliderQueue.id != null) {
      positioning.slideObjectTo(positioning._sliderQueue.id, positioning._sliderQueue.x, positioning._sliderQueue.y, positioning._sliderQueue.speed, positioning._sliderQueue.w, positioning._sliderQueue.callback);
      positioning._sliderQueue.id = null;
      positioning._sliderQueue.x = 0;
      positioning._sliderQueue.y = 0;
      positioning._sliderQueue.speed = 'normal';
      positioning._sliderQueue.w = 0;
      positioning._sliderQueue.callback = null;
    }
  }
  else {
    positioning._sliderTimerId = window.setTimeout(positioning._slide, positioning._sliderTimerInterval);
  }

}

// position the specified object in the center of the current window viewport width
positioning.centerObject = function(id) {
  var thisObject = this.getObject(id);
  var centerX = windowGeometry.getViewportWidth() / 2;
  var offsetX = this.getObjectWidth(thisObject) / 2;
  this.shiftObjectTo(thisObject, (centerX - offsetX), this.getObjectTop(thisObject));
}

// position the specified object ranged flush against the right and top edges of the current window viewport
positioning.rangeObjectRight = function(id) {
  var xOffset = 0;
  var yOffset = 0;
  var thisObject = this.getObject(id);
  if (arguments[1]) {xOffset = arguments[1];}
  if (arguments[2]) {yOffset = arguments[2];}
  var x = windowGeometry.getViewportWidth() - this.getObjectWidth(thisObject);
  var y = this.getObjectTop(thisObject) - yOffset;
  this.shiftObjectTo(thisObject, x + xOffset, y + yOffset);
}

// position the specified object ranged flush against the right and top edges of the current window viewport
positioning.rangeObjectRight2 = function(id) {
  var xOffset = 0;
  var yOffset = 0;
  var thisObject = this.getObject(id);
  if (arguments[1]) {xOffset = arguments[1];}
  if (arguments[2]) {yOffset = arguments[2];}
  var x = windowGeometry.getViewportWidth() - this.getObjectWidth(thisObject);
  var y = this.getObjectTop(thisObject) - yOffset;
//  if (thisObject.id == 'sectionContent') {
//    alert('thisObject = ' + thisObject.id + '\nwindowGeometry.getViewportWidth() = ' + windowGeometry.getViewportWidth() + '\nthis.getObjectWidth(thisObject) = ' + this.getObjectWidth(thisObject) + '\nx = ' + x + '\nxOffset = ' + xOffset + '\nx + xOffset = ' + (x + xOffset));
//  }
  this.shiftObjectTo(thisObject, x + xOffset, y + yOffset);
}


