var sliderHeight = "23px";
var textSliderHeight = "23px";
var togglePaneOn = false;
var min_time = 450;
var preview_open_time = 650;
var preview_height = 15 + "px";
var paneOpen = false;
var newsInterval = 5000
var newsTimer;
	
$(document).ready(function(){

	
	$("#work-list li .item-body").each(function () {
		var current = $(this);
		current.attr("box_h", current.height());
	});
	$("#work-list li").each(function () {
		var current = $(this);
		//current.attr("box_h", current.height());
		current.attr("is_on", false);
	});
	
	$("#work-list li .item-body").css("height", sliderHeight);
	$("#work-list .info-item .item-body").css("height", "0px");
	$("#work-list .text-item .item-body").css("height", textSliderHeight);
	
	$("#work-list li").click(function(event) {
		
		if (enlargeClick == false) {
			togglePane($(event.target));
		}
		enlargeClick = false;
	});
	
	$(".swapImageClick").click(function(){ 
		updateHeight();
	});
	
	//setTimeout();
	newsTimer = setTimeout("changeNews()", newsInterval);
	linkEnlargeFunctions();
});

/*
function updateHeight(me){ 
	var current = $(me.target); 
 
	var i = 0; 
	var max_depth = 8; 
  
	for (i=0; i <= max_depth; i++) 
	{ 

		if ( current.hasClass('work-item') || current.hasClass('info-item') ){ 
			// nothing 
			i = max_depth + 1; 
		} else { 
			current = current.parent(); 
		}      
	} 
  
	var current_body = current.children('.item-body'); 
	openSlider(current, current_body, 0);   
} 
*/


function updateHeight(me){
	
	var current = $(me.target);

	//var i = 0;
	//var max_depth = 8;
	current = findActiveItem(8, current);
	
	var current_body = current.children('.item-body');
	openSlider(current, current_body, 0);
	
}

var insideMedia = false;
function findActiveItem(_max_depth, _current){
	var i = 0;
	insideMedia = false;
	for (i=0; i <= _max_depth; i++)
	{
		if (_current.hasClass('little-image') || _current.hasClass('big-image')){
			insideMedia = true;
		}
		if ( _current.hasClass('work-item') || _current.hasClass('info-item') ){
			// nothing
			i = _max_depth + 1;
		} else {
			_current = _current.parent();
		}					
	}
	return _current;
}


function togglePane(_current){
		
	var current = _current;
	
	// Check for main nav 3 levels (so  li > div > div works but li > div > div > div doesn't)
	var i = 0;
	
	current = findActiveItem(5,current);
	
	// set the current body
	var current_body = current.children('.item-body')
	var is_info =  current.hasClass('info-item');
	
	if (current.hasClass('work-item') || current.hasClass('info-item') ){
		// Use Class 'is_on' as a variable to toggle the slider's state
		if ( current.hasClass('is_on') ){
			closeSlider(current, current_body);
			current.removeClass('is_on');
			paneOpen = false;
		} else {
			openSlider(current, current_body, 0, is_info);
			current.addClass('is_on');
			paneOpen = true;
		}

	}
	
}


function changeNews(){
	//alert('change news');
	newsTimer = setTimeout("changeNews()", newsInterval);
	
	$( '.news-item:first-child' ).fadeOut( 200, function(){ replaceNews() } );
		    
}


function replaceNews(){
	
	$('.news-item:first-child').clone().insertAfter('.news-item:last-child');
	
	var tempItem = $('.news-item').eq(1);
	$('.news-item:first-child').remove();
	
	tempItem.clone().insertBefore('.news-item:first-child');
	$('.news-item:first-child').fadeIn('slow');
	
	
	$('.news-item').eq(1).remove();
	
	$('.news-item:last-child').fadeIn('slow');


	//linkEnlargeFunctions();
}

var enlargeClick = false;
function linkEnlargeFunctions(){
	enlargeClick = false;
	
		
	$(".little-image").click(function(event){
		var cur = $(event.target);
		var par = findActiveItem(8, cur);
		
		if (par.hasClass('is_on')){
			enlargeClick = true;
			$(this).css("display", "none");
			$(this).next(".big-image").css("display", "inline");
			updateHeight(event);
		}

	});
	
	
	
	$(".big-image").click(function(event){
		var cur = $(event.target);
		var par = findActiveItem(8, cur);
		
		if (par.hasClass('is_on')) {
			$(this).css("display", "none");
			$(this).prev(".little-image").css("display", "inline");
			enlargeClick = true;
			//var current = $(this);
			updateHeight(event);
		}
	});
	
}


function openSlider(current, current_body, preset_height, is_info){
	
	var open_height = current_body.attr("box_h");
	if (preset_height != 0){
		open_height = preset_height;
	}
	
	//Use dynamic height in case of images (which can change heights)
	if (current.hasClass('news')){
		open_height = current_body.find('.news-list').height()+20;
		clearTimeout(newsTimer);
	} else {
		open_height = current_body.find('.media-list').height()+20;
		var desc_height = current_body.find('.item-description').height()+30;
		if (open_height < desc_height){
			open_height = desc_height;
		}
	}
	
	
	//If there are no images then use preset height
	
	if ( current.find('.media-list').length == 0 ){
		open_height = current_body.attr("box_h");
	}
	
	// Set timer
	var open_time = open_height * 2;
	if (open_time < min_time){
		open_time = min_time;
	}
	open_time = 1000;
						
	// Add suffix then go
	open_height = open_height + "px";
	current_body.stop().animate({"height": open_height}, {duration:open_time, easing: 'swing' });
}


function closeSlider(current, current_body)
{
	
	//var open_height = current_body.attr("box_h") + "px";
	var open_time = current_body.attr("box_h") * 2;
	var close_height = sliderHeight;
	if ( current.hasClass('info-item') ){
		close_height = 0 + "px";
	}
	if ( current.hasClass('text-item') ){
		close_height = textSliderHeight;
	}
	
	if (open_time < min_time){
		open_time = min_time;
	}

	current_body.stop().animate({"height": close_height}, {duration:open_time, easing: 'swing' });

}				
