$(document).ready(function() {
    
    var isOverNavigation = false;
    
    $('#navigation-main-dropdown').mouseenter(function() {
      $('#navigation-edition-wrapper').css('display', 'block');
    });
    
    $('#navigation-edition-close').click(function() {
      isOverNavigation = false;
      $('#navigation-edition-wrapper').css('display', 'none');
      return false;
    });
    
    $('#navigation-main').mouseleave(function() {
      if(isOverNavigation == false) {
        $('#navigation-edition-wrapper').css('display', 'none');
      }
    });
    
    $('#navigation-edition-wrapper').hover(function() {
      isOverNavigation = true;
    }, function() {
      isOverNavigation = false;
    });
    
    
    
    $('body').cyOverlabel();
    
    $('.gallery-slot .scrollable').css('overflow-x', 'hidden').css('height', '77px');
    $('.gallery-slot .scrollable').scrollable({
      items: 'ul',
      size: 3
    });
    
    $('.gallery-article .scrollable').css('overflow-x', 'hidden').css('height', '70px');
    $('.gallery-article .scrollable').scrollable({
      items: 'ul',
      size: 4
    });

    $('.pic-index .scrollable').css('overflow-x', 'hidden').css('height', '77px');
    $('.pic-index .scrollable ul').css('width', '20000em');
    $('.pic-index .scrollable').scrollable({
      easing: 'linear',
      items: 'ul',
      size: 5,
      speed: 3000
    }).circular().autoscroll({
      steps: 5,
      interval: 7000
    });
    
    $('.map').css('display', 'block');
    
    
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ==popup */
  $('a.internal-link-new-window').click(function() {
    window.open(this.href);
    return false;
  });
  
  $('a.external-link-new-window').click(function() {
    window.open(this.href);
    return false;
  });

  $('li.print a').click(function() {
    window.print();
    return false;
  });
  
  $('li.recommend a').click(function() {
    window.open(this.href,"Empfehlung","width=328,height=440,scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0")
    return false;
  });

 onclick="empfehlung(this.href);return false;"  
  
  
  var isOverTooltip = false;
  
  $('.picture-index li').mouseenter(function() {
      
    var pos  = $(this).position();
    var html = $(this).find('.tooltip').html();
    
    $('#tooltip').html(html)
                 .css('display', 'block')
                 .css('top', (pos.top + 55))
                 .css('left', (pos.left));
  });
  
  $('.picture-index').mouseleave(function() {
    if(!isOverTooltip) {
      isOverTooltip = false;
      $('#tooltip').css('display', 'none').html('');
    }
  });
  
  $('#tooltip').hover(function() {
    isOverTooltip = true;
  }, function() {
    isOverTooltip = false;
    $('#tooltip').css('display', 'none').html('');
  });
  
  
  
  /*
  $(".picture-index .thumbnail").tooltip({ 
    position: ['bottom', 'right'], 
    offset: [-30, -100],
    effect: 'toggle'
  });
  */
});

function showMap(state) {
  $('.register #map').attr('class', state);
}

