
function getElementsById(name) 
{ 
	var returns = document.getElementsByName(name); 
	if(returns.length > 0) return returns; 
	returns = new Array(); 
	var e = document.getElementsByTagName("div"); 
	for(i = 0; i < e.length; i++) {
		if(e[i].getAttribute("id") == name) { 
			returns[returns.length] = e[i]; 
		} 
	} 
	return returns; 
}

function EnhancedImage(src,onLoaded){
    var self = this;
    this.src = src;
    this.width = 0;
    this.height = 0;
    this.onLoaded = onLoaded;
    this.loaded = false;
    this.image = null;
    
    this.load = function(){
        if(this.loaded) return;
        this.image = new Image();
        this.image.src = this.src;
        function loadImage(){
            if(self.width != 0 && self.height != 0)
			{
                clearInterval(interval);
                self.loaded = true;
                self.onLoaded(self);
            }
            self.width = self.image.width;
            self.height = self.image.height;
        }
        var interval = setInterval(loadImage,100);
    }
}


function ImageLoader(src,obj)
{
	var img = new EnhancedImage(src,onImageLoad);
	function onImageLoad(image){
		var hm = "<img src=\"" + src + "\" width=\"304\" style=\"border:solid 3px #DEDEDE;\">"; // onclick=\"ImageShow(this)\">";
		obj.innerHTML = hm;
	}
    img.load();
}

function ImageLargeLoader(src,obj)
{
	var img = new EnhancedImage(src,onImageLoad);
	function onImageLoad(image){
		var hm = "<img src=\"" + src + "\" width=\"700\" onclick=\"ImageShow(this)\">";
		obj.innerHTML = hm;
	}
    img.load();
}

function ImageLargeLoader2(src,obj)
{
	var img = new EnhancedImage(src,onImageLoad);
	function onImageLoad(image){
		var hm = "<img src=\"" + src + "\" onload=\"imageResize2(this)\">";
		obj.innerHTML = hm;
	}
    img.load();
}

function ImageLargeLoader3(src,obj)
{
	var img = new EnhancedImage(src,onImageLoad);
	function onImageLoad(image){
		obj.innerHTML = "<img src=\"" + src + "\" id=\"project_graph\">";
	}
    img.load();
}

var last_scroll = null;

function LoadImages()
{
	var dvs = getElementsById("imgList");
	for(var i=0; i<dvs.length; i++)
	{
		var url = dvs[i].getAttribute("title");
		ImageLoader(url,dvs[i]);
	}
}

function LoadLargeImages()
{
	var dvs = getElementsById("imgList");
	for(var i=0; i<dvs.length; i++)
	{
		var url = dvs[i].getAttribute("title");
		ImageLargeLoader(url,dvs[i]);
	}
}

function LoadLargeImages2()
{
	var dvs = getElementsById("imgList");
	for(var i=0; i<dvs.length; i++)
	{
		var url = dvs[i].getAttribute("title");
		ImageLargeLoader2(url,dvs[i]);
	}
}

function LoadLargeImages3()
{
	var dvs = getElementsById("imgList");
	for(var i=0; i<dvs.length; i++)
	{
		var url = dvs[i].getAttribute("title");
		ImageLargeLoader3(url,dvs[i]);
	}
}

function ImageShow2(obj,src)
{
	if(obj.className.substr(0,6) == "image_") {
		obj.className = "loaded_" + obj.className;
		obj.innerHTML = "<div id=\"imgList\" title=\"" + src.replaceAll("_thumbs.",".") + "\"><img src=\"/images/ajax-loader.gif\"></div>";
		ImageLargeLoader2(src.replaceAll("_thumbs.","."), obj);
	}
	else {
		obj.className = "image_local";
		if(src.substr(0,4)=="http") obj.innerHTML = "<img src=" + src + " onload=\"imageAutoThumbs(this)\">"; 
		else obj.innerHTML = "<img src=" + src + ">"; 
	}
}

function ImageShow(obj)
{
	var fname = obj.src.replaceAll("_thumbs", "");
	window.open(fname);
}

