// Declare our current status variables -- false on page load
var dmls_show = false;
var dmls_maps = false;

// If they click on the map link, do the following:
$(function(){
	centerPopup();
	$('.dmls2-slideshow').unbind().click(function(){
		dmlsLoadPopup($(this).attr('href'));
		return false;
	});
	$('.dmls2-map').unbind().click(function(){
		dmlsLoadPopup($(this).attr('href'));
		return false;
	});
});

// This function displays our popup with the slide show for the current
// property.
function dmlsLoadPopup(url){
	if(dmls_show+dmls_maps == false) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("html,body").animate({scrollTop:0}, 'slow');
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").load(url).fadeIn("slow");
		dmls_show = true;
		dmls_maps = true;
	}
}

// Hide the popup once we're finished with it
function dmlsDisablePopup(){
	if(dmls_show+dmls_maps){
		$("#backgroundPopup").delay("slow").fadeOut("slow");
		$("#popupContact").hide().html('&nbsp;');
		dmls_show = false;
		dmls_maps = false;
	}
}

// This little snippet centers the popup on the screen.
function centerPopup(){
	//request data for centering
		// only do this once!!
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	var w_height = windowHeight/2-popupHeight/2;
	var w_width  = windowWidth/2-popupWidth/2;
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": w_height,
		"left": w_width
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

// Do the jQuery things needed to make this stuff happen!
$(document).ready(function(){
	// Close the popups one of three different ways...
	$("#popupContactClose").click(function(){
		dmlsDisablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		dmlsDisablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27){
			if(dmls_maps+dmls_show) {
				dmlsDisablePopup();
			}
		}
	});
});

