﻿window.onblur = function() { window.blurred = true; };
window.onfocus = function() { window.blurred = false; };

var slideShow =
{
	init: function ()
	{
		this.time = '7000';
		this.setList();
		
		if (this.countList > '0')
		{
			this.actualIndex = this.countList - '1';
			this.showItem(this.actualIndex, true);
			
			if (this.countList > '1')
			{
				this.setTimer();
				
				$('.arrowRight').click
				(
					function ()
					{
						slideShow.showNextItem();
					}
				);
				
				$('.arrowLeft').click
				(
					function ()
					{
						slideShow.showPrevItem();
					}
				);
			}
		}
	},
	setList: function ()
	{
		this.list = new Array();
		
		$('#slideshow div.panel').each
		(
			function (index)
			{
				var page_id = $(this).attr('id').substr('1');
				slideShow.list[index] =
				{
					index: index,
					page_id: page_id,
					title: $(this).find('.itemInfo').html(),
					anotation: $(this).find('div.itemPrice').html(),
					url: $('#f' + page_id).find('a').attr('href'),
					thumb: $(this).find('img').attr('src'),
					img: $('#f' + page_id).find('a').find('img').attr('src')
				};
				
				$(this).hover
				(
					function ()
					{
						slideShow.showItem(index);
						clearTimeout(slideShow.timer);
					},
					function ()
					{
						slideShow.timer = setTimeout('slideShow.showNextItem();', slideShow.time);
					}
				);
			}
		);
		
		this.countList = this.list.length;
	},
	showItem: function (index, start)
	{
		if (this.actualIndex != index || start == true)
		{
			$('#slideshow .panel').removeClass('showing');
			$('#hpBanner .hpBox .title').html(this.list[index].title);
			$('#hpBanner .hpBox .itemPrice').html(this.list[index].anotation);
			$('#slideshow .filmstrip img.show').fadeOut('slow').removeClass('show');
			$('#f' + this.list[index].page_id + ' img').fadeIn('slow').addClass('show');
			$('#p' + this.list[index].page_id).addClass('showing');
			
			this.actualIndex = index;
		}
	},
	showNextItem: function ()
	{
		clearTimeout(slideShow.timer);
		
		if ( window.blurred ) { return; }
		
		var index = this.actualIndex - '1';
		if (index < '0')
		{
			index = this.countList - '1';
		}
		
		this.showItem(index, true);
		
		this.timer = setTimeout('slideShow.showNextItem();', slideShow.time);
	},
	showPrevItem: function ()
	{
		clearTimeout(slideShow.timer);
		
		var index = this.actualIndex + 1;
		
		if (index == this.countList)
		{
			index = 0;
		}
		
		this.showItem(index, true);
		
		this.timer = setTimeout('slideShow.showNextItem();', slideShow.time);
	},
	setTimer: function ()
	{
		this.timer = setTimeout('slideShow.showNextItem();', slideShow.time);
	}
}

$(document).ready(function () {
	
	$('#basketFormInfo').submit
	(

		function()
		{
			var submit = true;

			if (submit && $('#to_first_name').val() == '')
			{
				alert('Vyplňte prosím své jméno.');
				$('#to_first_name').focus();
				submit = false;
			}

			if (submit && $('#to_surname').val() == '')
			{
				alert('Vyplňte prosím své příjmení.');
				$('#to_surname').focus();
				submit = false;
			}

			if (submit && $('#e_mail').val() == '')
			{
				alert('Vyplňte prosím svůj e-mail.');
				$('#e_mail').focus();
				submit = false;
			}
			
			if(submit && !checkEmailAddress($('#e_mail').val()))
			{
				alert('Zadaný e-mail není platný.');
				$('#e_mail').focus();
				submit = false;
			}

			if(submit && $('#phone').val() == '')
			{
				alert('Musíte vyplnit telefon.');
				$('#phone').focus();
				submit = false;
			}
			
			if (submit && $('#cislo_op').val() == '')
			{
				alert('Vyplňte prosím číslo svého občanského průkazu.');
				$('#cislo_op').focus();
				submit = false;
			}
			
			if (submit && $('#to_street').val() == '')
			{
				alert('Vyplňte prosím ulici a číslo popisné.');
				$('#to_street').focus();
				submit = false;
			}

			if (submit && $('#to_zip').val() == '')
			{
				alert('Vyplňte prosím PSČ.');
				$('#to_zip').focus();
				submit = false;
			}
			
			if (submit && $('#to_city').val() == '')
			{
				alert('Vyplňte prosím město.');
				$('#to_city').focus();
				submit = false;
			}
			
			return submit;
		}
	);
	
	$(".colorbox").colorbox();
	
	slideShow.init();
	
});

function checkEmailAddress(email)
{
	var re = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	if (!re.test(email))
	{
		return false;
	}
	else
	{
		return true;
	}
}

