var subsection = '';
var point = 8;

function enforcePrecision(n, nDecimalDigits){
	return +(n).toFixed(nDecimalDigits);
}

$(document).ready(function(){
	if (html5_video) {
		var video = document.getElementsByTagName('video')[0];
		video.addEventListener('timeupdate', checkTime, false);
	}
	$('#creation .section_menu_button').click(gotoSection);
	$('.palette').click(setColor);
	$('.palette').mouseover(function(){
		$(this).find('.palette_title').show();
	}).mouseout(function(){
		$(this).find('.palette_title').hide();
	});
	$('.creation_content_list').click(function(){
		$(this).parent().parent().parent().fadeOut('fast');
		var title = $('#'+$(this).attr('rel')+'_title').html();
		var text = $('#'+$(this).attr('rel')+'_text').html();
		var legal = $('#'+$(this).attr('rel')+'_legal').html();
		var thumb = $('#'+$(this).attr('rel')+'_thumb').html();
		var source = $('#'+$(this).attr('rel')+'_source').html();
		var type = $('#'+$(this).attr('rel')+'_type').html();
		var description = $('#'+$(this).attr('rel')+'_description').html();
		showOverlay(title, text, legal, thumb, source, type, description);
	});
});

function creation() {
	resizeHandler();
	subsection = 'performance';
	$('#color_image img').hide();
	$('#creation_content > div').hide();
	$('#color_palette').hide();
	if (html5_video) {
		var video = document.getElementsByTagName('video')[0];
		if (!isiPad()) {
			video.currentTime = 0.01;
			video.play();
		} else {
			video.play();
		}
	} else {
		$('#creation').fadeIn('fast', function(){
			var obj = new Object({method:'play', cue_point:8});
			setTimeout(function() {
				resizeHandler();
				sendToActionScript(obj);
			}, 800);
		});
	}
	$('#creation').find('.line').find('img').css({marginLeft: '-4px'});
	$('#creation').find('.line').find('img').stop().animate({marginLeft: '50px'}, {duration: 8000});
	$('#creation').fadeIn();
	resizeHandler();
}

function checkTime() {
	if (html5_video) {
		var video = document.getElementsByTagName('video')[0];
		var time = video.currentTime;
		if (time >= point) {
			video.pause();
			cuePoint();
		}
	}
}

function cuePoint() {
	if (subsection != '') {
		showSubsection();
	}
}

function gotoSection() {
	subsection = $(this).attr('section');
	if (subsection != 'colori') {
		$('#color_image img').fadeOut('fast');
		$('#color_palette').fadeOut('fast');
	}
	$('#creation_content').fadeOut({duration: 500, easing: 'easeOutQuint', complete: function() {
		$('#creation_content div').hide();
		$('#creation_content').show();
	}});
	if (html5_video) {
		var video = document.getElementsByTagName('video')[0];
		video.pause();
	} else {
		var obj = new Object({method:'pause'});
		sendToActionScript(obj);
	}
	var left = 0;
	if (Number($(this).attr('time')) > Number(point)) {
		var time = Number($(this).attr('time')) - Number(point);
		point = $(this).attr('time');
		if (html5_video) {
			video.play();
		} else {
			var obj = new Object({method:'play', cue_point:point});
			sendToActionScript(obj);
		}
		$('.section_menu_button').each(function(){
			left += parseInt($(this).css('marginLeft'),10);
			left += parseInt($(this).width(),10);
			if ($(this).attr('time') == point) {
				left -= parseInt($(this).width(),10)/2;
				left -= parseInt($('#creation').find('.line').find('img').width()/2,10);
				$('#creation').find('.line').find('img').stop().animate({marginLeft: left+'px'}, {duration: time*1000});
				return;
			}
		});
	} else {
		$('#creation video').fadeOut('fast');
		time = Number(point) - Number($(this).attr('time'));
		point = $(this).attr('time');
		if (Number(point) == 8) {
			if (html5_video) {
				video.currentTime = 0.01;
			} else {
				var obj = new Object({method:'seek', cue_point:0.01});
				sendToActionScript(obj);
				var obj = new Object({method:'play', cue_point:point});
				sendToActionScript(obj);
			}
			$('#creation').find('.line').find('img').stop().animate({marginLeft: '50px'}, {duration: 800});
		} else {
			if (html5_video) {
				video.currentTime = enforcePrecision(Number($(this).prev().attr('time')), 1);
			} else {
				var obj = new Object({method:'seek', cue_point:enforcePrecision(Number($(this).prev().attr('time')), 1)});
				sendToActionScript(obj);
				var obj = new Object({method:'play', cue_point:point});
				sendToActionScript(obj);
				$('#creation video').fadeIn('fast');
				$('#creation .section_menu_button').each(function(){
					left += parseInt($(this).css('marginLeft'),10);
					left += parseInt($(this).width(),10);
					if ($(this).attr('time') == point) {
						left -= parseInt($(this).width(),10)/2;
						left -= parseInt($('#creation').find('.line').find('img').width()/2,10);
						$('#creation').find('.line').find('img').stop().animate({marginLeft: left+'px'}, {duration: time*1000, easing: 'easeOutQuint'});
						return;
					}
				});
			}
		}
		if (html5_video) {
			var interval = setInterval(function() {
				if (!video.seeking) {
					clearInterval(interval);
					$('#creation video').fadeIn('fast');
					video.play();
					$('#creation .section_menu_button').each(function(){
						left += parseInt($(this).css('marginLeft'),10);
						left += parseInt($(this).width(),10);
						if ($(this).attr('time') == point) {
							left -= parseInt($(this).width(),10)/2;
							left -= parseInt($('#creation').find('.line').find('img').width()/2,10);
							$('#creation').find('.line').find('img').stop().animate({marginLeft: left+'px'}, {duration: time*1000, easing: 'easeOutQuint'});
							return;
						}
					});
				}
			}, 200);
		}
	}
}

function showSubsection() {
	$('#creation_content div[rel="'+subsection+'"]').show();
	$('#creation_content > div').fadeIn();
	if (subsection == 'colori') {
		$('#color_image img').hide();
		$('#color_palette').fadeIn();
	}
	subsection = '';
}

function setColor() {
	$('#color_image img[index='+$(this).attr('index')+']').fadeIn();
	$('#color_image img[index!='+$(this).attr('index')+']').fadeOut();
}
