$(function(){
   // ajax p2ringute seaded
   $.ajaxSetup({
     cache: false,
     timeout: 10000, //10s
     error:function(x,e){
      var msg;
      if (x === null){
            msg = 'no connection?\n';
      } else {
            if(x.status==0){
                    msg = 'You are offline!\nPlease check your network connection.';
            }else if(x.status==404){
                    msg = 'Requested URL not found.';
            }else if(x.status==500){
                    msg = 'Internal Server Error.';
            }else if(e=='parsererror'){
                    msg = 'Error.\nParsing JSON Request failed.';
            }else if(e=='timeout'){
                    msg = 'Request time out.\nPlease try again.';
            }else {
                    msg = 'Unknown error.\n'+x.responseText;
            }
      }
      alert(msg);
    }
   });
   $("#register_users").click(function(e){ 
      e.preventDefault();
      $.ajax({
         type: "GET",
         url: $(this).attr('href'),
         data: '_ajax=content',
         success: function( data ){
             popup_layer( data );	
         }
      });
   });
   //esilehe lisamise link ET
   var hash = self.document.location.hash.substring(1);
   if (hash == "register") {
      $("#register_users").click();
   }
});
function form_submit (id) {
   $("#"+id).submit(function() {
      var counter = 0;
      $(".required").each(function(){
         if( $(this).attr("type") == "select-one"){
            if ( $(this).val() == '') {
               $(this).addClass('error');
               counter++;
            } else {
               $(this).removeClass('error');
            }
         } else {
            if ( $(this).val() == '') {
               $(this).parents(".label").addClass('error');
               counter++;
            } else {
               $(this).parents(".label").removeClass('error');
            }
         }
      });
      if ( counter == 0 ) {
         $("#send_button").hide();
         $("#error_txt").html('');
         var formData = $(this).serialize();
            formData += '&_ajax=content';
            $.ajax({
               type: "POST",
               url: $(this).attr('action'),
               data: formData,
               success: function(msg){
                  popup_layer( msg );
               }
             });
         return false;
      } else {
         $("#error_txt").html('*teil on täitmata punasega märgitud väljad.'); 
         return false;
      }
   });
}

function popup_layer( data ){
$(".calendar_popup, .opaque").remove();
var output =	'<div class="calendar_popup">'+
         '<div class="top"><a href="#" class="close_popup">&nbsp;</a></div>'+
         '<div class="middle">'+data+'</div>'+
         '<div class="bottom">&nbsp;</div>'+
 '</div>'+
 '<div class="opaque">&nbsp;</div>';

                                        
$("body").append( output );
var popup = $(".calendar_popup");
var opaque = $(".opaque");

function positioning( action ){
				
   var left = ($(window).width() - popup.width() )/2;
   var top = $(window).scrollTop() +20;
   popup.css({
           
           left: left,
           top: top
           
   });
   
   if( !action ){
      opaque.removeAttr("style");
      opaque.css({
                   
            width: $(document).width(),
            height: $(document).height(),
            background: "#000",
            opacity: "0.62",
            top:0,
            left:0,
            zIndex: "998",
            position: "absolute",
            cursor: "pointer"
            
    });
   }
};

positioning();

$(window).resize(function(){      
        positioning("scroll");     
});

$(".calendar_popup .close_popup, .opaque").click(function(e){
        
        e.preventDefault();
        popup.remove();
        opaque.remove();
        
});


};
		

