function zoomImage(){
	var self = this;
	this.brd=new Object();
	this.brd.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.brd.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
	this.brd.isSafari = navigator.userAgent.indexOf('Safari') != -1;
	this.brd.isMac = navigator.userAgent.indexOf('Mac') != -1;
	this.brd.isMSIE5_0 = this.brd.isMSIE && ((navigator.userAgent.indexOf('MSIE 5') != -1) || (navigator.userAgent.indexOf('MSIE 6') != -1));
	
	this.imageList=null;
	this.curentImage=0;
	this.uselist = false;
	this.pimage = null;
	this.pimagetimmer;
	this.pimageMargin=5;
	this.pop=null;
	this.bgdiv = null;
	this.idiv = null;

	this.use_frame=false;
	this.bgframe=new Object();
	this.bgframe.isset=false;
	
	this.loadgif='img/loading.gif';
	this.loading_txt='Loading ...';
	this.root_dir='';
	this.link='';
	
	this.single=true;
	
	this.parsePozition = function(id){
		for(var i=0; i<this.imageList.length; i++){
			if(this.imageList[i] == id){
				this.curentImage = i;
				break;
			}
		}
	};
	this.getMaxSize = function(){
		var w = (screen.availWidth-40);
		var h = (screen.availHeight-200);
		if(w<90){
			w = 90;
		}
		if(h<90){
			h = 90;
		}
		return [w, h];
	};
	
	this.getLink = function(id){
		var msz = this.getMaxSize();
		return this.link + 'thumbnail/?id='+id+';w='+msz[0]+';h='+msz[1]+';';
	}
	
	this.getIdLink = function(id){
		var link = this.link + id + '/';
		if(arguments[1]){
			link += arguments[1] + '/';
		}
		return link;
	};
	
	this.popById = function(id){
		if(arguments[1] && this.uselist){
			this.single = false;
			this.parsePozition(id);
		}
		var link = this.getLink(id);
		this.popFrame(link);
	};
	
	this.popFrame = function(img){
		this.hideContent();
		this.createFrame()
		this.startImage(img);
	};
	
	this.startImage = function(img){
		//this.hideImage();
		this.pimage = new Image();
		this.pimage.src = img;
		this.popLoader();
		if(this.brd.isGecko){
			this.pimage.onload = function(){self.initImage()};
		}
		else{
			this.loadImageEnd();
		}
	}
	
	this.hideContent = function(){
		if(!this.bgframe.isset){
			this.initBgFrame();
		}
		var div = document.createElement("div");
		div.style.position = "absolute";
		div.style.left = "0px";
		div.style.margin = "0px";
		div.style.top = "0px";
		div.style.width = "100%";
		div.style.height = document.documentElement.scrollHeight+"px";
		div.style.backgroundColor = "#333";
		//div.className = 'opa7';
		if(this.brd.isMSIE){
			div.style.filter = "alpha(opacity = 70)";
		}
		else{
			div.style.opacity = 0.7;
		}
		div.onclick = function(){self.pClose();};
		div.style.zIndex = 100;
		document.body.appendChild(div);
		this.bgdiv = div;
		this.setBgFrame(0, 0, this.bgdiv.offsetWidth, this.bgdiv.offsetHeight);
	};
	
	this.loadImageEnd = function(){
		if(this.pimage.src && !this.pimage.complete){
			this.pimagetimmer=setTimeout("imageZoom.loadImageEnd()", 100);
		}
		else{
			clearTimeout(this.pimagetimmer);
			this.initImage();
		}
	};
	
	this.createFrame = function(){
		var div = document.createElement("div");
		var width = 10;
		var height = 10;
		div.style.position = "absolute";
	
		div.style.backgroundColor = "white";
		div.style.display = "none";
		div.style.border = '1px solid #999';
		document.body.appendChild(div);
		div.innerHTML = div.innerHTML;
		div.style.margin="0px";
		div.style.padding=this.pimageMargin+"px";
		
		var image = new Image();
		image.align="center";
		div.appendChild(image);
		
		this.pop=div;
		
		this.pop.onmouseover = function(){self.onPop();}
		this.pop.onmouseout = function(){self.outPop();}
		div.style.display = "block";
		this.hideFrame();
		this.pop._image = image;
		this.pop._image.onclick = function(){self.pClose();};
		
		this.resizeFrame(width, height);
	};
	
	this.resizeFrame = function(w, h){
		if(this.pop){
			var div_pos = this.calcCenter(w, h);
			this.pop.setAttribute("width", w);
			this.pop.setAttribute("height", h);
			this.pop.style.left = div_pos[0] + "px";
			this.pop.style.top = div_pos[1] + "px";
			this.pop.style.width = w + "px";
			this.pop.style.height = h + "px";
		}
	};
	
	this.popLoader = function(){
		this.idiv = document.createElement("div");
		this.idiv.style.position = "absolute";
		this.idiv.style.left = "100px";
		this.idiv.style.top = "100px";
		this.idiv.style.width = "180px";
		//idiv.style.height = "30px";
		this.idiv.style.paddingTop = "10px";
		this.idiv.style.paddingBottom = "10px";
		this.idiv.style.backgroundColor = "#fff";
		this.idiv.className = 'opa7';
		this.idiv.style.zIndex = 101;
		var img = new Image();
		img.src = this.root_dir + this.loadgif;
		img.align = "left";
		img.hspace = 10;
		this.idiv.appendChild(img);
		this.idiv.onclick=function(){self.pClose()};//this.pClose();
		
		var ltext = document.createElement("p");
		
		ltext.innerHTML = this.loading_txt;
		this.idiv.appendChild(ltext);
		document.body.appendChild(this.idiv);
		var idiv_pos = this.calcCenter(this.idiv.offsetWidth, this.idiv.offsetHeight);
		this.idiv.style.left = idiv_pos[0]+"px";
		this.idiv.style.top = idiv_pos[1]+"px";
	};
	
	///////////////////////////////
	
	this.initImage = function(){
		if(this.pop && this.pop._image && this.pimage){
			
			this.pimage.align="center";
			this.pop._image.src=this.pimage.src;
			
			if(this.brd.isGecko && typeof(this.pimage.naturalWidth) != 'undefined'){
				var width = parseInt(this.pimage.naturalWidth);
				var height = parseInt(this.pimage.naturalHeight);
			}
			else{
				var width = parseInt(this.pimage.width);
				var height = parseInt(this.pimage.height);
			}
			this.resizeFrame(width, height);
			if(this.uselist && !this.single){
				this.addNavigation();
			}
			this.remLoader();
			this.unhideFrame();
			this.unhideImage();
			this.pimage = null;
		}
	}
	
	this.remLoader = function(){
		if(this.idiv){
			document.body.removeChild(this.idiv);
			this.idiv=null;
		}
	}
	
	this.unhideFrame = function(){
		if(this.pop){
			this.pop.className = 'opa10';
		}
	}
	this.hideFrame = function(){
		if(this.pop){
			this.pop.className = 'opa5';
		}
	}
	
	this.hideImage = function(){
		if(this.pop._image){
			this.pop._image.className = 'opaimg';
		}
	}
	this.unhideImage = function(){
		if(this.pop._image){
			this.pop._image.className = 'opa10';
		}
	}
	
	
	
	this.pClose = function(){
		if(this.pop){
			if(this.pop._back){
				document.body.removeChild(this.pop._back);
			}
			if(this.pop._next){
				document.body.removeChild(this.pop._next);
			}
			document.body.removeChild(this.pop);
			this.pop=null;
		}
		if(this.bgdiv){
			document.body.removeChild(this.bgdiv);
			this.bgdiv=null;
			this.unSetBgFrame();
		}
		if(this.idiv){
			//document.body.removeChild(this.idiv);
			this.idiv=null;
		}
		this.single=true;
	}
	
	this.createImageContent = function(){
		if(this.pop){
			this.pimage.allign="center";
			this.pop.appendChild(this.pimage);
			this.pimage.onclick = function(){self.pClose()};
			//addNavigation();
		}
	}
	
	this.calcCenter = function(w,h){
		var top = 0;
		var left = 0;
		top = (((screen.availHeight-168) - h) / 2) + document.documentElement.scrollTop;
		left = ((screen.availWidth - w) / 2) + document.documentElement.scrollLeft;
		return [left, top];
	}
	
	this.onPop = function(){
		if(this.pop._next){
			this.pop._next.className='onpop';
			this.pop._next.onmouseout = function(){ this.className = 'onpop';};
		}
		if(this.pop._back){
			this.pop._back.className='onpop';
			this.pop._back.onmouseout = function(){ this.className = 'onpop';};
		}
	}
	
	this.outPop = function(){
		if(this.pop._next){
			this.pop._next.className='opa0';
			this.pop._next.onmouseout = function(){ this.className = 'opa0';};
		}
		if(this.pop._back){
			this.pop._back.className='opa0';
			this.pop._back.onmouseout = function(){ this.className = 'opa0';};
		}
	}
	
	this.setBgFrame = function(left, top, width, height){
		if(this.use_frame && this.bgframe.isset){
			this.bgframe.style.left=left;
			this.bgframe.style.top=top;
			this.bgframe.style.width=width;
			this.bgframe.style.height=height;
			this.bgframe.style.visibility='visible';
		}
	}
	
	this.unSetBgFrame = function(){
		if(this.use_frame && this.bgframe.isset){
			this.bgframe.style.left='0';
			this.bgframe.style.top='0';
			this.bgframe.style.visibility='hidden';
		}
	}
	
	this.initBgFrame = function(){
		if(this.brd.isMSIE5_0){
			this.use_frame=true;
			this.bgframe=document.createElement('iframe');
			this.bgframe.style.position='absolute';
			this.bgframe.style.visibility='hidden';
			this.bgframe.style.left='0';
			this.bgframe.style.top='0';
			this.bgframe.style.border='none';
			this.bgframe.scrolling="no";
			this.bgframe.frameBorder="0px";
			this.bgframe.style.width='10';
			this.bgframe.style.height='10';
			this.bgframe.style.filter = "alpha(opacity = 0)";
			this.bgframe.style.zIndex = 100;
			//bgframe.style.zIndex='9';
			this.bgframe.isset=true;
			document.body.appendChild(this.bgframe);
		}
		else{
			this.bgframe.isset=true;
		}
		
	}
	
	this.initList = function(list){
		if(list.length > 0){
			if(this.imageList){
				for(var i=0; i<list.length; i++){
					this.imageList.push(list[i]);
				}
			}
			else{
				this.imageList = list;
			}
			this.uselist = true;
		}
	};
	
	this.addNavigation = function(){
		var h = parseInt(this.pop.offsetHeight);
		var w = parseInt(this.pop.offsetWidth);
		var t = parseInt(this.pop.offsetTop);
		var l = parseInt(this.pop.offsetLeft);
		
		var dw = 64;
		var dh = 64;
		if((this.curentImage-1)>-1){
			if(!this.pop._back){
				var dvs = document.createElement('div');
				document.body.appendChild(dvs);
				//dvs.style.backgroundColor='#fff';
				dvs.innerHTML = '<img src="'+this.root_dir+'img/p_back.png" border="0" alt="" style="cursor:pointer;"/>';
				dvs.innerHTML = dvs.innerHTML;
				dvs.style.margin='0px';
				dvs.style.display = 'block';
				dvs.style.width = dw + 'px';
				dvs.style.height = dh + 'px';
				dvs.style.position = 'absolute';
				dvs.className = 'opa0';
				dvs.onmouseover = function(){ this.className='opaNav';};
				dvs.onmouseout = function(){ this.className = 'opa0';};
				this.pop._back = dvs;
			}
			this.pop._back.style.top = Math.round(t+((h-dh)/2))+'px';
			this.pop._back.style.left = (l+this.pimageMargin)+'px';
			this.pop._back.onclick = function(){self.zoomNext(-1);};
		}
		else{
			if(this.pop._back){
				document.body.removeChild(this.pop._back);
			}
			this.pop._back = null;
		}
		
		if((this.curentImage+1)<this.imageList.length){
			if(!this.pop._next){
				var dvs2 = document.createElement('div');
				document.body.appendChild(dvs2);
				//dvs2.style.backgroundColor='#fff';
				dvs2.innerHTML = '<img src="'+this.root_dir+'img/p_next.png" border="0" alt="" style="cursor:pointer;"/>';
				dvs2.innerHTML = dvs2.innerHTML;
				dvs2.style.margin='0px';
				dvs2.style.display = 'block';
				dvs2.style.width = dw + 'px';
				dvs2.style.height = dh + 'px';
				dvs2.style.position = 'absolute';
				dvs2.className = 'opa0';
				dvs2.onmouseover = function(){ this.className='opaNav';};
				dvs2.onmouseout = function(){ this.className = 'opa0';};
				this.pop._next = dvs2;
			}
			this.pop._next.style.top = Math.round(t+((h-dh)/2))+'px';
			this.pop._next.style.left = (l - this.pimageMargin + w - dw)+'px';
			this.pop._next.onclick = function(){ self.zoomNext(1);};
		}
		else{
			if(this.pop._next){
				document.body.removeChild(this.pop._next);
			}
			this.pop._next = null;
		}
	};
	
	this.zoomNext = function(sk){
		if(!this.idiv){
			this.hideImage();
			this.nvgs(sk);
		}
	};
	
	this.nvgs = function(sk){
		if((this.curentImage + sk) < (this.imageList.length) && (this.curentImage + sk)>-1){
			this.curentImage +=sk;
			var link = this.getLink(this.imageList[this.curentImage]);
			this.startImage(link);
		}
	};
	
	this.url2id = function(url){
		var arg = url.split('/');
		var id = 0;
		for(var i=0; i<arg.length; i++){
			if(/^[0-9]+$/.test(arg[i])){
				id = arg[i];
				break;
			}
		}
		return id;
	};
	
	this.autoScanImg = function(){
		var imgs = document.body.getElementsByTagName('a');
		var imgids = new Array();
		for(var i=0; i<imgs.length; i++){
			if(imgs[i].rel){
				this.parseRel(imgs[i]);
			}
		}
		var imgs = document.body.getElementsByTagName('img');
		var imgr = new RegExp("^max([listdobjrc]+)Image", "gi");
		for(var j=0; j<imgs.length; j++){
			if(imgs[j].getAttributeNode('onclick')){
				if(imgr.test(imgs[j].getAttributeNode('onclick').nodeValue)){
					imgs[j].className +=" maximgcursor";
				}
			}
		}
	};
	
	this.parseRel = function(obj){
		if(/^id:[0-9]+$/.test(obj.rel)){
			var arg = obj.rel.split(':');
			obj.onclick = function(){
				self.popById(arg[1]);
				return false;
			};
			obj.removeAttribute('rel');
		}
		else if(/^list:[0-9]+$/.test(obj.rel)){
			var arg = obj.rel.split(':');
			this.initList([arg[1]]);
			obj.onclick = function(){
				self.popById(arg[1], true);
				return false;
			};
			obj.removeAttribute('rel');
		}
		else if(/^url:(.+)$/.test(obj.rel)){
			var arg = obj.rel.split(':');
			obj.onclick = function(){
				self.popFrame(arg[1]);
				return false;
			};
			obj.removeAttribute('rel');
		}
		else if(/^id:[0-9]+:[0-9]+$/.test(obj.rel)){
			var arg = obj.rel.split(':');
			obj.onclick = function(){
				self.popFrame(self.getIdLink(arg[1], arg[2]));
				return false;
			};
			obj.removeAttribute('rel');
		}
		else if(/^maximagebox$/.test(obj.rel)){
			var id = this.url2id(obj.href);
			this.initList([id]);
			obj.onclick = function(){
				self.popById(id, true);
				return false;
			};
			obj.removeAttribute('rel');
		}
	}
}

var imageZoom = new zoomImage();

imageZoom.root_dir = maximage_root;
imageZoom.loading_txt = maximage_loading_txt;
imageZoom.link = maximage_link;

function maxListImage(id){
	imageZoom.popById(id, true);
}

function maxIdImage(id){
	if(arguments[1]){
		imageZoom.popFrame(imageZoom.getIdLink(id, arguments[1]));
	}
	else{
		imageZoom.popById(id);
	}
}

function maxObjImage(t){
	var id = imageZoom.url2id(t.src);
	if(id>0){
		if(arguments[1]){
			maxIdImage(id, arguments[1]);
		}
		else{
			maxIdImage(id);
		}
	}
}

function maxSrcImage(url){
	imageZoom.popFrame(url);
}

function scanImg(){
	imageZoom.autoScanImg();
}

