jQuery.noConflict()(function($){
  
  // Contact map initialization --------------------------------------------------------------------------
  if ($("#contactmap").size()) {
  
    // Set initial map      
    $("#contactmap").goMap({ 
    
      mapTypeControl: false,
      maptype:       "ROADMAP",
      
      latitude:      47.371405,
      longitude:     8.5133181, 
      zoom:          13,
      
      markers:       [{ latitude:47.371405, longitude:8.5133181, icon:"/static/img/map/red-dot.png", shadow:"/static/img/map/shadow.png" }]
    }); 
    
  }
  
  // Stores map initialization ---------------------------------------------------------------------------
  if ($("#storemap").size()) {

    // Set initial map      
    $("#storemap").goMap({ 
    
      mapTypeControl: false,
      maptype:       "ROADMAP",
      
      latitude:      47.3690239,
      longitude:     8.5380326, 
      zoom:          13, 
      
      markers:       shops
    });     
    
        
    // Act on search activity
    $("#searchform").submit(function() {
      $.goMap.markTheSpot($("#keywords").val(), 3, { icon:"/static/img/map/yellow-dot.png", shadow:"/static/img/map/shadow.png", html:"<span class='title'>Ihre gesuchte Adresse:</span><br/>"+$("#keywords").val(), clickable:false });
      return false;
    });
    
    
    // Reset default text
    $("#keywords").autoclear({
      defaultValue:$("#keywords").attr("alt"),
      defaultClass:"inputhint"
    });        
    
    
    // Set click handlers for dealers
    $("li.address .title").live("click", function () {
      for (var i=0; i<$.goMap.markers.length; i++) {
        if ($.goMap.markers[i].id == $(this).parent("li").attr("dealer")) {
          $.goMap.trigger($.goMap.markers[i], "click");	  
        }
      }
    });
    
    
    // Define map functions
    $.syncMapAndList = function() {
      var visibleMarkers = $.goMap.getVisibleMarkers();
      var visibleShops   = [];
      var visibleCafes   = [];
      var shophtml       = "";
      var cafehtml       = "";
      
      for (var i=0; i<visibleMarkers.length; i++) {
        if (visibleMarkers[i].title) {
          if (visibleMarkers[i].type == "laden") visibleShops.push(visibleMarkers[i]);
          if (visibleMarkers[i].type == "bar")   visibleCafes.push(visibleMarkers[i]);
        }
      }
      
      visibleShops.sort($.sortDealers);
      visibleCafes.sort($.sortDealers);
      
      for (var i=0; i<visibleShops.length; i++) {
        shophtml += "<li class='address' dealer='" + visibleShops[i].id + "'>" + visibleShops[i].html + "</li>";
      }
      
      for (var i=0; i<visibleCafes.length; i++) {
        cafehtml += "<li class='address' dealer='" + visibleCafes[i].id + "'>" + visibleCafes[i].html + "</li>";
      }
      
      $("#shoplist").html(shophtml);
      $("#cafelist").html(cafehtml);
    }
    
    $.sortDealers = function(a, b) {
      if (a.title.toUpperCase().charCodeAt(0) < b.title.toUpperCase().charCodeAt(0)) {
        return -1;
      } else if (a.title.toUpperCase().charCodeAt(0) < b.title.toUpperCase().charCodeAt(0)) {
        return 1
      } else {
        return 0;
      }
    }
  
    
    // Create event listener     
    $.goMap.addListener("bounds_changed", $.syncMapAndList);
    $.goMap.addListener("zoom_changed", $.syncMapAndList);
    $.goMap.addListener("drag", $.syncMapAndList);
  }
  
  
});



