/*************** lightbox ***************/
var LinkImageManager = new function()
{
	// public properties
	this.loadingImagePath = "";
	this.relateLink = null;
	this.opacity = 0.8;
	
	// private objects

	// whole container
	this.baseContainer = null;

	// the transparent background, a div
	this.transBackground = null;

	// the image, preloadimage, text container, a div
	this.elementsContainer = null;

	// an img object for display the big image
	this.imageObject = null;

	// an img object for preload the big image
	this.preloadImageObject = null;
	
	// a div contained the discription and the X key
	this.otherContainer = null;
	this.titleDiv = null;
	this.keypadDiv = null;

	this.resizeController = null;

	// private 
	this.imagePath = "";
	
	// getPageScroll()
	// Returns array with x,y page scroll values.
	// Core code from - quirksmode.org
	//
	this.getPageScroll = function()
	{
		var xScroll;

		if (self.pageXOffset) {
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			xScroll = document.body.scrollLeft;
		}

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		var objPageScroll = new Object();
		objPageScroll.xScroll = xScroll;
		objPageScroll.yScroll = yScroll;

		return objPageScroll;
	}

	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	this.getPageSize = function()
	{
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}

		var objPageSize = new Object();
		objPageSize.pageWidth = pageWidth;
		objPageSize.pageHeight = pageHeight;
		objPageSize.windowWidth = windowWidth;
		objPageSize.windowHeight = windowHeight;

		return objPageSize;
	}
}

LinkImageManager.init = function()
{
	// init all objects
	this.baseContainer = document.createElement("div");
	this.baseContainer.style.position = "absolute";
	this.baseContainer.style.display = "none";
	this.baseContainer.style.zIndex = "100";
	this.baseContainer.style.left = "0";
	this.baseContainer.style.top = "0";
	document.body.insertBefore(this.baseContainer, null);

	this.transBackground = document.createElement("div");
	if (window.ActiveXObject)
	{
		this.transBackground.style.filter = "alpha(opacity=" + (this.opacity * 100) + ");"
	}
	else
	{
		this.transBackground.style.opacity = this.opacity;
	}
	this.transBackground.style.backgroundColor = "#FFFFFF";
	this.transBackground.style.position = "absolute";
	this.transBackground.style.border = "1px solid #666666";
	this.baseContainer.appendChild(this.transBackground);

	this.elementsContainer = document.createElement("div");
	this.elementsContainer.style.position = "absolute";
	this.elementsContainer.style.padding = "0px";
	this.elementsContainer.style.left = "0";
	this.elementsContainer.style.top = "0";
	this.baseContainer.appendChild(this.elementsContainer);

	this.imageObject = document.createElement("img");
	this.imageObject.src = this.loadingImagePath;
	this.elementsContainer.appendChild(this.imageObject);

	this.otherContainer = document.createElement("div");
	this.elementsContainer.appendChild(this.otherContainer);

	this.titleDiv = document.createElement("div");
	this.titleDiv.innerHTML = "";
	this.titleDiv.style.paddingLeft = "10px";
	this.otherContainer.appendChild(this.titleDiv);

	this.keypadDiv = document.createElement("div");
	this.keypadDiv.style.textAlign = "right";
	this.keypadDiv.innerHTML = '<nobr>点击 <a href="#" onclick="LinkImageManager.hideImageBox(); return false;"><kbd>X</kbd></a> 返回2SIMPLE</nobr>';
	this.keypadDiv.style.margin = "15px";
	this.keypadDiv.style.paddingBottom = "0px";
	this.otherContainer.appendChild(this.keypadDiv);

	// create the preload object and load the Loading img
	this.preloadImageObject = document.createElement("img");
//	this.preloadImageObject.src = this.loadingImagePath;

	// set the resize object
	this.resizeController = new ObjectResizeController();
	this.resizeController.setObject(this.transBackground);

	// init all links
	var links = document.links;
	for (var i=0; i<links.length; i++)
	{
		if (links[i].rel != null && links[i].rel.toLowerCase() == "lightbox")
		{
			links[i].onclick = function()
			{
				return LinkImageManager.displayImageBoxByLink(this);
			}
		}
	}
}

LinkImageManager.setImageTitle = function(text)
{
	if (text != null && text != "")
	{
		this.titleDiv.innerHTML = "<nobr>" + text + "</nobr>";
	}
	else
	{
		this.titleDiv.innerHTML = "";
	}
}

