var geocoder;
var map;
var markers = new Array();
var infowindow;

jQuery(document).ready(function() {

  /* Google Maps Integration START */

	geocoder = new google.maps.Geocoder();	
	infowindow = new google.maps.InfoWindow();
	
  $('.storelisting').each(function() {
   var id = $(this).find(".marker").html();
   $(this).click(function() {
   	  infowindow.close();
		  infowindow.setContent(markers[id].text);
		  infowindow.open(map, markers[id]);
   });
  });

      var ziport = $("#ort-plz").val();
      markers = new Array();
      var address = ziport;

		// Mittelpunkt abfragen
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				
				// Mittelpunkt der Umkreissuche
				var latlng = results[0].geometry.location;
						// Karte anzeigen
						$("#map_canvas").show();
						
						var myOptions = {
							zoom: 10,
							center: latlng,
							mapTypeId: google.maps.MapTypeId.ROADMAP,
							mapTypeControl: false
						};
						
						map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
						
						// Center Marker setzen
						var centerMarker = new google.maps.Marker({
							position: 	latlng,
							map: 		map,
							title: 		"Ihr Standpunkt",
							icon:		new google.maps.MarkerImage("http://lancome.hmmh.de/vichy/apotheke/img/gmap-pin_home.png"),
							zIndex:		0
						});
            var count = 0;
            
            $('.storelisting').each(function() {
							 var markerID = $(this).find(".marker").html();
							 var title = $(this).find(".name").html();
							 var address = $(this).find(".address").html();
							 var phone = $(this).find(".phone").html();
							 var opening = $(this).find(".opening").html();
               
							 var innerHTML = "<strong>" + title + "</strong><br />" +
							                 address + "<br />" + phone + "<br />" +
							                 opening + "<br/>";
              if(arrGeoData[markerID]) {
                markers[markerID] = new google.maps.Marker({
                 position: 	new google.maps.LatLng(arrGeoData[markerID].lat, arrGeoData[markerID].long),
								 map: 		map,
								 title: 		title,
								 text:		innerHTML,
								 //icon:		new google.maps.MarkerImage("http://lancome.hmmh.de/vichy/apotheke/markerImage.php?text="+markerID),
								 zIndex:		1
							});
							
							google.maps.event.addListener(markers[markerID], 'click', function() {
								infowindow.close();
								infowindow.setContent(this.text);
								infowindow.open(map, this);
							});
              }
               
            });						
						// Zoom der Karte einstellen
						var zoom = 10;
						map.setZoom(zoom);
						
						// Pager
						
						// Größenanpassung des Frames auf Facebook
				
			} else {
				// alert("Geocode was not successful for the following reason: " + status);
			}
		});  	  

    /*
	$(".showMapLocation", "click", function(){
    var id = $(this).attr("title");
		infowindow.close();
		infowindow.setContent(markers[id].text);
		infowindow.open(map, markers[id]);
	});
		*/
		// Marker löschen
		markers = new Array();





	/* Google Maps Integration ENDE */
	$(".drucken").click(function() {
		window.print();
	});
	
	hideAllTabs();
	var tab = 0;
	if(document.getElementById("getTab")) {
    tab = parseInt(document.getElementById("getTab").innerHTML);
  } 
	if(tab != 0) {
    tab = tab - 1;
  }
	$(".tab").eq(tab).fadeIn(1000);
	$(".tab-navigation ul li a").eq(tab).addClass("active");
	$(".tab-navigation ul li a").click(function() {
		if(!$(this).hasClass("disabled")) {
			hideAllTabs();
			$(this).addClass("active");
			$("#"+$(this).attr("id").replace("link-", "")).fadeIn(1000);
		}
		return false;
	});
	
	hideArtikel();
	var pa = 0;
	if(document.getElementById("getPa")) {
    pa = parseInt(document.getElementById("getPa").innerHTML);
  }
	//$(".text-box.artikel:eq(0)").css("margin-bottom", "22px");
	$(".text-box.artikel:eq("+pa+") div").show();
	$(".text-box.artikel").eq(pa).next("a.weiterlesen").hide();  	  
  for(i = 0; i <= $(".schliessen").length-1; i++) { 
     $(".schliessen:eq("+i+")").hide();
  }
  $(".schliessen:eq("+pa+")").show(); 
  
  
	$("a.weiterlesen").click(function() {
		hideArtikel();
		$(this).prev(".text-box.artikel").children("div").show();
		for(i = 0; i <= $(".schliessen").length-1; i++) { 
     if ($(".schliessen:eq("+i+")").css("display") == "inline-block") 
     { 
        $(".schliessen:eq("+i+")").hide();
     }
    }
		$(this).next("a.schliessen").show();
		$(this).hide();
		return true;
	});
	
	$("a.schliessen").click(function() {
		hideArtikel();
		$(this).hide();
		return true;
	});
	
	$(".zoom").click(function() {
		$("#layer-window img").load(function() {
			$(this).parent().centerInClient();
		});
		$("#layer-window img").attr("src", $(this).attr("href"));
		$("#layer-background").fadeTo("slow", 0.5).queue(function() {
			$("#layer-window").fadeIn("slow");
			$(this).dequeue();
		});
		return false;
	});
	
	$("#layer-background, #close").click(function() {
		$("#layer-window").fadeOut("slow").queue(function() {
			$("#layer-background").fadeOut("slow");
			$(this).dequeue();
		});
	});
	
	$(".small").removeAttr("title");
	
	$(".small").hover(
	function() {
		$("#tooltip").css({top: $(this).position().top-20, left: $(this).position().left-10});
		$("#tooltip").html($(this).html());
		$("#tooltip").show();
	},
	function() {
		$("#tooltip").hide();
	});
	
	$("#carousel").jcarousel();

	
	$("#filialfinder .input-text, #filialfinder-2 .input-text").click(function() {
		if ($(this).val() == "Stadt oder PLZ") {
			$(this).val("");			
		}
	});
	
	$("#filialfinder .input-text, #filialfinder-2 .input-text").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Stadt oder PLZ");
		}
	});
	
	$("#newsletter .input-text").click(function() {
		if ($(this).val() == "Ihre E-Mail Adresse") {
			$(this).val("");			
		}
	});
	
	$("#newsletter .input-text").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Ihre E-Mail Adresse");
		}
	});

  $("#btn-bewerben").click(function(e){
    window.open(this.href,'Online Bewerben', 'width=1000, height=800, scrollbars=yes');
    e.preventDefault();
    return false;
  });	

  $(".onlinebewerben").click(function(e){
    window.open(this.href,'Online Bewerben', 'width=1000, height=800, scrollbars=yes');
    e.preventDefault();
    return false;
  });		
});

function hideAllTabs() {
	for(i = 0; i <= $(".tab").length-1; i++) {
		$(".tab:eq("+i+")").hide();
		$(".tab-navigation ul li a:eq("+i+")").removeClass("active");
	}
}

function hideArtikel() {
  for(i = 0; i <= $(".text-box.artikel").length-1; i++) {
		$(".text-box.artikel:eq("+i+")").css("margin-bottom", "0px");
		$(".text-box.artikel:eq("+i+") div").hide();
		$(".text-box.artikel:eq("+i+") .ecke-links-oben").show();
		$(".text-box.artikel:eq("+i+") .ecke-rechts-oben").show();
		$(".text-box.artikel:eq("+i+")").next("a.weiterlesen").show();
		$(".text-box.artikel:eq("+i+")").next("a.schliessen").hide();
	}
}



$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
};
