// Global page slider variables
var animateTimeout = 15; // animation delay
var scrolling = false;
var scroll = 0;
var timeInterval;

$(document).ready(function() {
	$("#terminy_select").change(function() {
		$("#terminy_form").submit();
	});
	$("#terminy_submit").hide();
    // Lightbox
    $("a.lightbox").lightbox({
		fileLoadingImage : 'http://www.morezajezdu.cz/images/loading.gif',
		fileBottomNavCloseImage : 'http://www.morezajezdu.cz/images/closelabel.gif',
		fitToScreen: true
	});
    // $(".pocasi .detaily").hide();
/*	$("table.pocasi tr.destinace").mouseover(function() {
		$(this).next("tr.detaily").show();
	}).mouseout(function() {
		$(this).next("tr.detaily").hide();
	}); */
	$("table.pocasi tr.destinace a.detaily").click(function() {
		$("table.pocasi tr.detaily").hide();
		$(this).closest("tr.destinace").next("tr.detaily").show();
		return false;
	});
	$("ul.nejzadanejsi li").mouseover(function() {
		$(this).css("cursor", "pointer");
		$(this).find("a").css("text-decoration", "underline");
	}).mouseout(function() {
		$(this).css("cursor", "");
		$(this).find("a").css("text-decoration", "");
	}).click(function() {
		window.location = $(this).find("a").attr("href");
	});
    // Photo slider 
    $('#next1').mouseover(function() {
		scroll = 1;
		if (!scrolling) {
	   		incrementPosition();           	           
		}
  	});
  	$('#next1').mouseout(function() {
		scroll = 0;
  	});
   	$('#prev1').mouseover(function() {
		scroll = 2;
		if (!scrolling) {
	   		decrementPosition();          
		}
  	});
  	$('#prev1').mouseout(function() {
		scroll = 0;
  	});
  	
  	// animate image	
  	function movementAction(scroll, timeout) {
		scrolling = true;
		$('#scrollable').animate({scrollLeft: '+=' + scroll + 'px'}, timeout);
    }

    function scrollingTimeout() {
	scrolling = false;
	switch (scroll) {
	case 1:
		incrementPosition();
		break;
	case 2:
		decrementPosition();
		break;
	}
    }

    // set next image
    function incrementPosition() {
	var images = $('#scrollable img');
	var width = $('#scrollable').width();
	for (var i = 0; i < images.length; i++) {
		var image = images.eq(i);
		var right = image.position().left + image.width();
		if (right > width) {
			var timeout = (right - width) * animateTimeout;
			movementAction(right - width, timeout);
        		timeInterval = setTimeout(scrollingTimeout, timeout);
			break;
		}
	}
    }

    // set previev image
    function decrementPosition() {
	var images = $('#scrollable img');
	for (var i = images.length - 1; i >= 0; i--) {
		var image = images.eq(i);
		var left = image.position().left;
		if (left < 0) {
			var timeout = -left * animateTimeout;
			movementAction(left, timeout);
        		timeInterval = setTimeout(scrollingTimeout, timeout);
			break;
		}
	}
    }
});

function checkOrder() {
	var e = document.forms['order'].elements;

	if (e['jmeno'].value == "") {
		alert("Zadejte prosím jméno!");
		e['jmeno'].focus();
		return false;
	}
	if (e['prijmeni'].value == "") {
		alert("Zadejte prosím příjmení!");
		e['prijmeni'].focus();
		return false;
	}
	if (e['telefon'].value == "") {
		alert("Zadejte prosím vaše telefonní číslo!");
		e['telefon'].focus();
		return false;
	}
	if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(e['email'].value)) {
		alert("Neplatný e-mail! Zadejte prosím znovu váš e-mail.");
		e['email'].focus();
		return false;
	}
	return true;
}
