// css detect
var css = (document.getElementById) ? 1 : 0;
var ns4 = (document.layers) ? 1 : 0;
var moz = (navigator.product == "Gecko") ? 1 : 0;
var mac = (navigator.platform == "MacPPC") ? 1 : 0;

var overMenus = [];
var upMenus = [];
var closeTimer = false;
var menus = [];

var menuItems = [
  ['ABOUT US',0],
  ['BEST BETS',0],
  ['BOOK NOW',
    ['AIRLINES','CAR RENTALS','CRUISE LINES','ESCORTED TOURS','RAIL','HOTELS','ADVENTURE TRAVEL']
  ],
  ['CONTACT US',
    ['QUOTE REQUEST','GIFT CERTIFICATES','WEDDING/GIFT REGISTRY:registry','INCENTIVE PROGRAM']
  ],
  ['INSURANCE'],
  ['GROUP TRAVEL',0],
  ['TRAVEL NEWS',
    ['TIPS','FAQS']
  ],
  ['TRAVEL LINKS',
    ['TOUR OPERATORS']
  ]
];

writeMainButtons = function(mi,y,page){
  var section = findPage(page);
  var d,m = '';
//  m += '<div id="mainMenu_div" style="position:absolute; top:0px; width:100%; z-index:2;">';
  m += '<table id="mainMenu" width="800" align="center" cellpadding=0 cellspacing=0 border=0><tr>';//<td><img src="/menuPics/spacer.gif" height=30 width=1></td>';
//  m += '<td colspan="'+ mi.length + '"><img src="/pics/menu_picBanner.jpg" style="display:inline;" /></td></tr><tr>';
  var prevl=0; var i=-1;
  while(++i < mi.length){
    m += '<td width="100" class="button" onMouseOver="showMenu(\''+ mi[i][0] + '\',this);highlightSB(this);"';
    if(mi[i][1]) m += ' onMouseOut="hideMenus(\''+ mi[i][0] + '\');normalSB(this,'+(section===i)+');"';
    else m += ' onMouseOut="normalSB(this,'+(section===i)+');"';
    m += ' onMouseUp="buttonAction(this, \'' + mi[i][0] + '\');"';
    if(section===i) m += ' style="background-color:#fea32d; color:#ffffff;"';
//    m += '><img src="/menuPics/' + mi[i][0] + '_0.gif" border=0></td>';
    m += '>' + mi[i][0] + '</td>';
    if(mi[i][1]){
      d = createMenu(mi[i][0],mi[i][1],i);
//      d.style.left = left + (prevl += (i==0 ? 0 : i<3 ? 140 : 110)) + "px";
      d.style.top = y ? y : "207px";
      d.xindex = i;
      menus.push(d);
    };
  };
//  m += '</tr><tr><td colspan=' + (mi.length+1) + '><img src="/menuPics/spacer.gif" height=7></td></tr></table>';
  m += '</tr></table>';
//  m += '</div>';
  // open a table to capture any absolute softvoyage stuff (why aren't they consistent?!?)
  // the divs are set style='relative' ... somewhere.
  m += '<table width=800 align="center" cellpadding=0 cellspacing=0 border=0><tr><td align="center"><center>';
//  m += '<div id="sv_pos" style="position:absolute; top:'+y+'px; text-align:center;">';
// --- //
  document.write(m);
};

createMenu = function(n,mi,zindex){
  var de = '<div id="' + n + '" class="menu" style="z-index:'+(100+zindex)+'"';
  de += ' onMouseOver="menuOverAction(this);"';
  de += ' onMouseOut="menuOutAction(this);"';
  de += '><table cellspacing=1>';
//*
  var i = -1;
  while(++i < mi.length){
    de += '<tr><td width="100%" class="subButton" style="background-color:#ffffff"';
    de += ' onMouseOver="highlightSB(this);"';
    de += ' onMouseOut="normalSB(this);"';
    de += ' onMouseUp="buttonAction(this,\'' + getLink( mi[i] ) + '\','+zindex+');"';
    de += '>' + getName( mi[i] ) + '</td></tr>';
  };
//*/
  de += '</table></div>';
  document.write(de);
  var d = getItem(n);
  d.style.visibility = 'hidden';
  return d;
}

getName = function( mi ) {
  mi = mi.split( ':' );
  return mi[0];
}

getLink = function( mi ) {
  mi = mi.split( ':' );
  return mi[1] ? mi[1] : mi[0];
}

findPage = function(page){
  var i,j;
  for(i in menuItems){
    if(menuItems[i][0] == page) return parseInt(i);
    for(j in menuItems[i][1]){
      if(menuItems[i][1][j] == page) return parseInt(i);
    };
  };
  return false;
};

