﻿

var extraImages = function() {
	return {
		init: function() {
			if($("#extraImages li").length == -1) return;
		
			$.getScript("/themes/dhein/assets/jquery.cross-slide.min.js", function() {
				var $images = $("div.content img.right").wrap('<div class="stage"></div>').each(function(index) {
				var	$this = $(this);
					$this.parent().css({width: $this.width(),height: $this.height()});
					$this.css({position: 'absolute', top: 0, left: 0});
					
					
					var imgs = $('#extraImages li[rel="' + index + '"] img').map(function() {
					  	var $this = $(this);
					  return {src: this.src, alt: $this.attr('alt'), from: $this.data('options').from, to: $this.data('options').to, time:  $this.data('options').speed}; 
					  
					}).get();
					
					$this.parent().crossSlide({
				  fade: 1
				}, imgs);
					
				});
			
			});
		}		
	};
}();

$(function() {
	if(typeof(mapsCode) !== 'undefined') {
		showMap(mapsCode);
	}
	
	$("ul.nav > li").hover(function() {
		$(this).children("ul").stop(true, true).css({display: "none"}).slideDown("fast", function() { $(this).width($(this).width()); }); 
		//		$(this).slideDown().siblings().slideUp();			
	}, function() {
		$(this).children("ul").stop(true).css({display: "block"}).slideUp("fast", function() { $(this).css({height: "auto"}); });
	}).find("ul").css({display: "none"});
	
	
	var brezel = $("#brezel");
	var brezelTop = brezel.offset().top + brezel.height() + 40;
	var checkPosition = function() {
		
		var hWindow = $(window).height();
		var xTop = $(window).scrollTop();
		
		if(brezelTop >= (hWindow + xTop)) {
			brezel.css({bottom: "auto", top: hWindow + xTop - brezel.height() - 40});
		} else {
			brezel.css({bottom: 40, top: "auto"});		
		}
		
	};
	$(window).scroll(checkPosition).resize(checkPosition);
	checkPosition();
	
	extraImages.init();
});


var geocoder;
var map;
var windows = [];
function showMap(entires) {
    geocoder = new google.maps.Geocoder();
	
	var latlng = new google.maps.LatLng(50.035974,7.771454);
    var myOptions = {
      zoom: 10,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	$(entires).each(function() {
		codeAddress(this.address, this.url, this.caption);
	});
};

  function codeAddress(address, url, caption) {
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location,
			  title: caption
          });
		  
		  var contentString = '<div id="content" style="width: 250px; height: 100px; font-size: 0.875em;">'+
			'<p><strong>' + caption + '</strong><br />' +
			address + 
			'</p><ul><li><a href="' + url + '">Details anzeigen</a></li></ul>' +			
	    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

			windows[windows.length] = infowindow;
		 google.maps.event.addListener(marker, 'click', function() {
			$(windows).each(function() { this.close();});
		 	infowindow.open(map,marker);
		 });
		  
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }

