$(document).ready(function(){
  
	$('input[name="jmeno"].focus').focus(function() { if($(this).val()=='Vaše jméno') $(this).val(''); });
	$('input[name="jmeno"].focus').blur(function() { if($(this).val()=='') $(this).val('Vaše jméno'); });

	$('input[name="mail"].focus').focus(function() { if($(this).val()=='Váš email') $(this).val(''); });
	$('input[name="mail"].focus').blur(function() {	if($(this).val()=='') $(this).val('Váš email'); });
	
	$('textarea[name="zprava"].focus').focus(function() { if($(this).val()=='Prostor pro Váš dotaz') $(this).val(''); });
	$('textarea[name="zprava"].focus').blur(function() { if($(this).val()=='') $(this).val('Prostor pro Váš dotaz'); });

	$('input[name="newsmail"].focus').focus(function() { if($(this).val()=='E-mail příjemce') $(this).val(''); });
	$('input[name="newsmail"].focus').blur(function() { if($(this).val()=='') $(this).val('E-mail příjemce'); });
	
	$('input[name="tel"].focus').focus(function() { if($(this).val()=='Váš telefon') $(this).val(''); });
	$('input[name="tel"].focus').blur(function() { if($(this).val()=='') $(this).val('Váš telefon'); });
	
	$('input[name="web"].focus').focus(function() { if($(this).val()=='Váš aktuální web') $(this).val(''); });
	$('input[name="web"].focus').blur(function() { if($(this).val()=='') $(this).val('Váš aktuální web'); });
	
	$("form#kontakt, form#kontakt2").submit(function() {
		
		if($('input[name="jmeno"]', this).val()=='' || $('input[name="jmeno"]', this).val()=='Vaše jméno'){
			alert('Zadejte prosím Vaše jméno.');
			$('input[name="jmeno"]', this).focus();
			return false;
		}
		
		if($('input[name="mail"]', this).val()=='' || $('input[name="mail"]', this).val()=='Váš email'){
			alert('Zadejte prosím Váš e-mail.');
			$('input[name="mail"]', this).focus();
			return false;
		}
		
		if($('textarea[name="zprava"]', this).val()=='' || $('textarea[name="zprava"]', this).val()=='Prostor pro Váš dotaz'){
			alert('Zadejte prosím text zprávy.');
			$('textarea[name="zprava"]', this).focus();
			return false;
		}
	});
	
	$("form#newsletter").submit(function() {
		if($('input[name="newsmail"]', this).val()=='' || !isEmail($('input[name="newsmail"]', this).val())){
			alert('Zadejte prosím správně Váš e-mail.');
			$('input[name="newsmail"]', this).focus();
			return false;
		}
	});

	/* HELP */
        var xOffset2 = 0; var yOffset2 = 35;
        $(".help").hover(function(e) { 
            $("<div id=\"help-popup\"></div>").appendTo(document.body);

             $("#help-popup")
				.css("top",(e.pageY - xOffset2) + "px")
				.css("left",(e.pageX + yOffset2) + "px")
                .fadeIn("fast");

            $('#help-popup').append($(this).attr('title'))
				.css("left",(e.pageX - ($("#help-popup").width()/2) - xOffset2) + "px")
                .css("top",(e.pageY - $("#help-popup").height() - yOffset2 - 10) + "px");

        },function(){
            $("#help-popup").remove();
        });

        // Presun okna
        $(".help").mousemove(function(e){
            var kompenzace = 0;
            if((e.pageX + ($("#help-popup").width()/2) + xOffset2) >= $("body").width()) {
                 kompenzace = $("body").width() - (e.pageX + ($("#help-popup").width()/2) + xOffset2);
            }
            $("#help-popup")
	  	.css("left",(e.pageX - ($("#help-popup").width()/2) - xOffset2 + kompenzace) + "px")
	  	.css("top",(e.pageY - $("#help-popup").height() - yOffset2 - 10) + "px");
	});
	
});

function isEmail(s)
{
	s += "";
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
	var r2 = new RegExp("^[a-zA-Z0-9\\-\\.]+\\@[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}$");
	if (r1.test(s) || !r2.test(s))
		return false;
	return true;
}
