
//Name:    suBridgeKit.js
//Author:  Anders Frostad
//Created: 06.25.2007
//Changed: 07.03.2007

/*
TABLE OF CONTENTS
- Class #1:    LiveTabControl(cssClassInactive, cssClassActive)
- Class #2:    suAjax(requestURL)
- Function #1: loadFlash()
- Function #2: loadFlash(id, file, width, height, bgcolor, flashvars)
- Function #3: doNothing()
- Function #4: toggleVisibility(id)
*/

//***********************************************
//********** Class #1: LiveTabControl ***********
//***********************************************

//*CONSTRUCTOR
//About the last parameter: Send the name you call this class object.
function LiveTabControl(cssClassInactive, cssClassActive)
{
  this._cssClassInactive = cssClassInactive;
  this._cssClassActive   = cssClassActive;
}

//*MEMBERS
LiveTabControl.prototype._tabContentIDs = new Array();
LiveTabControl.prototype._tabIDs = new Array();
LiveTabControl.prototype._cssClassInactive = '';
LiveTabControl.prototype._cssClassActive = '';
LiveTabControl.prototype._currentTab = '';

//*FUNCTIONS
//You must add the tab button id, and the DIV tab element id of each tab.
LiveTabControl.prototype.addTab = function(tabContentID, tabID)
{
  this._tabContentIDs[this._tabIDs.length] = tabContentID;
  this._tabIDs[this._tabIDs.length]        = tabID;
}

//Retrieve the currentTab value with this function
LiveTabControl.prototype.getCurrentTab = function()
{
  return this._currentTab;
}

//To change between tabs, pass the TAB BUTTON ID to this function.
LiveTabControl.prototype.changeTab = function(id)
{
  if( typeof id == "undefined" )
    id = this._tabIDs[0];

  var tabCount = this._tabIDs.length;
  for( i = 0; i < tabCount; i++ )
  {
    if( this._tabIDs[i] == id )
    {
      toggleVisibility(this._tabContentIDs[i], TOGGLE_ON);
      document.getElementById(id).className = this._cssClassActive;
    }
    else
    {
      toggleVisibility(this._tabContentIDs[i], TOGGLE_OFF);
      document.getElementById(this._tabIDs[i]).className = this._cssClassInactive;
    }
  }
  this._currentTab = id;
}

//***********************************************
//*************** Class #2: Ajax ****************
//***********************************************

function suAjax(requestURL)
{
  this.handleResponse = function()
  {
    if ( me._http.readyState == 4)
    {
      var rawdata = me._http.responseText.split('|');
      for( var i = 0; i < rawdata.length; i++ )
      {
        var item = (rawdata[i]).split('=>');
        if( item[0].substring(7, 26) == '<b>Parse error</b>:' )
        {
          reportBug(BUGCRITICAL, item[0]);
        }
        else if( item[0] != '' )
        {
          if( item[0] == "javascript" )
            eval(item[1]);
          else
            document.getElementById(item[0]).innerHTML = item[1];
        }
      }
    }
  }
  
  var me = this;
  
  if( ! ( typeof requestURL == "undefined" ) )
    this.request(requestURL);
}

try
{
  suAjax.prototype._http = new XMLHttpRequest();
}
catch(e)
{
  suAjax.prototype._http = new ActiveXObject("Microsoft.XMLHTTP");
}

suAjax.prototype.request = function(requestURL)
{
    this._busy = true;
    this._http.open('GET', requestURL, true);
    this._http.onreadystatechange = this.handleResponse;
    this._http.send(null);
}

//***********************************************
//******** Function #1: reportToTracker *********
//***********************************************

//Bug report types
var PHPCRASH       = 8;
var USERCRITICAL   = 7;
var GENERATEDERROR = 6;
var USERNORMAL     = 4;
var SUGGESTION     = 2;
//Reports a bug to the appropriate tracker.
reportBug = function(reportType, text, name, email)
{
  if( typeof email == "undefined" )
    reportAjax = new Ajax('subridge/report.php?site=' + g_siteID + '&text=' + text);
  else
    reportAjax = new Ajax('subridge/report.php?site=' + g_siteID + '&text=' + escape(text) + '&name=' + escape(name) + '&email=' + escape(email));
}

//***********************************************
//*********** Function #2: loadFlash ************
//***********************************************

loadFlash = function(file, width, height, bgcolor, flashvars)
{
  if( bgcolor == '#ff00ff' )
    windowmode = 'transparent';
  else
    windowmode = 'window';

  document.write( '' +
  '<object classid = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '">' +
  '  <param name = "movie" value="' + file + '" />' +
  '  <param name="quality" value="high" />' +
  '  <param name="bgcolor" value="' + bgcolor + '" />' +
  '  <param name="wmode" value="' + windowmode + '" />' +
  '  <param name="FlashVars" value="' + flashvars + '">' +
  '  <embed src="' + file + '" FlashVars="' + flashvars + '" quality="high" bgcolor="' + bgcolor + '" width="' + width + '" height="' + height + '" wmode="' + windowmode + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
  '</object>');
}

