var imz0; var imz1; var imz2;
var lit=null;
var miver="3.1";

// Dodatki
// 3.1:
//		tabienie się po linkach już działa

var OriginalMouseDisabled=false;

function MiniBrowser() {
	var b=navigator.appName;
	if (b=="Netscape") this.b="ns";
	else if (navigator.userAgent.indexOf("Opera")>0) this.b = "opera";
	else if (b=="Microsoft Internet Explorer") this.b="ie";
	this.version=navigator.appVersion;
	this.v=parseInt(this.version);
	this.ns=(this.b=="ns" && this.v>=4);
	this.ns4=(this.b=="ns" && this.v==4);
	this.ns5=(this.b=="ns" && this.v==5);
	this.ie=(this.b=="ie" && this.v>=4);
}
is = new MiniBrowser();


var ObjectsCollection = new Array;

function mxObject (name) {
	this.name = name;
	if (is.ie) this.parentobject=document.images[name];
	else if (is.ns) this.parentobject=document[name];

	this.Reposition = mxObjectReposition;
	this.IsHit = mxObjectIsHit;

	if (is.ie || is.ns5) {
		this.parentparent = this.parentobject.parentNode;
		this.parentparenttag = this.parentparent.tagName;
	}
	else {
		this.parentparent = null;
		this.parentparenttag = null;
	}

	this.mouseover = mxObjectMouseOver;
	this.onmouseover = null;
	this.mouseout = mxObjectMouseOut;
	this.onmouseout = null;
	this.mousemove = mxObjectMouseMove;
	this.onmousemove = null;
	this.mousedown = mxObjectMouseDown;
	this.onmousedown = null;
	this.mouseup = mxObjectMouseUp;
	this.onmouseup = null;

	this.isundermouse = false;
	this.ismousedown = false;

	this.Reposition();

	ObjectsCollection[ObjectsCollection.length] = this;
}

function mxObjectMouseOver (x,y) { with (this) { if (isundermouse) return; isundermouse=true; if (onmouseover) onmouseover(x,y); } }
function mxObjectMouseOut (x,y) { with (this) { if (!isundermouse) return; isundermouse=false; if (onmouseout) onmouseout(x,y); } }
function mxObjectMouseMove (x,y) { with (this) { if (!isundermouse) return; if (onmousemove) onmousemove(x,y); } }
function mxObjectMouseDown (x,y,b) { if (!this.isundermouse) return; this.ismousedown=true; if (this.onmousedown) this.onmousedown(x,y,b); }
function mxObjectMouseUp (x,y,b) { if (!this.ismousedown) return; if (b==0) this.ismousedown=false; if (this.onmouseup) this.onmouseup(x,y,b); }

function mxObjectReposition () {
	t=this.parentobject;
	if (is.ie || is.ns5) {
		op=t.offsetParent;
		this.x=t.offsetLeft; this.y=t.offsetTop;
		while (op) {
			this.x+=op.offsetLeft;
			this.y+=op.offsetTop;
			op = op.offsetParent;
		}
		this.w = t.offsetWidth;
		this.h = t.offsetHeight;
	} else {
		this.x = t.x;
		this.y = t.y;
		this.w = t.width;
		this.h = t.height;
	}
}

function mxObjectIsHit (x,y) {
//	status = x+">="+this.x+" "+x+"<"+this.x+this.w+" "+y+">="+this.y+" "+y+"<"+this.y+this.h;
	return ((x>=this.x) && (x<this.x+this.w) && (y>=this.y) && (y<this.y+this.h));
}

function Reposition (objects) {
	for (obj in objects) objects[obj].Reposition();
}

var MouseEvents = new Array;
MouseEvents = new Array;

function AddMouseEvent (fun) {
	MouseEvents[MouseEvents.length]=fun;
}

function MouseObject () {
	this.x=0;
	this.y=0;
	this.screenx=0;
	this.screeny=0;
	this.button=0;
	this.isdragging=false;
}
var Mouse = new MouseObject;

var log="";
var count=0;

function MouseHandler (ev) {
//	cur=document.all.curs.style;
//	cur.visibility="hidden";

	if (is.ie) ev=event;
	var x = (is.ns)? ev.pageX : ev.x+document.body.scrollLeft;
	var y = (is.ns)? ev.pageY : ev.y+document.body.scrollTop;
	var sx = (is.ns) ? ev.x : ev.x;
	var sy = (is.ns) ? ev.y : ev.y;
	var what = ev.type;
	var which = (is.ns) ? ( ev.which==1 ? 1 : ( ev.which==2 ? 4 : 2 ) ) : ev.button;

	var i,target;

	Mouse.x=x; Mouse.y=y;
	Mouse.screenx=sx; Mouse.screeny=sy;

	if (what=="mousedown") Mouse.button=which;
	if (what=="mouseup") Mouse.button = Mouse.button ^ which;

	if (is.ie) x-=2; y-=2;

	log+=what+" "+x+" "+y+"\n";	

//	cur.left=Mouse.x+20;
//	cur.top=Mouse.y+20;

	if (what=="mousemove")
		for (i in MouseEvents) {
			target = MouseEvents[i];
			if (target.isundermouse) {
				if (!target.IsHit(x,y)) {
					target.mouseout(x-target.x,y-target.y);
				}
			}
		}
	for (i in MouseEvents) {
		target = MouseEvents[i];
		if (target.IsHit(x,y)) {
			if (what=="mousemove") {
				if (!target.isundermouse) target.mouseover(x-target.x,y-target.y);
									 else target.mousemove(x-target.x,y-target.y);
				if (target.ismousedown) target.mousedown(x-target.x,y-target.y);
			}
			if (what=="mousedown") target.mousedown(x-target.x,y-target.y,which);
			if (what=="mouseup") target.mouseup(x-target.x,y-target.y,which);
			if (Mouse.button>0) Mouse.isdragging=true;
		}
		if (what=="mouseup") {
			target.ismousedown=false;
			Mouse.isdragging=false;
		}
	}
	if (is.ie) x+=2; y+=2;

//	cur.visibility="visible";
//	count++;
//	window.status=count;
	
	return (!OriginalMouseDisabled && (!Mouse.isdragging || (!is.ns5 && what=="mousedown")));
}
function CallMouseHandler (ev) {
	return MouseHandler (ev);
}

