// JavaScript for LOT 71

if ($(document).ready) {
  $(document).ready(initTabs);  
}

var currentTab = 1;

function initTabs(){
	if(document.getElementById('tabs')){
		var count = 1;
		$('#tabs li').each(function(){
			this.num = count;
			if($(this).children('a').length){
				$(this).children('a')[0].onclick = changeTabs;
			}
			count++;
		});	
	}
}

function changeTabs(){
	currentTab = this.parentNode.num;
	var count = 1;
	$('#tabs li').each(function(){
		if($(this).children('a').length){
			if(count == currentTab){
				$(this).html('');
				$('#'+$(this).attr('id')+'-content').removeClass('hidden');
			}
		}
		else{
			$(this).html('<a href="#" title="'+$(this).attr('title')+'"></a>');
			$(this).children('a')[0].onclick = changeTabs;
			$('#'+$(this).attr('id')+'-content').addClass('hidden');
		}
		count++;						
	});
	return false;
}

if ($(document).ready) {
  $(document).ready(initWorks);
}

var currentWork = null;

function initWorks(){
	if(document.getElementById('work')){
		var count = 1;
		$('#work li').each(function(){
			this.num = count;
			if($(this).children('a').length){
				$(this).children('a')[0].onclick = changeWork;
				if ($(this).children('a').attr('initial') == '1') {
      		$(this).children('a').addClass('active');
      		$(this).children('ul').show();		  
				}
			}
			count++;
		});
	}
}

function changeWork(){
	if(currentWork == this.parentNode.num){
		currentWork = null;
		$(this).removeClass('active');
		$(this).parent().children('ul').hide();
	}
	else{
		currentWork = this.parentNode.num;
		var count = 1;
		$('#work li').each(function(){
			if($(this).children('a').length){
				if(count == currentWork){
					$(this).children('a').addClass('active');
					$(this).children('ul').show();
				}
				else{
					$(this).children('a').removeClass('active');
					$(this).children('ul').hide();
				}
			}
			count++;
		});	
	}
	return false;
}

if ($(document).ready) {
  $(document).ready(initThumbs);
}
var currentThumb = 1;
var totalThumbs = null;

function initThumbs(){
	if(document.getElementById('thumbs')){
		var count = 1;
		$('#thumbs li a').each(function(){
			if(currentThumb == count){
				$(this).addClass('active');
			}
			$(this).attr('id', 'thumb'+count);
			$(this).attr('num', count);
			$(this).click(changeThumb);
			count++;
		});
		totalThumbs = count-1;
		
		if(document.getElementById('prev')){
			$('#prev').click(changeThumb);
		}
		if(document.getElementById('next')){
			$('#next').click(changeThumb);
		}
		if(document.getElementById('project-image')){
			$('#project-image').click(changeThumb);	
		}
	}
}

function changeThumb(){
	if(this.id == 'prev'){
		if(currentThumb - 1 < 1){
			currentThumb = totalThumbs;
		} else {
			currentThumb--;
		}
	}
	else if(this.id == 'next' || this.id == 'project-image'){
		if(currentThumb + 1 > totalThumbs){
			currentThumb = 1;
		} else {
			currentThumb++;
		}
	}
	else{
		currentThumb = Number($(this).attr('num'));
	}

	$('#thumb'+currentThumb).addClass('active');	
	$('#project-image').attr('src', projectImages[currentThumb - 1]);
	$('#project-caption').html(projectDescriptions[currentThumb - 1]);

	if (projectVideos[currentThumb - 1] != null && projectVideos[currentThumb - 1].length > 0) {
  	$('#project-image').hide();
	  insertVideoPlayer("video-player", projectVideos[currentThumb - 1], projectImages[currentThumb - 1], '635', '425');
	} else {
  	$('#project-image').show();
  	$('#video-player').html('');
	}

	$('#thumbs li a').each(function(){
		if($(this).attr('num') != currentThumb){
			$(this).removeClass('active');
		}
	});
		
	return false;
}
if ($(document).ready) {
  $(document).ready(function(){
  	if(document.getElementById('send')){
  		$('#send').click(function() { document.forms[0].submit(); });	
  	}
  });
}

function insertVideoPlayer(element, video_path, image_path, width, height) {
  	var s1 = new SWFObject('/swf/flvplayer.swf', "single", width, height, "7");
  	s1.addParam("allowfullscreen","false");
  	s1.addVariable("file", video_path);
  	s1.addVariable("image", image_path);
  	s1.addVariable("width", width);
  	s1.addVariable("height", height);
  	s1.addVariable("displayheight", height);
  	s1.write(element);  
}

function preloadImages(a) {
  var tempImages = new Array();
  for (i = 0; i < a.length; i++) {
    img = new Image();
    img.src = a[i];
    tempImages[tempImages.length] = img;
  }
}