var index_currentPhoto = -1;
var index_currentMenu = -1;
var index_currentSubmenu = -1;
var index_currenttmenu = -1;
var site_largetext = 0;
var menu_slide_duration = 750;
var curr_cal = -1;
var menu_fx = new Array();
var menu_width = new Array();
var m_sliding = false;
var t_background = null;
var menu = new Array (
  'ourgallery',
  'exhibitions',
  'programs',
  'media',
  'contact',
  'home',
  'calendar',
  this_year,
  next_year
);

var preload_rollovers = new Array(
  'm_ourgallery_h.gif',
  'm_exhibitions_h.gif',
  'm_programs_h.gif',
  'm_media_h.gif',
  'm_contact_h.gif',
  'sub_current.gif',
  'sub_upcoming.gif',
  'sub_about.gif',
  'sub_howtoexhibit.gif',
  'sub_current_h.gif',
  'sub_upcoming_h.gif',
  'sub_about_h.gif',
  'sub_howtoexhibit_h.gif'
);

function endTransition() {
  m_sliding = false;
}
function startTransition() {
  m_sliding = true;
}

function changeMenu(m_index) {
  var section = this.getAttribute('name');
  this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'_h.gif');
  if ($('sub_menu_'+section)) {
    $('sub_menu_'+section).fireEvent('mouseenter');
  }
}

function clearMenu(m_index) {
  var section = this.getAttribute('name');
  var menu_index = indexInArray(menu, section);

  if (menu_fx[section]) {menu_fx[section].cancel(); }

  if (menu_index != index_currentMenu) {
    if ($('sub_menu_'+section)) {
      $('sub_menu_'+section).fireEvent('mouseleave');
    }
    this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'.gif');
  }
}

function showMenu() {
  m_sliding = true;
  var section = this.getAttribute('name');
  if (menu_fx[section]) {menu_fx[section].cancel(); }
  var width = this.getStyle('width').toInt();
  var dur = menu_slide_duration;
  if (width > 0) {
    dur = Math.ceil(menu_slide_duration * (menu_width[section]-width)/menu_width[section]);
  }
  menu_fx[section] = $('sub_menu_'+section).effect('width', {duration: dur, wait: false});
  menu_fx[section].start(width, menu_width[section]);
  if (section == this_year) {
    curr_cal = next_year;
    $('sub_menu_'+next_year).fireEvent('mouseleave');
  }
  else if (section == next_year) {
    curr_cal = this_year;
    $('sub_menu_'+this_year).fireEvent('mouseleave');
  }
}

function hideMenu() {
  var section = this.getAttribute('name');
  var menu_index = indexInArray(menu, section);
  if (menu_index != index_currentMenu || curr_cal == section) {
    if ((section == this_year || section == next_year) && curr_cal == -1) {
      $('sub_menu_'+menu[index_currentMenu]).fireEvent('mouseenter');
    }
    curr_cal = -1;
    if (menu_fx[section]) {menu_fx[section].cancel(); }
    this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'.gif');
    var width = this.getStyle('width').toInt();
    var dur = menu_slide_duration;
    if (width < menu_width[section]) {
      dur = Math.ceil(menu_slide_duration * width/menu_width[section]);
    }
    menu_fx[section] = $('sub_menu_'+section).effect('width', {duration: dur, wait: false, onComplete: endTransition, onStart: startTransition});
    menu_fx[section].start(width, 0);
  }
}

function menurollOver(el) {
  this.setAttribute('src', base_url+'img/sub_'+this.getAttribute('name')+'_h.gif');
}
function menurollOut(el) {
  if (el.target.getAttribute('pos') != index_currentSubmenu) {
    this.setAttribute('src', base_url+'img/sub_'+this.getAttribute('name')+'.gif')
  }
}

function tmenurollOver(el) {
  if (!m_sliding) {
    this.setAttribute('src', base_url+'img/tm_'+this.getAttribute('name')+'_h.gif');
  }
}
function tmenurollOut(el) {
  if (el.target.getAttribute('pos') != index_currenttmenu) {
    this.setAttribute('src', base_url+'img/tm_'+this.getAttribute('name')+'.gif')
  }
}

function etmenurollOver(el) {
  this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'_h.gif');
}
function etmenurollOut(el) {
  if (!site_largetext) {
    this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'.gif')
  }
}
function etmenuClick(el) {
  if (site_largetext) {
    site_largetext = 0;
  }
  else {
    site_largetext = 1;
  }
  new Request({method: 'get', url: base_url+'setTextSize'}).send('large='+site_largetext);
  setTextSize();
}