function InitMouseEvents() {
	document.onmousemove = CallMouseHandler;
	document.onmousedown = CallMouseHandler;
	document.onmouseup = CallMouseHandler;
	if (is.ns) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP );
}

function MIerror(co) { alert ("mxImages - error\n\n"+co); return false; }

function MIinit(quiet) {
	var ok,i;

	if (!(is.ns || is.ie)) return false;

	imz0=new Array; imz1=new Array; imz2=new Array;
	for (i=0; i<document.images.length; i++) {
		var name = document.images[i].name;
		if (name && (name.substr(0,2) == "MI")) {
			var alt = document.images[i].alt;
			var src = document.images[i].src;
			document.images[i].alt = "";
			var srccore = src.substr(0,src.lastIndexOf('.')-1);
			var srcext  = src.substr(src.lastIndexOf('.'),9);
			if (!(MIactivate(name,srccore,srcext,alt))) return false;
		}
	}
	if (quiet!=1) { window.status="MImages v"+miver+" OK"; }

	InitMouseEvents();
	if (is.ns5) RunNetscape5StatusPreserver();
	RunPositionPreserver();

	return true;
}

function RunNetscape5StatusPreserver() {
	if (window.status!="") window.status=window.status;
	setTimeout ("RunNetscape5StatusPreserver()",300);
}

function RunPositionPreserver() {
	Reposition (ObjectsCollection);
	setTimeout ("RunPositionPreserver()",1000);
}

function MIpreload (src,ext,name,ile) {
	imz0[name]=new Image; imz0[name].src=src+"0"+ext;
	if (ile & 2) { imz1[name]=new Image; imz1[name].src=src+"1"+ext; }
	if (ile & 4) { imz2[name]=new Image; imz2[name].src=src+"2"+ext; }
}

function MIactivate(name,src,ext,text) {
	if (!(is.ns || is.ie)) return false;

	if ((ext) && (ext.charAt(0)!='.')) { text=ext; ext=".gif"; }
	if (!ext) ext=".gif";
	if (!src) src=name;

	if (!document.images[name]) return MIerror ('MIactivate: nie ma <img name="'+name+'">');

	if (text) {
		if (text.indexOf("'")>=0)
			for (var i=0;i<=text.length;i++)
				if (text.charAt(i)=="'") {
					text = text.substr(0,i) + "\\'" + text.substr(i+1,99);
					i+=2;
				}
		stat1=" window.status='"+text+"'; return true;";
		stat0=" window.status=''; return true;";
	}
	else stat0=stat1="";

	justbits=7;
	if (name.indexOf("__1")>=0) justbits=3;
	if (name.indexOf("__2")>=0) justbits=5;

	MIpreload(src,ext,name,justbits);

	var x = new mxObject(name);

	if (x.parentparenttag=="A") {
		x.parentparent.onfocus = new Function("MIblurnum(this,"+MouseEvents.length+")");
		x.parentparent.onmouseover = new Function(stat1); stat1="";
		x.parentparent.onmouseout = new Function(stat0); stat0="";
	}
	if (justbits & 2) x.onmouseover = new Function("MIlite('"+name+"');"+stat1);
	else x.onmouseover = new Function(stat1);
	x.onmouseout  = new Function("MIdark('"+name+"');"+stat0);
	if (justbits & 4) {
		x.onmousedown = new Function("MIclik('"+name+"');");
		if (justbits & 2) x.onmouseup = new Function("MIlite('"+name+"');"+stat0);
					 else x.onmouseup = new Function("MIdark('"+name+"');"+stat0);
	} else x.onmouseup = new Function("MIlite('"+name+"');"+stat1);
		
	AddMouseEvent(x);

	return true;
}

function MIblurnum(ja,ktory) { x=MouseEvents[ktory]; if ((is.ie) && (x.isundermouse) && (x.ismousedown)) ja.blur(); }

function MIclik(co) { document.images[co].src = imz2[co].src; }
function MIlite(co) { document.images[co].src = imz1[co].src; }
function MIdark(co) { document.images[co].src = imz0[co].src; }
