var locked = false;
var slideshowImages = [];

$(document).ready(function(){
  $('.info').addClass('hidden');
  $('#col1_content p a').hide();

  if(!$.historyInit(pageload, "jquery_history.html")) {
    try {
      $('ul.services li a')[0].onclick.call();
    } catch(e) {}
  }
});

function pageload(hash) {
  if(hash) {
    if($.browser.msie) {
      hash = encodeURIComponent(hash);
    }

    try{
    $('#item'+hash)[0].onclick.call();
    } catch(e){}
  }
}

function toggleActive(id, image) {
  if(locked) { return; }
  lock();

  $('.info').addClass('hidden', 1000);
  setTimeout("$('#info"+id+"').removeClass('hidden', 1000);", 1000);
  $('.services li a').removeClass('active');
  $('#item'+id).addClass('active');
  $('#col1_content').fadeTo(1000, 0.0001);

  image = '/media/'+image;
  var afterFade = "$('#col1_content').css({'background-image' : 'url("+image+")' });$('#col1_content p a').show();$('#col1_content').fadeTo(2000, 1);unlock();";

  wImage = new Image ;
  wImage.src = image;
  setTimeout("waitForImage(\""+afterFade+"\")", 1500);
}

// function toggleActiveAfterScroll(a) {
//   a[0].childNodes[0].onclick.call();
// }

function slideshow(index) {
  while(locked) { }
  lock();

  $('#col1_content').fadeTo(1000, 0.0001);

  var next = index+1;
  if(next >= slideshowImages.length) next = 0;

  image = '/media/'+slideshowImages[index];
  var afterFade = "$('#col1_content').css({'background-image' : 'url("+image+")' });$('#col1_content p a').show();$('#col1_content').fadeTo(2000, 1);unlock();setTimeout('slideshow("+next+")', 3000);";

  wImage = new Image ;
  wImage.src = image;
  setTimeout("waitForImage(\""+afterFade+"\")", 1500);
}

function lock() {
  locked = true;
  $('#loading').fadeTo(1000, 1);
}

function unlock() {
  locked = false;
  $('#loading').fadeTo(1000, 0);
}

var wImage = null;
function waitForImage(code) {
  if(wImage != null) {
    if(wImage.complete) {
      eval(code);
      wImage = null;
    } else {
      setTimeout("waitForImage(\""+code+"\")", 500);
    }
  }
}
