// Copyright 2003; Gravity Media Group.
// This code may be used only for the purposes for which it was purchased
// Unauthorized duplication or usage is prohibited
// Contact woody@gravitymedia.com with any questions

function MapArea(href, x1, y1, x2, y2, target) {
	this.href = href;
	this.target = null;
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.target = typeof(target) == "undefined" ? null : target;
	this.menu = null;
	this.onmouseover = null;
	this.onmouseout = null;
	this.altText = null;
}

function SimpleRollover(name, offimg, onimg, href, target) {
	if (typeof(is) == "undefined") alert("You need to load browsercheck.js before using two-image-rollovers.");
	if (typeof(dEl) == "undefined") alert("You need to load dEl.js before using two-image-rollovers.");
	this.imgswitchtime = 50; //time in milliseconds to switch back the image on an image rollover.
	
	this.name = name;
	this.offimg = offimg;
	this.onimg = onimg;
	this.href = typeof(href) == "undefined" ? null : href;
	this.menu = null;
	this.target = typeof(target) == "undefined" ? null : target;
	this.altText = null;
	this.areas = new Array();

	// Keep this section?  Dunno.
/*	if (is.dom==1) {
		if (typeof(window.gravity) != "object") window.gravity = new Object();
		window.gravity.reloadCheck = function() {
			if (window.gravity.reloadCheck.x != window.innerWidth || window.gravity.reloadCheck.y != window.innerHeight) {
				document.location = document.location;
			}
			window.gravity.reloadCheck.x = window.innerWidth;
			window.gravity.reloadCheck.y = window.innerHeight;
			window.onresize = window.gravity.reloadCheck;
		}
	} else if (is.dom >= 2) { */
	if (typeof(window.srarray) == "undefined") window.srarray = new Array();
	var q = window.srarray.length;
	window.srarray[q] = new Image();
	window.srarray[q+1] = new Image();
	window.srarray[q].src = offimg;
	window.srarray[q+1].src = onimg;
	
	addToOnload("el" + this.name + " = new dEl('el" + this.name + "', " + window.srarray[q].width + ");");
	if (typeof(href) == "boolean") addToOnload("el" + this.name + ".write(\"<img src=\\\"" + this.onimg + "\\\" border='0' name='im" + this.name + "' id='im" + this.name + "' ismap usemap='#map" + this.name + "'><br>\");");
	else addToOnload("el" + this.name + ".write(\"<a href=\\\"" + this.href + "\\\" onmouseover='" + this.name + ".switchImage();' onmouseout='" + this.name + ".switchImageOut();'" + (this.target == null ? "" : " target='" + this.target + "'>") + "</a><img src='" + this.onimg + "' border='0'></a>\");");
	if (typeof(window.simpleimagearray) == "undefined") window.simpleimagearray = new Array();
	window.simpleimagearray[window.simpleimagearray.length] = this;
}

function sr_addMapArea(maparea) {
	if (typeof(maparea) == "undefined" || typeof(maparea.y2) == "undefined") {
		alert("addMapArea requires a variable of type 'MapArea'.  Please check your syntax.");
		return;
	}
	this.areas[this.areas.length] = maparea;
}
SimpleRollover.prototype.addMapArea = sr_addMapArea;

function sr_setMenu(menu) { this.menu = menu; }
SimpleRollover.prototype.setMenu = sr_setMenu;

function sr_setMouseOver(mo) { this.onmouseover = mo; }
SimpleRollover.prototype.setMouseOver = sr_setMouseOver;

function sr_setMouseOut(mo) { this.onmouseout = mo; }
SimpleRollover.prototype.setMouseOut = sr_setMouseOut;

function sr_switchImage(which) {
	var hasWhich = typeof(which) != "undefined";
	eval("if (typeof(el" + this.name + ") != 'undefined') clearTimeout(el" + this.name + ".rolloverTimer);");
	if (this.menu != null) {
		eval("if (typeof(" + this.menu + ") != 'undefined') {" + this.menu + ".show(); " + this.menu + ".notifyMouseOver(); }");
	}
	if (hasWhich && this.areas[which].menu != null) {
		eval("if (typeof(" + this.areas[which].menu + ") != 'undefined') {" + this.areas[which].menu + ".show(); " + this.areas[which].menu + ".notifyMouseOver(); }");
	}
	if (typeof(this.onmouseover) != "undefined") eval(this.onmouseover);
	if (hasWhich && this.areas[which].onmouseover != null && typeof(this.areas[which].onmouseover) != null) eval(this.areas[which].onmouseover);
	eval("if (hasWhich && typeof(el" + this.name + ") != 'undefined') el" + this.name + ".clipTo(this.areas[which].y1, this.areas[which].x2, this.areas[which].y2, this.areas[which].x1);"); 
	eval("if (typeof(el" + this.name + ") != 'undefined') el" + this.name + ".show();");
}
SimpleRollover.prototype.switchImage = sr_switchImage;