function FileShow2(id)
{
	var iWidth = document.body.clientWidth;
	var iHeight = document.body.scrollHeight + 24;
	var vHeight = document.documentElement.clientHeight;
	
	var sHeight = document.body.clientHeight+24;
	if(sHeight < iHeight) sHeight = iHeight;
	if(sHeight < 900) sHeight = 900;

	var bgObj = document.createElement("div");
	bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:" + iWidth + "px;height:" + sHeight + "px;text-align:center;filter:alpha(opacity=60);opacity:0.6;background-color:#000000;z-index:3001;";
	document.body.appendChild(bgObj);

	var fgObj = document.createElement("div");
	fgObj.style.cssText = "position:absolute;left:10px;top:10px;width:800px;height:500px;border:solid 10px #888888;z-index:3002;";
	document.body.appendChild(fgObj);
	var html = "<iframe src=\"/Computer/Include/FileShow.aspx?id=" + id + "\" width=\"800\" height=\"500\" border=\"0\" bordercolor=\"#000000\" frameborder=\"no\" scrolling=\"no\"></iframe>";
	fgObj.innerHTML = html;

	var csObj = document.createElement("img");
	csObj.style.cssText = "position:absolute;left:10px;top:10px;z-index:3003;";
	document.body.appendChild(csObj);
	csObj.src = (isIE6) ? "/images/close.gif" : "/images/close.png";

	csObj.onclick = function() { 
		document.body.removeChild(bgObj);
		document.body.removeChild(fgObj);
		document.body.removeChild(csObj);
	}

	function getPosition() 
	{
		if(document.documentElement && document.documentElement.scrollTop) {
			var top = document.documentElement.scrollTop;
			var left = document.documentElement.scrollLeft;
			var height = document.documentElement.clientHeight;
			var width = document.documentElement.clientWidth;
		}
		else if (document.body) 
		{
			var top = document.body.scrollTop;
			var left = document.body.scrollLeft;
			var height = document.documentElement.clientHeight;
			var width = document.documentElement.clientWidth;
		}
		return {top:top,left:left,height:height,width:width };
	}

	function Fix()
	{
		var Position = getPosition();
		var leftadd = (Position.width - 800)/2;
		var topadd = (Position.height - 500)/2;

		fgObj.style.top = (Position.top + topadd) +"px";
		fgObj.style.left = ((iWidth - 800 - 20)/2) + "px";
		csObj.style.top = (Position.top + topadd) +"px";
		csObj.style.left = (parseInt(fgObj.style.left.replace("px",""), 10) + 800 - 4) + "px";
	}
	
	Fix();
    window.onscroll = Fix;
}

html_dump_info = function() 
{
	this.id;
	this.innerHTML;
}

var html_dumps = new Array();
var html_dumps_count = 0;

function dump_save(id,html)
{
	for(var i=0; i<html_dumps_count; i++) {
		if(html_dumps[i].id == id) return;
	}
	var a1 = new html_dump_info();
	a1.id = id;
	a1.innerHTML = html;

	html_dumps[html_dumps_count] = a1;
	html_dumps_count++;
}

function dump_load(id)
{
	for(var i=0; i<html_dumps_count; i++) {
		if(html_dumps[i].id == id) {
			return html_dumps[i].innerHTML
		}
	}
	return "";
}

function FileShow(obj,id)
{
	if(obj.className.substr(0,5) == "file_") {
		dump_save(id,obj.innerHTML); 
		$.get("/Utility/FileContent.aspx", { rnd : Math.random(), id : id }, function(result) {
			var html = "";
			html += "<div style=\"clear:both;width:568px;height:300px;padding:1px;background-color:#AAAAAA;cursor:pointer;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;\">";
			html += "<div style=\"float:left;width:500px;height:22px;color:#EEEEEE;overflow:hidden;padding-left:5px;\"></div><div style=\"width:50px;float:right;color:#FFFF00;font-size:10px;text-align:center;\">[close]</div>";
			html += result;
			html += "</div>";
			obj.className = "loaded_" + obj.className;
			obj.innerHTML = html;
		});
	}
	else {
		obj.className = "file_container";
		obj.innerHTML = dump_load(id);
	}
}


function VideoShow(obj, url)
{
	if(obj.className.substr(0,5) == "http_") {
		var html = "";
		if(url.indexOf(".swf") >= 0 || url.indexOf(".flv") >= 0) {
			html += "<div style=\"clear:both;width:570px;height:410px;background-color:#AAAAAA;cursor:pointer;\">";
			html += "<div style=\"float:left;width:500px;height:22px;color:#EEEEEE;overflow:hidden;padding-left:5px;\">" + url + "</div><div style=\"width:50px;float:right;color:#FFFF00;font-size:10px;text-align:center;\">[close]</div>";
			html += "<object codeBase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"600\" height=\"400\">";
			html += "<param name=\"movie\" value=\"" + url + "\">";
			html += "<param name=\"quality\" value=\"high\">";
			html += "<param name=\"menu\" value=\"false\">";
			html += "<embed src=\"" + url + "\" quality=\"high\" menu=\"false\" pluginspage=\"http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash\" width=\"570\" height=\"370\"></embed>";
			html += "</object>";
			html += "</div>";
		}
		else {
			html += "<div style=\"clear:both;width:570px;height:410px;background-color:#AAAAAA;cursor:pointer;\">";
			html += "<a href=\"" + url + "\" target=\"_blank\">Download to see video...</a>";
			html += "</div>";
		}

		obj.className = "loaded_" + obj.className;
		obj.innerHTML = html;
	}
	else {
		obj.className = "http_container";
		obj.innerHTML = "<div style=\"padding-left:10px;\"><div class=\"file_icon\" style=\"background-position:-18px -715px;\"></div></div>play...";
	}
}

