var NUM_IMGS = 15;
var FADE_DUR = .5;
var SLIDE_DUR = .5;
var ROTATION_WAIT = 3.3;
var VISIBLE_ID_PREFIX = 'visible_a';

var imgEvents = {
  sImgClicked: false,
  nextLoaded: false,
  prefetchLoaded: false,
  nextAId: 0,
  currImgId: null,
  nextImgId: null,
  fadeEvent: null,
  rotating: false,
  rotatingId: -1
};

var textEvents = {
  currTextItemId: null,
  currTextItemBodyItemId: {}
};

function s() {
  var textItems = document.getElementsByClassName('textItem');
  for (var i = 0; i < textItems.length; i++) {
    textEvents.currTextItemBodyItemId[textItems[i].id] =
        textItems[i].getElementsByClassName('textItem_body_item')[0].id;
    fadeOut(textItems[i].id, 0);
  }
  fadeOut('text', 0);
  prefetchImg('IMG01');
  startRotation();
}

function startRotation(shouldWait) {
  if (imgEvents.rotating) {
    return false;
  }
  imgEvents.rotating = true;
  imgEvents.rotatingId++;
  if (shouldWait) {
    setTimeout('rotate(' + imgEvents.rotatingId + ');', ROTATION_WAIT * 250);

  } else {
    rotate(imgEvents.rotatingId);
  }
  return true;
}

function stopRotation() {
  if (!imgEvents.rotating) {
    return false;
  }
  imgEvents.rotating = false;
  return true;
}

function rotate(id) {
  if (!imgEvents.rotating || imgEvents.rotatingId != id) {
    return;
  }
  sImg();
  setTimeout('rotate(' + id + ');', ROTATION_WAIT * 1000);
}

function sTitle() {
  var shouldWait = false;
  if ($('text').className != 'bodyItem i') {
    sText();
    shouldWait = true;

  } else if ($('photos_nav').className == 'active') {
    sPhotos();
    shouldWait = true;
  }
  if (!startRotation(shouldWait)) {
    stopRotation();
  }
  return false;
}

function sText(a) {
  if (!textEvents.currTextItemId) {
    if ($('photos_nav').className == 'active') {
      setTimeout('fadeIn("text");', SLIDE_DUR * 500);
    } else {
      fadeIn('text');
    }
    stopRotation();
  }
  if (a) {
    a.blur();
    if ($('liSelected') != a.parentNode) {
      if ($('liSelected')) {
        $('liSelected').id = '';
        if (textEvents.currTextItemId) {
          $(textEvents.currTextItemId).className = 'textItem';
          fadeOut(textEvents.currTextItemId);
        }
      }
      if ($('photos_nav').className == 'active') {
        sPhotos();
      }
      a.parentNode.id = 'liSelected';
      var id = a.href.substring(a.href.indexOf('#') + 1);
      $(id).className = 'textItem active';
      textEvents.currTextItemId = id;
      $('text').className = 'bodyItem';
      fadeIn(id);
    }
  } else {
    if (textEvents.currTextItemId) {
      $(textEvents.currTextItemId).className = 'textItem';
      fadeOut(textEvents.currTextItemId);
      textEvents.currTextItemId = null;
    }
    fadeOut('text');
    setTimeout('$("text").className = "bodyItem i";', FADE_DUR * 1000);
    if ($('liSelected')) {
      $('liSelected').id = '';
    }
  }
  return false;
}

function sLi(a) {
  if (a) {
    a.blur();
  }
  if (a.parentNode.className != 'selected') {
    a.parentNode.className = 'selected';
    var lis = a.parentNode.parentNode.getElementsByTagName('li');
    var selectedFound = false;
    for (var i = 0; i < lis.length; i++) {
      if (lis[i] != a.parentNode) {
        lis[i].className = 'li' + i;
      
      } else {
        selectedFound = true;
      }
    }
  }
  var id = a.href.substring(a.href.indexOf('#') + 1);
  if (textEvents.currTextItemBodyItemId[textEvents.currTextItemId]) {
    $(textEvents.currTextItemBodyItemId[textEvents.currTextItemId]).className = 'textItem_body_item i';
  }
  $(id).className = 'textItem_body_item';
  textEvents.currTextItemBodyItemId[textEvents.currTextItemId] = id;
  return false;
}

