$(document).ready(function() {
	cur = window.location.hash.substr(1);
	$.timer(500, change);
	$('#find').hide();
	$('#timecode-help').hide();
	$('#find .error').hide();
	$('#buy').hide();
	
	fixWidth();
	
	$('.object img, .quote').live('click', function(e) {
		e.preventDefault();
		$.scrollTo('+=600px',200,{axis:'x'});
	});
	
	$('.object p a.time').live('click', function(e) {
		e.preventDefault();
		e.stopPropagation()
		var obj = $($(this).attr('href'));
		var idx = idxFromObj($(obj));
		if (obj.hasClass('with-time')) {
			obj.children('img').attr('src', captures + '/' + prepend(idx, 4, '0') + '.png');
		}	else {
			obj.children('img').attr('src', capturesWithTime + '/' + prepend(idx, 4, '0') + '.png');
		}
		obj.toggleClass('with-time');
	});
	$('.start').click(function(e) {
		e.preventDefault();
		$.scrollTo($('.object:first').position().left - 640,200,{axis:'x'});
	});
	$('.find').click(function(e) {
		e.preventDefault();
		showByAnchor('#find');
	});
	$('.buy').click(function(e) {
		e.preventDefault();
		showByAnchor('#buy');
	});
	$('.timecode-help').click(function(e) {
		e.preventDefault();
		showByAnchor('#timecode-help');
	});
	$('#find-form').submit(function(e) {
		e.preventDefault();
		findByForm();
	});
	$('#find-go').click(function(e) {
		e.preventDefault();
		findByForm();
	});
	
});

function showByAnchor(a) {
	window.location.hash = a;
	$(a).show();
	$.scrollTo($(a).position().left - 60,200,{axis:'x'});
	cur = a.substr(1);
	fixWidth();
}

var cur = null;
function change() {
	if (window.location.hash.substr(1) != cur) {
		if (window.location.hash.substr(1,2) == 'o') {
			addIfNotOnStage(window.location.hash.substr(2));
		} else if ($(window.location.hash).size() == 1) {
			var obj = $(window.location.hash);
			if (obj.size() > 0) {
				showByAnchor(window.location.hash);
			}
		}
	}
}

function getAndValidateInput() {
	var input = $('#find-index').val();
	while(input.length > 0 && (input.substr(0, 1) == 0 || input.substr(0, 1) == '#')) {
		input = input.substr(1);
	}
	var i = parseInt(input);
	if(isNaN(i) || i <= 0 || i > 3252) {
		return false;
	} else {
		return i;
	}
}

function findByForm() {
	var i = getAndValidateInput();
	if (!i || !addIfNotOnStage(i)) {
		$('#find .error').show();
	}
}

function fixWidth() {
	$('#all').css('width', $('.video, .object, .quote, .info:visible').size() * 600 + 180);
}

function addIfNotOnStage(i) {
	i = Number(i);
	if(isNaN(i) || i <= 0 || i > 3252) {
		return false;
	}
	
	if ($('#' + oid(i)).size() == 0) {
		var h = '<div class="object" id="' + oid(i) + '">';
		h += '<img src="' + captures + '/' + prepend(i, 4, "0") + '.png" />';
		h += '<div class="label">';
		h += '<p><a href="#' + oid(i) + '" title="Permalink">#<a>' + prepend(i, 4, "0") + ' Untitled</p>'
		h += '<p class="time"><a class="time" href="#' + oid(i) + '" title="Show/Hide Time"><span>Show/Hide Time</span></a></div>';
		h += '</div>';
		h += '</div>';
		
		var prev = null;
		$('.object').each(function(j) {
			if (idxFromObj($(this)) > i) {
				return false;
			}
			prev = $(this);
		});
		
		if(prev == null) {
			prev = $('#start');
		}
		$(prev).after(h);
		
		fixWidth();
		
	}
	
	cur = oid(i);
	window.location.hash = '#' + oid(i);
	$.scrollTo($('#'.concat(oid(i))).position().left - 120,200,{axis:'x'});
	return true;
}

function oid(i) {
	return 'o' + prepend(i, 4, "0");
}

function idxFromObj(obj) {
	return Number(obj.attr('id').substring(1));
}

function prepend(s, len, prepend) {
	while (String(s).length < len) {
		s = String(prepend) + s;
	}
	return s;
}