(function(){
	/*@cc_on
		@if (@_jscript_version<5.6)
			return false;
		@elif (@_jscript_version<5.8)
			if (!window.XMLHttpRequest) return false;
		@end
	@*/

	this.addChild=function(tag,id,parent) {
		var t=document.createElement(tag);
		t.id='boxSir'+(id=='' ? '' : '_'+id);
		if (parent) {
			parent.appendChild(t);
			if (parent!=document.body) t.bx=this.bx;
		} else t.bx=this.bx;
		return t;
	}

	this.addControl=function(id,content) {
		var t=this.addChild('div',id,this.bx);
		t.className='boxSir_control';
		t.innerHTML=content;
		return t;
	}

	this.addHook=function(target) {

		target.onclick=function() {
			this.blur();
			with (document.boxSir) {
				loading.style.zIndex='auto';
				isFullScreen=false;
				pullScreenSize();
				with (iShow.style) {
					cCanvas.style.left=left='-999em';
					width=height='auto';
				}
				if ((this.title) && (this.title!='')) {
					iShow.alt=this.title;
					description.innerHTML=this.title;
				} else {
					iShow.alt='';
					description.innerHTML='';
				}
				addEventList();
				style.left='0';
				if (iLoad.src==this.href) {
					iLoad.onload();
				} else {
					iLoad.src=this.href;
				}
			}
			return false;
		}

		target.onkeypress=function() {
			if (!e) var e=window.event;
			switch (e.keyCode) {
				case 13:
				case 32:
					return this.onclick();
			}
			return true;
		}

	}

	with (document) {

		var t=createElement('link');
		with (t) {
			href=(
				(boxSir_Directory) ? boxSir_Directory+'/' : ''
			)+'boxSir.css';
			type='text/css';
			rel='stylesheet';
			media='screen,projection,tv';
		}
		body.appendChild(t);

		with (this) {
			this.bx=document.boxSir=addChild('div','',body);
			addChild('div','shadow',bx);
			bx.loading=addChild('div','loading',bx);
			bx.iLoad=addChild('img','loader');
			bx.iShow=addChild('img','image',bx);
			bx.cCanvas=createElement('canvas');
			if (bx.cCanvas.getContext) {
				bx.appendChild(bx.cCanvas);
				bx.cCanvas.id='boxSir_canvas';
				bx.cContext=bx.cCanvas.getContext('2d');
				bx.isCanvas=true;
			} else {
				bx.isCanvas=false;
				bx.cClose=addControl('closeNoCanvas','X');
			}
			/* have to do these AFTER to get depth sorting right */
			bx.description=addChild('div','description',bx);
			if (bx.isCanvas) {
				bx.cClose=addControl('close','X<span>Close</span>');
				bx.cZoomIn=addControl('zoomIn','+<span>Zoom In</span>');
				bx.cZoomOut=addControl('zoomOut','-<span>Zoom Out</span>');
				bx.cHelp=addControl('help','?<span>Help</span>');
				bx.tHelp=addChild('div','helpText',bx);
				bx.tHelp.request=(
					window.XMLHttpRequest ?
					new window.XMLHttpRequest :
					new window.ActiveXObject('Microsoft.XMLHTTP')
				);
				bx.tHelp.request.bx=bx;
				bx.tHelp.request.onreadystatechange=function(e) {
					if (!e) var e=window.event;
					with (this.bx.tHelp) {
						if (request.readyState==4) {
							innerHTML=(
								request.status==200 ?
								request.responseText.replace('|versionString|',document.boxSir.versionString) :
								'HTTP Error '+request.status
							);
							style.left='50%';
							style.marginTop=-(clientHeight/2)+'px';
						}
					}
					this.bx.loading.style.zIndex='auto';
				}
				bx.tHelp.onclick=function() {
					this.style.left='-999em';
				}
				bx.cHelp.onclick=function(e) {
					if (!e) var e=window.event;
					with (e.target.parentNode) {
						loading.style.zIndex='100';
						tHelp.request.open('get',(
							(boxSir_Directory) ? boxSir_Directory+'/' : ''
						)+'boxSir.help.html',true);
						tHelp.request.send(null);
						prevent(e);
					}
				}
			}
		}
	}

	bx.zoomA=1;
	bx.oX=bx.oY=bx.dragX=bx.dragY=bx.mouseX=bx.mouseY=bx.calcW=bx.calcH=0;
	bx.isFullScreen=bx.mouseDown=bx.mouseMoved=false;

	bx.addEvent=function(eventName,eventFunc) {
		if (window.addEventListener) {
			window.addEventListener(eventName,eventFunc,false);
		} else if (document.attachEvent) {
			document.attachEvent('on'+eventName,eventFunc);
		}
	}

	bx.removeEvent=function(eventName,eventFunc) {
		if (window.removeEventListener) {
			window.removeEventListener(eventName,eventFunc,false);
		} else if (document.detachEvent) {
			document.detachEvent('on'+eventName,eventFunc);
		}
	}

	bx.addEventList=function() {
		with (this) for (var t in eventList) addEvent(eventList[t][0],eventList[t][1]);
	}

	bx.removeEventList=function() {
		with (this) for (var t in eventList) removeEvent(eventList[t][0],eventList[t][1]);
	}

	bx.prevent=function(e) {
		if (e) {
			if (e.preventDefault) e.preventDefault();
			e.returnValue=false;
		}
	}

	bx.hKey=function(e) {
		if (!e) var e=window.event;
		var kCode=e.charCode ? e.charCode : e.keyCode;
		var doPrevent=true;
		with (document.boxSir) {
			switch (kCode) {
				case 43:
				case 61:
					cZoomIn.onclick();
				break;
				case 45:
					cZoomOut.onclick();
				break;
				case 63:
				case 47:
					cHelp.onclick();
				break;
				case 27:
					cClose.onclick();
				break;
				case 9:
				case 32:
				break;
				default:
					doPrevent=false;
			}
			if (doPrevent) prevent(e);
		}
	}

	bx.hWheel=function(e) {
		if (!e) var e=window.event;
		var delta=e.detail ? e.detail : -e.wheelDelta;
		with (document.boxSir) {
			if (isCanvas) {
				if (delta<0) cZoomIn.onclick(); else cZoomOut.onclick();
			}
			prevent(e);
		}
	}

	bx.hMouseDown=function(e) { with (document.boxSir) {
		if (!e) var e=window.event;
		if (isFullScreen) {
			mouseDown=true;
			mouseMoved=false;
			if (e.pageX) {
				mouseX=e.pageX;
				mouseY=e.pageY;
			} else {
				mouseX=e.clientX;
				mouseY=e.clientY;
			}
			prevent(e);
		}
		return false;
	}}

	bx.hMouseUp=function(e) {
		if (!e) var e=window.event;
		e.target.parentNode.mouseDown=false;
		return false;
	}

	bx.hMouseOut=function(e) {
		if (!e) var e=window.event;
		document.boxSir.mouseDown=false;
	}

	bx.hMouseMove=function(e) { 
		if (!e) var e=window.event;
		if (e.target.parentNode.id=='boxSir') with (e.target.parentNode) {
			if (isFullScreen && mouseDown) {
				mouseMoved=true;
				if (!e.pageX) {
					e.pageX=e.clientX;
					e.pageY=e.clientY;
				}
				dragX-=mouseX-e.pageX;
				mouseX=e.pageX;
				dragY-=mouseY-e.pageY;
				mouseY=e.pageY;
				render();
			}
		}
	}

	bx.pullScreenSize=function() {
		this.screenW=this.screenH=0;
		with (this) {
			var e;
			if (((
				document.compatMode=='CSS1Compat' &&
				(e=document.documentElement)
			) || (
				e=document.body
			)) && e.offsetWidth) {
				screenW=e.offsetWidth;
				screenH=e.offsetHeight;
			}
			if ((e=window) && e.innerWidth) {
				screenW=e.innerWidth;
				screenH=e.innerHeight;
			}
			if (isCanvas) {
				cCanvas.width=screenW;
				cCanvas.height=screenH;
			}
		}
	}

	bx.loading.onclick=bx.cClose.onclick=function(e) {
		if (!e) var e=window.event;
		with (this.bx) {
			if (isCanvas) tHelp.style.left='-999em';
			style.left='-999em';
			removeEventList();
		}
		return false;
	}

	bx.render=function() {
		with (this) {
			if (oX>48) {
				var t=oX+48;
				if (dragX>t) dragX=t;
				if (dragX<-t) dragX=-t;
			} else dragX=0;
			if (oY>48) {
				var t=oY+48;
				if (dragY>t) dragY=t;
				if (dragY<-t) dragY=-t;
			} else dragY=0;
			if (isCanvas) {
				cContext.clearRect(0,0,screenW,screenH);
				cContext.drawImage(
					iShow,
					dragX-oX,dragY-oY,
					iLoad.width*zoomA,iLoad.height*zoomA
				);
			}
		}
	}

	bx.cCanvas.rescale=function() {
		with (this.parentNode) {
			cCanvas.width=screenW;
			cCanvas.height=screenH;
			cCanvas.style.width=screenW+'px';
			cCanvas.style.height=screenH+'px';
		}
	}

	bx.showFullScreen=function() {
		with (this) if (isCanvas) {
			cCanvas.style.left=0;
			cCanvas.rescale();
		}
	}

	bx.calcOffsets=function() {
		with (this) {
			oX=Math.floor((iLoad.width*zoomA-screenW)/2);
			oY=Math.floor((iLoad.height*zoomA-screenH)/2);
		}
	}

	if (bx.isCanvas) {

		bx.cZoomIn.onclick=function(e) {
			if (!e) var e=window.event;
			with (document.boxSir) {
				if (isFullScreen) {
					if (zoomA<16) {
						zoomA*=2;
						dragX*=2;
						dragY*=2;
						calcOffsets();
					}
				} else {
					zoomA=(
						(calcW==iLoad.width) &&
						(calcH==iLoad.height)
					) ? 2 : 1;
					dragX=dragY=0;
					isFullScreen=true;
					calcOffsets();
					showFullScreen();
				}
				render();
				prevent(e);
			}
		}

		bx.cZoomOut.onclick=function(e) {
			if (!e) var e=window.event;
			with (document.boxSir) {
				if (isFullScreen) {
					if (zoomA>1) {
						zoomA/=2;
						dragX/=2;
						dragY/=2;
						if (
							(zoomA==1) &&
							(calcW==iLoad.width) &&
							(calcH==iLoad.height)
						) {
							iShow.rescale();
						} else {
							calcOffsets();
							render();
						}
					} else iShow.rescale();
				}
				prevent(e);
			}
		}

	}

	bx.iShow.onmousedown=bx.iShow.onclick=function(e) {
		if (!e) var e=window.event;
		this.parentNode.prevent(e);
	}

	bx.iShow.rescale=function() {
		with (this.parentNode) {
			pullScreenSize();
			zoomA=1;
			if (isCanvas) {
				cCanvas.style.left='-999em';
				cCanvas.style.width='1px';
			}
			isFullScreen=false;
			var maxW=screenW-96;
			var maxH=screenH-description.clientHeight-80;
			if ((iLoad.width>maxW) || (iLoad.height>maxH)) {
				calcH=Math.floor(iLoad.height*maxW/iLoad.width);
				if (calcH>maxH) {
					calcW=Math.floor(iLoad.width*maxH/iLoad.height);
					calcH=maxH;
				} else calcW=maxW;
			} else {
				calcW=iLoad.width;
				calcH=iLoad.height;
			}
			oX=Math.floor(calcW/2)+4;
			oY=Math.floor((calcH+description.clientHeight-40)/2);
			with (iShow.style) {
				margin='-'+oY+'px 0 0 -'+oX+'px';
				width=calcW+'px';
				height=calcH+'px';
				left='50%';
			}
			if (!isCanvas) {
				cClose.style.margin='-'+(oY+16)+'px 0 0 '+(oX-16)+'px';
			}
		}
	}

	bx.iLoad.onload=function() {
		with (this.bx) {
			iShow.src=iLoad.src;
			iShow.rescale();
		}
	}

	with (document) {
		if (document.getElementsByClassName) {
			var targets=getElementsByClassName('boxMe');
		} else {
			var aList=getElementsByTagName('a');
			var targets=[];
			for (t in aList) {
				test=' '+aList[t].className+' ';
				if (test.indexOf(' boxMe ')!=-1) targets[targets.length]=aList[t];
			}
		}
		for (t in targets) addHook(targets[t]);

		if (typeof boxSir_Hooks=='string') {
			var parentList=boxSir_Hooks.split(',');
			for (var t in parentList) {
				var parent=getElementById(parentList[t]);
				if (parent) {
					var targets=parent.getElementsByTagName('a');
					for (var n in targets) addHook(targets[n]);
				}
			}
		}
	}

	with (bx) {
		bx.eventList=[
			['resize',iShow.rescale],
			['keypress',hKey],
			[(
				(/Firefox/i.test(navigator.userAgent)) &&
				!(/Opera/i.test(navigator.userAgent))
			) ? 'DOMMouseScroll' : 'mousewheel',hWheel],
			['mousedown',hMouseDown],
			['mouseup',hMouseUp],
			['mouseout',hMouseOut],
			['mousemove',hMouseMove]
		];
		if (isCanvas) {
			pullScreenSize();
			cCanvas.rescale();
		}
	}
	
	bx.versionString='BoxSir Revision 1.0 RC1 - 10 November 2011';

}());
