/* Generica Javascript functions required for the site */

function BrowserInfo() {
  var agent = window.navigator.userAgent;
  if (agent.indexOf("MSIE") != -1) {
      var start = agent.indexOf("MSIE");
      this.name = "MSIE";
      this.version = parseFloat(agent.substring(start + 5, agent.indexOf(";", start)));
  } else if (agent.indexOf("Firefox") != -1) {
      var start = agent.indexOf("Firefox");
      this.name = "Firefox";
      this.version = agent.substring(start + 8, agent.length);
      var firstDec = this.version.indexOf(".") + 1;
      while (this.version.indexOf(".", firstDec) != -1)
        this.version = this.version.substring(0, firstDec) + this.version.substring(firstDec).replace(".", "");
      this.version = parseFloat(this.version);
  } else {
      this.name = "Unknown";
      this.version = 0;
  }
}

var info = new BrowserInfo();
var userAgent = window.navigator.userAgent
var isIE = (window.navigator.userAgent.indexOf("MSIE") != -1);
var isIE6 = (info.name == "MSIE" && info.version < 7);
var isFireFox = (window.navigator.userAgent.indexOf("Firefox") != -1);

/* Javascript for controlling a popping window */

var PopStage=0;
var PopHeight=1;
var PopTimer;
var PopInterval;
var PopAnchorLeft = 1;
var PopAnchorTop = 1;
var PopEndHeight = 0;

function ShowPop(what, MyHeight) {

  PopEndHeight = MyHeight;
  if (!(PopEndHeight)) {
    PopEndHeight = 150;
  }
  var mainWindow = document.getElementById('FullPopWindow');
  if (mainWindow) {
    var frameWindow = document.getElementById('FullPopCenter');
    frameWindow.setAttribute('src', what);
    frameWindow.style.height = '0px';

  } else {

    mainWindow = document.createElement('div');
    mainWindow.setAttribute('id', 'FullPopWindow');

    var frameWindow = document.createElement('iFrame');
    frameWindow.setAttribute('id', 'FullPopCenter');
    frameWindow.setAttribute('src', what);
    frameWindow.setAttribute('name', 'FullPopCenter');
    frameWindow.setAttribute('noresize', 'noresize');
    frameWindow.setAttribute('scrolling', 'no');
    frameWindow.setAttribute('frameborder', '0');
    frameWindow.setAttribute('border', '0');
    frameWindow.setAttribute('marginwidth', '0');
    frameWindow.setAttribute('marginheight', '0');
    frameWindow.setAttribute('vspace', '0');
    frameWindow.setAttribute('vspace', '0');
    if (isIE6) {
      // Block trasnparency as it lets underlying form fields show through.
      frameWindow.setAttribute('allowtransparency', 'false');
      frameWindow.allowTransparency = false;  // This is required for IE to getit right :)
    } else {
      frameWindow.setAttribute('allowtransparency', 'true');
      frameWindow.allowTransparency = true;  // This is required for IE to getit right :)
    }
    frameWindow.frameBorder = '0';  // This is required for IE to getit right :)
    frameWindow.width = '352px';

    mainWindow.appendChild(frameWindow);
    document.body.appendChild(mainWindow);
  }

  if (mainWindow) {
    /* Clip both main and frame windows */
    var aSize = GetInnerSize();
    if (mainWindow.attachEvent) {
      mainWindow.attachEvent('onclick', ClosePop);
    } else {
      mainWindow.onclick = ClosePop;
    }

    /* Clip Frame Window (whilst it loads) */
    var aScroll = GetScrollPos();
    var frameWindow = document.getElementById('FullPopCenter');
    frameWindow.style.height = '0';
    frameWindow.style.display = 'none';
    frameWindow.style.left = ((aSize[0] - parseInt(frameWindow.width)) / 2) + 'px';
    frameWindow.style.top = '500px';

    /* Position main */
    mainWindow.style.height = '0px';
    mainWindow.style.left = '0';
    mainWindow.style.top = aScroll[1] + 'px';
    mainWindow.style.width = '100%';
    mainWindow.style.display = 'block';

    /* Start the expansion */
    PopHeight = 1;
    PopStage = 1;

    /* Add Timer to expand window, frame */
    PopTimer = setTimeout(ExpandPop, 30);
    return false;
  } else {
    return true;
  }
}