function setTextSize() {
  if (site_largetext) {
    if ($('content')) {
      $('content').setStyle('font-size', '140%');
    }
    if ($('calendar')) {
      $('calendar').setStyle('font-size', '140%');
      if (typeof cal != "undefined") {
        cal.resizeCalendar();
      }
    }
  }
  else {
    if ($('content')) {
      $('content').setStyle('font-size', '100%');
    }
    if ($('calendar')) {
      $('calendar').setStyle('font-size', '100%');
      if (typeof cal != "undefined") {
        cal.resizeCalendar();
      }
    }
  }
}
function transparencyOff(index) {
  this.setOpacity(1.0);
  this.addClass('sm_mouseover');
}
function transparencyOn(index) {
  if (index_currentPhoto == -1 || index != sidemenu_indexes[index_currentPhoto]) {
    this.setOpacity(0.5);
    this.removeClass('sm_mouseover');
  }
}
var footer_content;
function fixFooter() {
  // get client height
  var offset = window.getSize().y;
  var scroll = window.getScroll().y;

  footer_content.style.top = (scroll + offset - 22) + 'px';
  // add margin to the element above to remove overlapping
//  main.style.marginBottom = footer.offsetHeight + 'px';
}

window.addEvent('domready', function(){

  // place the footer
//  if (Browser.Engine.trident4) {
    footer_content = $('footer-content');
    window.addEvent('resize', fixFooter);
    window.addEvent('scroll', fixFooter);
    fixFooter();
//  }

  $('footer').setOpacity(0.6);
  if ($('sub_menu_opaque')) {
    $('sub_menu_opaque').setOpacity(0.6);
  }
  if ($('transparent_notification')) {
    $('transparent_notification').setOpacity(0.6);
  }

  menu.each(function(section) {
  });

  $$('img.m_img').each(function(el) {
    var section = el.getAttribute('name');
    var index = indexInArray(menu, section);
    if ($('sub_menu_'+section)) {
      menu_fx[section] = null;
      menu_width[section] = $('sub_menu_'+section).offsetWidth;
      if (index != index_currentMenu) {
        $('sub_menu_'+section).setStyle('width', '0px');
      }
      $('sub_menu_'+section).addEvent('mouseenter', showMenu);
      $('sub_menu_'+section).addEvent('mouseleave', hideMenu);
    }
    el.addEvent('mouseenter', changeMenu);
    el.addEvent('mouseleave', clearMenu);
  });
  $$('img.sub_img').each(function(el) {
    el.addEvent('mouseenter', menurollOver);
    el.addEvent('mouseleave', menurollOut);
  });
  $$('img.tm_img').each(function(el) {
    el.addEvent('mouseenter', tmenurollOver);
    el.addEvent('mouseleave', tmenurollOut);
  });
  $('enlargetext').addEvent('mouseenter', etmenurollOver);
  $('enlargetext').addEvent('mouseleave', etmenurollOut);
  $('enlargetext').addEvent('click', etmenuClick);
  setTextSize();
  var i = 0;
  $$('div.sm_content').each(function(el) {
    el.setOpacity(0.5);
    el.addEvent('mouseenter', transparencyOff.bind(el, i));
    el.addEvent('mouseleave', transparencyOn.bind(el, i));
    i++;
  });
  $$('div.csm_content').each(function(el) {
    el.setOpacity(0.5);
    el.addEvent('mouseenter', transparencyOff.bind(el, i));
    el.addEvent('mouseleave', transparencyOn.bind(el, i));
    i++;
  });

  i = 0;
  preload_rollovers.each(function(img) {
    preload_rollovers[i] = new Image();
    preload_rollovers[i].src = base_url+'img/'+img;
    i++;
  });

  if ($('sub_menu') && ((typeof index_currenttmenu != 'string' && index_currenttmenu == 0) || typeof index_currenttmenu == 'string')) {
    sm_height = $('sub_menu').getSize().y;
    $('sub_menu').setStyle('height', '0px');
    $('sub_menu_opaque').setStyle('height', '0px');
    $('sub_menu').set('tween', {duration:1000});
    $('sub_menu_opaque').set('tween', {duration:750});
    $('sub_menu_opaque').tween('height', sm_height);
    $('sub_menu').tween('height', sm_height);
  }

  if (Browser.Engine.trident && Browser.Engine.version < 5) {
    t_background = $('background');
    if (t_background) {
      t_background.setStyle('height', window.getScrollSize().y+'px');
    }
  }

});

function indexInArray(theArray, theValue) {
  var arLength = theArray.length;
  for(var i=0; i<arLength; i++) {
    if (theArray[i] == theValue){
    return i;
    }
  }
  return -1;
}
