var APT = window.APT || {};

// Hide/Show contact box
APT.Contact = (function() {
  return {
    init: function() {
      $('#contact-us').click(function(e) {
        e.preventDefault();
        if ($('#contact').is(':hidden')) {
          $(this).addClass('close-contact-us');
          $(this).text('Close');
        } else {
          $(this).removeClass('close-contact-us');
          $(this).text('Contact us');
        }
        $('#contact').slideToggle();
      });
    }
  }
})();


APT.Accordion = (function() {
  return {
    init: function() {
      $('#questions .question a').click(function(e) {
        e.preventDefault();
        var answer = $(this).parent().siblings('.answer');
        var question = $(this);
        if (answer.is(':hidden')) {
          $(this).children('span').fadeOut('slow', function() {
            question.removeClass('open').addClass('close');
            $(this).fadeIn('slow');
          });
        } else {
          $(this).children('span').fadeOut('slow', function() {
            question.removeClass('close').addClass('open');
            $(this).fadeIn('slow');
          });
        }
        answer.slideToggle();
      });
    }
  }
})();


APT.Places = (function() {
  return {
    init: function() {
      // $('.opening-times .button').click(function(e) {
      //   e.preventDefault();
      //   var width = $('.panel').width();
      //   $(this).parents('.clinic').animate({'margin-left': '-' + width + 'px'}, 'slow');
      //   // $(this).find('.clinic').animate({'margin-left': '-' + width + 'px'}, 'slow');
      //   // $(this).siblings('.answer').slideToggle();
      // });
      var width = $('.panel').width();
      $('#places li .place').each(function() {
        var opening_times = $(this).find('.opening-times .button');
        var place = $(this);
        opening_times.click(function(e) {
          e.preventDefault();
          place.animate({'margin-left': '-' + width + 'px'}, 'slow');
        })
        var back = $(this).find('.back .button');
        back.click(function(e) {
          e.preventDefault();
          place.animate({'margin-left': 0}, 'slow');
        })
      });
    }
  }
})();

APT.Map = (function() {
  // The callback from loading the map api
  window.map = function() {
    var zoom_level = 16;
    for (var i = 0; i < map_data.length; i++) {
      if (map_data[i].lat && map_data[i].lon) {
        if (map_data[i].zoom) {
          zoom_level = map_data[i].zoom;
        }
        var options = {
          zoom: parseInt(zoom_level),
          center: new google.maps.LatLng(map_data[i].lat, map_data[i].lon),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById(map_data[i].id), options);
        if (map_data[i].marker_lat && map_data[i].lon) {
          var location = new google.maps.LatLng(map_data[i].marker_lat, map_data[i].marker_lon);
          var marker = new google.maps.Marker({
            position: location,
            map: map,
            title: map_data[i].marker_title
          });
        }
      }
    };
  }

  return {
    init: function() {
      if ($('#places').length) {
        $.getScript('http://maps.googleapis.com/maps/api/js?sensor=false&callback=map');
      }
    }
  }
})();


APT.BackToTop = (function() {
  return {
    init: function() {
      $('a[href="#top"]').click(function(e) {
        e.preventDefault();
        var target = $(this).attr('href');
        $('html, body').animate({ scrollTop: 0 }, 'slow', function() {
          location.hash = target;
        });
      });
    }
  }
})();


$(document).ready(function() {
  APT.Contact.init();
  APT.Accordion.init();
  APT.Places.init();
  APT.Map.init();
  APT.BackToTop.init();
});
