;var ca_module = ca_module || {};
(function($){
    ca_module = {
        init: function() {
            this.bind();
        },

        bind: function() {
            var _this = this,
                $topic = $("#topic"),
                $subTopic = $("#subtopic");

            // list page
            $topic.change(function(){
                $subTopic
                    .empty()
                    .append('<option value="">' + i18nMessages['modules.culturalagenda.search.topicAny'] + '</option>');
                if ($topic.val()) {
                    var topicId = parseInt($topic.val(),10);
                    if(!topicId){
                        return;
                    }

                    // TODO: should this be reverse-routed?
                    var route = "/culturalagenda/" + ca_slug + "/user/subtopics/?topicId=" + topicId;
                    $.getJSON(route, function(data) {

                        $.each(data, function(){
                            $subTopic.append('<option value="' + this.id + '">' + this.label + '</option>');
                        })

                    });
                }
            });

            // Show page

            // Google map link
            $(".ca_popupMap").click(function(e){
                e.preventDefault();
                _this.popupMap();
            });

            $("#ca-orgEmail, #ca-bookEmail").click(function(e){
                e.preventDefault();
                _this.mail($(this).attr('href'));
            });
        },

        // Popup google map in dialog
        popupMap: function() {
          var $position = $('[itemtype="http://schema.org/Place"]');
          if ($position.length > 0) {
            // Retrieve event location
            var latitude = $position.find('[itemprop="latitude"]').attr('content');
            var longitude = $position.find('[itemprop="longitude"]').attr('content');
            var containerElt = document.createElement('div');
            // Create a map placeholder HTML element
            var mapElt = document.createElement('div');
            mapElt.style.cssText = 'width: 425px; height: 350px;';
            var target = new google.maps.LatLng(latitude, longitude);
            // Create the google map
            var map = new google.maps.Map(mapElt, {
              zoom: 16,
              center: target,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            // Put a pin on the event location
            new google.maps.Marker({
              position: target,
              map: map
            });
            containerElt.appendChild(mapElt);
            var link = document.createElement('a');
            link.href = 'http://maps.google.fr?q=' + target.toString();
            link.textContent = 'Agrandir le plan';
            link.target = '_blank';
            link.style.cssText = 'float: right; font-size: 14px; font-weight: bold; padding-top: 5px;';
            containerElt.appendChild(link);
            // Show the map
            $.fancybox({ content: containerElt });
          } else {
            // TODO show an error message? (location not found)
          }
        },

        mail: function(url) {
            $.fancybox({
                href: url,
                scrolling: 'no'
            });
        }
    };

    $(function () {
        ca_module.init();
        yepnope([
            '/public/stylesheets/modules/culturalagenda/culturalagenda.css',
            '/public/javascripts/modules/culturalagenda/form.js',
            '/public/javascripts/modules/culturalagenda/location/geolocation.js',
            {
              test: google && google.maps,
              nope: 'http://maps.googleapis.com/maps/api/js?sensor=false'
            }
        ]);
    });
})(jQuery);