placeMenus = function(){
  var left = Math.floor((Math.max(getBrowserSize().width,800) - 800) / 2);
  // account for scrollbar in mozilla & mac
  if(document.body.scrollHeight && !ns4){
    var offset = mac && moz ? 0 : mac ? 6 : 8;
    if(document.body.scrollHeight > window.innerHeight) left -= offset;
  };
  var i = -1;
  while(++i < menus.length){
    var xi = menus[i].xindex;
    var offset = 100*xi;// + (xi>1 ? 65 : xi>0 ? 35 : 0);
    menus[i].style.left = (left + offset) + "px";
  };
  // if the softvoyage div exists, center it too
//  var d = getItem('sv_pos');
//  if(d) d.style.left = left;
//  alert('css:'+css+'\nns4:'+ns4+'\nmoz:'+moz+'\nmac:'+mac);
};

buttonAction = function(b,lnk){
  lnk = lnk.replace(/[ &]/g,'_').toLowerCase();

  if(lnk == 'quote_request' || lnk == 'registry' || lnk == 'gift_certificates'){
   var root = 'https:/jubileetravel.domain7.com/';
  }else{
    var root = 'http://jubileetravel.domain7.com/';
  }

  if(top.frames.length) top.location = root + '?p=' + lnk;
  else location = root + '?p=' + lnk;
//  document.location = root + '?p=' + lnk;


/*  var i,o = '';
  for(i in document.location){
    o += i+ ":" + document.location[i] + "\n";
  };
  alert(o);
*/
}

highlightSB = function(b){
  b.style.backgroundColor = "#fea32d"; //currColour.dark; //"#7f6f5b";
  b.style.color = "#ffffff";
}

normalSB = function(b,isSection){
  b.style.backgroundColor = isSection ? "#fea32d":"#ffffff"; //currColour.light; //"#a89a87";
  b.style.color = isSection ? "#ffffff":"#666666";
}

menuOverAction = function(m){
  overMenus.push(m);
}

menuOutAction = function(m){
  hideMenus(null,m);
}


showMenu = function(menu,button){
//  button.style.backgroundColor = "#eeeeee";
  var m;
  if(m = getItem(menu)){
    // hack to put parent button ref in menu
    m.button = button;
    overMenus.push(m);
    upMenus.push(m);
    m.style.visibility = 'visible';
  }
}

//*
hideMenus = function(menu,m){
  if(!m && !(m = getItem(menu))) return;
//  alert('hm - menu:'+menu+' m:'+m);
  overMenus.removeItem(m);
  if(!closeTimer){
    closeTimer = setTimeout('closeAllMenus()',250);
  }
}

closeAllMenus = function(){
  closeTimer = false;
  var m,i = upMenus.length;
  while(i--){
    m = upMenus[i];
    if(overMenus.getplace(m) == -1){
      m.style.visibility = 'hidden';
//      m.button.style.backgroundColor = "#9b8b77";
      upMenus.removeItem(m);
    }
  }
}
//*/

getItem = function(targ){
  if(css) return document.getElementById(targ);
  else if(ns4) return document[targ];
  else return eval(targ);
}

traceObj = function(o){
  var i,s='';
  for(i in o){
    s += i + ":" + o[i] + "<br>\n";
  }
  alert(s);
}

Array.prototype.getplace = function(item){
  var i = this.length;
  while(i--){
    if(this[i]==item){ return(i);}
  }
  return(-1);
};

Array.prototype.removeItem = function(item){
  var p = this.getplace(item);
  if(p>-1){
    this.splice(p,1);
    return true;
  };
  return false;
};

Array.prototype.shuffle = function(){
  var t,r,i = this.length;
  var l = i;
  while(i--){
    r = Math.floor(Math.random()*l);
    t = this[i];
    this[i] = this[r];
    this[r] = t;
  };
};

if(!Array.prototype.push){
  Array.prototype.push = function(){
    i = -1;
    while(++i < arguments.length) this[this.length] = arguments[i];
  };
};

if(!Array.prototype.splice){
  Array.prototype.splice = function(i,l){
    var len = this.length-l;
    while(i < len){
      this[i] = this[i+l];
      i++;
    };
    this.length = len;
  };
};

// browser size function grabbed from... um... somewhere.  amended.
function getBrowserSize() {
  var w,h;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    w = window.innerWidth;
    h = window.innerHeight;
  }else{
//*                            ie6 - standards compliant : ie4
    var ref = document.documentElement.clientWidth ? document.documentElement : document.body ;
    w = ref.clientWidth;
    h = ref.clientHeight;
  };
/*/
    if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
      alert('standards');
    } else {
      if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
        alert('ie4');
      }
    }
  }
//*/
  return {height:h,width:w};
};


// banner image swap --------------------------------------------

function Banner(id,picpath){
  this.picpath = picpath ? picpath : '';
  this.pics = [];
  this.currPic = 0;
  this.div = this.makeBanner(id);
  this.playing = setInterval(this.rotateImages, 2500);
  // put a reference in the global space ... sigh, must be a better way.
  window.rotatingBanner = this;
};

