$(document).ready(function() {	

  //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});

	//select all the a tag with name equal to modal
	$('a.out_of_stock').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 = $(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').css({'display':'block'});
		$('#mask').fadeTo("slow",0.5);
		
		// nastavení předmětu
		$('#predmet').attr('value',id);

		//transition effect
    $('#message').css({'display':'block'});

    // odrolování na header - pro zobrazení formuláře na obrazovce v případě že má uživatel odrolávno dolů
    $.scrollTo('#header');    
	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});

//    //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="#"></a>'+
                                                          '<br />' +
                                                          '</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();
    	});

  	}
	});
	
  // nahrazení chybových hlášení 
	$('.message_error').ready(function (e) {
    if ($('.message_error').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').css({'display':'block'});
      $('#mask').fadeTo("slow",0.5);
  
      // setting message content
      var message;
  
      if($('.message_error').html() != null)
        var message = $('.message_error').html();
  
      // replacement
      $('.message_error').replaceWith('<div class="window" id="message_error">' +
                                                 '<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="#"></a>'+
                                                          '<br />' +
                                                          '</div></td>' +
                                                          '<td class="b"></td>' +
                                                        '</tr>' +
                                                        '<tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr>' +
                                                 '</table>' +
                                              '</div>');
      //transition effect
      $('#message_error').css({'display':'block'});

      // odrolování na header - pro zobrazení formuláře na obrazovce v případě že má uživatel odrolávno dolů
      $.scrollTo('#header');
    
    	//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('./');
  });
});
