﻿Casade.PopupMap = (function() {
	/* START PRIVATE */
	var _heightOptionsBar = 498;

	function startGoogleMapsInPopup(myPlaatsId) {

		oMap = new Estate.GoogleMaps(mapConfig, "oMap")
		oMap.Init()

		// Show/hide div popup
		jQuery("#CloseDivPopupWindow").click(function() {
			jQuery("#DivPopupWindow").slideUp()
			jQuery("#DivPopupWindow").fadeOut()
			jQuery("#DivPopupBackground").fadeOut()
		})

		// Show/hide left overlay
		jQuery("#HidePanel").toggle(
			function() {
				//jQuery("#OverlayWijk").animate({ left: "0px" }, 500, "easeOutQuint")
				jQuery("#HidePanel").removeClass("hidePanelFoldOut")
			},
			function() {
				//jQuery("#OverlayWijk").animate({ left: "-210px" }, 500, "easeOutQuint")
				jQuery("#HidePanel").addClass("hidePanelFoldOut")
			}
		)

		// Show/hide middle overlay
		jQuery("#Description").click(function() {
			if (jQuery("#Info").is(":visible")) {
				jQuery("#Info").fadeOut("fast")
			} else {
				jQuery("#Info").fadeIn("fast")
			}
		})
		jQuery("#InfoClose").click(function() {
			jQuery("#Info").fadeOut("fast")
		})

		//Show right bar
		jQuery("#OptionsBar dd").attr("style", "height: 0px; padding: 0; overflow: hidden;")
		jQuery("#OptionsBar dt").click(function() {
			mapConfig.locationsUrl = "/sitecore/content/Global-Content/Plaatsen.aspx?scitem=" + jQuery(this).attr("scitem");
			oMap.LoadExternalData(mapConfig);

			jQuery(this).parents("dd").load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + jQuery(this).attr("scitem"))


			var nextDd = jQuery(this).next("dd")
			nextDd.load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + jQuery(this).attr("scitem"), function() { bindAClicks(nextDd) })


			jQuery(nextDd).data("clicked", true)
			jQuery("#OptionsBar dd").each(function() {
				if (jQuery(this).data("clicked") != jQuery(nextDd).data("clicked")) {
					jQuery(this).attr("style", "height: 0px; padding: 0; overflow: hidden;")
					jQuery(this).hide()
				}
			})
			jQuery(nextDd).data("clicked", false)

			if (jQuery(nextDd).height() > 0) {
				/*
				jQuery(nextDd).attr("style", "height: auto; padding: 0; overflow: hidden;")
				jQuery(nextDd).animate({ height: "0px" }, 500, "easeOutQuint")
				*/
			} else {
				var maxHeight = _heightOptionsBar - jQuery("#OptionsBar").height()
				jQuery(nextDd).attr("style", "height: 0px; overflow: auto;")
				jQuery(nextDd).animate({ height: maxHeight + "px" }, 500, "easeOutQuint")
			}

		})
		jQuery("#OptionsBar dd a").click(function() {
			mapConfig.locationsUrl = "/sitecore/content/Global-Content/Plaatsen.aspx?scitem=" + jQuery(this).attr("scitem");
			oMap.LoadExternalData(mapConfig);
			jQuery(this).parents("dd").load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + jQuery(this).attr("scitem"))
		})
		jQuery("#InfoClose").click(function() {
			jQuery("#Info").fadeOut("fast")
		})
		if (myPlaatsId) {

			var plaatsId = myPlaatsId.replace(/[^\w|\d]/gi, "");


			var e = jQuery("#plaats" + plaatsId)
			var nextDd = jQuery(e).next("dd")
			jQuery(nextDd).data("clicked", true)
			jQuery("#OptionsBar dd").each(function() {
				if (jQuery(this).data("clicked") != jQuery(nextDd).data("clicked")) {
					jQuery(this).attr("style", "height: 0px; padding: 0; overflow: hidden;")
					jQuery(this).hide()
				}
			})
			jQuery(nextDd).data("clicked", false)

			if (jQuery(nextDd).height() > 0) {
				/*
				jQuery(nextDd).attr("style", "height: auto; padding: 0; overflow: hidden;")
				jQuery(nextDd).animate({ height: "0px" }, 500, "easeOutQuint")
				*/
			} else {
				var maxHeight = _heightOptionsBar - jQuery("#OptionsBar").height()
				jQuery(nextDd).attr("style", "height: 0px; overflow: auto;")
				jQuery(nextDd).animate({ height: maxHeight + "px" }, 500, "easeOutQuint")
			}
		}
	}


	function ClickPlaats(e) {
		var myScitem = jQuery(e).attr("scitem");

		mapConfig.locationsUrl = "/sitecore/content/Global-Content/Plaatsen.aspx?scitem=" + myScitem;
		oMap.LoadExternalData(mapConfig);

		jQuery(e).parents("dd").load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + myScitem)


		var nextDd = jQuery(e).next("dd")
		nextDd.load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + myScitem, function() { bindAClicks(nextDd) })


		jQuery(nextDd).data("clicked", true)
		jQuery("#OptionsBar dd").each(function() {
			if (jQuery(this).data("clicked") != jQuery(nextDd).data("clicked")) {
				jQuery(this).attr("style", "height: 0px; padding: 0; overflow: hidden;")
				jQuery(this).hide()
			}
		})
		jQuery(nextDd).data("clicked", false)

		if (jQuery(nextDd).height() > 0) {
			/*
			jQuery(nextDd).attr("style", "height: auto; padding: 0; overflow: hidden;")
			jQuery(nextDd).animate({ height: "0px" }, 500, "easeOutQuint")
			*/
		} else {
			var maxHeight = _heightOptionsBar - jQuery("#OptionsBar").height()
			jQuery(nextDd).attr("style", "height: 0px; overflow: auto;")
			jQuery(nextDd).animate({ height: maxHeight + "px" }, 500, "easeOutQuint")
		}
	}

	function bindAClicks(nextDd) {
		jQuery(nextDd).find("a").click(function() {
			mapConfig.locationsUrl = "/sitecore/content/Global-Content/Plaatsen.aspx?scitem=" + jQuery(this).attr("scitem");
			oMap.LoadExternalData(mapConfig);
			jQuery(this).parents("dd").load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + jQuery(this).attr("scitem"))
		})

	}
	function ClickWijk(e) {
		jQuery("#OptionsBar dd a").click(function() {
			mapConfig.locationsUrl = "/sitecore/content/Global-Content/Plaatsen.aspx?scitem=" + jQuery(this).attr("scitem");
			oMap.LoadExternalData(mapConfig);
			jQuery(this).parents("dd").load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + jQuery(this).attr("scitem"))
		})
	}
	/* END PRIVATE */


	/* START PUBLIC */
	return {

		ShowMapPopup: function(plaatsId, wijkId) {
			jQuery("body").append("<div id='DivPopupHolder'></div>")
			if (plaatsId && wijkId) {
				jQuery("#DivPopupHolder").load("/Standard-Items/wijkkaartpopup.aspx #HtmlData", function() { Casade.PopupMap.OpenDivPopupWindow(plaatsId, wijkId) })
			}
			else {
				jQuery("#DivPopupHolder").load("/Standard-Items/wijkkaartpopup.aspx #HtmlData", Casade.PopupMap.OpenDivPopupWindow)
			}

		},

		OpenDivPopupWindow: function(plaatsId, wijkId) {
			jQuery("#DivPopupWindow").hide()
			jQuery("#DivPopupWindow").slideDown(500)
			jQuery("#DivPopupWindow").hide()
			jQuery("#DivPopupWindow").fadeIn(500)
			jQuery("#DivPopupBackground").hide()
			jQuery("#DivPopupBackground").css("height", jQuery(document).height() + "px")
			jQuery("#DivPopupBackground").show()
			jQuery("#DivPopupBackground").css("opacity", 0)
			if (plaatsId) {

				jQuery("#DivPopupBackground").animate({ opacity: 0.75 }, 500, function() { Casade.PopupMap.StartGoogleMapsInPopup(plaatsId) })
				plaatsId = plaatsId.replace(/[^\w|\d]/gi, "");
				var e = jQuery("#plaats" + plaatsId)
				var nextDd = jQuery(e).next("dd")
				if (wijkId) {
					nextDd.load("/sitecore/content/Global-Content/Plaatsen.aspx?a=1&scitem=" + wijkId, function() { bindAClicks(nextDd) })
				}
			}
		},

		StartGoogleMapsInPopup: function(myPlaatsId) {

			startGoogleMapsInPopup(myPlaatsId)
		},

		ClickMapPlaats: function(e) {
			ClickPlaats(e);
		}
	}
	/* END PUBLIC */
})();
