var full = {}, numFull = 0, preload = [], loading, currentHash = location.hash;

function css(el,rule,value) {
  getEl(el).style[rule] = value;
}
function attr(el,name,value) {
  el = getEl(el);
  if (value)
    el.setAttribute(name, value);
  return el.getAttribute(name);
}
function html(el,htmlText) {
  el = getEl(el);
  if (htmlText)
    el.innerHTML = htmlText;
  return el.innerHTML;
}
function getEl(el) {
  if (el.nodeType)
    return el;
  return document.getElementById(el);
}
function show(el,inline) {
  css(el, 'display', inline ? 'inline' : 'block' );
}
function hide(el) {
  css(el, 'display', 'none' );
}

function loadFull(item,hash) {
  var loaded, newClass, photo = getEl('portfolio-photo');
  switch(item.type) {
    case 'photo':
      html(photo,'<img src="'+item.src+'"/>');
      break;
      
    case 'flash':
      html(photo,'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"\
      width=336" height="280" id="" align="" bgcolor="#000000"><param name=movie value="'+item.src+'"><param name=quality value=high> \
      <embed src="'+item.src+'" quality=high bgcolor="#000000" width="336" height="280" \
      name="" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">\
      </embed>\
      </object>');
      break;
  }
  html('portfolio-photo-caption',html('caption'+hash));
  show('photo-panel');
  
}

function pageload(hash) {
  if (hash && (hash = hash.substring(1))) {
    var pos = full[hash].pos;
    loadFull(full[hash],hash);
    hide('portfolio-panel');
    html('subheader-count', (pos<10?'0':'')+pos+'/'+(numFull<10?'0':'')+numFull);
    show('subheader-count',true);
    attr('subheader-prev','href','#'+full[hash].prev);
    attr('subheader-next','href','#'+full[hash].next);
    show('subheader-controls',true);
  } else {
    hide('subheader-count');
    hide('subheader-controls');
    hide('photo-panel');
    show('portfolio-panel');
  }
}

function preloadPhoto() {
  if (preload.length) {
    loading = new Image();
    loading.onload = preloadPhoto;
    loading.src = preload.shift();
  }
}

var readyFired = false;
function onReady() {
  
  if (readyFired)
    return;
  
  readyFired = true;
    
  var lastHash = '', hash = '', thumbs = getEl('portfolio-thumbs').getElementsByTagName('A'), 
    thumbCount = thumbs.length, i, a;
  
  for (i = 0; i < thumbCount; i++) {
    a = thumbs[i];
    if (a.className == 'thumb') {
      numFull++;
      hash = a.name.substring(2);
      full[hash] = {
        'src':a.href,
        'pos': i+1,
        'type':a.rel,
        'prev':lastHash,
        'next':''
      };
      if (lastHash) {
        full[lastHash].next = hash;
      }
      if (a.rel == 'photo') {
        preload[preload.length] = a.href;
      }
      lastHash = hash;
      a.href = '#'+hash;
    } 
  }
  
      
  if (currentHash)
    pageload(currentHash);
  
  setInterval(function() {
    if (location.hash != currentHash) {
      pageload(currentHash = location.hash);
    }
  }, 200);

}

if ( document.readyState === "complete" ) {
  onReady();
  preloadPhoto();
} else if (document.addEventListener) {
  document.addEventListener('DOMContentLoaded', onReady, false);
  document.addEventListener('load', onReady, false);
  document.addEventListener('load', preloadPhoto, false);
} else if (document.attachEvent) {
  document.attachEvent('onreadystatechange', onReady);
  document.attachEvent('onload', onReady);
  document.attachEvent('onload', preloadPhoto);
}