$(document).ready(function(){

      $('#send_message').click(function(e){
            
            //stop the form from being submitted
            e.preventDefault();
            
            /* declare the variables */
            var error = false;
            var email = $('#email').val();
            var subject = $('#subject').val();
            var message = $('#message').val();
            
            /* in the next section we do the checking by using VARIABLE.length */
            if(email.length == 0 || email.indexOf('@') == '-1'){
                var error = true;
                $('#email_error').fadeIn(500);
            }else{
                $('#email_error').fadeOut(500);
            }
            if(subject.length == 0){
                var error = true;
                $('#subject_error').fadeIn(500);
            }else{
                $('#subject_error').fadeOut(500);
            }
            if(message.length == 0){
                var error = true;
                $('#message_error').fadeIn(500);
            }else{
                $('#message_error').fadeOut(500);
            }
            
            //now when the validation is done we check if the error variable is false (no errors)
            if(error == false){
                //disable the submit button to avoid spamming
                //and change the button text to Sending...
                $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
                
                /* using the jquery's post(ajax) function and a lifesaver
                function serialize() which gets all the data from the form
                we submit it to odeslani-formulare.php */
                $.post("odeslani-formulare.php", $("#contact_form").serialize(),function(result){
                    //and after the ajax request ends we check the text returned
                    if(result == 'sent'){
                        //if the mail is sent remove the submit paragraph
                         $('#cf_submit_p').remove();
                        //and show the mail success div with fadeIn
                        $('#mail_success').fadeIn(500);
                    }else{
                        //show the mail failed div
                        $('#mail_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#send_message').removeAttr('disabled').attr('value', 'odeslat');
                    }
                });
            }
        });    

    $(".clearField").clearField({
    });

    $("#slider").easySlider({
      auto: true,
      pause: 4000,
      continuous: true,
       speed: 400
    });

     $("a#[rel=obrazky]").fancybox({
      'showCloseButton'	: true,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'none',
      'speedIn'         : 200,
      'speedOut'        : 200,
      'changeSpeed'     : 200,
      'changeFade'      : 'fast',
      'overlayOpacity'  : 0.8,
      'overlayColor'    : '#000',
      'titlePosition' 	: 'over',
      'titleFormat'     : function(title, currentArray, currentIndex, currentOpts) {
      return '<span id="fancybox-title-over"><span>' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' | </span>' + title : '') + '</span>';
				}
      });

			$("#flashnonox").fancybox({
      'autoScale'     	: false,
      'width'           : 888,
      'height'          : 646,
      'showNavArrows'  	: false,
		  'type'				    : 'iframe',

      'showCloseButton'	: true,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'none',
      'speedIn'         : 200,
      'speedOut'        : 200,
      'changeSpeed'     : 200,
      'changeFade'      : 'fast',
      'overlayOpacity'  : 0.8,
      'overlayColor'    : '#000',
      'titlePosition' 	: 'over',
      'titleFormat'     : function(title, currentArray, currentIndex, currentOpts) {
      return '<span id="fancybox-title-over"><span>' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' | </span>' + title : '') + '</span>';
				}
      });

  });
