var DDSPEED = 10;
var DDTIMER = 15;
// HDRPICS = new Array("hdr01","hdr02","hdr03","hdr04","hdr05","hdr06","hdr07","hdr08","hdr09","hdr10");
// HDRPICW = new Array(151,160,156,218,158,176,182,294,279,224);
// HDRNAME = new Array("Ceirog Viaduct","Forth Road Bridge","Greta Bridge","Runnymede Bridge","Florida Keys","Skye Bridge","Shoreham A27 Bridge","Orwell Bridge","Plougastel Bridge","Severn Bridge")
// HDRLPIC = new Array("Ceirog_Viaduct.jpg","Forth_Road_Bridge.jpg","Greta_Bridge.jpg","Runnymede_Bridge.jpg","Keys_Florida.jpg","Skye_Bridge.jpg","Shoreham_A27_Bridge.jpg","Orwell_Bridge.jpg","Plougastel_Bridge.jpg","Severn_Bridge.jpg")
 HDRPICS = new Array("hdr01","hdr02","hdr03","hdr06","hdr08","hdr10","hdr11","hdr12","hdr13","hdr14");
 HDRPICW = new Array(151,160,156,176,294,224,207,161,175,160);
 HDRNAME = new Array("Ceirog Viaduct","Forth Road Bridge","Greta Bridge","Skye Bridge","Orwell Bridge","Severn Bridge","Evesham Abbey Bridge & Viaduct","Medway Bridges - M2 & Rail-link","Crooked River Bridge - Terrebonne Oregon","Millau Viaduct")
 HDRLPIC = new Array("Ceirog_Viaduct.jpg","Forth_Road_Bridge.jpg","Greta_Bridge.jpg","Skye_Bridge.jpg","Orwell_Bridge.jpg","Severn_Bridge.jpg","Evesham_Abbey_Bridge.jpg","Medway_Bridges.jpg","Crooked_River_Bridge.jpg","Millau_Viaduct.jpg")
HDRLOAD = new Array(10)
var HDRPICN = HDRPICS.length;

