var WWW_ROOT = '/';

// Prevent firebug errors
if (window.console === undefined)
{
	window.console = {log: function(){}};
}

var isAnimating = false;

	
/**
 * Initial event bindings/setup
 */
$(document).ready(function()
{	
	$('#scroll_contents a').click(projectClick);
	
	$('#scroll_contents a').each(function() {
		totalScrollWidth += $(this).outerWidth();
	});
	
	$('#scroll_left').click(scrollRight); //arrows scroll oposite dirrections
	$('#scroll_right').click(scrollLeft);
	
	$('#portfolio_nav a').click(highlightCategory);
	
	$('#top_thumbs a').live('click', loadLargeImage);
	
	//initialize the lightbox on any "a" element that has the class "lightbox"
	$('a.lightbox').lightbox();
	
	//load content if specific url
	if (window.location.hash)
	{
		var temp_href = window.location.hash;

		if (temp_href.substring(0, 1) == '#')
		{
			temp_href = temp_href.substring(1);
		}

		loadProject(temp_href);
	}
	
	$("#newsList").scrollbar({buttonHeight: 0});
	$("#newsArticleText_wrapper").scrollbar({buttonHeight: 0});
	
	$('#newsList a').click(loadNewsItem);
});


/**
 * Event Handlers
 */

	function loadNewsItem()
	{
		if (!$(this).hasClass('selected'))
		{
			$('#newsList a.selected').removeClass('selected');
			$(this).addClass('selected');
			
			var news_id = $(this).attr('id').split('_')[1];

			$.get(WWW_ROOT + "ajax/get-news.php?id=" + news_id, function(html)
			{
				$('#newsContent').fadeOut(250, function()
				{
					$(this).html(html).fadeIn(250);
				});
			});
		}

		
		return false;
	}


function projectClick(event)
{
	if (!$(this).hasClass('selected'))
	{
		$('#scroll_contents a.selected').removeClass('selected');
		$(this).addClass('selected');
		
		var project_id = $(this).attr('href');
		window.location.hash = project_id;
		loadProject(project_id);
	}
	
	return false;
}
 
function loadProject(project_id)
{
	if (isAnimating)
	{
		return false;
	}
	
	isAnimating  = true;
	
	$.ajax({
		url: WWW_ROOT + "ajax/get-project.php",
		data: "id=" + project_id.split('_')[1],
		success: function(data)
		{
			if (data != 'error')
			{
				$("#project_frame").animate({opacity: 0}, 300, function() {
					$("#project_frame").html(data);

					$("#project_image a").click(function()
					{
						$('#top_thumbs a.selected').next('.lightbox').lightbox({trigger: true});
						return false;
					});
					
					$("#project_info .description").scrollbar({buttonHeight: 0});
					$("#project_frame").animate({opacity: 1}, 300);
					isAnimating = false;
				});
			}
			else
			{
				console.log('error!');
			}
		},
		error: function() {
			//console.log('error loading data');
			isAnimating = false;
		}
	});
	
	return false;
}

var scrollWidth = 596;
var scrollSpeed = 400;
var currentScrollPosition = 0;
var totalScrollWidth = 0;

function scrollContents(position)
{
	$('#scroll_contents').animate({left: position + 'px'}, scrollSpeed);
}

function scrollLeft(event)
{	
	var offset = $('#scroll_contents').css('left');
	offset = parseInt(offset);
	offset = offset - scrollWidth;
	
	if (offset > -totalScrollWidth)
	{
		scrollContents(offset);
	}
	
	return false; 
}

function scrollRight(event)
{
	var offset = $('#scroll_contents').css('left');
	offset = parseInt(offset);
	offset = offset + scrollWidth;
	
	if (offset < 0)
	{	
		scrollContents(offset);
	}
	else
	{
		scrollContents(0);
	}
	
	return false;
}


function highlightCategory(event)
{
	$('#portfolio_nav a.selected').removeClass('selected');
	$(this).addClass('selected');
	
	var first = true;
	var scrollToIndex;
	var type = $(this).attr('title');
	
	if (type == 'all')
	{
		$('#scroll_contents a').animate({opacity: 1});
		scrollContents(0);
	}
	else
	{
		$('#scroll_contents a').each(function()
		{
			if (!$(this).hasClass(type))
			{
				$(this).animate({opacity: .4});
			}
			else
			{
				if (first === true)
				{
					scrollToIndex = $('#scroll_contents a').index($(this));
					first = false;
					scrollContents((-scrollToIndex * 99));
				}
				
				$(this).animate({opacity: 1});
			}
		});
	}
	
	return false;
}


function loadLargeImage(event)
{
	if (isAnimating || $(this).hasClass('selected'))
	{
		return false;
	}
	
	$('#top_thumbs a.selected').removeClass('selected');
	$(this).addClass('selected');
	
	isAnimating  = true;

	var newSrc = $(this).attr('href');
	
	$('<img alt="">').load(function()
	{
		$('#project_image a').prepend($(this));
		$('#project_image a img:last').fadeOut(400, function() {
			$(this).remove();
			isAnimating = false;
		});
	}).error(function() {
		isAnimating = false;
	}).attr('src', newSrc);
	
	return false;
}