function sPhotos(a) {
  var thisY = 0;
  if (a) {
    a.blur();
    sText();
    if ($('liSelected')) {
      $('liSelected').id = '';
    }
    a.parentNode.id = 'liSelected';
    if ($('photos_nav').className == 'inactive') {
      thisY = 1;
      $('photos_nav').className = 'active';
    }
  } else if ($('photos_nav').className == 'active') {
    thisY = -1;
    $('photos_nav').className = 'inactive';
    if ($('liSelected')) {
      $('liSelected').id = '';
    }
  }
  if (thisY != 0) {
    new Effect.Parallel([
      new Effect.Move('head', {
          duration: SLIDE_DUR,
          x: 0,
          y: (thisY * 54)}),
      new Effect.Move('photos_nav', {
          duration: SLIDE_DUR,
          x: 0,
          y: (thisY * 54)})], {duration: SLIDE_DUR});
    stopRotation();
  }
  return false;
}

function sImg(a) {
  if (a) {
    a.blur();
    var id = gId(a);
    if (id && id != imgEvents.currImgId) {
      prefetchImg(id);
      imgEvents.nextLoaded = false;
      imgEvents.nextImgId = id;
    }
  }
  if (imgEvents.sImgClicked) {
    return false;
  }
  if (!imgEvents.nextLoaded || imgEvents.fadeEvent) {
    imgEvents.sImgClicked = true;
    return false;
  }
  imgEvents.fadeEvent = crossFade(imgEvents.currImgId, imgEvents.nextImgId);
  setTimeout('prepareNext();', FADE_DUR * 1000 + 10);
  return false;
}

function gId(a) {
  if (a.className == 'photos_nav_a') {
    var tId = a.href;
    return tId.substring(tId.lastIndexOf('/') + 1, tId.lastIndexOf('.'));

  } else {
    return a.firstChild && a.firstChild.id;
  }
}

function crossFade(visible, invisible) {
  if (visible) {
    return new Effect.Parallel([
        fadeOut(visible),
        fadeIn(invisible)
      ], {duration: FADE_DUR});
  } else {
    return new fadeIn(invisible);
  }
}

function fadeOut(id, dur) {
  return new Effect.Opacity(id, {
      duration: (dur ? dur : FADE_DUR),
      from: 1.0,
      to: 0.0});
}

function fadeIn(id) {
  return new Effect.Opacity(id, {
      duration: FADE_DUR,
      from: 0.0,
      to: 1.0});
}

function prepareNext() {
  var newA = $(VISIBLE_ID_PREFIX + imgEvents.nextAId);
  imgEvents.nextAId = (imgEvents.nextAId + 1) % 2;
  var oldA = $(VISIBLE_ID_PREFIX + imgEvents.nextAId);
  oldA.innerHTML = '';
  oldA.className = 'photosItem_a next';
  imgEvents.currImgId = newA.firstChild.id;
  newA.className = 'photosItem_a current';

  imgEvents.fadeEvent = null;
  imgEvents.nextLoaded = false;
  if (imgEvents.prefetchLoaded) {
    var img = movePrefetchToNext();
    prefetchImg(gNextId(img.id));
  }
}

function movePrefetchToNext() {
  if (imgEvents.nextLoaded || !imgEvents.prefetchLoaded) {
    return;
  }
  var pImg = $('prefetch_a').firstChild;
  $('prefetch_a').removeChild(pImg);
  $(VISIBLE_ID_PREFIX + imgEvents.nextAId).innerHTML = '';
  $(VISIBLE_ID_PREFIX + imgEvents.nextAId).appendChild(pImg);
  imgEvents.nextLoaded = true;
  imgEvents.prefetchLoaded = false;
  imgEvents.nextImgId = pImg.id;
  return pImg;
}

function prefetchImg(id) {
  $('prefetch_a').innerHTML = '<img src="img/md/' + id + '.jpg" width="830px" height="545px" id="' + id + '" onload="regPrefetch(\'' + id + '\');" style="opacity:0;filter:alpha(opacity=0);-moz-opacity:0.0;"/>';
}

function gNextId(currId) {
  var currIdNum = parseInt(currId.substring(3), 10);
  var nextIdNum = currIdNum + 1 > NUM_IMGS ? 1 : currIdNum + 1;
  return 'IMG' + (nextIdNum < 10 ? '0' : '') + nextIdNum;
}

function regPrefetch(id) {
  if (!imgEvents.prefetchLoaded) {
    imgEvents.prefetchLoaded = true;
  }
  if (!imgEvents.nextLoaded) {
    var nextImg = movePrefetchToNext();
    imgEvents.nextImgId = nextImg.id;
    prefetchImg(gNextId(nextImg.id));
  }
  if (imgEvents.sImgClicked) {
    imgEvents.sImgClicked = false;
    sImg();
  }
}

function debug(s) {
  $('debug').innerHTML += s + '<br/>';
}