
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function Zoom () {
	var self = this;
	this._reticule = undefined;
	this._viewport = undefined;
	this._thumbnail = undefined;
	this._zoomImage = undefined;
	this._thumbnailRect = undefined;
	this._coords = undefined;
	this.previousX = undefined;
	this.previousY = undefined;
	this.activatedThumbMO = undefined;
	this.activatedReticuleMO = undefined;
	this.active = false;

	this.reticuleMouseMove = function (e) {
		// just move the reticule back under the mouse.
		self.moveReticuleToCursor(e);
	};

	this.reticuleMouseover = function(e) {
		if (self.activatedReticuleMO == undefined)
		{
			self.activatedReticuleMO = true;
			self._reticule.mousemove(self.reticuleMousemove);
		}
	};

	this.reticuleMousemove = function(e) {
		self.reticuleOut = false;
		self.mouseMoved(e);
	};

	this.turnOn = function() {
		self.active = true;
	}

	this.turnOff = function() {
		self.active = false;
	}

	this.thumbMousemove = function(e) {
		self.thumbOut = false;
		self.mouseMoved(e);
	};

	this.mouseMoved = function(e) {
		if (self.active)
		{
			if (!self.checkOutOfBounds(e))
			{
				if (e.pageX != self.previousX || e.pageY != self.previousY)
				{
					self.show();
					self.previousX = e.pageX;
					self.previousY = e.pageY;
					self.moveReticuleToCursor(e);
					self.moveZoomImageToCursor(e);
				}
			}
		}
	};

	this.show = function () {
		if (self._reticule != undefined)
		{
			self._reticule.show();
			self._viewport.show();
		}
	};

	this.start = function(e) {
		if (self.active)
		{
			self.show();
			var thumbnailOffset = self._thumbnail.offset();

			self._thumbnailRect = {
				top: 	thumbnailOffset.top,
				left: 	thumbnailOffset.left,
				right: 	thumbnailOffset.left + self._thumbnail.width(),
				bottom:	thumbnailOffset.top + self._thumbnail.height()
			};
			self.setReticuleSize();
			self.moveReticuleToCursor(e);

			if (self.activatedThumbMO == undefined) // if the mouse moves over the image we need to put it back in the reticule - this will fire a mouseover on the reticule
			{
				self.activatedThumbMO = true;
				self._thumbnail.mousemove(self.reticuleMouseMove);
			}
		}
	};

	this.end = function() {
		self._reticule.hide();
		self._viewport.hide();
		// Clear out old mousemove handlers
	};

	this.setReticuleSize = function() {
		if (self.active)
		{
			self._viewport.show();  			// Have to show the viewport to get the width
			var reticuleWidth = Math.round((self._viewport.width()/self._zoomImage.width())*self._thumbnail.width());
			var reticuleHeight = Math.round((self._viewport.height()/self._zoomImage.height())*self._thumbnail.height());
			self._reticule.css("width", reticuleWidth).css("height", reticuleHeight).show();
		}
	};

	this.moveReticuleToCursor = function(e) {
		if (self.active)
		{
			var y = 0;
			var x = 0;
			if(BrowserDetect.browser == 'Explorer') {
				y = e.clientY;
				x = e.clientX;
				if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
					y = e.clientY + document.body.scrollTop;
					x = e.clientX + document.body.scrollLeft
				}
				else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
					y = e.clientY + document.documentElement.scrollTop;
					x = e.clientX + document.documentElement.scrollLeft
				}
			}
			else {
				y = e.clientY;
				x = e.clientX;
				y += window.pageYOffset;
				x += window.pageXOffset
			}
			x = x - self._thumbnail.offset().left;
			y = y - self._thumbnail.offset().top;
			var reticuleX = x-(self._reticule.outerWidth()/2);
			var reticuleY = y-(self._reticule.outerHeight()/2);

			var trueLeft = self._thumbnailRect.left-self._thumbnail.offset().left;
			var trueRight = self._thumbnailRect.right-self._thumbnail.offset().left;

			var trueTop = self._thumbnailRect.top-self._thumbnail.offset().top;
			var trueBottom = self._thumbnailRect.bottom-self._thumbnail.offset().top;

			if (reticuleX > trueLeft && (reticuleX+self._reticule.outerWidth()) < trueRight)
			{
				self._reticule.css("left", reticuleX);
			}
			if (reticuleY > trueTop && (reticuleY+self._reticule.outerHeight()) < trueBottom)
			{
				self._reticule.css("top", reticuleY);
			}
		}
	};

	this.moveZoomImageToCursor = function(e) {
		if (self.active)
		{
			var offset = self._thumbnail.offset();
			var height = self._thumbnail.height();
			var width = self._thumbnail.width();
			var percentX = (e.pageX-offset.left)/width;
			var percentY = (e.pageY-offset.top)/height;
			/*self._coords.text("("+(Math.round(percentX*10000)/100)+"%,"+(Math.round(percentY*10000)/100)+"%)");*/

			//console.log("("+(Math.round(percentX*10000)/100)+"%,"+(Math.round(percentY*10000)/100)+"%)");
			// Move zoom image
			var viewportWidth = self._viewport.width();
			var viewportHeight = self._viewport.height();

			var zoomImgHeight = self._zoomImage.height();
			var zoomImgWidth = self._zoomImage.width();

			var left = -((zoomImgWidth*percentX)-(viewportWidth/2));
			var right = -((zoomImgWidth*percentX)+(viewportWidth/2));
			var top = -((zoomImgHeight*percentY)-(viewportHeight/2));
			var bottom = -((zoomImgHeight*percentY)+(viewportHeight/2));

			if (left <= 0 && right > (zoomImgWidth*-1)) {
				self._zoomImage.css("left", left);
			}

			if (top <= 0 && bottom > (zoomImgHeight*-1)) {
				self._zoomImage.css("top", top);
			}
		}

	};

	this.checkOutOfBounds = function(e) { // this will be good for something else
		var y = 0;
		var x = 0;
		y = e.pageY;
		x = e.pageX;
		var area = self._thumbnailRect;

		if (area != undefined && !(
			x > area.left &&
			x < area.right &&
			y > area.top &&
			y < area.bottom
				))
		{
			self.end();
			return true;
		}

		return false;
	};

	this.thumbMouseOut = function(e) {
		self.checkOutOfBounds(e);
	};

	this.reticuleMouseOut = function(e) {
		self.checkOutOfBounds(e);

	};

	this.init = function(reticule, zoom, zoomimg, thumbnailimage, coords) {
		
		thumbnailimage.mouseover(self.start);
		reticule.mouseover(self.reticuleMouseover);
		thumbnailimage.mouseout(self.thumbMouseOut);
		reticule.mouseout(self.reticuleMouseOut);


		self._coords = coords;
		self._reticule = reticule;
		self._viewport = zoom;
		self._zoomImage = zoomimg;
		self._thumbnail = thumbnailimage;
		self.active=true;
	};
}