var imgViewers=new Array();

function writeViewer(id,width,height){
	var obj=new Object();
	obj.id=id;
	obj.width=width;
	obj.height=height;
	obj.index=0;
	obj.visibility=0;
	obj.interval=null;
	imgViewers[id]=obj;
	document.write('<div id="parent.'+id+'" style="margin-left:0px;float:left;border:1px solid;border-color:#3366FF;margin-top:5px;width:'+width+'px;height:'+(height+20)+'px;"><div id="'+id+'" style="width:'+width+'px;height:'+height+'px;overflow:hidden;"></div></div>');
}

function initViewer(id,pics,titles,links){
	imgViewers[id].cnt=pics.length;
	imgViewers[id].pics=pics;
	imgViewers[id].titles=titles;
	imgViewers[id].links=links;
	imgViewers[id].img=new Array();
	
	var html='';
	for(var i=0;i<pics.length;i++){
		html+='<div id="'+id+'.'+i+'" style="margin-top:3px;display:none;height:"'+imgViewers[id].height+'px;" align="center"><img id="'+id+'.img.'+i+'" class="visibleLevel0" src="'+pics[i]+'" border="0" onclick="clickImg(\''+id+'\','+i+');" onload="adjustImagesSize(\''+id+'\','+i+');"/></div>';
	}
	getElement(id).innerHTML=html;
	
	html='<div style="padding-top:2px;padding-left:5px;height:20px;float:left;width:'+(imgViewers[id].width-152)+'px;overflow:hidden;" class="visibleLevelNormal" id="'+id+'.tit"></div>'
	html+='<div style="float:right;height:20px;padding-top:2px;width:150px;padding-right:5px;" class="fucusNumber" align="right">';
	for(var i=1;i<=pics.length;i++){
		html+='<a id="'+id+'.num.'+(i-1)+'" onclick="showImage(\''+id+'\','+(i-1)+');"><span>'+i+'</span></a>';
	}
	html+='</div>';
	getElement('parent.'+id).innerHTML=getElement('parent.'+id).innerHTML+html;
}

function autoView(id){
	showImages(id,0,0);
}

function showImages(id){
	if(imgViewers[id].interval){
		clearTimeout(imgViewers[id].interval);
	}
	if(imgViewers[id].visibility<5){
		for(var i=0;i<imgViewers[id].cnt;i++){
			if(i==imgViewers[id].index){
				getElement(id+'.'+i).style.display='';
				getElement(id+'.img.'+i).className='visibleLevel'+imgViewers[id].visibility;
				getElement(id+'.num.'+i).className='expli';				
			}else{
				getElement(id+'.'+i).style.display='none';
				getElement(id+'.num.'+i).className='expli1';	
			}
		}
	}
	if(imgViewers[id].visibility==5){
		imgViewers[id].index++;
		if(imgViewers[id].index==imgViewers[id].cnt){
			imgViewers[id].index=0;
		}
		imgViewers[id].visibility=0;
		imgViewers[id].interval=setTimeout("showImages('"+id+"','"+imgViewers[id].index+"','"+imgViewers[id].visibility+"')",3000);
	}else{
		getElement(id+'.tit').innerHTML=imgViewers[id].titles==undefined?'':imgViewers[id].titles[imgViewers[id].index];	
		imgViewers[id].visibility++;
		imgViewers[id].interval=setTimeout("showImages('"+id+"','"+imgViewers[id].index+"','"+imgViewers[id].visibility+"')",200);
	}
}

function showImage(id,index){
	if(imgViewers[id].interval){
		clearTimeout(imgViewers[id].interval);
	}
	imgViewers[id].index=index;
	imgViewers[id].visibility=0;
	showImages(id);
}

function adjustImagesSize(id,index){
	if(imgViewers[id].img[index]==null){
		imgViewers[id].img[index]=new Image();
		imgViewers[id].img[index].src=getElement(id+'.img.'+index).src;
	}
	var img=imgViewers[id].img[index];
	if(img.width>0){
		var w=img.width;
		var h=img.height;
		if(img.width>img.height){
			if(img.width>imgViewers[id].width){
				w=imgViewers[id].width;
				h=img.height/(img.width/imgViewers[id].width);
			}
		}else{
			if(img.height>imgViewers[id].height){
				h=imgViewers[id].height;
				w=img.width/(img.height/imgViewers[id].height);
			}
		}
		getElement(id+'.img.'+index).width=w-6;
		getElement(id+'.img.'+index).height=h-6;
	}else{
		setTimeout("adjustImagesSize('"+id+"','"+index+"')",500);
	}
}

function clickImg(id,index){
	if(imgViewers[id].links&&imgViewers[id].links[index]){
		window.open(imgViewers[id].links[index],'','');
	}else{
		window.open(imgViewers[id].pics[index],'','');
	}
}

/////////////////////////////////////图片御览
//调用页面必须有一个隐藏的层，内容如下：
//<div id="imgLayer" style="position:absolute;height:350px;top:0px; width:300px; background-color:#FFFFFF;visibility:hidden;" class="bord_gray1 alignC pad3">
//	<div class="pad3"><span style="cursor:pointer; text-decoration:underline;" onClick="window.open(currentImg);">查看大图</span> <span style="cursor:pointer; text-decoration:underline;" onClick="hiddenImg();">关闭</span> <span style="cursor:pointer; text-decoration:underline;">收藏</span></div>
//	<iframe id="frmImgLayer" name="frmImgLayer" src="about:blank" frameborder="0" scrolling="no" vspale="0" height="300" width="100%"></iframe>
//	<div class="padT6"><span style="cursor:pointer; text-decoration:underline;" onClick="window.open(currentImg);"">查看大图</span> <span style="cursor:pointer; text-decoration:underline;" onClick="hiddenImg();">关闭</span> <span style="cursor:pointer; text-decoration:underline;">收藏</span></div>
//</div>
var currentImg='';
var showImgTimer=null;
var x=0;
var y=0;
function previewImg(event,src){
	if(event.clientX){
		x=event.clientX;
		y=event.clientY;
	}else if(event.pageX){
		x=event.pageX;
		y=event.pageY;
	}	
	currentImg=src;
	ImagePreLoader.addWithoutHW(src);
	showImgTimer=setTimeout(showImg,100);
}

function showImg(){
	if(currentImg==''){
		return;
	}
	
	hiddenImg();
	
	var img=ImagePreLoader.get(currentImg);
	if(img&&img.width&&img.width>0){
		var w=img.width;
		var h=img.height;
		if(img.width>img.height){
			if(img.width>300){
				w=300;
				h=img.height/(img.width/300);
			}
		}else{
			if(img.height>300){
				h=300;
				w=img.width/(img.height/300);
			}
		}	
		
		IFrame.setContent(frmImgLayer,'<body style="margin:0;"><center><img src="'+currentImg+'" height="'+h+'" width="'+w+'"/></center></body>');
				
		getElement('imgLayer').style.left=((x-150)<10?10:((x-150)))+'px';
		getElement('imgLayer').style.top=((y-150)<10?10:((y-150)))+'px';
		getElement('imgLayer').style.visibility='visible';
	}else{
		setTimeout(showImg,500);
	}
}

function hiddenImg(){
	if(showImgTimer){
		try{clearTimeout(showImgTimer);}catch(e){}
	}
	getElement('imgLayer').style.visibility='hidden';	
}