var title = document.title;
var currentSection = 'work';
var visibleCategories = new Array();
var infoHidden = true;

$(document).ready(function() {
	sizeContents();
	createEventHandlers();
	updateStatusIndicator(true);
});

function sizeContents()
{
	$('DIV#contentWrapper').css('height', ($('BODY').height() - ($('#mainHeader').height() + $('#indicator').height())))
	$('DIV#contentScroller').css('width',(5 * $('BODY').width()));
	$('DIV.contentContainer').css('width',$('BODY').width());
	switch(currentSection)
	{
		case 'work':
			slideTo(4, true);
			break;
		case 'about':
			slideTo(3, true);
			break;
		case 'availability':
			slideTo(2, true);
			break;
		case 'download':
			slideTo(1, true);
			break;
		case 'contact':
			slideTo(0, true);
			break;
	}
}

function scrollToTop()
{
	$('DIV#contentWrapper').scrollTop(0);
}

function createEventHandlers()
{
	$(document).keydown(function(e){
		if(e.keyCode == 37 || e.keyCode == 39)
		{
			e.preventDefault();
			switch(e.keyCode)
			{
				case 37:
					handle_leftKey();
					break;
				case 39:
					handle_rightKey();
					break;
			}
		}	
	})
	$(window).resize(function(){ sizeContents(); updateStatusIndicator(true);  })
}

function handle_leftKey()
{
	switch(currentSection)
	{
		case 'work':
			goto_contact();
			break;
		case 'about':
			goto_work();
			break;
		case 'availability':
			goto_about();
			break;
		case 'download':
			goto_availability();
			break;
		case 'contact':
			goto_download();
			break;
	}
}

function handle_rightKey()
{
	switch(currentSection)
	{
		case 'work':
			goto_about();
			break;
		case 'about':
			goto_availability();
			break;
		case 'availability':
			goto_download();
			break;
		case 'download':
			goto_contact();
			break;
		case 'contact':
			goto_work();
			break;
	}
}


//  NAVIGATING
/////////////////////////////////////////////////////////////
function goto_work()
{
	$.address.value('/work');
}

function goto_about()
{
	$.address.value('/about');
}

function goto_availability()
{
	$.address.value('/availability');
}

function goto_download()
{
	$.address.value('/download');
}

function goto_contact()
{
	$.address.value('/contact');
}

function goto_project(int,title)
{
	$.address.value('/project/'+int+'/'+title);	
}


//  ADDRESS MANAGEMENT & DEEP LINKING
/////////////////////////////////////////////////////////////

$.address.change(function(event)
{
	switch(event.pathNames[0])
	{
		case 'project':
			_gaq.push(['_trackEvent', 'Project', 'Display', event.pathNames[2]]);
			toggleWorkList(false);
			setTimeout('loadProject('+event.pathNames[1]+')',500)
			$.address.title(title+' - '+event.pathNames[2]);
			slideTo(4);
			currentSection = 'work';
			break;
		case 'work':
			_gaq.push(['_trackEvent','Work List', 'Display']);
			toggleProject(false);
			setTimeout('toggleWorkList(true)',500)
			$.address.title(title+' - Work');
			slideTo(4);
			currentSection = 'work';
			break;
		case 'about':
			_gaq.push(['_trackEvent','About', 'Display']);
			slideTo(3);
			$.address.title(title+' - About');
			currentSection = 'about';
			break;
		case 'availability':
			_gaq.push(['_trackEvent','Availability', 'Display']);
			slideTo(2);
			$.address.title(title+' - Availability');
			currentSection = 'availability';
			break;
		case 'download':
			_gaq.push(['_trackEvent','Download', 'Display']);
			slideTo(1);
			$.address.title(title+' - Download');
			currentSection = 'download';
			break;
		case 'contact':
			_gaq.push(['_trackEvent','Contact', 'Display']);
			slideTo(0);
			$.address.title(title+' - Contact');
			currentSection = 'contact';			
			break;
		default:
			goto_work();
			break;
	}

	updateStatusIndicator()
})

function toggleWorkList(bool)
{
	if(bool == true)
	{
		$('DIV#workList').fadeTo(500,1);
	}
	else
	{
		$('DIV#workList').fadeTo(500,0, function(){ $('DIV#workList').hide() });
	}
}

function toggleProject(bool)
{
	if(bool == true)
	{
		$('DIV#project').fadeTo(500,1);		
	}
	else
	{
		$('DIV#project').fadeTo(500,0, function(){ $('DIV#project').hide() });		
	}

}

function loadProject(ID)
{
	var form_data = { ID: ID }
	$.ajax({
		url: "/site/project",
		type: "POST",
		data: form_data,
		dataType: 'html',
		success: function(projectData)
		{	
			populateProject(projectData);
		},
		error: function(err){
			console.error(err);
		}
	});
}

function populateProject(projectData)
{
	$('DIV#project').remove(); // this is sent over from the server
	$('DIV#workList').parent().append(projectData);
	if(infoHidden == true)
	{
		toggleProjectInfo();
	}
	$('DIV#project').fadeTo(0,0);
	$('DIV#project').fadeTo(500,1);
	$('DIV#project').fadeTo(500,1);
	$('DIV.projectThumbs IMG:last').fadeTo(0,0.1);
}

