
function byid (id) {
	return document.getElementById(id);
}

function px (v) {
	return v + 'px';
}


// Entry point
$(document).ready(function () {
	var behaviours = [
		splashBehaviour,
		orderFormBehaviour,
		screenshotsGalleryBehaviour,
		subscriptionFormBehaviour
	];
	// Apply each behaviour function	
	for (var i=0; i<behaviours.length; i++) {
		behaviours[i]();
	}
});


function orderFormBehaviour () {
	var orderForm = byid('order-form');
	if (!orderForm) {
		return;
	}
	

	var elems = orderForm.elements;
	var pageid = elems['pageid'].value; 
	
	// First step
	if (pageid == 'start') {
		var installBeh = function (ev) {
			var el = elems['onetime_install'];
			el.disabled = elems['premium_support'].checked || elems['license'][0].checked;
			
			var priceQ = $(el).parents(".order-form").find(".price");			
			if (el.disabled) {
				el.checked = true;
				if (!window.onetime_install_price) {
					window.onetime_install_price = priceQ.html();
				}
				priceQ.html("Free");
			} else {
				el.checked = window.onetime_install_checked || false;
				priceQ.html(window.onetime_install_price);
				window.onetime_install_price = null
			}
		}
	
		$(elems['premium_support']).click(installBeh);
		$(elems['license']).click(installBeh);
		$(elems['onetime_install']).click(function (ev) { 
			var el = elems['onetime_install'];
			window.onetime_install_checked = el.checked;
		});
		installBeh();
	}
	
	// Check client
	else if (pageid == 'check_client') {
		$('input[name="login"], input[name="pass"]', orderForm).focus(function (ev) {
			$('input[name="client_type"][value="e"]', orderForm).get(0).checked = true;
		});
		
		var ch = false;
		$('input[name="client_type"]', orderForm).each(function () {
			ch = ch || this.checked;
		});
		if (!ch) {
			$('input[name="client_type"][value="n"]', orderForm).get(0).checked = true;
		}
	}
	
	// New client
	else if (pageid == 'new_client') {
		
	}
	
	// Checkout
	else if (pageid == 'checkout') {
	
	}
	
	
	// Submit order form on button click
	$('.buttons button[type="button"]', orderForm).click(function (ev) {
		orderForm.elements['action'].value = this.name; 
		orderForm.submit();
	});
	
	// Submit form on Enter
	$('input', orderForm).keypress(function (ev) {
		if (ev.which == 13) {
			this.form.submit();
		}
	})
	
} 

function splashBehaviour () {
	if (!window.screenshots || !window.screenshots.length) {
		return;
	}
	slideshow(byid('movie-screen'), window.screenshots)
}

function slideshow (el, images) {
	// Create animation sandbox
	return new function () {
		var i = -1;
		var elQ = $(el);
		var imgQ = $('img', el);
		
		function showImage () {
			elQ.fadeIn('slow');
		}
		
		function nextImage () {
			elQ.fadeOut('slow', function () {
				if (++i > images.length - 1) {
					i = 0;
				}
				imgQ.get(0).src = images[i] + '?rnd='+Math.random();
			});
			setTimeout(nextImage, 4500);
		}

		imgQ.load(showImage)
		nextImage();
	}
}

function screenshotsGalleryBehaviour() {
	var gallery = byid('screenshots');
	if (!gallery) {
		return;
	}
	
	$('a', gallery).lightBox({
		fixedNavigation:false
	});
}


function subscriptionFormBehaviour () {
	if (!byid('subscribtion')) {
		return;
	}

	var initialValue = 'E-mail address';
	var emailQ = $('#subscribtion input[name="email"]');
	var email = emailQ.get(0);
	email.value = initialValue;
	
	emailQ.focus(function () {
		if (email.value == initialValue) {
			email.value = '';
		}
	});
	emailQ.blur(function () {
		if (!email.value.length) {
			email.setAttribute('value', initialValue);
			email.value == initialValue;
		}
	});
	emailQ.keypress(function (ev) {
		if (ev.which == 13) {
			setTimeout(function () {
				$.ajax({
					url: '/subscribe.php',
					type: 'POST',
					data: {email: email.value},
					success: onSuccess 
				});
			}, 50);
		}	
	});
	
	function onSuccess (text, status) {
		email.readOnly = true;
		email.value = text;
		emailQ.css({
			background: 'gold',
			color:'#00070C'
		});
	}
}