/*
 * Copyright 2008 Marc Mongenet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

// Multi-browser functions

// Stop event bubbling.
function w2mlStopEvent(evt)
{
	if (evt.stopPropagation) evt.stopPropagation();
	else evt.cancelBubble = true;
	if (evt.preventDefault) evt.preventDefault();
	else evt.returnValue = false;
}

// Get scrollLeft, scrollTop, ...
function w2mlGetScroll(scroll)
{
	if (document.documentElement && document.documentElement[scroll])
		return document.documentElement[scroll]; // IE 6 DOCTYPE, Moz
	return document.body[scroll];  // IE 5, IE 6 compat
}


// From http://www.quirksmode.org/js/findpos.html
function w2mlGetYPosOfElm(elm)
{
	var curtop = 0;
	if (elm.offsetParent) {
		curtop = elm.offsetTop
		while (elm = elm.offsetParent) {
			curtop += elm.offsetTop
		}
	}
	return curtop;
}


// From http://www.quirksmode.org/viewport/compatibility.html
function w2mlViewportHeight()
{
	if (self.innerHeight) // all except Explorer
		return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		return document.documentElement.clientHeight;
	else if (document.body) // other Explorers
		return document.body.clientHeight;
}


function w2mlUnderElmOnScreen(elm)
{
	return Math.min(w2mlViewportHeight() - 50, w2mlGetYPosOfElm(elm) + elm.offsetHeight - w2mlGetScroll("scrollTop"));
}


//------------------------
// Utility functions

function w2mlFirstElmOfClass(elm, className)
{
	if (elm.className && elm.className.match("\\b" + className + "\\b"))
		return elm;
	// Search for a descendant element of given class.
	var found = null;
	for (var c = elm.firstChild; c && found == null; c = c.nextSibling)
		found = w2mlFirstElmOfClass(c, className);
	return found;
}

function w2mlSetPropOnElmClass(elm, prop, className, val)
{
	if (elm.className && elm.className.match("\\b" + className + "\\b"))
		elm[prop] = val;
	for (var c = elm.firstChild; c; c = c.nextSibling)
		w2mlSetPropOnElmClass(c, prop, className, val);
}

//------------------------
// Hover pointer functions

function w2mlMouseHoverImgMove(event)
{
	var img = this.w2mlHoverImg;
	event = event || window.event;
	img.style.left = (event.clientX + w2mlGetScroll("scrollLeft") + 2) + "px";
	img.style.top = (event.clientY + w2mlGetScroll("scrollTop") - img.offsetHeight - 2) + "px";
}

// Set an image to display near mouse pointer when it hovers an element.
function w2mlSetMouseHoverImg(elm, image_URI)
{
	var img = document.createElement("img");
	img.src = image_URI;
	img.style.position = "absolute";
	img.style.zIndex = 2*1024*1024*1023;
	img.style.visibility = "hidden";
	document.body.appendChild(img);
	elm.w2mlHoverImg = img;
	elm.w2mlMouseHoverImgMove = w2mlMouseHoverImgMove;
	elm.onmouseover = function(event) {
		this.w2mlHoverImg.style.visibility = "visible";
		this.w2mlMouseHoverImgMove(event);
		this.onmousemove = w2mlMouseHoverImgMove;
	}
	elm.onmouseout = function(event) {
		this.w2mlHoverImg.style.visibility = "hidden";
		this.onmousemove = null;
	}

	img.w2mlHoverImg = img;
	img.w2mlMouseHoverImgMove = w2mlMouseHoverImgMove;
	img.onmouseover = function(event) {
		this.style.visibility = "visible";
		this.w2mlMouseHoverImgMove(event);
		this.onmousemove = w2mlMouseHoverImgMove;
	}
	img.onmouseout = function(event) {
		this.w2mlHoverImg.style.visibility = "hidden";
		this.onmousemove = null;
	}
}

// Undo w2mlSetMouseHoverImg()
function w2mlUnsetMouseHoverImg(elm)
{
	elm.w2mlHoverImg.style.visibility = "hidden";
	elm.onmouseover = elm.onmousemove = elm.onmouseout = null;
}


// Main function for W2ML client support
function w2mlMain()
{
	if (w2mlLinkEditors) {
		w2mlLinkEditors(document.getElementsByTagName('html')[0]);
	}
}
