/* $Id: tygryski.js 10 2009-09-30 10:15:33Z choman $ */

$(document).ready(function() {

	function start_slideshow(id, start_frame, end_frame, delay, speed) {
		setTimeout(switch_slides(id, start_frame,start_frame,end_frame, delay, speed), delay);
	}
	                        
	function switch_slides(id, frame, start_frame, end_frame, delay, speed) {
		return (function() {
			setTimeout("$('#" + id + frame + "').fadeOut(" + speed + ")");
			if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
			setTimeout("$('#" + id + frame + "').fadeIn(" + speed + ")");
			setTimeout(switch_slides(id, frame, start_frame, end_frame, delay, speed), delay + speed);
		})
	}
	
	start_slideshow('slide', 1, 3, 6000, 2000);

	$('.imagesList a').lightBox({
		imageLoading: _tpl_root + 'img/lightbox-ico-loading.gif', // (string) Path and the name of the loading icon
		imageBtnPrev: _tpl_root + 'img/lightbox-btn-prev.gif', // (string) Path and the name of the prev button image
		imageBtnNext: _tpl_root + 'img/lightbox-btn-next.gif', // (string) Path and the name of the next button image
		imageBtnClose: _tpl_root + 'img/lightbox-btn-close.gif', // (string) Path and the name of the close btn
		imageBlank: _tpl_root + 'img/lightbox-blank.gif'
	});
	
	$('#contact-form').submit(function(ev) {
		function setError(f, msg) {
			$('#req-' + f).text(msg);
			$('#req-' + f).closest('li').addClass('error');
		}
		
		function clearError(f) {
			$('#req-' + f).text('*');
			$('#req-' + f).closest('li').removeClass('error');
		}
		
		function validateField(f, min_l, e_msg, s_msg) {
			var s = $('#' + f)[0].value;
			if (s == '') {
				setError(f, e_msg);
				return false;
			}
			if (s.length < min_l) {
				setError(f, s_msg);
				return false;
			}
			clearError(f);
			return true;
		}

		var b = false;
		if ($('#email2').length > 0) {
			var b = validateField('name', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('email', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('email2', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('msg', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('result', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		} else {
			var b = validateField('name', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('phone', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('msg', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
			b &= validateField('result', 0, '* to pole jest wymagane', '* zbyt krótkie imię i nazwisko');
		}	
		
		if (b) {
			return;
		}
		
		ev.preventDefault();
		
	});
	
	function initLinks(elems) {
		$(elems).attr('origsrc', function(attr) {
			return $(this).attr('src');
		});
		$(elems).hover(function() {
			$(this).attr('src', $(this).attr('origsrc').replace(/\.png/, 'h.png'));
		},
		function() {
			$(this).attr('src', $(this).attr('origsrc'));
		});
	}
	
	initLinks($('#button-menu img'));
	initLinks($('#tab-menu img'));

});