function AudioShow(obj, url)
{
	if(obj.className.substr(0,5) == "http_") {
		var html = "";
		if(url.indexOf("mp3") >= 0) {
			html += "<div style=\"padding-left:10px;\">正在播放...";
			html += "<div class=\"file_icon\" style=\"background-position:-18px -715px;\">";
			html += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='1' height='1'>";
			html += "<param name='movie' value='/images/music/1.swf'>";
			html += "<param name='wmode' value='transparent'>";
			html += "<embed src='/images/music/1.swf?foo=" + url + "' width='1' height='10' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' wmode='transparent'></embed>";
			html += "</object>";
			html += "</div>";
			html += "</div>";
		}
		else {
			html += "<div style=\"clear:both;width:570px;height:410px;background-color:#AAAAAA;cursor:pointer;\">";
			html += "<a href=\"" + url + "\" target=\"_blank\">Download to listen...</a>";
			html += "</div>";
		}
		obj.className = "loaded_" + obj.className;
		obj.innerHTML = html;
	}
	else {
		obj.className = "http_container";
		obj.innerHTML = "<div style=\"padding-left:10px;\"><div class=\"file_icon\" style=\"background-position:-18px -815px;\"></div></div>play...";
	}
}

function MoveImageAction(event)
{
	event = event || window.event;
	var doc = document.documentElement, body = document.body;
	var y = event.clientY + (doc && doc.scrollTop  || body && body.scrollTop  || 0) - (doc && doc.clientTop  || body && body.clientTop  || 0);
	var x = event.clientX + (doc && doc.scrollLeft  || body && body.scrollLeft  || 0) - (doc && doc.clientLeft  || body && body.clientLeft  || 0);
	var p = AbsolutePosition(this);

	if(this.scrollHeight > this.clientHeight) {
		y -= p.y;
	
		var y2 = parseInt((this.scrollHeight - this.clientHeight) * (y-5) / (this.clientHeight-10), 10);
		if(y2<0) y2=0;
		if(y2>(this.scrollHeight-5)) y2=this.scrollHeight; 
		this.scrollTop = y2;
	}

	if(this.scrollWidth > this.clientWidth) {
		x -= p.x;
		var x2 = parseInt((this.scrollWidth - this.clientWidth) * (x-5) / (this.clientWidth-10), 10);
		if(x2<0) x2=0;
		if(x2>(this.scrollWidth-5)) x2=this.scrollWidth; 
		this.scrollLeft = x2;
	}
}

