$(document).ready(function() {	

  //Get the screen height and width
  var maskHeight = $(window).height();
  var maskWidth = $(window).width();
  
  //Set height and width to mask to fill up the whole screen
  $('#mask').css({'width':maskWidth,'height':maskHeight});



	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(window).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
//     $('#mask').fadeIn(400);	
		$('#mask').fadeTo("slow",0.5);	
	
		//Get the window height and width
// 		var winH = $(window).height();
// 		var winW = $(window).width();
              
		//Set the popup window to center
// 		$(id).css('top',  winH/2-$(id).height()/2);
// 		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$(id).fadeIn(500); 
	
	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
		$('.message_red').hide();
		$('.message').hide();
	});

  //if .message_red is ready
	$('.message_red').ready(function (e) {
	if ($('.message_red').length>0) { 
      //Get the screen height and width
  		var maskHeight = $(document).height();
  		var maskWidth = $(window).width();
  
  		//Set height and width to mask to fill up the whole screen
  		$('#mask').css({'width':maskWidth,'height':maskHeight});
  
  		//transition effect		
  		$('#mask').fadeTo("slow",0.5);	
  
  		$('.message_red').fadeIn(500);		
		}
	});
 
   //if .message is ready
	$('.message').ready(function (e) {
	if ($('.message').length>0) { 
      //Get the screen height and width
  		var maskHeight = $(document).height();
  		var maskWidth = $(window).width();
  
  		//Set height and width to mask to fill up the whole screen
  		$('#mask').css({'width':maskWidth,'height':maskHeight});
  
  		//transition effect		
  		$('#mask').fadeTo("slow",0.5);	
  
  		$('.message').fadeIn(500);		
		}
	});

  // nahrazení chybových hlášení 
	$('.message_red, .message').ready(function (e) {
    if ($('.message_red').length>0 || $('.message').length>0)
    {
      //Get the screen height and width
      var maskHeight = $(document).height();
      var maskWidth = $(window).width();
  
      //Set height and width to mask to fill up the whole screen
      $('#mask').css({'width':maskWidth,'height':maskHeight});
      
      //transition effect
      $('#mask').fadeTo("slow",0.5);
  
      // setting message content
      var message;
  
      if($('.message_red').html() != null)
        var message = $('.message_red').html();
  
      if($('.message').html() != null)
        var message = $('.message').html();
  
      // replacement
      $('.message_red, .message').replaceWith('<div class="window" id="message">' +
                                                 '<table cellspacing="0" cellpadding="0" border="0">' +
                                                        '<tr><td class="tl"></td><td class="b"></td><td class="tr"></td></tr>' +
                                                        '<tr>' +
                                                          '<td class="b"></td>' +
                                                          '<td class="white_bg"><div class="window_content">' + message + '<a class="close" href="#"><img src="./images/cancel.png" alt="Zavřít" /></a>'+
                                                          '</div></td>' +
                                                          '<td class="b"></td>' +
                                                        '</tr>' +
                                                        '<tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr>' +
                                                 '</table>' +
                                              '</div>');
  		$('#message').fadeTo("slow",1);
  		
    	//if close button is clicked
    	$('.window .close').click(function (e) {
    		//Cancel the link behavior
    		e.preventDefault();
    		$('#mask, .window').hide();
    	});

  	}
	});

	//if #modal_close is clicked
  $('.window #modal_close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $('#mask, .window').hide();
  });

	//if #modal_close is clicked
  $('#dokonceno #modal_close').click(function (e) {
    //Cancel the link behavior
    window.location.replace('./');
  });

	//if dokonceni button is clicked
  $('#dokonceni').click(function (e) {

    var message = $('#message_wait').html();

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    $('#mask').fadeTo("slow",0.5);

    // replacement
    $('#message_wait').replaceWith('<div class="window" id="message_wait">' +
                                                 '<table cellspacing="0" cellpadding="0" border="0">' +
                                                        '<tr><td class="tl"></td><td class="b"></td><td class="tr"></td></tr>' +
                                                        '<tr>' +
                                                          '<td class="b"></td>' +
                                                          '<td class="white_bg"><div class="window_content">' + message + ''+
                                                          '</div></td>' +
                                                          '<td class="b"></td>' +
                                                        '</tr>' +
                                                        '<tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr>' +
                                                 '</table>' +
                                              '</div>');
  	$('#message_wait').fadeTo("slow",1);
  });
  
	//if #modal_close is clicked
  $('.button').click(function (e) {
    //Cancel the link behavior
    $('.window').hide();
    $('#mask').hide();
  });

});

  function showWait()
  {
      // replacement
      $('#message_wait').replaceWith('<div class="window" id="message_wait">' +
                                                   '<table cellspacing="0" cellpadding="0" border="0">' +
                                                          '<tr><td class="tl"></td><td class="b"></td><td class="tr"></td></tr>' +
                                                          '<tr>' +
                                                            '<td class="b"></td>' +
                                                            '<td class="white_bg"><div class="window_content"><span class="center"><img src="./images/loader.gif" alt"" /> Chvilku strpení</span>'+
                                                            '</div></td>' +
                                                            '<td class="b"></td>' +
                                                          '</tr>' +
                                                          '<tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr>' +
                                                   '</table>' +
                                                '</div>');
    	$('#message_wait').fadeTo("slow",1);
  }
