// JavaScript Document



(function(){
	var global = this;
	var topLeftImg = {x:0,y:0,dx:0,dy:0};
	var imgBigDiv, imgBigDivStyle, imgBigEl, imgBigElStyle;
	var divStyleClip, sizeModx, sizeMody, posMod, targetWidth, targetHeight;
	var pageState, inited = false,isOpen = false,active = false,atBottom = true;
	var blankImage = new Image();
	blankImage.src = "images/imageLoading.gif";
	function compatModeTest(obj){
		if((document.compatMode)&&(document.compatMode.indexOf('CSS') != -1)&&(document.documentElement)){
			return document.documentElement;
		}else if(document.body){
			return document.body;
		}else{
			return obj;
		}
	};
	var windowState = (function(){
		var readScroll = {scrollLeft:0,scrollTop:0};
		var readSize = {clientWidth:0,clientHeight:0};
		var readScrollX = 'scrollLeft';
		var readScrollY = 'scrollTop';
		var readWidth = 'clientWidth';
		var readHeight = 'clientHeight';
		if((typeof global.innerHeight == 'number')&&(typeof global.innerWidth == 'number')){
			readSize = global;
			readWidth = 'innerWidth';
			readHeight = 'innerHeight';
		}else{
			readSize = compatModeTest(readSize);
		}
		if((typeof global.pageYOffset == 'number')&&(typeof global.pageXOffset == 'number')){
			readScroll = global;
			readScrollY = 'pageYOffset';
			readScrollX = 'pageXOffset';
		}else{
			readScroll = compatModeTest(readScroll);
		}
		return {
			getScrollX:function(){
				return readScroll[readScrollX]|0;
			},
			getScrollY:function(){
				return readScroll[readScrollY]|0;
			},
			getWidth:function(){
				return readSize[readWidth]|0;
			},
			getHeight:function(){
				return readSize[readHeight]|0;
			}
		};
	})();
	function getPageState(){
		var readPageSize = {scrollWidth:0,scrollHeight:0};
		var readPageWidth = 'scrollWidth';
		var readPageHeight = 'scrollHeight';
		if((typeof document.width == 'number')&&(typeof document.height == 'number')){
			readPageSize = document;
			readPageWidth = 'width';
			readPageHeight = 'height';
		}else{
			readPageSize = compatModeTest(readPageSize);
			if((!readPageSize[readPageWidth])&&(!readPageSize[readPageHeight])){
				if(readPageSize.offsetHeight){
					readPageWidth = 'offsetWidth';
					readPageHeight = 'offsetHeight';
				}
			}
		}
		return {
			getPageWidth:function(){
				return readPageSize[readPageWidth]|0;
			},
			getPageHeight:function(){
				return readPageSize[readPageHeight]|0;
			}
		};
	};
	function retTrue(){return true;}
	function getEl(iD){
		var obj;
		if(document.getElementById){
			obj = document.getElementById(iD);
		}else if(document.all){
			obj = document.all[iD];
		}else if(document.layers){
			obj = document.layers[iD];
		}
		return obj;
	}
	function getImgObje(elName, divEl){
		var obj;
		if(document.images){
			obj = document.images[elName];
			if((!obj)&&
			  (divEl)&&
			  (divEl.document)&&
			  (divEl.document.images)){
				obj = divEl.document.images[elName];
			}
		}
		return obj;
	}
	function getImageOffset(img, div){
		var x = 0, y = 0;
		if(img.offsetParent){
			do{
				x += img.offsetLeft|0;
				y += img.offsetTop|0;
				img = img.offsetParent
			}while(img &&(img != div));
		}else if(typeof img.x == 'number'){
			x = img.x;
			y = img.y;
		}
		topLeftImg.x = x;
		topLeftImg.y = y;
	}
	var lastWidth = 10,lastHeight = 10;
	var setContent = function(wd, ht, lnkEl, altText){
		lastWidth = wd||lastWidth;
		lastHeight = ht||lastHeight;
		divStyleClip.height = (imgBigDivStyle.pixelHeight =(lastHeight+(topLeftImg[sizeMody]*2)))+posMod;
		divStyleClip.width = (imgBigDivStyle.pixelWidth =(lastWidth+(topLeftImg[sizeModx]*2)))+posMod;
		imgBigEl.src = lnkEl.href||lnkEl.src;
		imgBigEl.alt = altText;
		imgBigElStyle.width = (imgBigElStyle.pixelWidth=(imgBigEl.width = lastWidth))+posMod;
		imgBigElStyle.height = (imgBigElStyle.pixelHeight=(imgBigEl.height = lastHeight))+posMod;
	}
	function init(){
		var layersOut;
		pageState = getPageState();
		imgBigDiv = getEl("popUp");
		imgBigDivStyle = (imgBigDiv && imgBigDiv.style)||imgBigDiv;
		if((imgBigDiv)&&(imgBigDivStyle)&&(typeof imgBigDivStyle != 'boolean')){
			divStyleClip =  (imgBigDivStyle &&(typeof imgBigDivStyle.clip != 'string')&&imgBigDivStyle.clip)||imgBigDivStyle;
			posMod = ((typeof divStyleClip.height == 'string')?'px':0);
			if(typeof imgBigDivStyle.padding == 'string'){
				sizeModx = 'dx';
				sizeMody = 'dy';
			}else{
				sizeModx = 'x';
				sizeMody = 'y';
			}
			if((document.layers)&&(typeof Layer == 'function')&&(imgBigDiv.document)&&(imgBigDiv.document.write)){
				layersOut = [
					'<div style="padding:10px;"><a href="',location.href,'" onclick="return window.InWinPopUp.closeDiv();"><img name="bigImage" src="','','" width="','','" height="','','" alt="','','" border="0"><\/a><\/div>'
				];
				setContent = function(wd, ht, lnkEl, altText){
					lastWidth = wd||lastWidth;
					lastHeight = ht||lastHeight;
					layersOut[3] = lnkEl.href||lnkEl.src;
					layersOut[5] = lastWidth;
					layersOut[7] = lastHeight;
					layersOut[9] = altText;
					divStyleClip.height = lastHeight+(topLeftImg.y*2)+posMod;
					divStyleClip.width = lastWidth+(topLeftImg.x*2)+posMod;
					imgBigDiv.document.write(layersOut.join(''));
					imgBigDiv.document.close();
					imgBigDiv.document.bgColor = "#007686";
					imgBigEl = getImgObje("bigImage", imgBigDiv);
					imgBigElStyle = imgBigEl && imgBigEl.style || imgBigEl;
				}
			}

			atBottom = (((pageState.getPageHeight()-windowState.getHeight())>>1)>windowState.getScrollY());
			inited = true;
		}
		return inited;
	}
	function closeDiv(){
		isOpen = false;
		if(inited){
			if(imgBigEl&&imgBigDivStyle){
				imgBigDivStyle.visibility = "hidden";
				setContent(false, false, blankImage, 'Loading Image');
				return false;
			}
		}
		return true;
	}
	var lastTopOff = 0, lastLeftOff = 0;
	function setPos(){
		if(isOpen){
			var topOffset,availWidth = windowState.getWidth();
			var availHeight = windowState.getHeight();
			var pageWidth = pageState.getPageWidth();
			var pageHeight = pageState.getPageHeight();
			var scrollTop = windowState.getScrollY();
			var tHeight = imgBigDiv.offsetHeight||targetHeight;
			var tWidth = imgBigDiv.offsetWidth||targetWidth;
			if(pageWidth < availWidth)pageWidth = availWidth;
			if(pageHeight <= availHeight){
				pageHeight = availHeight;
				topOffset = (windowState.getScrollY()+((availHeight > tHeight)?((availHeight - tHeight)>>1):0));
			}else{
				atBottom = (atBottom)?((pageHeight - availHeight - 2) > scrollTop): !scrollTop;
				topOffset = (scrollTop+(((atBottom)&&(availHeight > tHeight))?(availHeight - tHeight):0));
			}
			var leftOffset = (windowState.getScrollX()+((availWidth > tWidth)?((availWidth - tWidth)>>1):0));
			if((leftOffset+tWidth) > pageWidth){
				leftOffset = pageWidth - tWidth;
				if(leftOffset < 0)leftOffset = 0;
			}
			if((topOffset+tHeight) > pageHeight){
				topOffset = pageHeight - tHeight;
				if(topOffset < 0)topOffset = 0;
			}
			lastTopOff = (lastTopOff&&((topOffset+lastTopOff)>>1))||topOffset;
			lastLeftOff = (lastLeftOff&&((leftOffset+lastLeftOff)>>1))||leftOffset;
			imgBigDivStyle.top = lastTopOff + posMod;
			imgBigDivStyle.left = lastLeftOff + posMod;
		}else{
			lastTopOff = 0;
			lastLeftOff = 0;
			active = false;
		}
		return isOpen;
	}
	function imgSwap(lnkEl, wd ,ht, altText){
		if(!inited)if(!init()){
			retTrue.closeDiv = retTrue;
			global.InWinPopUp = retTrue;
			return true;
		}
		imgBigEl = getImgObje("bigImage", imgBigDiv);
		imgBigElStyle = imgBigEl && imgBigEl.style || imgBigEl;
		closeDiv();
		if((imgBigEl)&&(imgBigElStyle)){
			isOpen = true;
			getImageOffset(imgBigEl, imgBigDiv);
			targetWidth = wd+(topLeftImg.x*2);
			targetHeight = ht+(topLeftImg.y*2);
			if(!altText)altText = '';
			setContent(wd, ht, lnkEl, altText);
			lastTopOff = 0;
			lastLeftOff = 0;
			setPos();
			if(!active){
				TimedQue(setPos);
				active = true;
			}
			imgBigDivStyle.visibility = "visible"
			return false;
		}else{
			return true;
		}

	}
	imgSwap.closeDiv = closeDiv;
	document.write([
				'<div id="popUp" onclick="window.InWinPopUp.closeDiv();"><a href="',location.href,'" onclick="return window.InWinPopUp.closeDiv();"><img name="bigImage" src="',blankImage.src,'"><\/a><\/div>'
				].join(''));
	global.InWinPopUp = imgSwap;
})();

