﻿function constraintImageSize(img, cwidth, cheight) {
	if (cwidth > 0) {
		try {
			if ($j(img).width() > cwidth) {
				$j(img).width(cwidth);
			}
		}
		catch (e) {
		}
	}

	if (cheight > 0) {
		try {
			if ($j(img).height() > cheight) {
				$j(img).height(cheight);
			}
		}
		catch (e) {
		} 
	}
}

function makeImageSwappable(img) {
	$j(img).each(function(i) {
		// preload images
		var img1 = new Image();
		img1.src = $j(this).attr("swapmouseout");
		var img2 = new Image();
		img2.src = $j(this).attr("swapmouseover");

		if (!$j(this).hasClass("selected")) {
			// mouse over
			$j(this).mouseover(function(evt) {
				$j(this).attr("src", $j(this).attr("swapmouseover"));
			});
			$j(this).mouseout(function(evt) {
				$j(this).attr("src", $j(this).attr("swapmouseout"));
			});
		}
		else {
			$j(this).attr("src", $j(this).attr("swapmouseover"));
		}
	});
}

function isInside(obj, x, y) {
	var offset = obj.offset();
	return x >= offset.left &&
		x <= offset.left + obj.width() &&
		y >= offset.top &&
		y <= offset.top + obj.height();
}