// **
//	Showing / Hiding blank screen
// **
function setBlankScreenVisible(visible) {
	if (visible)
		new Fx.Tween($('blankscreen')).start('opacity', 0, 0.5);
	else
		new Fx.Tween($('blankscreen')).start('opacity', 0);
}

// **
//	Showing / Hiding popup
// **
function clearPopup() {
	$('details-popup-content').empty();
}
function resizePopup(width, height, tweened) {
	var dp = $('details-popup');
	var windowSize = window.getSize();
	var newLeft = Math.floor((windowSize.x - width) / 2);
	var newTop = Math.floor((windowSize.y - height) / 2);
	
	if (tweened)
	{
		var newFX = new Fx.Tween(dp);
		newFx.start('width', width);
		newFx.start('height', height);
		newFx.start('left', newLeft);
		nexFx.start('top', newTop);
	}
	else
		dp.setStyles({ width: width, height: height, left: newLeft, top: newTop });
}
function setPopupVisible(visible) {
	if (visible)
	{
		var dp = $('details-popup');
		var windowSize = window.getSize();
		var popupSize = dp.getSize();
		
		var newTop = Math.max(Math.floor((windowSize.y - popupSize.y) / 2), 20);
		var newLeft = Math.max(Math.floor((windowSize.x - popupSize.x) / 2), 20);
		
		dp.setStyles({ left: newLeft, top: newTop });
		
		new Fx.Tween(dp).start('opacity', 0, 1);
	}
	else
		new Fx.Tween($('details-popup')).start('opacity', 0);
}

// **
//	onUnload
// **
window.addEvent('unload', function() { GUnload(); });

// **
//	onScroll
// **
window.addEvent('scroll', function(event) {
	var windowScroll = window.getScroll();
	// Updating blank screen
	$('blankscreen').setStyles({ left: windowScroll.x, top: windowScroll.y });
});

// **
//	onResize
// **
window.addEvent('resize', function(event) {
	var dp = $('details-popup');
	var windowSize = window.getSize();
	var popupSize = dp.getSize();
	
	var newTop = Math.max(Math.floor((windowSize.y - popupSize.y) / 2), 20);
	var newLeft = Math.max(Math.floor((windowSize.x - popupSize.x) / 2), 20);
	
	dp.setStyles({ left: newLeft, top: newTop });
});

// **
//	onDOMReady
// **
window.addEvent('domready', function() {
	// **
	//	Loading Google Maps
	// **
	if (GBrowserIsCompatible() && (mapElement = document.getElementById("plan-acces"))) {
		var map = new GMap2(mapElement);
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng('63 avenue Jean-Jaures, 38320, Eybens',
				function(point) {
					if (point)
					{
						map.setCenter(point, 15);
						var marker = new GMarker(point);
              			map.addOverlay(marker);
						
						map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));
        				map.addControl(new GLargeMapControl());
					}
				});
	}

	// **
	//	Initializing global variables
	// **
	document.POPUP_WIDTH = 320;
	document.POPUP_HEIGHT = 240;
	document.popupIsOpen = false;
	
	// **
	//	Opening popup links
	// **
	var popupLinks = $$('a.details-popup');
	popupLinks.each(function(item) {
			item.addEvent('click', function(event) {
						event.stop();
						
						if (document.popupIsOpen)
							return;
						
						document.popupIsOpen = true;
						new Request.HTML({ update: 'details-popup-content', url: item.href + '?__postonly', method: 'get', async: false}).send();
						setBlankScreenVisible(true);
						setPopupVisible(true);
			});
	});
	
	// **
	//	Closing popup link
	// **
	var closeLinks = $$('#details-popup-close a');
	closeLinks.each(function(item) {
			item.addEvent('click', function(event) {
						event.stop();
						
						document.popupIsOpen = false;
						setBlankScreenVisible(false);
						setPopupVisible(false);
			});
	});
});

