var locked = false;
var defaultLabel = 'Select a project below to see it in full size.';
var currentLabel = defaultLabel;
var currentCarousel = null;
var currentCarouselInterval = null; 
var slideshowImages = [];

var wImage = null;

$(window).resize(function(){
  checkAlignment();
});

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

  $.historyInit(pageload, "jquery_history.html");
});

function checkAlignment() {
  var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
  if(viewportHeight < 504) {
    $('body').removeClass('valign');
  } else {
    $('body').addClass('valign');
  }
}

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

function toggleActive(id) {
  if(locked) { return; }
  lock();
  if(currentCarousel != null) {
    clearInterval(currentCarouselInterval);
    currentCarousel = null;
  }
  currentCarousel = null;
  $('.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);
  $('#thumbcontainer').fadeTo(1000, 0.0001);
  $('#imglabelbg').fadeTo(1000, 0.0001);
  var image = $("#"+id+" ul a")[0];
  if(image != undefined) {
    image = image.href;
  } else {
    image = '/media/demo.jpg';
  }
  var afterFade = "$('#videocontainer').html('');$('#videocontainer').hide();currentLabel=defaultLabel;\
    setLabel(currentLabel);\
    $('#thumbcontainer').html($('#"+id+"').html());\
    $('#col1_content').css({'background-image' : 'url("+image+")' });\
    $('#col1_content p a').show();\
    $('#col1_content').fadeTo(2000, 1);\
    $('#thumbcontainer').fadeTo(2000, 1);\
    $('#imglabelbg').fadeTo(2000, .4);\
    currentCarousel = $('#col1 .carousel').jCarouselLite({btnNext: '#col1 .tnext', btnPrev: '#col1 .tprev', visible: 4, auto: 5000, speed: 500, afterEnd:toggleActiveAfterScroll});\
    setTimeout(\"unlock();\", 1000);";
  setTimeout(afterFade, 1500);
}

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

function switchPicture(url, a, moveon) {
  if(locked) { return; }  
  lock();
  currentLabel = a.title;
  $("#col1 a").removeClass('current');
  $(a).addClass('current');
  setLabel(currentLabel);
  $('#col1_content').fadeTo(1000, 0.001);
  var afterLoad = "$('#col1').css({'background-color': 'white' });$('#videocontainer').html('');$('#videocontainer').hide();\
    $('#col1_content').css({'background-image' : 'url("+url+")', 'background-color': 'white' });\
    $('#col1_content').fadeTo(2000, 1);unlock();";
  wImage = new Image ;
  wImage.src = url;
  setTimeout("waitForImage(\""+afterLoad+"\")", 1500);
}

function displayVideo(url, a, iframe) {
  if(locked) { return; }
  clearInterval(currentCarouselInterval); // No autoload with videos
  lock();
  currentLabel = a.title;
  $("#col1 a").removeClass('current');
  $(a).addClass('current');
  setLabel(currentLabel);
  $('#col1_content').fadeTo(1000, 0.001);
  if(iframe) {
	setTimeout("$('#col1').css({'background-color': '#999999' });$('#col1_content').css({'background-image' : 'none', 'background-color': '#999999' });\
		$('#videocontainer').show();\
		$('#videocontainer').html('<iframe src=\""+url+"\" width=\"600\" height=\"310\" frameborder=\"0\"></iframe>');\
		$('#col1_content').fadeTo(2000, 1);\
		unlock();", 1500
	);
  } else {
	setTimeout("$('#col1').css({'background-color': '#999999' });$('#col1_content').css({'background-image' : 'none', 'background-color': '#999999' });\
		$('#videocontainer').show();\
		$('#videocontainer').load('"+url+"');\
		$('#col1_content').fadeTo(2000, 1);\
		unlock();", 1500
	);
  }
}

function setLabel(label) {
  $('#imglabel').html(label);
}

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

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

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

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 showSingleImage(imagesrc) {
  if(locked) return;
  //lock();

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

  image = '/media/'+imagesrc;
  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 stopCarousel() {
  clearInterval(currentCarouselInterval);
//   currentCarousel = null;
}
