var tallest_col = 0;
var t;

$(document).ready(function(){

$('.column').each(function(index){
	var col_height = parseInt($(this).height());
	tallest_col = (col_height > tallest_col) ? col_height : tallest_col ;
});

$('.column').css("height", tallest_col);

// work column

$("#work_col a.expand").live("click", function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#work_col").topZIndex();
	$(this).fadeOut();
	$('#work_col .white_bg').fadeIn();
	$('#work_col').animate({width: '765px'});

});

$("#work_col a.collapse, #work_col a.expand_tweets_link").live("click", function(){

	t=setTimeout("work_slideshow()",3000);

	$("#work_enlarged").fadeOut();
	$("#work_items").fadeIn(function(){

		$('#work_col').animate({width: '180px'}, function(){
			$("#work_col a.expand").fadeIn("fast");
			$('#work_col .white_bg').fadeOut("fast");
		});
	
	});

});

$("#work_col .expand_type_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#type_col").topZIndex();
	$('#type_col .white_bg').fadeIn();
	$('#type_col').css("width", "0");
	$("#type_col a.expand").hide();
	$('#type_col').animate({width: '765px'}, function(){
		$("#work_col").css("width", "180px");
		$('#work_col .white_bg').hide();
		$("#work_col a.expand").show();
		$("#work_enlarged").hide();
		$("#work_items").show();
	});

});

$("#work_col .expand_art_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#art_col").topZIndex();
	$("#work_col").topZIndex();
	$('#art_col .white_bg').show();
	$('#art_col').css("width", "765px");
	$('#art_col').css("right", "173px");
	$('#art_col_inner').css("right", "0");
	$("#art_col a.expand").hide();
	$('#work_col').animate({width: '0'}, function(){
		$("#art_col").topZIndex();
		$("#work_col").css("width", "180px");
		$('#work_col .white_bg').hide();
		$("#work_col a.expand").show();
		$("#work_enlarged").hide();
		$("#work_items").show();
	});

});

// type column

$("#type_col a.expand").live("click", function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#type_col").topZIndex();
	$(this).fadeOut();
	$('#type_col .white_bg').fadeIn();
	$('#type_col').animate({width: '765px'});

});

$("#type_col a.collapse, #type_col a.expand_tweets_link").live("click", function(){

	t=setTimeout("work_slideshow()",3000);
	
	$("#type_enlarged").fadeOut();
	$("#type_items").fadeIn(function(){

		$('#type_col').animate({width: '180px'}, function(){
			$('#type_col .white_bg').fadeOut("fast");
			$("#type_col a.expand").fadeIn("fast");
		});
		
	});

});

$("#type_col .expand_work_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#work_col").topZIndex();
	$('#work_col .white_bg').show();
	$('#work_col').css("width", "0");
	$("#work_col a.expand").hide();
	$('#work_col').animate({width: '765px'}, function(){
		$("#type_col").css("width", "180px");
		$('#type_col .white_bg').hide();
		$("#type_col a.expand").show();
		$("#type_enlarged").hide();
		$("#type_items").show();
	});

});

$("#type_col .expand_art_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#art_col").topZIndex();
	$("#type_col").topZIndex();
	$('#art_col .white_bg').show();
	$('#art_col').css("width", "765px");
	$('#art_col').css("right", "173px");
	$('#art_col_inner').css("right", "0");
	$("#art_col a.expand").hide();
	$('#type_col').animate({width: '0'}, function(){
		$("#art_col").topZIndex();
		$("#type_col").css("width", "180px");
		$('#type_col .white_bg').hide();
		$("#type_col a.expand").show();
		$("#type_enlarged").hide();
		$("#type_items").show();
	});

});

// art column

$("#art_col a.expand").live("click", function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);
	
	$("#art_col").topZIndex();
	$(this).fadeOut();
	$('#art_col .white_bg').fadeIn();
	$("#art_col").animate({right: '173px', width: '765px'});
	$("#art_col_inner").animate({right: '0'});

});

$("#art_col a.collapse, #art_col a.expand_tweets_link").live("click", function(){

	t=setTimeout("work_slideshow()",3000);
	
	$("#art_enlarged").fadeOut();
	$("#art_items").fadeIn(function(){

		$('#art_col').animate({right: '354px', width: '90px'}, function(){
			$("#art_col a.expand").fadeIn("fast");
			$('#art_col .white_bg').fadeOut("fast");
		});
		$("#art_col_inner").animate({right: '-15px'});
	
	});

});

