//Config
var maxWidth=(window['glzMaxWidth']!=null)?glzMaxWidth:500;
var maxHeight=(window['glzMaxHeight']!=null)?glzMaxHeight:500;
var fixedWidth=(window['glzFixedWidth']!=null)?glzFixedWidth:0;
var fixedHeight=(window['glzFixedHeight']!=null)?glzFixedHeight:0;
var preg=/(.*)thumb_(.*)$/;

//Initialization
var onWin=false;
var imgLoaded=false;
var agent;
var timer=0;
var eventX=0;
var eventY=0;

function showWin(event, use_orig, use_other){
	//Check user browser
	agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';
	
	var item=(event.target!=null)?event.target:event.srcElement;
	var div=document.getElementById('img_div');
	var image=document.getElementById('image');
	if(use_other!='' && use_other!=null){
		image.src=use_other;
	}
	else{
		if(use_orig==false || use_orig==null){
			var path=item.src;
			var res=path.match(preg);
			if(res==null) return false;
			image.src=res[1]+res[2];
		}
		else{
			image.src=item.src;
		}
	}

	div.style.display='block';
	onWin=true;
	
	timer=setTimeout('resizeImage()', 100);
}

function hideWin(){
	var div=document.getElementById('img_div');
	var image=document.getElementById('image');
	var spacer=document.getElementById('image_spacer');
	
	div.style.display='none';
	div.innerHTML='<img src="'+spacer_path+'" id="image_spacer" border="1px" style="border-color: black; display: block;">';
	div.innerHTML+='<img src="#" id="image" border="1px" style="border-color: black; display: none;">';
	onWin=false;
	imgLoaded=false;
	clearTimeout(timer);
}

function moveImgWin(event, screenX, screenY){
	var div=document.getElementById('img_div');
	if(onWin==false) return false;

	var xoffset=0;	
	var yoffset=0;
	var xPos=0;
	var yPos=0;
	if(event!=null){
		xPos=event.clientX;
		yPos=event.clientY;
	}
	else{
		xPos=screenX;
		yPos=screenY;
	}

	//Get window size
	var offsetTop=0;
	var offsetLeft=0;
	if(window.navigator.appName=='Netscape') {
		offsetTop=window.pageYOffset;
		offsetLeft=window.pageXOffset;
	}
	else{
		offsetTop=document.documentElement.scrollTop;
		offsetLeft=document.documentElement.scrollLeft;
	}
	
	if(window.navigator.appName=='Opera'){
		var scr_width=document.body.clientWidth+offsetLeft;
		var scr_height=document.body.clientHeight+offsetTop;
	}
	else{
		var scr_width=document.documentElement.clientWidth+offsetLeft;
		var scr_height=document.documentElement.clientHeight+offsetTop;		
	}

	if(imgLoaded){
		var image=document.getElementById('image');
		if((image.width+xPos+60)>(scr_width)) xoffset=(image.width+xPos)-scr_width+(scr_width-xPos)+80;
		if((image.height+yPos+offsetTop+10)>(scr_height)) yoffset=(image.height+yPos+offsetTop)-scr_height+10;
	}

	div.style.left=(xPos+40-xoffset)+"px";
	div.style.top=(yPos+offsetTop-yoffset)+"px";
	
	if(event!=null){
		eventX=event.clientX;
		eventY=event.clientY;		
	}
}

function resizeImage(){
	var image=document.getElementById('image');
	var spacer=document.getElementById('image_spacer');
	
	imgLoaded=IsImageOk(image);
	if(imgLoaded==true){
		var coof=1;
		image.style.display='block';
		spacer.style.display='none';
		var width=image.width;
		var height=image.height;
		if(fixedWidth>0 && fixedHeight>0){
			if(width>=height) coof= fixedWidth/width;
			if(height>width) coof= fixedHeight/height;			
		}
		else{
			if(width> maxWidth && width>=height) coof= maxWidth/width;
			if(height> maxHeight && height>width) coof= maxHeight/height;			
		}
		image.width=width*coof;
		image.height=height*coof;
		imgLoaded=true;
		moveImgWin(null, eventX, eventY);
		return true;
	}
	timer=setTimeout('resizeImage()', 100);
	return false;
}

function IsImageOk(img) {
     if (!img.complete) {
        return false;
    }
	
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    if(img.width==5 || img.height==5) return false;
    // No other way of checking: assume it's ok.
    return true;
}

function initImages(){
	var len=document.images.length;
	agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';
	for(var i=0; i<len; i++){
		var hasParent=checkParent(document.images[i]);
		if(hasParent){
			var elem=document.images[i];
			if(agent=='Firefox'){
				elem.setAttribute('onmouseover', 'javascript: showWin(event, true);');
				elem.setAttribute('onmouseout', 'javascript: hideWin();');
				elem.setAttribute('onmousemove', 'javascript: moveImgWin(event);');
				elem.setAttribute('style', 'cursor: pointer;');
			}
			else{
				elem.onmouseover=function(){showWin(window.event, true);}
				elem.onmouseout=function(){hideWin();}
				elem.onmousemove=function(){moveImgWin(window.event);}
				elem.style.cursor='pointer';
			}
		}
	}
}

function checkParent(elem){
	if(elem.parentNode==null || elem.tagName=='BODY') return false;
	if(elem.parentNode.className=='images_div') return true;
	return checkParent(elem.parentNode);
}
