(function($) {
  $(document).ready(function() {
  
    if($("#subForm").length){
      // adds an effect called "wall" to the validator
      $.tools.validator.addEffect("wall", function(errors, event) {
        // get the message wall
        var wall = $(this.getConf().container).fadeIn();
        // remove all existing messages
        wall.find("li").remove();
        listWall = wall.find('ul');
        // add new ones
        $.each(errors, function(index, error) {
          name = "#"+error.input[0].id;
          label = $(name).parent().find('label');
          label.find('span').remove();
          listWall.append(
            "<li><strong>" +label.html()+ "</strong> " +error.messages[0]+ "</li>"
          );    
        });
        
      // the effect does nothing when all inputs are valid  
      }, function(inputs)  {
        
      });
      $("#subForm").validator({
         effect: 'wall', 
         container: '#subForm-errors',
         // do not validate inputs when they are edited
         errorInputEvent: null        
      });
    }
      
    if($("#stop-subscription-form").length) {
      // adds an effect called "wall" to the validator
      $.tools.validator.addEffect("wall", function(errors, event) {
        // get the message wall
        var wall = $(this.getConf().container).fadeIn();
        // remove all existing messages
        wall.find("li").remove();
        listWall = wall.find('ul');
        // add new ones
        $.each(errors, function(index, error) {
          name = "#"+error.input[0].id;
          label = $(name).parent().find('label');
          label.find('span').remove();
          listWall.append(
            "<li><strong>" +label.html()+ "</strong> " +error.messages[0]+ "</li>"
          );    
        });
        
      // the effect does nothing when all inputs are valid  
      }, function(inputs)  {
        
      });
      $("#stop-subscription-form").validator({
         effect: 'wall', 
         container: '#subForm-errors',
         // do not validate inputs when they are edited
         errorInputEvent: null        
      });
    } 
    
    /* Delete black bloc if empty */
    if($('#sub-navigation').length){
      if($("#sub-navigation").html() == ""){
        /*$("#sub-navigation").remove();*/
      }
    }
    
    if( $('.ui-select').length ){
      $('.ui-select h2').click(function(){
        $(this).parent().toggleClass('open');
      });
    }
    
    if( $('#gmap').length ){
      initGmap();
    }
    
    
    function initGmap(){
       // getting all the information to initialize the gmap
        var latitude    = 49.61174;
        var latitude2   = 49.611082;
        var longitude   = 6.119866;
        var longitude2  = 6.119251;
        var maptitle    = 'Elvinger, Hoss & Prussen';
        var address      = '2, Place Winston Churchill <br /> BP 425 <br />L-2014 Luxembourg';
        var address2     = '5, Place Winston Churchill <br /> BP 425 <br />L-2014 Luxembourg';
        
        
        var infoTitle = '<h2>'+maptitle+'</h2>';
        //var infoImage = container.attr('data-image');
        var infoAddress  = '<p class="address">'+address+'</p>';
        var infoAddress2 = '<p class="address">'+address2+'</p>';
        
        var info        = '<div class="bubble-map">'+infoTitle+infoAddress+'</div>';
        var info2       = '<div class="bubble-map">'+infoTitle+infoAddress2+'</div>';
        
        // query Google Map
        var latlng  = new google.maps.LatLng(latitude, longitude);
        var latlng2 = new google.maps.LatLng(latitude2, longitude2);
        var myOptions = {
          zoom: 16,
          center: latlng,   
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          streetViewControl: false
        };

        var map = new google.maps.Map(document.getElementById('gmap'), myOptions);
        var infowindow = new google.maps.InfoWindow({
                content: info
        });       
         var infowindow2 = new google.maps.InfoWindow({
                content: info2
        });
        
        var marker = new google.maps.Marker({
            position: latlng, 
            map: map,
            title: maptitle
        }); 
        var marker2 = new google.maps.Marker({
            position: latlng2, 
            map: map,
            title: maptitle
        });  
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        }); 
        google.maps.event.addListener(marker2, 'click', function() {
          infowindow2.open(map,marker2);
        }); 
               
        infowindow.open(map,marker);
        map.panBy( 0, -100  ); 
    }

    
    if( $('#staff-name').length ){
      var strorageId = $('#staff-name').attr('data-pid');
      var searchForm = $('#staff-name').parents('form')
      
      
      
      var formAction = searchForm.attr('action');
      
      
       $('#staff-name').autocomplete({
        source: function( request, response ) {
          $.ajax ({
            url: "index.php",
            dataType : "json",
            data: {
              eID: "ehpstaff",
              pid: strorageId,
              term: request.term
            },
            success: function(data) {
              if( data.length){
                response( $.map( data, function( item ) {
                  return {
                    label: item.fullname,
                    value: item.fullname,
                    uid: item.uid
                  }
                }));           
              } else {
                $('#staff-id').val('');
              }   
            }
          });
        },
        delay: 500,
        select: function( event, ui ) {
          $('#staff-id').val(ui.item.uid)
        }
       }); 
    }
      
  });
})(jQuery);