LinkImageManager.displayImageBoxByLink = function(link)
{
	LinkImageManager.imagePath = link.href;

	// set the image title
	LinkImageManager.setImageTitle(link.title);

	// show the whole light box
	LinkImageManager.baseContainer.style.display = "";

	// hide the image object and the otherContainer
	LinkImageManager.imageObject.src = LinkImageManager.loadingImagePath;
	LinkImageManager.imageObject.width = 128;
	LinkImageManager.imageObject.height = 128;
	LinkImageManager.imageObject.style.margin = "30px";
	LinkImageManager.imageObject.style.display = "none";
	LinkImageManager.otherContainer.style.display = "none";

	// resize the elementsContainer to fit the loading image with 30 px margin, and center it.
	var objPageSize = LinkImageManager.getPageSize();
	var objPageScroll = LinkImageManager.getPageScroll();
	
	var imageWidth;
	var imageHeight;
	if (LinkImageManager.imageObject.currentStyle != null)
	{
		imageWidth = parseInt(LinkImageManager.imageObject.currentStyle.width);
		imageHeight = parseInt(LinkImageManager.imageObject.currentStyle.height);
	}
	else
	{
	
	}

	var cornerPoint = LinkImageManager.getCenterCornerPoint(new Rectangle(0, 
		0, 
		imageWidth + 60,
		imageHeight + 60));

	LinkImageManager.resizeController.targetRegion = new Rectangle(cornerPoint.left, 
		cornerPoint.top, 
		imageWidth + 60,
		imageHeight + 60);

	var point = SelfXY();
	LinkImageManager.resizeController.startRegion = new Rectangle(point[0], 
		point[1], 
		0,
		0);

	LinkImageManager.preloadImageObject.onload = null;

	LinkImageManager.resizeController.onFinish = function()
	{
		// on the element resize finished
		LinkImageManager.resizeController.startRegion = LinkImageManager.resizeController.targetRegion;

		LinkImageManager.preloadImageObject.onload = function()
		{

			LinkImageManager.preloadImageObject.onload = null;

			LinkImageManager.imageObject.style.display = "none";

			// set the image object's property, use the preload image data
			LinkImageManager.imageObject.height = LinkImageManager.preloadImageObject.height;
			LinkImageManager.imageObject.width = LinkImageManager.preloadImageObject.width;

			var objPageSize = LinkImageManager.getPageSize();
			var objPageScroll = LinkImageManager.getPageScroll();
			
			var imageWidth = LinkImageManager.preloadImageObject.width;
			var imageHeight = LinkImageManager.preloadImageObject.height;

			var point = LinkImageManager.getCenterCornerPoint(new Rectangle(0, 
				0, 
				imageWidth + 20,
				imageHeight + 86));

			LinkImageManager.resizeController.targetRegion = new Rectangle(point.left, 
				point.top, 
				imageWidth + 20,
				imageHeight + 86);
			
			LinkImageManager.resizeController.onFinish = function()
			{
				LinkImageManager.elementsContainer.style.left = LinkImageManager.transBackground.offsetLeft;
				LinkImageManager.elementsContainer.style.top = LinkImageManager.transBackground.offsetTop;
				LinkImageManager.elementsContainer.style.width = LinkImageManager.transBackground.offsetWidth;
				LinkImageManager.elementsContainer.style.height = LinkImageManager.transBackground.offsetHeight;

				LinkImageManager.resizeController.onFinish = null;
				LinkImageManager.imageObject.style.margin = "10px";
				LinkImageManager.imageObject.src = LinkImageManager.imagePath;
				LinkImageManager.imageObject.style.display = "";
				LinkImageManager.otherContainer.style.display = "";
			}

			LinkImageManager.resizeController.run();
			// use the image object to display the loaded image

			document.onkeypress = function(e)
			{
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				key = String.fromCharCode(keycode).toLowerCase();
				
				if(key == 'x'){ LinkImageManager.hideImageBox(); }
			}
		}

		LinkImageManager.elementsContainer.style.left = LinkImageManager.transBackground.offsetLeft;
		LinkImageManager.elementsContainer.style.top = LinkImageManager.transBackground.offsetTop;
		LinkImageManager.elementsContainer.style.width = LinkImageManager.transBackground.offsetWidth;
		LinkImageManager.elementsContainer.style.height = LinkImageManager.transBackground.offsetHeight;

		LinkImageManager.imageObject.style.display = "";
		LinkImageManager.preloadImageObject.src = LinkImageManager.imagePath;
	};


	// use the preload image to load the loading image
			
	LinkImageManager.preloadImageObject.onerror = function()
	{
	}

	LinkImageManager.resizeController.run();

	return false;
}

LinkImageManager.hideImageBox = function()
{
	LinkImageManager.baseContainer.style.display = "none";
	document.onkeypress = null;

	return false;
}

LinkImageManager.getCenterCornerPoint = function(region)
{
	var objPageSize = LinkImageManager.getPageSize();
	var objPageScroll = LinkImageManager.getPageScroll();

	var top=0;
	if (region.height > objPageSize.windowHeight)
	{
		top = objPageScroll.yScroll;
	}
	else
	{
		top = objPageScroll.yScroll + (objPageSize.windowHeight - region.height)/2;
	}

	var left=0;
	if (region.width > objPageSize.windowWidth)
	{
		left = objPageScroll.xScroll;
	}
	else
	{
		left = objPageScroll.xScroll + (objPageSize.windowWidth - region.width)/2;
	}
	
	var result = new Point(left, top);

	return result;
}

