/// <reference path="jquery-1.4.4-vsdoc.js" />
/// <reference path="pageManager.js" />

function ModalManager(pMan, wcf, minified)
{
	var isMinified = minified;
	var ModalRequested = null;
	var ContactScriptLoaded = false;
	var AppScriptLoaded = false;
	var wcfService = wcf;
	var pm = pMan;
	var directionDisplay;
	var directionsService;
	var curLocation = null;
	var directionsMap = null;
	var showPrint = null;
	var modalOpened = false;
	this.showApplication = function (store)
	{
		url = "/PopupHTML/employment.aspx?location=" + store;
		ModalRequested = "app";
		showOSXModal("VIP Tanning Employment Application", url, false, false, false);
	};

	this.showContactInfo = function (store)
	{
		url = "/PopupHTML/site_article.html?location=" + store;
		ModalRequested = "contact";
		showOSXModal("Contact VIP Tanning", url, false, false, false);
	};

	this.showLocationError = function ()
	{
		showOSXModal("VIP Tanning encountered a problem!", "PopupHTML/LocationsError.htm", false, true, false);
	};

	this.showMVT = function (store)
	{
		url = "/PopupHTML/mvt.aspx?location=" + store;
		showOSXModal("Join MVT Program", url, false, false, false);
	};

	this.showTruthArticle = function ()
	{
          
		url = "/PopupHTML/site_article.html";
		showOSXModal("The Truth", url, false, true, true);
	};

	this.showSpecials = function ()
	{
		showOSXModal("Current Specials", "PopupHTML/Specials.htm", true, true, true);
	};

	this.showSunlessVideo = function () {
		showOSXModal("VersaSpa Instructional Video", "PopupHTML/SunlessVideo.htm", true, false, true);
	};

	this.showError = function ()
	{
		showOSXModal("VIP Tanning encountered a problem!", "PopupHTML/Error.htm", true, false, false);
	};

	var CloseOnOverLayClick;
	var canScroll;

	function showOSXModal(modalTitle, modalContentHTMLFile, closeOnOverLayClick, scroll, printable)
	{
		if (!modalOpened)
		{
			modalOpened = true;
			$("#DirectionsAreaContainer").hide();
			$("#osx-modal-data-content").show();
			$("#Body_Content").addClass("NoPrint");
			if (!scroll)
				$("#osx-modal-data").css("overflow-y", "hidden");
			else
				$("#osx-modal-data").css("overflow-y", "scroll");

			showPrint = printable;
			$(".AjaxLoading").fadeIn();
			$("#osx-modal-title").html(modalTitle);
			$("#osx-modal-data-content").html("").load(modalContentHTMLFile, function (response, status, xhr) { onGetOsxModalContent_Complete(response, status, xhr); });
			CloseOnOverLayClick = closeOnOverLayClick;
		}
	};

	this.showOSXDirectionsModal = function (modalTitle, curLoc)
	{
		if (!modalOpened)
		{
			modalOpened = true;
			curLocation = curLoc;
			$("#osx-modal-data-content").hide();
			$("#DirectionsAreaContainer").show();
			$("#osx-modal-data").css("overflow-y", "scroll");
			$("#osx-modal-title").html(modalTitle);
			$(".PrintButton").unbind().click(function () { PrintWindow("Directions", $("#directionsOutput").html()); });
			$("#txtAddressFrom").val("");
			$("#retieveDirections").unbind().click(function () { onRetrieveDirections_Clicked(); });
			$("#osx-modal-content").modal({
				overlayId: 'osx-overlay',
				containerId: 'osx-container',
				closeHTML: null,
				opacity: 50,
				position: ['0', ],
				overlayClose: true,
				onOpen: OpenDirections,
				onClose: closeOSX
			});
		}
	};


	function OpenDirections(d)
	{
		var self = this;
		self.container = d.container[0];
		d.overlay.fadeIn('slow', function ()
		{
			$("#osx-modal-content", self.container).show();
			var title = $("#osx-modal-title", self.container);
			title.show();
			d.container.slideDown('slow', function ()
			{
				setTimeout(function ()
				{
					var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
					d.container.animate(
							{ height: h },
							200,
							function ()
							{
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
								$("#directionsMapContainer").show();
								$("#modalPrint").show();
								addSmallMap(curLocation);							    
								directionsDisplay = new google.maps.DirectionsRenderer();
								directionsService = new google.maps.DirectionsService();
								directionsDisplay.setMap(directionsMap);
								directionsDisplay.setPanel(document.getElementById("DirectionsArea"));
							}
						);
				}, 300);
			});
		});
}

function openOSX(d)
{
	$("#modalPrint").hide();
	var self = this;
	self.container = d.container[0];
	d.overlay.fadeIn('slow', function ()
	{
		$("#osx-modal-content", self.container).show();
		var title = $("#osx-modal-title", self.container);
		title.show();
		d.container.slideDown('slow', function ()
		{
			setTimeout(function ()
			{
				var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
				d.container.animate(
							{ height: h },
							200,
							function ()
							{
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
								$("#directionsMapContainer").show();
								if (showPrint)
								{
									$(".PrintButton").unbind().click(function () { PrintWindow($("#osx-modal-title").html(), $("#osx-modal-data").html()); });
									$("#modalPrint").show();
								}
							}
						);
			}, 300);
		});
	});
}

	function addSmallMap(Location)
	{
		var latlng = new google.maps.LatLng(Location.Latitude, Location.Longitude);
		var myOptions =
	{
		zoom: 15,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		draggable: true,
		streetViewControl: false,
		navigationControl: false,
		mapTypeControl: false,
		keyboardShortcuts: true,
		scaleControl: true,
		scrollwheel: true
	};
		directionsMap = new google.maps.Map(document.getElementById("directionsMap"), myOptions);

		setTimeout(function () { addMarker(Location, directionsMap, ''); }, 750);

	}

	function addMarker(loc, directionsMap, event)
	{
		var myLatLng = new google.maps.LatLng(loc.Latitude, loc.Longitude);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: directionsMap,
			draggable: false,
			animation: google.maps.Animation.DROP,
			title: loc.Label,
			zIndex: loc.zIndex,
			pageName: loc.Option1
		});
	}



	function onRetrieveDirections_Clicked()
	{
		var start = $("#txtAddressFrom").val();
		var end = new google.maps.LatLng(curLocation.Latitude, curLocation.Longitude);
		GetGoogleDirections(start, end);
	}
	function GetGoogleDirections(start, end)
	{
		var request = {
			origin: start,
			destination: end.toUrlValue(),
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};

		directionsService.route(request, function (response, status)
		{
			if (status == google.maps.DirectionsStatus.OK)
			{
				directionsDisplay.setDirections(response);
			}
		});
	}

	function onGetOsxModalContent_Complete(response, status, xhr)
	{
		if (status != "success")
		{
			$("#osx-modal-data-content").html("Error receiving content");
		}
		else
		{
			switch (ModalRequested)
			{
				case "contact":
					if (!ContactScriptLoaded)
					{
						if (isMinified)
							var url = "Scripts/PageScripts/Contact.aspx.min.js";
						else
							var url = "Scripts/PageScripts/Contact.aspx.js";

						$.getScript(url, function ()
						{
							var cf = new ContactForm(wcfService);
							cf.initContactForm();
						});
					}
					break;
				case "app":
					if (!AppScriptLoaded)
					{
					 if (isMinified)
							var url = "Scripts/PageScripts/Employment.aspx.min.js";
						else
							var url = "Scripts/PageScripts/Employment.aspx.js";
						$.getScript(url, function ()
						{
							var app = new application(wcfService);
							app.initApplication();
						});
					}
					break;

				default:
			}
		}
		$(".AjaxLoading").fadeOut();
		$("#osx-modal-content").modal({
			overlayId: 'osx-overlay',
			containerId: 'osx-container',
			closeHTML: null,
			opacity: 50,
			position: ['0', ],
			overlayClose: CloseOnOverLayClick,
			onOpen: openOSX,
			onClose: closeOSX
		});
	}

	function PrintWindow(title, content)
	{
		var output = window.open('', '', 'width=800,height=600,toolbar=no,scrollbars=no,location=no,resizable=no');
		var html = '<html><head><title>'+title+'</title></head><body onLoad="window.print();"><div style="overflow:auto; height:100%">' + content + '</div></body>';

		output.document.open();
		output.document.write(html);
		output.document.close();
	}

	function closeOSX(d)
	{
		modalOpened = false;
		var self = this; // this = SimpleModal object
		d.container.animate(
				{ top: "-" + (d.container.height() + 0) },
				500,
				function ()
				{
					d.overlay.fadeOut('slow', function ()
					{
						self.close(); // or $.modal.close();
					});
				}
			);
	}
}