Banner.prototype.makeBanner = function(id){
  document.write("\n" + '<table id="'+id+'" width="800" height="144" cellpadding="0" cellspacing="0" border="0" style="margin-left: auto; margin-right: auto;"><tr>');

  document.write("\n" + '<td width="200" style="background: url(/images/banner_logo_bg_new.gif) no-repeat;"><a href="/"><img src="/images/banner_logo_dev.gif" alt="Travel Best Bets Logo - click to see our Best Bets" border="0" style="padding: 40px 0 0 10px;" /></a></td>');

  var widths = [158,151,150,142];
  var picArrays = [
    ['banner0.jpg','banner0a.jpg','banner0b.jpg','banner0c.jpg','banner0d.jpg','banner0e.jpg','banner0f.jpg','banner0g.jpg','banner0h.jpg','banner0i.jpg','banner0j.jpg','banner0k.jpg','banner0l.jpg','banner0m.jpg'],
    ['banner1.jpg','banner1a.jpg','banner1b.jpg','banner1c.jpg','banner1d.jpg','banner1e.jpg','banner1f.jpg','banner1g.jpg','banner1h.jpg','banner1i.jpg','banner1j.jpg','banner1k.jpg','banner1l.jpg','banner1m.jpg'],
    ['banner2.jpg','banner2a.jpg','banner2b.jpg','banner2c.jpg','banner2d.jpg','banner2e.jpg','banner2f.jpg','banner2g.jpg','banner2h.jpg','banner2i.jpg','banner2j.jpg','banner2k.jpg','banner2l.jpg','banner2m.jpg'],
    ['banner3a.jpg','banner3.jpg','banner3b.jpg','banner3c.jpg','banner3d.jpg','banner3e.jpg','banner3f.jpg','banner3g.jpg','banner3h.jpg','banner3i.jpg','banner3j.jpg','banner3k.jpg','banner3l.jpg','banner3m.jpg']
  ];
  var p = -1;
  while(++p < 4){
    document.write("\n" + '<td width="'+widths[p]+'">');
    this.pics.push(new BannerPic('pic'+p, '/'+this.picpath+'pics/banner/'+p+'/', picArrays[p]));
    document.write('</td>');
  };
  document.write("\n" + '</tr></table>');
	
  return getItem(id);
};

Banner.prototype.rotateImages = function(){
  t = window.rotatingBanner;
  t.pics[t.currPic].fadeInNextPic();
  t.currPic = Math.floor(Math.random()*t.pics.length); //(t.currPic+1)%t.pics.length;
};



function BannerPic(id,path,pics){
  this.pics = pics;
  this.pics.shuffle()
  this.path = path;
  this.currPic = 0;
  this.opacity = 0;
  this.img0 = this.makeImage(id+'_0',this.path+this.pics[this.currPic]);
  this.img1 = this.makeImage(id+'_1',this.path+this.pics[this.currPic]);
  this.nextPic = new Image();
  this.nextPic.src = this.path+this.pics[++this.currPic];
//	alert('path:'+this.path);
//	this.nextPic.src = this.pics[++this.currPic];
};

BannerPic.prototype.makeImage = function(id,src){
  var d = '<div style="position:absolute; top:0px;">';
  d += '<img id="'+id+'" class="bannerPic" src="'+src+'" /></div>';

  document.write(d);
  return getItem(id);
};

BannerPic.prototype.fadeInNextPic = function(){
  this.currPic = (this.currPic+1)%this.pics.length;
  this.setOpacity(1,0);
  // make the back the same as the front
  this.img0.src = this.img1.src;
  this.img1.src = this.nextPic.src;
  // preload the next image
  this.nextPic.src = this.path+this.pics[this.currPic];
//  alert('img0: ' + this.img0.src + '\nimg1: '+ this.img1.src + '\nnext: '+this.nextPic.src);

  this.opacity = 0;
  this.isFading = setInterval(this.fadePic,100,this);
  // set a reference to here in the global space ... ie won't take the argument in setInterval
  window.fadingPic = this;
};

BannerPic.prototype.fadePic = function(t){
  if(!t) t = window.fadingPic;
  t.opacity += 10;
  if(t.opacity >= 100){
    clearInterval(t.isFading);
    // 99 prevents mozilla from flickering on 100
    t.setOpacity(1,99);
  }else{
    t.setOpacity(1,t.opacity);
  };
};

BannerPic.prototype.setOpacity = function(imgNum,opacity){
  var o = this['img'+imgNum].style;
  o.opacity = (opacity / 100);
  o.MozOpacity = (opacity / 100);
  o.filter = "alpha(opacity=" + opacity + ")";
};

BannerPic.prototype.getOpacity = function(img){
  return img.style.MozOpacity;
};

// -------------------------------------------------------
