/**
 * i started with http://webmatze.de/wirklich-einfache-javascript-tooltips/, but
 * added ajax loading and determination of tooltip dimensions and positioning.
 *
 * you will have to have added a div id=tooltip and css for that in your html
 */

wmtt = null;
floater = null;

function updateWMTT(e) {
    if (wmtt != null && wmtt.style.display == 'block') {

		delta_x = 10;
		delta_y = 10;

        mouse_x = Event.pointerX(e);
        mouse_y = Event.pointerY(e);

		window_width = document.viewport.getWidth();
		window_height = document.viewport.getHeight();
		dimensions = wmtt.getDimensions();
		element_width = dimensions.width;
		element_height = dimensions.height;

		if ((element_width + mouse_x) >= (window_width - delta_x)) { // too big for X
			mouse_x = mouse_x - element_width;
			// apply delta to make sure that the mouse is not on the tool-tip
			mouse_x = mouse_x - delta_x;
		} else {
			mouse_x = mouse_x + delta_x;
		}
		if ((element_height + mouse_y) >= (window_height - delta_y)) { // too big for Y
			mouse_y = mouse_y - element_height;
		    // apply delta to make sure that the mouse is not on the tool-tip
			mouse_y = mouse_y - delta_y;
		} else {
			mouse_y = mouse_y + delta_y;
		}
        wmtt.style.left = (mouse_x) + "px";
        wmtt.style.top = (mouse_y) + "px";
    }
}

function showWMTT(id, url) {
    Event.observe(document, 'mousemove', updateWMTT);
	new Ajax.Updater(id, url, {
		onLoaded: function(transport) {
	        wmtt = $(id);
			wmtt.style.display = "block";
		},
		onFailure: function(transport) {
			alert('Failed to load tooltip');
		}
	});
}

function hideWMTT() {
    Event.stopObserving(document, 'mousemove', updateWMTT);
    wmtt.style.display = "none";
	wmtt.style.left = "-1000px";
	wmtt.style.top = "-1000px";
	wmtt = null;
}

/**
 * Floating window
 */
function updateFloater(e) {
    if (floater != null && floater.style.display == 'block') {

		delta_x = 10;
		delta_y = 10;

        mouse_x = Event.pointerX(e);
        mouse_y = Event.pointerY(e);

		window_width = document.viewport.getWidth();
		window_height = document.viewport.getHeight();
		dimensions = floater.getDimensions();
		element_width = dimensions.width;
		element_height = dimensions.height;
		delta_y = element_height / 2 * -1;
		
		if ((element_width + mouse_x) >= (window_width - delta_x)) { // too big for X
			mouse_x = mouse_x - element_width;
			// apply delta to make sure that the mouse is not on the tool-tip
			mouse_x = mouse_x - delta_x;
		} else {
			mouse_x = mouse_x + delta_x;
		}
		if ((element_height + mouse_y) >= (window_height - delta_y)) { // too big for Y
			mouse_y = mouse_y - element_height;
		    // apply delta to make sure that the mouse is not on the tool-tip
			mouse_y = mouse_y - delta_y;
		} else {
			mouse_y = mouse_y + delta_y;
		}
        floater.style.left = (mouse_x) + "px";
        floater.style.top = (mouse_y) + "px";
    }
}


function showFloater(id, url) {
    Event.observe(document, 'mousemove', updateFloater);
	new Ajax.Updater(id, url, {
		onLoaded: function(transport) {
	        floater = $(id);
			floater.style.display = "block";
		},
		onFailure: function(transport) {
			alert('Failed to load floating window');
		}
	});
}

function closeFloater() {
    Event.stopObserving(document, 'mousemove', updateFloater);
    floater.style.display = "none";
	floater.style.left = "-1000px";
	floater.style.top = "-1000px";
	floater = null;
}

function moviePlayer(url) {
	new Ajax.Updater('movie-container', url, {
		onLoaded: function(transport) {
			$('movie-stage').style.display = 'block';
			$('movie-container').style.display = 'block';
		},
		onFailure: function(transport) {
			alert('Failed to load video. Sorry.');
		}
	});
}

function movieClose() {
	$('movie-container').hide();
	$('movie-stage').hide();
	return true;
}
