////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                    //
// Precious JavaScript support code                                                                   //
// Copyright (c) 2004 by Wenzel Jakob                                                                 //
//                                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////////////////////

var ie5 = document.all && document.getElementById
var ns6 = document.getElementById && !document.all

if (ns6) {
    document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove=__precious_mouseMove;

/* Layer management */

var __precious_layers = new Array();

function __precious_getLayer(layer_id) {
    if (__precious_layers[layer_id]!=null) {
        return __precious_layers[layer_id];
    } else {
        var layer = new __precious_Layer(layer_id);
        __precious_layers[layer_id]=layer;
        return layer;
    }
}


/* Precious Layer class constructor */

function __precious_Layer(layer_id) {
    this.id         = layer_id;
    this.visible    = true;
    this.getID      = __precious_Layer_getID;
    this.moveTop    = __precious_Layer_moveTop;
    this.moveBottom = __precious_Layer_moveBottom;
    this.moveLeft   = __precious_Layer_moveLeft;
    this.moveRight  = __precious_Layer_moveRight;
    this.getTop     = __precious_Layer_getTop;
    this.getLeft    = __precious_Layer_getLeft;
    this.getWidth   = __precious_Layer_getWidth;
    this.getHeight  = __precious_Layer_getHeight;
    this.show       = __precious_Layer_show;
    this.hide       = __precious_Layer_hide;
    this.toggle     = __precious_Layer_toggle;
    this.isVisible  = __precious_Layer_isVisible;
}

/* Precious Layer class members */

function __precious_Layer_getID() {
    return this.id;
}

function __precious_Layer_isVisible() {
    return this.visible;
}

function __precious_Layer_moveTop(value) {
    document.getElementById(this.getID()).style.top = value + "px";
}

function __precious_Layer_moveBottom(value) {
    document.getElementById(this.getID()).style.bottom = value + "px";
}

function __precious_Layer_moveLeft(value) {
    document.getElementById(this.getID()).style.left = value + "px";
}

function __precious_Layer_moveRight(value) {
    document.getElementById(this.getID()).style.right = value + "px";
}

function __precious_Layer_hide() {
    document.getElementById(this.getID()).style.display = 'none';
    this.visible = false;
}

function __precious_Layer_show() {
    document.getElementById(this.getID()).style.display = 'block';
    this.visible = true;
}

function __precious_Layer_toggle() {
    if (this.isVisible()) {
        this.hide();
    } else {
        this.show();
    }
}

function __precious_getX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function __precious_getY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}


function __precious_Layer_getTop(value) {
    return __precious_getY(document.getElementById(this.getID()));
}

function __precious_Layer_getLeft(value) {
    return __precious_getX(document.getElementById(this.getID()));
}

function __precious_Layer_getHeight(value) {
    return document.getElementById(this.getID()).offsetHeight;
}

function __precious_Layer_getWidth(value) {
    return document.getElementById(this.getID()).offsetWidth;
}


var __precious_activeMenu = null;
var __precious_activeMenuAnchor = null;
var __precious_activeInfo = null;

function __precious_showMenu(menuId) {
    var menu = __precious_getLayer(menuId);
    var menu_anchor = document.getElementById(menuId+"_anchor");
    if (menu != null) {
        __precious_hideMenu();
        menu.moveTop(__precious_getY(menu_anchor)+menu_anchor.offsetHeight);
        menu.moveLeft(__precious_getX(menu_anchor));
        menu.show();
        __precious_activeMenu = menu;
        __precious_activeMenuAnchor = menu_anchor;
    }
}

function __precious_showInfo(infoId, x, y) {
	var info = __precious_getLayer(infoId);
	if (info != null) {
		__precious_hideInfo();
		info.moveTop(x);
		info.moveLeft(y);
		info.show();
		__precious_activeInfo = info;
	}
}

function __precious_hideInfo() {
	if (__precious_activeInfo != null) {
		__precious_activeInfo.hide();
		__precious_activeInfo = null;
	}
}

function __precious_hideMenu() {
    if (__precious_activeMenu != null) {
        __precious_activeMenu.hide();
        __precious_activeMenu = null;
    }
}


var __precious_infos = new Array();

function __precious_mouseMove(e) {
	if (!e)
		var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

    if (__precious_activeMenu != null) {
        var x1 = __precious_activeMenu.getLeft();
        var x2 = x1 + __precious_activeMenu.getWidth();
        var y1 = __precious_activeMenu.getTop()-5;
        var y2 = y1 + __precious_activeMenu.getHeight();
        var x1_a = __precious_getX(__precious_activeMenuAnchor);
        var x2_a = x1_a + __precious_activeMenuAnchor.offsetWidth;
        var y1_a = __precious_getY(__precious_activeMenuAnchor);
        var y2_a = y1_a + __precious_activeMenuAnchor.offsetHeight;

        if (posx < x1 || posx > x2) {
            if (posx < x1_a || posx > x2_a)
                __precious_hideMenu();
        } else if (posy < y1 || posy > y2) {
            if (posy < y1_a || posy > y2_a) {
                __precious_hideMenu();
			}
        }
    }
	
	for (i=0; i<__precious_infos.length; i++) {
		info = __precious_infos[i];
		var imgobj = document.getElementById(info.image);
		if (imgobj != null) {
			var ix= __precious_getX(imgobj);
			var iy= __precious_getY(imgobj);
		
			var infolayer = __precious_getLayer(info.image + "," + info.x1 + "," + info.y1 + "," + info.x2 + "," + info.y2 + "," + info.x3 + "," + info.y3);

			if (posx >= (ix + info.x1) && posx <= (ix + info.x2) && posy >= (iy + info.y1) && posy <= (iy + info.y2)) {
				var contentobj = document.getElementById("content_box");

				var xoffset = 0;
				var yoffset = 0;

				if (contentobj != null) {
					xoffset = __precious_getX(contentobj);
					yoffset = __precious_getY(contentobj);
				}
				
				if (!infolayer.isVisible()) {
					infolayer.hide();
					infolayer.moveLeft(xoffset + info.x3);
					infolayer.moveTop(yoffset + info.y3);
					infolayer.show();
				}
			} else {
				if (infolayer.isVisible()) {
					infolayer.hide();
				}
			}
		}
	}
}

function __precious_Info(image, x1, y1, x2, y2, x3, y3) {
	this.image = image;
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.x3 = x3;
	this.y3 = y3;
}

function __precious_info_register(image, x1, y1, x2, y2, x3, y3) {
	__precious_infos.push(new __precious_Info(image, x1, y1, x2, y2, x3, y3));
}

function __precious_popup(page, title, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
	var win = window.open(page, title, winprops);
	
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
	return win;
}