function sr_switchImageOut(which) {
	var hasWhich = typeof(which) != "undefined";
	if (this.menu != null) {
		eval("if (typeof(" + this.menu + ") != 'undefined') {" + this.menu + ".takeShowDuty(); " + this.menu + ".notifyMouseOut(); }");
	}
	if (hasWhich && this.areas[which].menu != null) {
		eval("if (typeof(" + this.areas[which].menu + ") != 'undefined') {" + this.areas[which].menu + ".takeShowDuty(); " + this.areas[which].menu + ".notifyMouseOut(); }");
	}
	if (typeof(this.onmouseout) != "undefined") eval(this.onmouseout);
	eval("if (typeof(el" + this.name + ") != 'undefined') el" + this.name + ".rolloverTimer = setTimeout('el" + this.name + ".hide()'," + this.imgswitchtime + ");");
}
SimpleRollover.prototype.switchImageOut = sr_switchImageOut;

function sr_moveToImage() {
	eval("el" + this.name + ".moveToImage('im" + this.name + "', 0, 0);");
}
SimpleRollover.prototype.moveToImage = sr_moveToImage;

function sr_getHtml() {
	rollElementStyles();
	if (this.areas.length == 0) {
		document.write("<table cellspacing='0' cellpadding='0' border='0'><tr><td>");
		document.write("<a href=\"" + this.href + "\" onmouseover=\"" + this.name + ".switchImage();\" onmouseout=\"" + this.name + ".switchImageOut();\"");
		if (this.target != null) document.write(" target=\"" + this.target + "\"");
		document.write("><img src=\"" + this.offimg + "\" border='0' name='im" + this.name + "' id='im" + this.name + "'");
		if (this.altText != null) document.write(" alt=\"" + this.altText + "\"");
		document.write("></a><br>");
		document.write("</td></tr></table>");
	} else {
		document.write("<table cellspacing='0' cellpadding='0' border='0'><tr><td>");
		document.write("<img src=\"" + this.offimg + "\" border=\"0\" name=\"im" + this.name + "\" id=\"im" + this.name + "\" ismap usemap=\"#map" + this.name + "\"><br>");
		document.write("</td></tr></table>");
		document.write("<map name='map" + this.name + "'>");
		for (var i = 0; i < this.areas.length; i++) {
			document.write("<area shape='rect' coords='" + this.areas[i].x1 + ", " + this.areas[i].y1 + ", " + this.areas[i].x2 + ", " + this.areas[i].y2);
			document.write("' href=\"" + this.areas[i].href + "\" onmouseover='" + this.name + ".switchImage(" + i + ");' onmouseout='" + this.name + ".switchImageOut(" + i + ");'");
			if (this.areas[i].target != null) document.write(" target='" + this.areas[i].target + "'");
			if (this.areas[i].altText != null) document.write(" alt='" + this.areas[i].altText + "'");
			document.write(">\n");
		}
		document.write("</map>");
	}
}
SimpleRollover.prototype.getHtml = sr_getHtml;

function rollElementStyles() {
	var initstr = "";
	var stylestr = "";
	for (var i = 0; i < simpleimagearray.length; i++) {
		initstr += simpleimagearray[i].name + ".moveToImage();\n";
		stylestr += "#el" + simpleimagearray[i].name + "{position:absolute; visibility:hidden;}\n";
		stylestr += "#im" + simpleimagearray[i].name + "{position:relative;}\n";
	}
	simpleimagearray = new Array();
	if (stylestr.length > 0) {
		document.write("<STYLE TYPE=\"text/css\">\n<!--\n" + stylestr + "-->\n</STYLE>\n");
		addToOnload(initstr);
		window.onreload = initstr;
	}
}