function ExpandPop() {
    if (PopStage > 0) {
      var mainWindow = document.getElementById('FullPopWindow');
      var aSize = GetInnerSize();
      if (PopStage > 1) {
        /* Always check position of inner window */
        mainWindow.style.height = aSize[1] + 'px';
        var aScroll = GetScrollPos();
        var frameWindow = document.getElementById('FullPopCenter');
/*        PopHeight = math.max(PopHeight + 50, 300) ;  */
        //PopHeight = PopHeight + 30;
        PopHeight = PopEndHeight;
        frameWindow.style.height = PopHeight + 'px';
        frameWindow.style.top = (400 - PopHeight) + 'px';
/*        frameWindow.style.marginTop = (500 - PopHeight) + 'px';      */
        if (PopHeight >= PopEndHeight) {
          PopStage++;
        } else {
          PopTimer = setTimeout(ExpandPop, 30);
        }

      } else {
        /* Expand the div downwards */
        PopHeight = Math.max(PopHeight + 80, aSize[1]);
        mainWindow.style.height = PopHeight + 'px';
        if (PopHeight >= aSize[1]) {
          if (window.attachEvent) {
            window.attachEvent('onresize', PopMoveResize);
            window.attachEvent('onscroll', PopMoveResize);
          } else {
            PopInterval = setInterval("PopMoveResize()", 30);
/*            window.onresize = PopMoveResize;
            window.onccroll = PopMoveResize;  */
          }
          PopHeight = 0;
          PopStage++;
          var frameWindow = document.getElementById('FullPopCenter');
/*          frameWindow.style.marginTop = '490px';  */
          frameWindow.style.height = '0px';
          frameWindow.style.display = 'block';
          PopTimer = setTimeout(ExpandPop, 300);
        } else {
          PopTimer = setTimeout(ExpandPop, 30);
        }

      }
    }
}

function ClosePop() {
  var mainWindow = document.getElementById('FullPopWindow');
  return ClosePopSub(mainWindow);
}

function ClosePop2() {
  /* Returns to the parent window */
  mainWindow = parent.document.getElementById('FullPopWindow');
  return ClosePopSub(mainWindow);
}

function ClosePopSub(mainWindow) {
  clearTimeout(PopTimer);
  if (mainWindow) {
    if (mainWindow.detachEvent) {
       mainWindow.detachEvent('onclick');
       window.detachEvent('onresize');
       window.detachEvent('onscroll');
    } else {
       mainWindow.onclick = '';
    }
    if (PopInterval) {
       clearInterval(PopInterval);
    }
    var aSize = GetInnerSize();
    PopHeight = aSize[0];
    PopTimer = setTimeout(FadePop, 1);
    return false;
  } else {
    return true;
  }
}

function FadePop() {
  var mainWindow = document.getElementById('FullPopWindow');
  var frameWindow = document.getElementById('FullPopCenter');
  if (mainWindow == null) {
    mainWindow = parent.document.getElementById('FullPopWindow');
    frameWindow = parent.document.getElementById('FullPopCenter');
  }

  if (mainWindow) {
    PopHeight = Math.max(0,PopHeight - 80);
    mainWindow.style.height = PopHeight + 'px';
    if (frameWindow) {
      var frameHeight = Math.min(300, Math.max(0, PopHeight-200));
      frameWindow.style.height = frameHeight + 'px';
    }
    //if (PopHeight == 0) {
    mainWindow.style.left = '-800px';
    mainWindow.style.width = '352px';
    mainWindow.style.display = 'none';
    clearTimeout(PopTimer);
  }
}

function PopMoveResize(var1, var2) {
  /* Positions the pop window in the right spot on resize or move */
  if (PopStage>2) {
    var mainWindow = document.getElementById('FullPopWindow');
    var frameWindow = document.getElementById('FullPopCenter');
    var aSize = GetInnerSize();
    mainWindow.style.height = aSize[1] + 'px';
    var aScroll = GetScrollPos();
    mainWindow.style.top = aScroll[1] + 'px';
    frameWindow.style.left = (aSize[0] - parseInt(frameWindow.width)) / 2 + 'px';
  }
}

/* Generic functions for chackign windo position */
function GetInnerSize () {
  var x,y;
  if (self.innerHeight) { // all except Explorer
    x = self.innerWidth;
    y = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    x = document.body.clientWidth;
    y = document.body.clientHeight;
  }
  return [x,y];
}

/* Generic functions for chackign windo position */
function GetScrollPos () {
  var x,y;
  var x = document.body.scrollLeft;
  var y = document.body.scrollTop;
  if (x == 0) {
    if (window.pageYOffset) {
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else {
        x = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
        y = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
  }
  return [x,y];
}




