/**
 *
 * @access public
 * @return void
 **/
function ImageZoom(name,width,height){
	this.name       = name;
	this.maxWidth   = width;
	this.maxHeight  = height;

	this.images;
	this.preImage   = new Image();
	this.speed      = Array();
	this.needZoom   = Array();
	this.origWidth  = Array();
	this.origHeight = Array();

	this.init = function () {
		this.images = this.getElementsByClassName('zoom', 'img');
		var parent;
		var img, i;

		for (i in this.images) {
			if (this.images[i].tagName != "IMG") continue;
			this.origWidth[i] = 184; //this.images[i].offsetWidth;
			this.origHeight[i] = 139; //this.images[i].offsetHeight;

			this.preImage.src = this.images[i].src;

			var ratio = this.preImage.height / this.preImage.width;
			var ratioX = this.preImage.width / this.origWidth[i];
			var ratioY = this.preImage.height / this.origHeight[i];

			var maxsize = (this.origWidth[i] > this.origHeight[i]) ? this.origWidth[i] : this.origHeight[i];

			imgname = 'imgZoom' + i;
			if (this.isIE() && (parseFloat(navigator.appVersion.split("MSIE")[1]) < 9)) img = document.createElement('<img src="' + this.images[i].src + '" id="' + imgname + '" onmouseover="' + this.name + '.start(' + i + ');" onmouseout="' + this.name + '.cancel(' + i + ');" border="0">');
			else {
				img = document.createElement('img');
				img.setAttribute('src', this.images[i].src);
				img.setAttribute('id', imgname);
				img.setAttribute('onmouseover', this.name + '.start(' + i + ')');
				img.setAttribute('onmouseout', this.name + '.cancel(' + i + ')');
			}

			parent = this.images[i].parentNode;
			parent.replaceChild(img, this.images[i]);

			img.style.border = "none";
			img.style.zIndex = "50";
			img.style.position = "relative";

			if (ratioX >= ratioY) {
				//img.style.width = this.origWidth[i] + "px";
				//img.style.height = (this.preImage.height / ratioX) + "px";
				img.setAttribute('width', this.origWidth[i]);
				if(this.isIE() && (parseFloat(navigator.appVersion.split("MSIE")[1]) > 7))
					img.setAttribute('height', parseInt(this.origWidth[i]*ratio));	
			} else {
				//img.style.width = (this.preImage.width / ratioY) + "px";
				//img.style.height = this.origHeight[i] + "px";
				img.setAttribute('height', this.origHeight[i]);
				if (this.isIE() && (parseFloat(navigator.appVersion.split("MSIE")[1]) > 7))
					img.setAttribute('width', parseInt(this.origWidth[i] / ratio));
			}

			this.needZoom[i] = false;
			this.origWidth[i] = img.offsetWidth;
			this.origHeight[i] = img.offsetHeight;
		}
	}

	this.zoom = function (imgID) {
		var img = document.getElementById('imgZoom' + imgID);
		var diffX = (this.maxWidth - img.offsetWidth) / 5;
		var diffY = (this.maxHeight - img.offsetHeight) / 5;
		var imgRatio = (this.origWidth[imgID] / this.origHeight[imgID]);

		if (!img.style.width) img.style.width = img.offsetWidth + 'px';
		if (!img.style.height) img.style.height = img.offsetHeight + 'px';

		if (imgRatio < 1) {
			this.speed[imgID] = (diffY > 2) ? diffY : 2;
			if (img.offsetHeight < this.maxHeight) {
				img.style.width = (parseInt(img.style.width) + this.speed[imgID] * imgRatio) + "px";
				img.style.height = (parseInt(img.style.height) + this.speed[imgID]) + "px";
			} else this.needZoom[imgID] = false;
		} else {
			this.speed[imgID] = (diffX > 2) ? diffX : 2;
			if (img.offsetWidth < this.maxWidth) {
				img.style.width = (parseInt(img.style.width) + this.speed[imgID]) + "px";
				img.style.height = (parseInt(img.style.height) + this.speed[imgID] / imgRatio) + "px";
			} else this.needZoom[imgID] = false;
		}
		if (this.needZoom[imgID]) setTimeout(this.name + '.zoom(\'' + imgID + '\');', 50);
	}

	this.zoomOut=function(imgID){
		if(this.needZoom[imgID]) return true;

		var img=document.getElementById('imgZoom'+imgID);
		var diffX=(img.offsetWidth-this.origWidth[imgID])/5;
		var diffY=(img.offsetHeight-this.origHeight[imgID])/5;
		var imgRatio = (this.origWidth[imgID] / this.origHeight[imgID]);

		if (!img.style.width) img.style.width = img.offsetWidth + 'px';
		if (!img.style.height) img.style.height = img.offsetHeight + 'px';

		if(imgRatio<1){
			this.speed[imgID]=(diffY>2)?diffY:2;
			if(img.offsetHeight>this.origHeight[imgID]){
				img.style.width = (parseInt(img.style.width) - this.speed[imgID] * imgRatio) + "px";
				img.style.height = (parseInt(img.style.height) - this.speed[imgID]) + "px";
				setTimeout(this.name+'.zoomOut('+imgID+');',50);
			} else {
				img.style.width=this.origWidth[imgID]+'px';
				img.style.height=this.origHeight[imgID]+'px';
			}
		} else {
			this.speed[imgID]=(diffX>2)?diffX:2;
			if(img.offsetWidth>this.origWidth[imgID]){
				img.style.width = (parseInt(img.style.width) - this.speed[imgID]) + "px";
				img.style.height = (parseInt(img.style.height) - this.speed[imgID] / imgRatio) + "px";
				setTimeout(this.name+'.zoomOut('+imgID+');',50);
			} else {
				img.style.width=this.origWidth[imgID]+'px';
				img.style.height=this.origHeight[imgID]+'px';
			}
		}
	}

	this.start=function(imgID){
		var img=document.getElementById('imgZoom'+imgID);
		if(!this.needZoom[imgID]){
			this.needZoom[imgID]=true;
			if(img.offsetWidth<this.maxWidth) setTimeout(this.name+'.zoom('+imgID+');',50);
		}
	}

	this.cancel=function(imgID){
		var img=document.getElementById('imgZoom'+imgID);
		this.needZoom[imgID]=false;
		if(img.offsetWidth>=this.origWidth[imgID]) setTimeout(this.name+'.zoomOut('+imgID+');',50);
	}

	this.getElementsByClassName=function(className, tag, elm){
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}
		return returnElements;
	}

	this.isIE=function(){
		return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	}

	this.addInit(this.name);
}