//***********************************************
//*********** Function #3: doNothing ************
//***********************************************

doNothing = function()
{
}

//***********************************************
//******** Function #3: toggleVisibility ********
//***********************************************
var TOGGLE_AUTO = 0;
var TOGGLE_ON   = 1;
var TOGGLE_OFF  = 2;
toggleVisibility = function(id, toggle, tween)
{
  element = document.getElementById(id);
  
  if( ! element.tweening )
  {
    if( ( element.style.visibility == 'hidden' || toggle == TOGGLE_ON ) && toggle != TOGGLE_OFF )
    {
      element.style.display = 'block';
      element.style.visibility = 'visible';
      if( tween == true )
        changeHeight(id, element.originalHeight, '');
    }
    else
    {
      if( tween == true )
      {
        element.originalHeight = element.offsetHeight;
        changeHeight(id, 0, 'toggleVisibility("' + id + '", TOGGLE_OFF, false);');
      }
      else
      {
        element.style.display = 'none';
        element.style.visibility = 'hidden';
      }
    }
  }
}

changeHeight = function(id, newHeight, evalWhenDone)
{
  targetObject = document.getElementById(id);
  targetObject.tweening = true;
  
  if( ! ( targetObject.offsetHeight == newHeight ) )
    setTimeout("changeHeight('" + id + "'," + newHeight + ", '" + evalWhenDone + "')", 50);
  else
  {
    targetObject.tweening = false;
    eval(evalWhenDone);
  }
  
  if( targetObject.offsetHeight < newHeight )
  {
    var suggestChange = (newHeight - targetObject.offsetHeight) / 5;
    if( suggestChange < 1 )
      targetObject.style.height = newHeight + "px";
    else
      targetObject.style.height = targetObject.offsetHeight + Math.ceil(suggestChange) + "px";
  }

  if( targetObject.offsetHeight > newHeight )
  {
    var suggestChange = (targetObject.offsetHeight - newHeight) / 5;
    if( suggestChange < 1 )
      targetObject.style.height = newHeight + "px";
    else
      targetObject.style.height = targetObject.offsetHeight - Math.floor(suggestChange) + "px";
  }
}

//***********************************************
//******* Function #4: dockOutsideObject ********
//***********************************************

function dockOutsideObject(objectID, dockID, margin, direction)
{
  var dockee = document.getElementById(objectID);
  var dock =   document.getElementById(dockID);
  
  if( direction == "left" )
  {
    var suggest = dock.offsetLeft - dockee.offsetWidth - margin;
    dockee.style.left = suggest + "px";
  }
  else if( direction == "right" )
  {
    if( document.documentElement.clientWidth )
      var clientWidth = document.documentElement.clientWidth;
    else
      var clientWidth = document.body.clientWidth;
  
    var suggest = dock.offsetLeft + dock.offsetWidth + margin;
    dockee.style.left = suggest + "px";
  }
  else if( direction == "top" )
  {
    var suggest = dock.offsetTop - dockee.offsetHeight - margin;
    dockee.style.top = suggest + "px";
  }
  else if( direction == "bottom" )
  {
    if( document.documentElement.clientHeight )
      var clientHeight = document.documentElement.clientHeight;
    else
      var clientHeight = document.body.clientHeight;
  
    var suggest = dock.offsetTop + dock.offsetHeight + margin;
    dockee.style.top = suggest + "px";
  }

  setTimeout("dockOutsideObject('" + objectID + "','" + dockID + "'," + margin + ",'" + direction + "')", 100);     
}

//***********************************************
//******** Function #4: dockInsideObject ********
//***********************************************

function dockInsideObject(objectID, dockID, margin, direction)
{
  if( document.documentElement.scrollTop )
    var scroll = document.documentElement.scrollTop;
  else
    var scroll = document.body.scrollTop;
  
  var dockee = document.getElementById(objectID);
  var dock   = document.getElementById(dockID);
  
  if( direction == "left" )
    dockee.style.left = ( dock.offsetLeft + margin ) + "px";
  else if( direction == "right" )
    dockee.style.left = ( dock.offsetLeft + dock.offsetWidth - dockee.offsetWidth - margin ) + "px";
  else if( direction == "top" )
  {
    var suggest = dock.offsetTop + margin;
    if( suggest - margin < scroll )
      suggest = scroll + margin;
    dockee.style.top = suggest + "px";
  }
  else if( direction == "bottom" )
    dockee.style.top = ( dock.offsetTop + dock.offsetHeight - dockee.offsetWidth - margin ) + "px";

  setTimeout("dockInsideObject('" + objectID + "','" + dockID + "'," + margin + ",'" + direction + "')", 100);     
}