$("#art_col .expand_work_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);
	
	$("#work_col").topZIndex();
	$('#work_col .white_bg').show();
	$('#work_col').css("width", "0");
	$("#work_col a.expand").hide();
	$('#work_col').animate({width: '765px'}, function(){
		$("#art_col").css("width", "90px");
		$("#art_col").css("right", "354px");
		$("#art_col_inner").css("right", "-15px");
		$('#art_col .white_bg').hide();
		$("#art_col a.expand").show();
		$("#art_enlarged").hide();
		$("#art_items").show();
	});

});

$("#art_col .expand_type_link").click(function(){

	$("#work_items a img").stop(true, true);

	clearTimeout(t);

	$("#type_col").topZIndex();
	$('#type_col .white_bg').show();
	$('#type_col').css("width", "0");
	$("#type_col a.expand").hide();
	$('#type_col').animate({width: '765px'}, function(){
		$("#art_col").css("width", "90px");
		$("#art_col").css("right", "354px");
		$("#art_col_inner").css("right", "-15px");
		$('#art_col .white_bg').hide();
		$("#art_col a.expand").show();
		$("#art_enlarged").hide();
		$("#art_items").show();
	});

});

// image enlarge

$("#work_items a").live("click", function(e){

e.preventDefault();

var rel = $(this).attr("rel");
var image = $(this).attr("href");
var long_desc = $("span.long_desc", this).html();

$("#work_items a img").stop(true, true);

clearTimeout(t);

if($("#work_col").css("width") == '765px'){

	$(function(){
		var img = new Image();
		$(img).load(function(){
		$(this).css('display', 'none');
		
		$("#work_enlarged h3 span span").html(rel);
		$("#work_enlarged p").html(long_desc);
		$("#work_enlarged img.enlarged_img").attr("src", image);
		$("#work_enlarged img.enlarged_img").attr("rel", "0");
		$("#work_enlarged").fadeIn();
		$("#work_items").fadeOut();
			
		}).attr('src', image);
	});
	
}

else{

	$("#work_col").topZIndex();
	$("#work_col .expand").fadeOut();
	$('#work_col .white_bg').fadeIn();
	$('#work_col').animate({width: '765px'}, function(){
	
	$(function(){
		var img = new Image();
		$(img).load(function(){
		$(this).css('display', 'none');
		
		$("#work_enlarged h3 span span").html(rel);
		$("#work_enlarged p").html(long_desc);
		$("#work_enlarged img.enlarged_img").attr("src", image);
		$("#work_enlarged img.enlarged_img").attr("rel", "0");
		$("#work_enlarged").fadeIn();
		$("#work_items").fadeOut();
			
		}).attr('src', image);
	});
	
	});

}
	
});

$("#art_items a").live("click", function(e){

e.preventDefault();

var rel = $(this).attr("rel");
var image = $(this).attr("href");

$("#work_items a img").stop(true, true);

clearTimeout(t);

if($("#art_col").css("width") == '765px'){

	$(function(){
		var img = new Image();
		$(img).load(function(){
		$(this).css('display', 'none');
			
		$("#art_enlarged h3 span span").html(rel);
		$("#art_enlarged img.enlarged_img").attr("src", image);
		$("#art_enlarged").fadeIn();
		$("#art_items").fadeOut();
			
		}).attr('src', image);
	});

}

else{

	$("#art_col").topZIndex();
	$("#art_col .expand").fadeOut();
	$('#art_col .white_bg').fadeIn();
	$("#art_col").animate({right: '173px', width: '765px'});
	$("#art_col_inner").animate({right: '0'}, function(){
	
	$(function(){
		var img = new Image();
		$(img).load(function(){
		$(this).css('display', 'none');
			
		$("#art_enlarged h3 span span").html(rel);
		$("#art_enlarged img.enlarged_img").attr("src", image);
		$("#art_enlarged").fadeIn();
		$("#art_items").fadeOut();
			
		}).attr('src', image);
	});
	
	});

}
	
});

