// ***************************************************************************
//                          wm.js  -  description
//                             -------------------
//    begin                : Fri Dec 9 2005
//    copyright            : (C) 2005 by Andrei Gavrila
//    email                : andrei.gavrila@gmail.com
// ***************************************************************************
//
// ***************************************************************************
// *                                                                         *
// *   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 2 of the License, or     *
// *   (at your option) any later version.                                   *
// *                                                                         *
// ***************************************************************************

var wmEditorId        =      'editor';
var wmContainerId     =   'container';
var wmPageId          =        'page';
var wmStatusId        =      'status';
var wmHelpId          =        'help';
var wmSaveId          =        'save';
var wmSaveStringId    =  'savestring';
var wmTipOfTheDayId   = 'tipoftheday';
var wmWindowListId    =  'windowlist';

var wmEditorX         =    0;
var wmEditorY         =  161; // Must include the with of the clearer, too !
var wmEditorWidth     = 1000;
var wmEditorHeight    = 2000;

if (navigator.userAgent.indexOf('MSIE 5.') != -1)
{
	// IE 5
	//

	var wmEditorX         = 0;
	var wmEditorY         = 0;
}

var wmBackgroundColor = 'ffffff';

var wmZIndexIndex     = 1000;

var mouse_captured    = 0;

var mouse_x    = 0;
var mouse_y    = 0;

var mouse_oldX = 0;
var mouse_oldY = 0;

function wmGetElementById(id)
{
	// If we don't receive a string, assume it's the object
	//

	if (typeof(id) != 'string')
		return id;

	if (document.getElementById)
		return document.getElementById(id);
	else
		if (document.all)
			return document.all[id];
		else
			return null;
}

function wmAddEventListener(element, event, handler, capture)
{
	if (!mouse_captured) {
		// Capture the mouse
		//

		wmCaptureMouse();

		// Start the status check
		//

		wmWindowStatusCheck();
	}

	event = event.toLowerCase();

	if (wmGetElementById(element).addEventListener) {
		// Mozilla & Opera
		//

		wmGetElementById(element).addEventListener(event, handler, capture);
	}
	else
		if (wmGetElementById(element).attachEvent) {
			// IE
			//

			wmGetElementById(element).attachEvent('on' + event, handler);
		}
		else {
			// Unknown
			//

			eval('element.on' + event + ' = handler;');
		}
}

function wmGetX(element)
{
	return parseInt(wmGetElementById(element).style.left);
}

function wmGetY(element)
{
	return parseInt(wmGetElementById(element).style.top);
}

function wmGetW(element)
{
	return parseInt(wmGetElementById(element).style.width);
}

function wmGetH(element)
{
	return parseInt(wmGetElementById(element).style.height);
}

function wmMove(element, x, y)
{
	wmGetElementById(element).style.left = x + 'px';
	wmGetElementById(element).style.top  = y + 'px';
}

function wmResize(element, w, h)
{
	wmGetElementById(element).style.width  = w + 'px';
	wmGetElementById(element).style.height = h + 'px';
}

function wmShow(element)
{
	wmGetElementById(element).style.display = 'block';
}

function wmHide(element)
{
	wmGetElementById(element).style.display = 'none';
}

function wmIsShown(element)
{
	return (wmGetElementById(element).style.display == 'block');
}

function wmIsHidden(element)
{
	return !wmIsShown(element);
}

function wmZIndex(element, zIndex)
{
	wmGetElementById(element).style.zIndex = zIndex;
}

function wmCaptureMouse()
{
	if (!mouse_captured) {
		mouse_captured = 1;

		wmAddEventListener(wmEditorId, 'mousemove', wmCapturedMouse, false);
	}
}

function wmCapturedMouse(event)
{
	if (typeof(event.pageX) == 'number') {
		// Mozilla & Opera
		//

		mouse_x = parseInt(event.pageX);
		mouse_y = parseInt(event.pageY);
	}
	else {
		// IE
		//

		if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			mouse_x = parseInt(event.clientX) + document.body.scrollLeft;
			mouse_y = parseInt(event.clientY) + document.body.scrollTop;
		}
		else {
			if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				mouse_x = parseInt(event.clientX) + document.documentElement.scrollLeft;
				mouse_y = parseInt(event.clientY) + document.documentElement.scrollTop;
			}
			else {
				mouse_x = parseInt(event.clientX);
				mouse_y = parseInt(event.clientY);
			}
		}
	}
}

function wmGetMouseX()
{
	return mouse_x;
}

function wmGetMouseY()
{
	return mouse_y;
}

function wmSlide(element, x, y, time, elementHide)
{
	var dx = 0;
	var dy = 0;

	if (wmGetX(element) < x)
		dx =  10;
	if (wmGetX(element) > x)
		dx = -10;

	if (wmGetY(element) < y)
		dy =  10;
	if (wmGetY(element) > y)
		dy = -10;

	if (Math.abs(wmGetX(element) - x) < Math.abs(dx))
		dx = x - wmGetX(element);

	if (Math.abs(wmGetY(element) - y) < Math.abs(dy))
		dy = y - wmGetY(element);

	wmMove(element, wmGetX(element) + dx, wmGetY(element) + dy);

	if (dx || dy)
		eval('setTimeout("wmSlide(\'' + element + '\', '+ x +', ' + y + ', ' + time + ', \'' + elementHide + '\')", time);');
	else
		if (wmGetElementById(elementHide))
			wmHide(elementHide);
}

function wmSetBackgroundColor(src)
{
	wmBackgroundColor = src;

	wmGetElementById(wmEditorId).style.backgroundColor = '#' + src;
}