var slide_index = 0;
function SlideShow(idx)
{
	slide_index = idx;
	var fname = "";
	var objs = getElementsById("imgList");
	for(var i=0; i<objs.length; i++) if(objs[i].getAttribute("tips")==idx) { fname = objs[i].getAttribute("title"); break; }

	var iWidth = document.body.clientWidth;
	var iHeight = document.body.scrollHeight + 24;
	var vHeight = document.documentElement.clientHeight;
	
	var sHeight = document.body.clientHeight+24;
	if(sHeight < iHeight) sHeight = iHeight;
	if(sHeight < 800) sHeight = 800;

	var bgObj = document.createElement("div");
	bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:" + iWidth + "px;height:" + sHeight + "px;text-align:center;filter:alpha(opacity=60);opacity:0.6;background-color:#000000;z-index:1001;";
	document.body.appendChild(bgObj);

	var dgObj = document.createElement("div");
	dgObj.style.cssText = "position:absolute;left:0px;top:0px;z-index:1002;background-color:#FFFFFF;padding:10px;";
	dgObj.innerHTML = "<table style=\"\"><tr><td><img id=\"leftButton\" src=\"/images/left.png\"></td><td id=\"tdImage\"><img id=\"thisImage\" style=\"border:solid 5px #888888;\" src=\"" + fname + "\"></td><td><img id=\"rightButton\" src=\"/images/right.png\"></td></tr></table>";
	document.body.appendChild(dgObj);
	
	var fgObj = document.getElementById("thisImage");

	var csObj = document.createElement("img");
	csObj.style.cssText = "position:absolute;left:0px;top:0px;z-index:1003;";
	document.body.appendChild(csObj);
	csObj.src = "/images/close.png";

	function SlideShowLast()
	{
		if(slide_index<=0) { alert("NO MORE IMAGE"); return; }
		if(slide_index>0) slide_index--;
		var fname = "";
		var objs = getElementsById("imgList");
		for(var i=0; i<objs.length; i++) if(objs[i].getAttribute("tips")==slide_index) { fname = objs[i].getAttribute("title"); break; }
		if(fname.length==0) { alert("NO MORE BEFORE " + slide_index); slide_index=0; return; }
		var xgObj = document.getElementById("tdImage");
		xgObj.innerHTML = slide_index + ":" + fname + "<br><img id=\"thisImage\" src=\"" + fname + "\">";
		fgObj = document.getElementById("thisImage");
		interval = setInterval(fsize,100);
	}

	function SlideShowNext()
	{
		var objs = getElementsById("imgList");
		if(slide_index > (objs.length-1)) { alert("NO MORE IMAGE"); return; }
		slide_index++;
		var fname = "";
		for(var i=0; i<objs.length; i++) if(objs[i].getAttribute("tips")==slide_index) { fname = objs[i].getAttribute("title"); break; }
		if(fname.length==0) { alert("NO MORE AFTER " + slide_index); return; }

		var xgObj = document.getElementById("tdImage");
		xgObj.innerHTML = slide_index + ":" + fname + "<br><img id=\"thisImage\" src=\"" + fname + "\">";
		fgObj = document.getElementById("thisImage");
		interval = setInterval(fsize,100);
	}

	function fsize()
	{
		if(fgObj.width !=0 && fgObj.height!=0)
		{
			clearInterval(interval);

			var xyscale = fgObj.width / fgObj.height;
			if(fgObj.width > 1000) { fgObj.width = 1000; fgObj.height = fgObj.width / xyscale; }
			if(fgObj.width < 200) { fgObj.width = 200; fgObj.height = fgObj.width / xyscale; }
			if(fgObj.height > (vHeight - 60)) { fgObj.height = vHeight - 60; fgObj.width = fgObj.height * xyscale; }

			dgObj.style.top = ((vHeight - fgObj.height - 20)/2) + "px";
			dgObj.style.left = ((iWidth - fgObj.width - 180)/2) + "px";

			csObj.style.top = ((vHeight - fgObj.height - 20)/2 + 20) + "px";
			csObj.style.left = (parseInt(dgObj.style.left.replace("px",""), 10) + fgObj.width + 170) + "px";

			Fix();
		}
	}
	
	fgObj.onclick = function()
	{
		window.open(fgObj.src);
	}

	function Hide() 
	{ 
		document.body.removeChild(bgObj);
		document.body.removeChild(dgObj);
		document.body.removeChild(csObj);
		window.onscroll = last_scroll;
	}

	csObj.onclick = Hide;

	function getPosition() 
	{
		if(document.documentElement && document.documentElement.scrollTop) {
			var top = document.documentElement.scrollTop;
			var left = document.documentElement.scrollLeft;
			var height = document.documentElement.clientHeight;
			var width = document.documentElement.clientWidth;
		}
		else if (document.body) 
		{
			var top = document.body.scrollTop;
			var left = document.body.scrollLeft;
			var height = document.documentElement.clientHeight;
			var width = document.documentElement.clientWidth;
		}
		return {top:top,left:left,height:height,width:width };
	}

	function Fix()
	{
		var pos = getPosition();
		var leftadd = (pos.width - dgObj.width)/2;
		var topadd = (pos.height - fgObj.height)/2;

		dgObj.style.top = (pos.top + topadd) +"px";
		csObj.style.top = (pos.top + topadd) +"px";

		if(last_scroll!=null) last_scroll();
	}

	var interval = setInterval(fsize,100);

	var leftObj = document.getElementById("leftButton");
	leftObj.onclick = SlideShowLast;
	var rightObj = document.getElementById("rightButton");
	rightObj.onclick = SlideShowNext;

	last_scroll = window.onscroll;
    window.onscroll = Fix;

	function keyUp(e) {
		var currKey=0,e=e||event;
		currKey=e.keyCode||e.which||e.charCode;
		var keyName = String.fromCharCode(currKey);
		if(currKey==37 || currKey==38) { SlideShowLast(); }
		if(currKey==39 || currKey==40) { SlideShowNext(); }
		if(currKey==27) { Hide(); }
	}
	document.onkeyup = keyUp;

}
