var mainFormId = 'mainForm';
var backLinkId = 'back';
var nextLinkId = 'next';

var mainForm = null;
var backLink = null;
var nextLink = null;

function init()
{
	pngFix();

	mainForm = document.getElementById(mainFormId);
	backLink = document.getElementById(backLinkId);
	nextLink = document.getElementById(nextLinkId);

	// hide back/next links
	backLink.style.visibility = (Navigation.hasBack) ? '' : 'hidden';
	nextLink.style.visibility = (Navigation.hasNext) ? '' : 'hidden';

	// call page init function if available
	// page init has to enable next/back links
	if (typeof(pageInit) != 'undefined') {
		Navigation.disableBack();
		Navigation.disableNext();
		pageInit();
	} else {
		Navigation.enableBack();
		Navigation.enableNext();
	}
}

var Navigation = {}

Navigation.backEnabled = false;
Navigation.nextEnabled = false;

Navigation.back = function ()
{
	if (!Navigation.backEnabled) {
		if (Navigation.tryBackMessage) {
			alert(Navigation.tryBackMessage);
		}
		return;
	}
	if (typeof(validateBack) != 'undefined') {
		if (!validateBack()) {
			return;
		}
	}
	if (Navigation.hasBack) {
		Navigation.move(Navigation.backURL);
	}
}

Navigation.next = function ()
{
	if (!Navigation.nextEnabled) {
		if (Navigation.tryNextMessage) {
			alert(Navigation.tryNextMessage);
		}
		return;
	}
	if (typeof(validateNext) != 'undefined') {
		if (!validateNext()) {
			return;
		}
	}
	if (Navigation.hasNext) {
		Navigation.move(Navigation.nextURL);
	}
}

Navigation.mainPage = 'production.php';

Navigation.move = function(url)
{
	if (url == '') {
		return false;
	}
	closeHelp();
	closeVideo();
	if (mainForm) {
		var pageInput = document.getElementById('pageInput');
		if (!pageInput) {
			alert('No pageInput in HTML form !');
		}
		mainForm.action = Navigation.mainPage;
		pageInput.value = url;
		if (mainForm.onsubmit && !mainForm.onsubmit()) {
			return;
		}
		mainForm.submit();
	} else {
		window.location.href = '?page=' + url;
	}
}

Navigation.backClass = 'back';
Navigation.nextClass = 'next';

Navigation.enabledClass = 'enabled';
Navigation.disabledClass = 'disabled';

Navigation.enableBack = function () {
	Navigation.backEnabled = true;
	backLink.className = Navigation.backClass + ' ' + Navigation.enabledClass; }

Navigation.disableBack = function () {
	Navigation.backEnabled = false;
	backLink.className = Navigation.backClass + ' ' + Navigation.disabledClass; }

Navigation.enableNext = function () {
	Navigation.nextEnabled = true;
	nextLink.className = Navigation.nextClass + ' ' + Navigation.enabledClass; }

Navigation.disableNext = function () {
	Navigation.nextEnabled = false;
	nextLink.className = Navigation.nextClass + ' ' + Navigation.disabledClass; }

// flash components

function captureEventHandler(event)
{
	if (event.name == 'captureFinished') {
		document.getElementById('captureDoneInput').value = 'true';
		document.getElementById('captureDurationInput').value =
			event.position;
		Navigation.enableNext();
	}
}

function dubbingEventHandler(event)
{
	if (event.name == 'modified' && (event.isCapture || event.trackId)) {
		document.getElementById('dubbingDoneInput').value = '1';
		document.getElementById('dubbingIsCaptureInput').value = 
			(event.isCapture) ? '1' : '';
		document.getElementById('dubbingDurationInput').value = 
			event.duration;
		document.getElementById('dubbingTrackIdInput').value = 
			event.trackId;
		Navigation.enableNext();
	}
}

function veditingEventHandler(event)
{
	if (event.name == 'modified') {
		document.getElementById('selectionInput').value = 
			event.selection;
		if (event.selection.length > 0) {
			Navigation.enableNext();
		} else {
			Navigation.disableNext();
		}
	}
}

var showVideoSettings = 'width=335, height=275, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no';
var showVideoBaseUrl = '../pages/show_video.php';
var showVideoWindow = null;

function showVideo(kind, id)
{
	closeVideo();
	var url = showVideoBaseUrl + '?kind=' + kind + '&id=' + id;
	var name = 'showVideoWindow';
	var settings = showVideoSettings;
	showVideoWindow = window.open(url, name, settings);
	showVideoWindow.focus();
}

function closeVideo()
{
	if (showVideoWindow && !showVideoWindow.closed) {
		showVideoWindow.close();
	}
}

Navigation.page = null;

var showHelpSettings = 'width=1030, height=600, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no';
var showHelpBaseUrl = 'help.php';
var showHelpWindow = null;

function showHelp()
{
	closeHelp();
	var url = showHelpBaseUrl + '?page=' + Navigation.page;
	var name = 'showHelpWindow';
	var settings = showHelpSettings;
	showHelpWindow = window.open(url, name, settings);
	showHelpWindow.focus();
}

function closeHelp()
{
	if (showHelpWindow && !showHelpWindow.closed) {
		showHelpWindow.close();
	}
}

var showRulesSettings = {
	'cluf' : 'width=620, height=600, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no',
	'rules' : 'width=1200, height=600, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no'};
var showRulesUrl = {
	'cluf' : 'cluf.html',
	'rules' : 'rules_popup.php'};
var showRulesWindow = null;

function showRules(id)
{
	closeRules();
	var url = showRulesUrl[id];
	var name = 'showRulesWindow';
	var settings = showRulesSettings[id];
	showRulesWindow = window.open(url, name, settings);
	showRulesWindow.focus();
}

function closeRules()
{
	if (showRulesWindow && !showRulesWindow.closed) {
		showRulesWindow.close();
	}
}

function onAcceptRulesCheckboxModified()
{
	var clufInput = document.getElementById('acceptCLUFCheckbox');
	var rulesInput = document.getElementById('acceptRulesCheckbox');
	var checked = (!clufInput || clufInput.checked)
		&& (!rulesInput || rulesInput.checked)
	if (checked) {
		Navigation.enableNext();
	} else {
		Navigation.disableNext();
	}
}
