var index_currentPhoto = -1;
var index_currentMenu = -1;
var menu = new Array (
  'about',
  'apply',
  'gallery',
  'winners',
  'sponsors',
  'archive'
);

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',
  'ts/m_about_h.gif',
  'ts/m_about.gif',
  'ts/m_apply_h.gif',
  'ts/m_apply.gif',
  'ts/m_winners.gif',
  'ts/m_winners_h.gif',
  'ts/m_gallery.gif',
  'ts/m_gallery_h.gif',
  'ts/m_sponsors.gif',
  'ts/m_sponsors_h.gif',
  'ts/m_archive.gif',
  'ts/m_archive_h.gif'
);

function changeMenu(m_index) {
  this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'_h.gif');
}

function clearMenu(m_index) {
  if (this.getAttribute('name') != 'programs') {
    this.setAttribute('src', base_url+'img/m_'+this.getAttribute('name')+'.gif')
  }
}

function changeMenuBooks(m_index) {
  this.setAttribute('src', base_url+'img/ts/m_'+this.getAttribute('name')+'_h.gif');
}

function clearMenuBooks(m_index) {
  var menu_index = indexInArray(menu, this.getAttribute('name'));
  if (menu_index != index_currentMenu) {
    this.setAttribute('src', base_url+'img/ts/m_'+this.getAttribute('name')+'.gif')
  }
}

function transparencyOff(index) {
  this.setOpacity(1.0);
  this.addClass('sm_mouseover');
}
function transparencyOn(index) {
  if (index != index_currentPhoto) {
    this.setOpacity(0.5);
    this.removeClass('sm_mouseover');
  }
}

window.addEvent('domready', function(){
  $('footer').setOpacity(0.8);
  if (index_currentMenu >= 0 && $('sub_menu_'+menu[index_currentMenu])) {
    $('sub_menu_'+menu[index_currentMenu]).setStyle('display', 'block');
  }
  $$('#main_menu img.m_img').each(function(el) {
    el.addEvent('mouseenter', changeMenu);
    el.addEvent('mouseleave', clearMenu);
  });
  $$('#ts_menu img.m_img').each(function(el) {
    el.addEvent('mouseenter', changeMenuBooks);
    el.addEvent('mouseleave', clearMenuBooks);
  });
  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));
    if (index_currentPhoto < 0) {
      deadline = Date.parse($E('.sm_content_title', el).innerHTML.split(',')[0]);
      if (deadline > Date.parse(new Date())) {
        index_currentPhoto = i;
        el.fireEvent('mouseenter');
      }
    }
    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++;
  });
});

function indexInArray(theArray, theValue) {
  var arLength = theArray.length;
  for(var i=0; i<arLength; i++) {
    if (theArray[i] == theValue){
    return i;
    }
  }
  return -1;
}

function jsEmail(name, domain) {
  window.location = 'mailto:'+name+'@'+domain;
}