$("#type_items a").live("click", function(e){

e.preventDefault();

var rel = $(this).attr("rel");
var image = $(this).attr("href");

$("#work_items a img").stop(true, true);

clearTimeout(t);

if($("#type_col").css("width") == '765px'){

// load image

$(function(){
    var img = new Image();
    $(img).load(function(){
    $(this).css('display', 'none');
		
	$("#type_enlarged h3 span span").html(rel);
	$("#type_enlarged img.enlarged_img").attr("src", image);
	$("#type_enlarged").fadeIn();
	$("#type_items").fadeOut();
		
    }).attr('src', image);
});

}

else{

	$("#type_col").topZIndex();
	$("#type_col .expand").fadeOut();
	$('#type_col .white_bg').fadeIn();
	$('#type_col').animate({width: '765px'}, function(){
	
	$(function(){
		var img = new Image();
		$(img).load(function(){
		$(this).css('display', 'none');
			
		$("#type_enlarged h3 span span").html(rel);
		$("#type_enlarged img.enlarged_img").attr("src", image);
		$("#type_enlarged").fadeIn();
		$("#type_items").fadeOut();
			
		}).attr('src', image);
	});
	
	});

}
	
});

// close button

$("#work_enlarged h3 a").click(function(){
	$("#work_enlarged").fadeOut();
	$("#work_items").fadeIn();
});

$("#art_enlarged h3 a").click(function(){
	$("#art_enlarged").fadeOut();
	$("#art_items").fadeIn();
});

$("#type_enlarged h3 a").click(function(){
	$("#type_enlarged").fadeOut();
	$("#type_items").fadeIn();
});

// image nav

$(".previous_image").click(function(){

var parent = $(this).parent("div");
var grand_parent = $(this).parent("div").parent("div");

if(!$("img.enlarged_img", parent).is(':animated')){

$("h3 span span", parent).html("Loading...");

// get previous image index
var src_for_search = $("img.enlarged_img", parent).attr("src");
var img_index = $("a[href='"+src_for_search+"']", grand_parent).index();
var new_img_index = ((img_index - 1) < 0) ? $("div a.item img", grand_parent).length - 1 : img_index - 1;

// get attributes

var href = $("div a.item:eq("+new_img_index+")", grand_parent).attr("href");
var rel = $("div a.item:eq("+new_img_index+")", grand_parent).attr("rel");

// load image

$(function(){
    var img = new Image();
    $(img).load(function(){
    $(this).css('display', 'none');
		
	// set attributes

	$(parent).css("background-image", "url("+href+")");
	$("h3 span span", parent).html(rel);

	// fade out
	$("img.enlarged_img", parent).animate({opacity: '0'}, function(){

		$("img.enlarged_img", parent).attr("src", href).css({opacity: '1'});

	});
		
    }).attr('src', href);
});

}

});

$(".next_image").click(function(){

var parent = $(this).parent("div");
var grand_parent = $(this).parent("div").parent("div");

if(!$("img.enlarged_img", parent).is(':animated')){

$("h3 span span", parent).html("Loading...");

// get previous image index
var src_for_search = $("img.enlarged_img", parent).attr("src");
var img_index = $("a[href='"+src_for_search+"']", grand_parent).index();
var new_img_index = ((img_index + 1) > ($("div a.item img", grand_parent).length - 1)) ? 0 : img_index + 1;

// get attributes

var href = $("div a.item:eq("+new_img_index+")", grand_parent).attr("href");
var rel = $("div a.item:eq("+new_img_index+")", grand_parent).attr("rel");

// load image

$(function(){
    var img = new Image();
    $(img).load(function(){
        $(this).css('display', 'none');
		
// set attributes

$(parent).css("background-image", "url("+href+")");
$("h3 span span", parent).html(rel);

// fade out
$("img.enlarged_img", parent).animate({opacity: '0'}, function(){

	$("img.enlarged_img", parent).attr("src", href).css({opacity: '1'});

});
		
    }).attr('src', href);
});

}

});

