﻿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="background-color:#dddddd;float:left;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++){
		if(pics[i].indexOf('.swf')>0){
			html+='<div onclick="clickImg(\''+id+'\','+i+');" id="'+id+'.'+i+'" style="margin-top:0px;display:none;height:"'+imgViewers[id].height+'px;" align="center"><embed id="'+id+'.img.'+i+'" type="application/x-shockwave-flash" width="'+imgViewers[id].width+'" height="'+imgViewers[id].height+'" play="true" src="'+pics[i]+'"/></div>';
		}else{
			html+='<div id="'+id+'.'+i+'" style="margin-top:0px;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>';
		}
	}
	$(id).innerHTML=html;
	
	html='<div style="float:left;margin-left:3px;width:400px;overflow:hidden;" id="'+id+'.tit"></div>'
	html+='<div style="float:right;height:20px;padding-top:2px;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>';
	$('parent.'+id).innerHTML=$('parent.'+id).innerHTML+html;
	
	var obj=$(id);
	var t = obj.offsetTop;
	while (obj = obj.offsetParent) t += obj.offsetTop;

	$(id+'.tit').style.top=(t+3)+'px';
}

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){
				$(id+'.'+i).style.display='';
				$(id+'.img.'+i).className='visibleLevel'+imgViewers[id].visibility;
				$(id+'.num.'+i).className='expli';				
			}else{
				$(id+'.'+i).style.display='none';
				$(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+"')",15000);
	}else{
		$(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=$(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);
			}
		}
		$(id+'.img.'+index).width=w;
		$(id+'.img.'+index).height=h;
	}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(obj,src){	
	var t = obj.offsetTop;
	var l = obj.offsetLeft;
	while (obj = obj.offsetParent){
		 t += obj.offsetTop;
		 l += obj.offsetLeft;
	}
	t-=250;
	if(t<0) t=0;	
	
	l-=250;
	if(l<0) l=0;	
	
	x=l;
	y=t;
	
	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>250){
				w=250;
				h=Math.floor(img.height/(img.width/250));
			}
		}else{
			if(img.height>250){
				h=250;
				w=Math.floor(img.width/(img.height/250));
			}
		}	
		
		IFrame.setContent(frmImgLayer,'<body style="margin:0;"><center><img onload="parent.IFrame.adjustSize(\'frmImgLayer\');" src="'+currentImg+'" height="'+h+'" width="'+w+'"/></center></body>');
				
		$('imgLayer').style.left=x+'px';
		$('imgLayer').style.top=y+'px';
		$('imgLayer').style.visibility='visible';
	}else{
		setTimeout(showImg,500);
	}
}

function hiddenImg(){
	if(showImgTimer){
		try{clearTimeout(showImgTimer);}catch(e){}
	}
	$('imgLayer').style.visibility='hidden';	
}

var tobeAdjustImages=new Array();
function adjustImage(id,okW,okH){
	if(tobeAdjustImages[id]==null){
		tobeAdjustImages[id]=new Image();
		tobeAdjustImages[id].src=$(id).src;
	}
	var img=tobeAdjustImages[id];
	if(img.width>0){
		var w=img.width;
		var h=img.height;
		if(img.width>img.height){
			if(img.width>okW){
				w=okW;
				h=Math.floor(img.height/(img.width/okW));
				$(id).style.marginTop=Math.floor((okH-h)/2)+'px';
			}
		}else{
			if(img.height>okH){
				h=okH;
				w=Math.floor(img.width/(img.height/okH));
				$(id).style.marginLeft=Math.floor((okW-w)/2)+'px';
			}
		}
		$(id).width=w;
		$(id).height=h;
	}else{
		setTimeout("adjustImage('"+id+"',"+okW+","+okH+")",500);
	}
}