window.onload = function()
{
	LinkImageManager.loadingImagePath = "/loading.gif";
	LinkImageManager.init();
}


function ObjectResizeController()
{
	this.object = null;
	this.startRegion = null;
	this.targetRegion = new Rectangle();
	this.currentRegion = null;

	this.pixelPerSecond = 2500;
	this.timeInterval = 10;
	this.totalStep = 0;
	this.currentStep = 0;

	this.onFinish = null;
	this.isFinished = false;

	this.setObject = function(obj)
	{
		this.object = obj;
		if (this.startRegion == null && obj != null)
		{
			this.startRegion = new Rectangle(obj.offsetLeft, obj.offsetTop, obj.offsetWidth, obj.offsetHeight);
		}
		this.currentRegion = this.startRegion;
	}

	this.run = function()
	{
		// calculate how much steps need
		var maxChangeLength = Math.max(Math.abs(this.targetRegion.width - this.startRegion.width),
			Math.abs(this.targetRegion.height - this.startRegion.height),
			Math.abs(this.targetRegion.left - this.startRegion.left),
			Math.abs(this.targetRegion.top - this.startRegion.top)
			);
		this.totalStep = Math.ceil(maxChangeLength * 1000 / (this.timeInterval * this.pixelPerSecond));

		this.currentStep = 0;
		this.isFinished = false;

		if (this.totalStep != 0)
		{
			var widthChangePerInterval = (this.targetRegion.width - this.startRegion.width) / this.totalStep;
			var heightChangePerInterval = (this.targetRegion.height - this.startRegion.height) / this.totalStep;
			var leftChangePerInterval = (this.targetRegion.left - this.startRegion.left) / this.totalStep;
			var topChangePerInterval = (this.targetRegion.top - this.startRegion.top) / this.totalStep;

			this.changeSizePerInterval = new Rectangle(leftChangePerInterval, topChangePerInterval, widthChangePerInterval, heightChangePerInterval);
		}

		this.startTime = new Date();
		this.runByCycle();


	}

	this.resizeTo = function(newRegion)
	{
		if (this.object != null)
		{
			this.object.style.left = newRegion.left;
			this.object.style.top = newRegion.top;
			this.object.style.width = newRegion.width;
			this.object.style.height = newRegion.height;

			this.currentRegion = newRegion;
		}
	}

	var me = this;

	this.runByCycle = function()
	{
		if (me.totalStep == 0 || me.currentStep >= me.totalStep)
		{
			me.isFinished = true;
			me.endTime = new Date();
			if (me.onFinish != null)
			{
				me.onFinish();
			}

			return;
		}

		var newLeft;
		var newTop;
		var newWidth;
		var newHeight;

		if (me.currentStep == me.totalStep - 1)
		{
			newLeft = me.targetRegion.left;
			newTop = me.targetRegion.top;
			newWidth = me.targetRegion.width;
			newHeight = me.targetRegion.height;
		}
		else
		{
			newLeft = me.startRegion.left + me.changeSizePerInterval.left * (me.currentStep + 1);
			newTop = me.startRegion.top + me.changeSizePerInterval.top * (me.currentStep + 1);
			newWidth = me.startRegion.width + me.changeSizePerInterval.width * (me.currentStep + 1);
			newHeight = me.startRegion.height + me.changeSizePerInterval.height * (me.currentStep + 1);
		}

		var newRegion = new Rectangle(newLeft, newTop, newWidth, newHeight);

		me.currentStep++;
		me.resizeTo(newRegion);

		window.setTimeout(me.runByCycle, me.timeInterval);
	}
}

function Rectangle(left, top, width, height)
{
	this.left = left != null?left:0;
	this.top = top != null?top:0;
	this.width = width != null?width:0;
	this.height = height != null?height:0;
}

function Point(left, top)
{
	this.left = left != null?left:0;
	this.top = top != null?top:0;
}

function getLocation(obj)
{
	var left = 0;
	var top = 0;

	while (obj.offsetParent != null)
	{
		left += obj.offsetLeft;
		top += obj.offsetTop;

		obj = obj.offsetParent;
	}

	var result = new Point(left, top);

	return result;
}

function SelfXY(){
    var yScrolltop;
    var xScrollleft;
    if (self.pageYOffset || self.pageXOffset) {
        yScrolltop = self.pageYOffset;
        xScrollleft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){     // Explorer 6 Strict
        yScrolltop = document.documentElement.scrollTop;
        xScrollleft = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScrolltop = document.body.scrollTop;
        xScrollleft = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScrollleft + event.clientX ,yScrolltop + event.clientY) 
    return arrayPageScroll;
}