$(".work_previous_image").click(function(){

var parent = $(this).parent("div");
var grand_parent = $(this).parent("div").parent("div");
var rel_for_search = $("img.enlarged_img", parent).attr("rel");
var src_for_search = $("img.enlarged_img", parent).attr("src");

if(!$("img.enlarged_img", parent).is(':animated')){

$("h3 span span", parent).html("Loading...");

		$.ajax({
		  url: '/get_image.php',
		  dataType: 'json',
		  data: {'prev':'', 'img_id':rel_for_search, 'src':src_for_search},
		  success: function(json_data){

			var href = 'images/work/'+json_data.image;
			var rel = json_data.caption;
			var img_id = json_data.id;
			
			// load image

			$(function(){
				var img = new Image();
				$(img).load(function(){
					$(this).css('display', 'none');
					
			// set attributes

			$(parent).css("background-image", "url("+href+")");
			$("h3 span span", parent).html(rel);

			// fade out
			$("img.enlarged_img", parent).animate({opacity: '0'}, function(){

				$("img.enlarged_img", parent).attr("src", href).css({opacity: '1'});
				$("img.enlarged_img", parent).attr("rel", img_id);

			});
					
				}).attr('src', href);
			});		  

		  }
		});

}

});

$(".work_next_image").click(function(){

var parent = $(this).parent("div");
var grand_parent = $(this).parent("div").parent("div");
var rel_for_search = $("img.enlarged_img", parent).attr("rel");
var src_for_search = $("img.enlarged_img", parent).attr("src");

if(!$("img.enlarged_img", parent).is(':animated')){

$("h3 span span", parent).html("Loading...");

		$.ajax({
		  url: '/get_image.php',
		  dataType: 'json',
		  data: {'next':'', 'img_id':rel_for_search, 'src':src_for_search},
		  success: function(json_data){

			var href = 'images/work/'+json_data.image;
			var rel = json_data.caption;
			var img_id = json_data.id;
			
			// load image

			$(function(){
			
				var img = new Image();
				$(img).load(function(){
				
				$(this).css('display', 'none');
					
			// set attributes

			$(parent).css("background-image", "url("+href+")");
			$("h3 span span", parent).html(rel);

			// fade out
			$("img.enlarged_img", parent).animate({opacity: '0'}, function(){

				$("img.enlarged_img", parent).attr("src", href).css({opacity: '1'});
				$("img.enlarged_img", parent).attr("rel", img_id);

			});
					
				}).attr('src', href);
			});		  

		  }
		});

}

});

t=setTimeout("work_slideshow()",3000);

});

function work_slideshow(){

var total_images = $("#work_items a").length;
var total_rows = Math.floor(total_images / 8);

// choose random from vertical limit
var chosen_row = randomFromTo(1,total_rows) - 1;

// choose random between first two cols
var chosen_col_1 = randomFromTo(0,1);
var replace_this = ((chosen_row) * 8) + chosen_col_1;

// choose random from last 6 cols
var chosen_col_2 = randomFromTo(2,7);
var with_this = ((chosen_row) * 8) + chosen_col_2;

// get images
var old_image = $("#work_items a:eq("+replace_this+") img").attr("src");
var new_image = $("#work_items a:eq("+with_this+") img").attr("src");

// href, rel, title

var new_href = $("#work_items a:eq("+with_this+")").attr("href");
var new_rel = $("#work_items a:eq("+with_this+")").attr("rel");
var new_title = $("#work_items a:eq("+with_this+")").attr("title");

var old_href = $("#work_items a:eq("+replace_this+")").attr("href");
var old_rel = $("#work_items a:eq("+replace_this+")").attr("rel");
var old_title = $("#work_items a:eq("+replace_this+")").attr("title");

// set background of link to new image
$("#work_items a:eq("+replace_this+")").css("background-image", "url("+new_image+")");

// fade image opacity
$("#work_items a:eq("+replace_this+") img").animate({opacity: '0'}, function(){

	// set new attributes
	$("#work_items a:eq("+replace_this+") img").attr("src", new_image);
	$("#work_items a:eq("+replace_this+") img").css({opacity: '1'});
	
	$("#work_items a:eq("+replace_this+")").attr("href", new_href);
	$("#work_items a:eq("+replace_this+")").attr("rel", new_rel);
	$("#work_items a:eq("+replace_this+")").attr("title", new_title);
	
	// set old attributes
	$("#work_items a:eq("+with_this+") img").attr("src", old_image);
	
	$("#work_items a:eq("+with_this+")").attr("href", old_href);
	$("#work_items a:eq("+with_this+")").attr("rel", old_rel);
	$("#work_items a:eq("+with_this+")").attr("title", old_title);
	
	t=setTimeout("work_slideshow()",3000);

});

}

function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