// main function to handle the mouse events
//
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){
      return
    }
    else{
      if(!c.maxh){
        c.style.display = 'block';
        c.style.height = 'auto';
        c.maxh = c.offsetHeight;
        c.style.height = '0px';
      }
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
  else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu
//
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown
//
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity
//
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

// close header picture
//
function closeHdrPic(){
  var p  = document.getElementById('hdrpic');
  clearInterval(p.interval);
  var ps = p.src
  var sl = document.getElementById('hdrspl');
  var sr = document.getElementById('hdrspr');
  w = p.width
  if (w < 13){
    sl.width = 220;
    p.width = 1;
    sr.width = 209; 
    p.style.opacity = 0;
    p.style.filter = 'alpha(opacity=0)';
    p.interval = setInterval(function(){chngHdrPic()},10);

  } else {
    w = w - 12
    sl.width = 220 - Math.round(w/2)
    p.width  = w;
    sr.width = 430 - (w + sl.width) 
    xtemp = w / p.maxwidth;
    xtemp = xtemp.toFixed(2)
    p.style.opacity = xtemp;
    p.style.filter = 'alpha(opacity=' + (xtemp * 100) + ')';
    p.interval = setInterval(function(){closeHdrPic()},30);
  }
}

// open header picture
//
function openHdrPic(){
  var p  = document.getElementById('hdrpic');
  clearInterval(p.interval);
  var sl = document.getElementById('hdrspl');
  var sr = document.getElementById('hdrspr');
  w = p.width
  if (w + 12 >= p.maxwidth){
    w = p.maxwidth
    sl.width = 220 - Math.round(w/2)
    p.width  = w;
    sr.width = 430 - (w + sl.width) 
    p.style.opacity = 1;
    p.style.filter = 'alpha(opacity=100)';
    p.interval = setInterval(function(){closeHdrPic()},10000);

  } else {
    w = w + 12
    sl.width = 220 - Math.round(w/2)
    p.width  = w;
    sr.width = 430 - (w + sl.width) 
    xtemp = w / p.maxwidth;
    xtemp = xtemp.toFixed(2)
    p.style.opacity = xtemp;
    p.style.filter = 'alpha(opacity=' + (xtemp * 100) + ')';
    p.interval = setInterval(function(){openHdrPic()},30);
  }
}

// change header picture
//
function chngHdrPic(){
  var p  = document.getElementById('hdrpic');
  clearInterval(p.interval);
  var ps = p.src
  var i  = 0;
  var q  = ps;

  while (q == ps || i == HDRPICN) {
    i  = Math.round(Math.random() * HDRPICN);
    q  = "http://www.cbdg.org.uk/images/Hdrs/" + HDRPICS[i] + ".gif";
  }
  p.maxwidth = HDRPICW[i]
  p.title    = HDRNAME[i]
  p.width = 1;
  p.src = q
  p.width = 1;
  p.style.opacity = 0;
  p.style.filter = 'alpha(opacity=0)';
  p.which = i
  p.interval = setInterval(function(){openHdrPic()},5000);   
}

// start header picture
//
function startHdrPic(){
  for (i = 0; i < HDRPICN; i++) {
    HDRLOAD[i]     = new Image()
    HDRLOAD[i].src = "http://www.cbdg.org.uk/images/Hdrs/" + HDRPICS[i] + ".gif"
    HDRLOAD[i].onLoad = hdrLoaded(i)
  }
}

function hdrLoaded(i){
  HDRLOAD[i].rdy = "1"
  var gofi = true
  for (j = 0; j < HDRPICN; j++) {
    if (HDRLOAD[j] == undefined){
      gofi = false
    }
  }
  if (gofi){
    startHdrPic2()
  }
}

function startHdrPic2(){
  var p  = document.getElementById('hdrpic');
  var sl = document.getElementById('hdrspl');
  var sr = document.getElementById('hdrspr');
  clearInterval(p.interval);
  var i  = HDRPICN;
  while (i == HDRPICN) {
    i  = Math.round(Math.random() * HDRPICN);
    q  = HDRLOAD[i].src
//    q  = "http://www.cbdg.org.uk/images/Hdrs/" + HDRPICS[i] + ".gif";
  }
  p.maxwidth = HDRPICW[i]
  p.title    = HDRNAME[i]
  sl.width = 217;
  p.width = 1;
  p.src = q
  p.width = 1;
  sr.width = 206; 
  p.style.opacity = 0;
  p.style.filter = 'alpha(opacity=0)';
  p.which = i
  p.interval = setInterval(function(){openHdrPic()},30);    
}

// show picture
//
function showHDRPic() {
  var p = document.getElementById('hdrpic');
  i=p.which
  f = "http://www.cbdg.org.uk/images/Hdrs/" + HDRLPIC[i]
  docwindow = window.open(f,"","");
  return;
}

// key press in site search
//
function kpss(evt) {
  evt = evt || window.event;
  var kc = evt.keyCode || evt.which;
  if (kc == 13) {
    siteSearch();
  }
  return;
}

// get cookie value
//
function getCookieValue(CookieName) {
  CookieName = CookieName + "=";
  var NameLength = CookieName.length;
  var CookieLength = document.cookie.length;
  var fc = 0;
  var lc = 0;
  while (fc < CookieLength) {
    lc = fc + NameLength;
    if (document.cookie.substring(fc,lc) == CookieName) {
      lc1 = document.cookie.indexOf(";",lc)
      if (lc1 == -1) {lc1 = CookieLength}
      return unescape(document.cookie.substring(lc,lc1))
    }
    fc = document.cookie.indexOf(" ",fc) + 1;
    if (fc == 0) {break};
  }
  return null;
}

// site search
//
function siteSearch() {
  var q = document.ss.zq.value
  if (q == "") {
    alert ("Search for what?");
    return;
  }
  t = 0;
  if (document.ss.zt[1].checked == true) {t = 1}
  f = "search.asp?zoom_sort=0&zoom_query=" + q + "&zoom_per_page=10&zoom_and=" + t;
  var g = "dependent=yes, height=512, width=1024, resizable=yes, menubar=no, toolbar=no, location=no, status=no, scrollbars=yes"
  searchwindow = window.open(f,"SiteSearch",g);
  searchwindow.focus()
  return;
}

// show login window for Members
//
function showLogin(){
  var d = new Date();
  var s = d.getUTCSeconds() + 10;
  d.setUTCSeconds(s);
  var cookiedata = "TestCookieOn=Enabled;expires=" + d.toUTCString();
  document.cookie = cookiedata;
  if (getCookieValue("TestCookieOn") != "Enabled") {
    alert ("Cookies must be enabled before you log-in");
    return;
  }
  
  f = "http://www.cbdg.org.uk/loginM.asp";
  w = Math.round((pageWidth() - 250)/2)
  h = Math.round((pageHeight() - 150)/2)
  NUA = navigator.userAgent
//  alert ("NUA:" + NUA)
  b = NUA.indexOf("MSIE")
  if (b != -1) {
//      alert ("MSIE")
    xPos = event.screenX
    yPos = event.screenY
//    alert(xPos + " XXleft " + yPos + " down")
    w = xPos - 500
    h = yPos + 20

 } else {
//      alert ("FF")
    w = w + window.screenX
    h = h + window.screenY
  }
  g = "dependent=yes, height=150, width=300, resizable=no, menubar=no, toolbar=no, location=no, status=no, top=" + h +", left=" + w
  temp = window.open("http://www.cbdg.org.uk/blank.html","Submissions_Window",g)
  temp.close()
  subWindow = window.open(f,"LogIn",g);
  subWindow.focus()
}

// show login window for Guests
//
function showGLogin(p){
  var d = new Date();
  var s = d.getUTCSeconds() + 10;
  d.setUTCSeconds(s);
  var cookiedata = "TestCookieOn=Enabled;expires=" + d.toUTCString();
  document.cookie = cookiedata;
  if (getCookieValue("TestCookieOn") != "Enabled") {
    alert ("Cookies must be enabled before you log-in");
    return;
  }
  f = "http://www.cbdg.org.uk/loginG.asp?cby=" + p
  subWindow=window.open(f,"Submissions_Window","resizable=yes,scrollbars=yes,width=700,height=300,menubar=no");
  subWindow.focus();
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {
  return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;}

function pageHeight() {
  return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;}

function posLeft() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;}

function posTop() {
  return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;}

function posRight() {
  return posLeft()+pageWidth();}

function posBottom() {
  return posTop()+pageHeight();}
                    