function displayProjectVideo(params,element)
{
	var pathNames = $.address.path().split('/');
	
	_gaq.push(['_trackEvent','Show Project Video', pathNames[2], params.mp4]);
	$("DIV#projectImageAndInfo").css('height',800);
	$("DIV#projectImageAndInfo").css('background','none');
	$("DIV#projectImageAndInfo").videoBG({
		mp4:'/assets/files/projectMedia/video/'+params.mp4,
		ogv:'/assets/files/projectMedia/video/'+params.ogv,
		webm:'/assets/files/projectMedia/video/'+params.webm,
		poster: params.poster,
		loop:true,
		scale:true,
		zIndex:0
	});

	$('DIV.projectThumbs IMG').removeClass('selected');
	$(element).addClass('selected');
	$('DIV.projectThumbs IMG.selected').fadeTo(200,0.1);
	$('DIV.projectThumbs IMG').not('.selected').fadeTo(200,1);	
}

function removeProjectVideo()
{
	var contents = $('.videoBG_wrapper').html();
	$('.videoBG_wrapper').before($('.videoBG_wrapper').html());
	$('.videoBG_wrapper, .videoBG').remove();
}

function toggleProjectImage(filename, element)
{
	removeProjectVideo();
	var pathNames = $.address.path().split('/');
	
	_gaq.push(['_trackEvent','Show Project Image', pathNames[2], filename]);
	if($('IMG#projectImage').length == 0)
	{
		$('#projectImageAndInfo').append('<img src="" alt="" id="projectImage" />');
	}
	
	$('#project #projectImageAndInfo IMG#projectImage').load(function(){
		$('#project #projectImageAndInfo').css('height', $('#project #projectImageAndInfo IMG#projectImage').height());
		$('#project #projectImageAndInfo').css('background', 'url(\''+$('#project #projectImageAndInfo IMG#projectImage').attr('src')+'\') no-repeat center');	
	});
	$('#project #projectImageAndInfo IMG#projectImage').attr('src', '/assets/img/projectMedia/image/'+filename);


	$('#project DIV.projectThumbs IMG').removeClass('selected');
	$(element).addClass('selected');
	$('DIV.projectThumbs IMG.selected').fadeTo(200,0.1);
	$('DIV.projectThumbs IMG').not('.selected').fadeTo(200,1);
}

function toggleProjectInfo()
{
	var pathNames = $.address.path().split('/');

	if($('#projectImageAndInfo DIV.projectInfo').is(':visible'))
	{
		_gaq.push(['_g','Toggle Project Info', 'Hide', pathNames[2]]);
		infoHidden = true;
		$('A#infoToggle').html('Show Info');
		$('#projectImageAndInfo DIV.projectInfo').hide();
	}
	else
	{
		_gaq.push(['_trackEvent','Toggle Project Info', 'Show', pathNames[2]]);
		infoHidden = false;
		$('A#infoToggle').html('Hide Info');
		$('#projectImageAndInfo DIV.projectInfo').show();
	}
}

//  EFFECTS
/////////////////////////////////////////////////////////////

function updateStatusIndicator(immediate)
{
	var alignmentTarget = $('#lnk_'+currentSection);
	var indicator = $('#indicator IMG');
	var leftMargin = alignmentTarget.offset().left;
	var targetWidth = alignmentTarget.width();
	var indicatorWidth = indicator.width();
	var position = leftMargin + (targetWidth / 2) - (indicatorWidth / 2);
	if(immediate != undefined)
	{
		indicator.css({marginLeft: position });	
	}
	else
	{
		indicator.animate({marginLeft: position, duration:5000 });	
	}
}

function slideTo(int,immediate)
{
	var newMargin = (int * $('BODY').width()); // + $('BODY').width()
	scrollToTop();
	if(!immediate)
	{
		$('DIV#contentScroller').animate({ duration:8000, marginLeft: -newMargin })
	}
	else
	{
		$('DIV#contentScroller').css({ marginLeft: -newMargin })
	}
}

function filterWork()
{
	var catCount = 0;
	visibleCategories['identity'] = $('#cbx_identity:checked').length;
	visibleCategories['interactive'] = $('#cbx_interactive:checked').length;
	visibleCategories['mobile'] = $('#cbx_mobile:checked').length;
	visibleCategories['print'] = $('#cbx_print:checked').length;
	visibleCategories['leisure'] = $('#cbx_leisure:checked').length;
	
	for(var key in visibleCategories)
	{
		catCount += visibleCategories[key];
	}
	
	if(catCount == 0)
	{
		visibleCategories['identity'] = 1;
		visibleCategories['interactive'] = 1;
		visibleCategories['mobile'] = 1;
		visibleCategories['print'] = 1;
		visibleCategories['leisure'] = 1;
	}
	
	hideInactiveWork();
}

function hideInactiveWork()
{
	var show = '';
	
	for(var key in visibleCategories)
	{
		if(visibleCategories[key] == 1)
		{
			show += 'LI.'+key+' IMG, ';
		}
	}
	
	$(show).parent().show();
	$(show).fadeTo(600,1);
	$('DIV#workList DIV#listContainer UL LI IMG').not(show).fadeTo(600,0, function(){ 
		$('DIV#workList DIV#listContainer UL LI IMG').not(show).parent().hide();
	});
	
}