if($('#gmap').length > 0 || $('#gmap-large').length > 0) {

  /* load google maps version 2 */
  google.load('maps', '2');
  
  /* reserve a var for a base icon */
  var baseIcon;
  
  function showTooltip(map, marker) {
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
      }

  
  /* function for the creation of a marker */
  function createMarker(map, point, index, html, tooltip, link) {
    
    var letter = String.fromCharCode('A'.charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = 'http://www.monumente-online.de/images/maps_marker.png';
    
    markerOptions = { icon:letteredIcon };
    var marker = new GMarker(point, markerOptions);
    
    if(html) {
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      
      map.addOverlay(marker);
    }
    
    if(tooltip) {
      if($('#gmap-large').length > 0) {
        var large = true;
      }

      marker.tooltip = '<div class="tooltip">'+ tooltip +'</div>';
      map.addOverlay(marker); 
      
      if(large) {
        GEvent.addListener(marker,"mouseover", function() {
            $('#gmap-large').append(marker.tooltip);
            $('#gmap-large .tooltip').css('left', '0px')
                               .css('top', '0px');
        });        
        GEvent.addListener(marker,"mouseout", function() {
            $('#gmap-large .tooltip').remove();
        }); 
      } else {
        GEvent.addListener(marker,"click", function() {
          window.location.href = link;
        }); 
        GEvent.addListener(marker,"mouseover", function() {
            $('#gmap').append(marker.tooltip);
            $('#gmap .tooltip').css('left', (marker.oj.x - $('#gmap .tooltip').width() / 2))
                               .css('top',  (marker.oj.y - 50));
        });        
        GEvent.addListener(marker,"mouseout", function() {
            $('#gmap .tooltip').remove();
        }); 
      }
    }
  }
  
  /* function for sorting a multidimensional array by its 4th value */
  function multisort(a, b) {
    var tmp1 = parseFloat(a[4]);
    var tmp2 = parseFloat(b[4]);
    
    return tmp1 > tmp2 ? 1 : tmp1 < tmp2 ? -1 : a[4] > b[4] ? 1 : a[4] < b[4] ? -1 : 0;
  }
  
  /* initialization function */
  function initialize() {
    
    /* create the map and center on the user's location and show about 50km around */
    if($('#gmap').length > 0) {
      var map = new google.maps.Map2(document.getElementById('gmap'));
    }

    if($('#gmap-large').length > 0) {
      var map = new google.maps.Map2(document.getElementById('gmap-large'));
      var large = true;
    }

    var bas = new google.maps.LatLng(Base[1], Base[2]);
    map.setCenter(bas, 8);
    bounds = new GLatLngBounds();
    
    /* create a base icon */
    baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
    /* laton of starting point */
    var latlon = new GLatLng(Base[1], Base[2]);
    bounds.extend(latlon); 
    
    /* add base point */
    if(large) {
      var tooltip = Base[5] +', '+ Base[8] +' '+ Base[9];
      if(tooltip.length > 40) {
        tooltip = tooltip.substring(0, 40) + '...';
      }

      var html = Base[5] +'<br /><br />'+ 
                 Base[7] +'<br />'+ 
                 Base[8] +' '+ Base[9] +'<br />'+ 
                 Base[10] +'<br />'+ 
                 '<a href="'+ Base[6] +'">Artikel lesen</a>';
      createMarker(map, bas, 0, html, tooltip, Base[6]);
    } else {
      var tooltip = Base[5] +', '+ Base[8] +' '+ Base[9];
      if(tooltip.length > 40) {
        tooltip = tooltip.substring(0, 40) + '...';
      }
      createMarker(map, bas, 0, null, tooltip, Base[6]);
    }
    
    /* calculate distance */
    for(var tmp in Data) {
      Data[tmp][3] = new GLatLng(Data[tmp][1], Data[tmp][2]);
      Data[tmp][4] = Data[tmp][3].distanceFrom(bas);
    }
    
    var tmp  = Data.sort(multisort);
    var DataSorted = tmp;
    
    /* add overlays */
    var i = 0;
    for(var monument in DataSorted) {
     if(large) {
        var tooltip = DataSorted[monument][5] +', '+ DataSorted[monument][8] +' '+ DataSorted[monument][9];
        if(tooltip.length > 40) {
          tooltip = tooltip.substring(0, 40) + '...';
        }

        var html = DataSorted[monument][5] +'<br /><br />'+ 
                   DataSorted[monument][7] +'<br />'+ 
                   DataSorted[monument][8] +' '+ DataSorted[monument][9] +'<br />'+ 
                   DataSorted[monument][10] +'<br />'+ 
                   '<a href="'+ DataSorted[monument][6] +'">Artikel lesen</a>';
        createMarker(map, DataSorted[monument][3], DataSorted[monument][0], html, tooltip, DataSorted[monument][6]);
      } else {
        var tooltip = DataSorted[monument][5] +', '+ DataSorted[monument][8] +' '+ DataSorted[monument][9];
        if(tooltip.length > 40) {
          tooltip = tooltip.substring(0, 40) + '...';
        }
        createMarker(map, DataSorted[monument][3], DataSorted[monument][0], null, tooltip, DataSorted[monument][6]);
      }
      
      if(i < 3) {
        i++;
        bounds.extend(DataSorted[monument][3]);
      }
    }
    
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
  }
  
  /* init, when google maps has loaded */
  google.setOnLoadCallback(initialize);
}
