//
//	LighterBox v1.1 with LinkBox
//	http://stfw.hu
//	01/06/08
//
//	Copyright (C) 2008, Attila Magyar
//	
//	All rights reserved.
//
//	You should download http://stfw.hu/upload/lighterbox.zip insted of this
//	file.
//	
//	Redistribution and use in source and binary forms, with or without
//	modification, are permitted provided that the following conditions are met:
//		* Redistributions of source code must retain the above copyright notice,
//		  this list of conditions and the following disclaimer.
//		* Redistributions in binary form must reproduce the above copyright
//		  notice, this list of conditions and the following disclaimer in th
//		  documentation and/or other materials provided with the distribution.
//		* Neither the name of the <ORGANIZATION> nor the names of its
//		  contributors may be used to endorse or promote products derived from
//		  this software without specific prior written permission.
//
//	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
//	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

var LighterBox =
{
	autoInitialize : true,
	linkBoxEnabled : false,
	loadingImage : '/images/loading.gif',
	backgroundColor : '#100000',
	backgroundOpacity : 0.8,
	imageBorderColor : '#392701',
	imageBorderWidth : 2,
	animationRunTime : 1000,
	statusTemplate : '<strong>$title</strong><br />$counter. kép $all közül (<a href="$url" target="blank" style="color:#ffffff;font-weight:normal;">Eredeti méret: $width x $height px</a>)',
	statusOpacity : 0.8,
	statusColor : '#392701',
	statusBackgroundColor : '#edd090',
	
	gallery : [],
	positions : [],
	imgCount : 0,
	active : -1,
	activateFirst : 0,
	loaded : 0,
	background : null,
	maxSize : null,
	loading : null,
	hidden : [],
	scrollPos : { top : 0, left : 0 },
	wndSize : { width : 800, height : 600 },
	pageSize : { width : 800, height : 600 },
	status : null,
	
	getWindowSize : function (oWindow)
	{
		var windowWidth = 0;
		var windowHeight = 0;
		if (!oWindow) oWindow = window;
		try
		{
			windowWidth = oWindow.innerWidth;
			windowHeight = oWindow.innerHeight;
			if (!(windowWidth * windowHeight)) throw 'Window size problem.';
		}
		catch (ex)
		{
			try {
				windowWidth = oWindow.document.documentElement.clientWidth;
				windowHeight = oWindow.document.documentElement.clientHeight;
				if (!(windowWidth * windowHeight)) throw 'Window size problem.';
			}
			catch (ex)
			{
				try
				{
					windowWidth = oWindow.document.body.clientWidth;
					windowHeight = oWindow.document.body.clientHeight;
					if (!(windowWidth * windowHeight)) throw 'Window size problem.';
				}
				catch (ex)
				{
					return false;
				}
			}
		}
		return {
			width : windowWidth,
			height : windowHeight
		};
	},
	
	getPageSize : function (oWindow)
	{
		// based on LightBox 2.0
		if (!oWindow) oWindow = window;
		var xScroll, yScroll;
		var windowWidth, windowHeight;
		if (oWindow.innerHeight && oWindow.scrollMaxY)
		{
			xScroll = oWindow.innerWidth + oWindow.scrollMaxX;
			yScroll = oWindow.innerHeight + oWindow.scrollMaxY;
		}
		else if (oWindow.document.body.scrollHeight > oWindow.document.body.offsetHeight)
		{
			xScroll = oWindow.document.body.scrollWidth;
			yScroll = oWindow.document.body.scrollHeight;
		}
		else
		{
			xScroll = oWindow.document.body.offsetWidth;
			yScroll = oWindow.document.body.offsetHeight;
		}
		if (oWindow.innerHeight)
		{
			if (oWindow.document.documentElement.clientWidth)
			{
				windowWidth = oWindow.document.documentElement.clientWidth; 
			}
			else
			{
				windowWidth = oWindow.innerWidth;
			}
			windowHeight = oWindow.innerHeight;
		}
		else if (oWindow.document.documentElement && oWindow.document.documentElement.clientHeight)
		{
			windowWidth = oWindow.document.documentElement.clientWidth;
			windowHeight = oWindow.document.documentElement.clientHeight;
		}
		else if (oWindow.document.body)
		{
			windowWidth = oWindow.document.body.clientWidth;
			windowHeight = oWindow.document.body.clientHeight;
		}
		return {
			width : ((xScroll < windowWidth) ? windowWidth : xScroll),
			height : ((yScroll < windowHeight) ? windowHeight : yScroll)
		};
	},
	
	getScrollPos : function (oWindow)
	{
		if (!oWindow) oWindow = window;
		var sTop = 0;
		var sLeft = 0;
		sLeft = oWindow.pageXOffset;
		sTop = oWindow.pageYOffset;
		if (undefined == sLeft || undefined == sTop)
		{
			if (oWindow.document && oWindow.document.body)
			{
				sLeft = oWindow.document.body.scrollLeft * 1;
				sTop = oWindow.document.body.scrollTop * 1;
			}
		}
		if ((!(sLeft && sTop)) && (oWindow.document && oWindow.document.documentElement))
		{
			var l = oWindow.document.documentElement.scrollLeft * 1;
			var t = oWindow.document.documentElement.scrollTop * 1;
			if (l > sLeft) sLeft = l;
			if (t > sTop) sTop = t;
		}
		return {
			top : sTop,
			left : sLeft
		};
	},
	
	imgDisplaySize : function (nWidth, nHeight)
	{
		if (nWidth > LighterBox.maxSize.width)
		{
			var ow = nWidth ? nWidth : 1;
			nWidth = LighterBox.maxSize.width;
			nHeight *= (nWidth / ow);
		}
		if (nHeight > LighterBox.maxSize.height)
		{
			var oh = nHeight ? nHeight : 1;
			nHeight = LighterBox.maxSize.height;
			nWidth *= (nHeight / oh);
		}
		return { width : Math.round(nWidth), height : Math.round(nHeight) };
	},
	
	stop : function ()
	{
		setTimeout('LighterBox.close()', LighterBox.animationRunTime);
		if (LighterBox.status.animation) Animation.clear(LighterBox.status.animation);
		if (LighterBox.background.animation) Animation.clear(LighterBox.background.animation);
		if (LighterBox.gallery[LighterBox.active] && LighterBox.gallery[LighterBox.active].animation)
		{
			Animation.clear(LighterBox.gallery[LighterBox.active].animation);
		}
		if (LighterBox.linkBoxEnabled) LinkBox.stop();
		Animation.animate(
			LighterBox.background.animation = Animation.attach(
				LighterBox.background,
				[
					new AnimationKeyFrame(
						{ style : { opacity : LighterBox.backgroundOpacity } },
						Math.round(LighterBox.animationRunTime / 2) + 1
					),
					new AnimationKeyFrame(
						{ style : { opacity : 0 } },
						1
					)
				]
			)
		);
		Animation.animate(
			LighterBox.status.animation = Animation.attach(
				LighterBox.status,
				[
					new AnimationKeyFrame(
						{ style : { opacity : 0.8 } },
						Math.round(LighterBox.animationRunTime / 2) + 1
					),
					new AnimationKeyFrame(
						{ style : { opacity : 0 } },
						1
					)
				]
			)
		);
		var b = document.getElementsByTagName('body')[0];
		for (var i = 0; i < LighterBox.gallery.length; ++i)
		{
			b.removeChild(LighterBox.gallery[i].domObj);
		}
	},
	
	close : function ()
	{
		Animation.clear(LighterBox.status.animation);
		Animation.clear(LighterBox.background.animation);
		if (LighterBox.gallery[LighterBox.active] && LighterBox.gallery[LighterBox.active].animation)
		{
			Animation.clear(LighterBox.gallery[LighterBox.active].animation);
		}
		LighterBox.background.style.display = 'none';
		LighterBox.loading.style.display = 'none';
		LighterBox.status.style.display = 'none';
		for (var i = 0; i < LighterBox.hidden.length; ++i)
		{
			LighterBox.hidden[i].style.visibility = 'visible';
		}
	},
	
	imgLoaded : function ()
	{
		if ((++LighterBox.loaded) == LighterBox.imgCount)
		{
			LighterBox.loading.style.display = 'none';
			LighterBox.show(LighterBox.activateFirst);
		}
	},
	
	show : function (nNumber)
	{
		if ('number' != typeof(nNumber)) nNumber = (LighterBox.active < (LighterBox.gallery.length - 1)) ? LighterBox.active + 1 : 0;
		if (0 <= LighterBox.active)
		{
			if (LighterBox.gallery[LighterBox.active].animation)
			{
				Animation.clear(LighterBox.gallery[LighterBox.active].animation);
			}
			if (LighterBox.status.animation)
			{
				Animation.clear(LighterBox.status.animation);
			}
			var ds = LighterBox.gallery[LighterBox.active].displaySize;
			Animation.setState(LighterBox.status, { style : { display : 'none' } });
			Animation.animate(
				LighterBox.gallery[LighterBox.active].animation = Animation.attach(
					LighterBox.gallery[LighterBox.active].domObj,
					[
						new AnimationKeyFrame(
							{ 
								style : 
								{
									display : 'block',
									height : ds.height + 'px',
									width : ds.width + 'px',
									top : Math.round(LighterBox.scrollPos.top + (LighterBox.wndSize.height - ds.height) / 2) + 'px',
									left : Math.round(LighterBox.scrollPos.left + (LighterBox.wndSize.width - ds.width) / 2) + 'px',
									opacity : 0.5
								}
							},
							Math.round(LighterBox.animationRunTime / 1.5) + 1
						),
						new AnimationKeyFrame(
							{
								style :
								{
									display : 'block',
									height : '0px',
									width : '0px',
									top : Math.round(LighterBox.scrollPos.top + ds.height / 2) + 'px',
									left : LighterBox.scrollPos.left + 'px',
									opacity : 0
								}
							},
							1
						),
						new AnimationKeyFrame(
							{ style : { display : 'none' } },
							1
						)
					]
				)
			);
		}
		LighterBox.active = nNumber;
		var c = nNumber + 1;
		var a = LighterBox.gallery.length;
		var t = LighterBox.gallery[LighterBox.active].title;
		var w = LighterBox.gallery[LighterBox.active].width;
		var h = LighterBox.gallery[LighterBox.active].height;
		var u = LighterBox.gallery[LighterBox.active].src;
		var st = LighterBox.statusTemplate.replace(/\$url/gi, u).replace(/\$counter/gi, c).replace(/\$all/gi, a).replace(/\$title/gi, t).replace(/\$width/gi, w).replace(/\$height/gi, h);
		LighterBox.status.innerHTML = st;
		var ds = LighterBox.gallery[LighterBox.active].displaySize;
		LighterBox.status.animation = Animation.attach(
			LighterBox.status,
			[
				new AnimationKeyFrame(
					{
						style :
						{
							display : 'block',
							top : (Math.round(LighterBox.scrollPos.top + (LighterBox.wndSize.height - ds.height) / 2) + LighterBox.imageBorderWidth) + 'px',
							left : (Math.round(LighterBox.scrollPos.left + (LighterBox.wndSize.width - ds.width) / 2) + LighterBox.imageBorderWidth) + 'px',
							width : (ds.width - LighterBox.imageBorderWidth * 2) + 'px',
							height : '0px',
							opacity : 0
						}
					},
					Math.round(LighterBox.animationRunTime * 2) + 1
				),
				new AnimationKeyFrame(
					{
						style :
						{
							display : 'block',
							top : (Math.round(LighterBox.scrollPos.top + (LighterBox.wndSize.height - ds.height) / 2) + LighterBox.imageBorderWidth) + 'px',
							left : (Math.round(LighterBox.scrollPos.left + (LighterBox.wndSize.width - ds.width) / 2) + LighterBox.imageBorderWidth) + 'px',
							width : (ds.width - LighterBox.imageBorderWidth * 2) + 'px',
							height : '34px',
							opacity : LighterBox.statusOpacity
						}
					},
					1
				)
			]
		);
		Animation.animate(
			LighterBox.gallery[LighterBox.active].animation = Animation.attach(
				LighterBox.gallery[LighterBox.active].domObj,
				[
					new AnimationKeyFrame(
						{
							style :
							{
								display : 'block',
								overflow : 'hidden',
								top : Math.round(LighterBox.scrollPos.top + (LighterBox.wndSize.height - Math.round(ds.height / 2)) / 2) + 'px',
								left : Math.round(LighterBox.scrollPos.left + (LighterBox.wndSize.width - Math.round(ds.width / 2)) / 2) + 'px',
								width : Math.round(ds.width / 2) + 'px',
								height : Math.round(ds.height / 2) + 'px',
								opacity : 0.2
							},
							firstChild :
							{
								style :
								{
									marginLeft : (-Math.round(ds.width / 4)) + 'px',
									marginTop : (-Math.round(ds.height / 4)) + 'px'
								}
							}
						},
						LighterBox.animationRunTime
					),
					new AnimationKeyFrame(
						{ 
							scrollLeft : 0,
							scrollTop : 0,
							style : 
							{
								display : 'block',
								top : Math.round(LighterBox.scrollPos.top + (LighterBox.wndSize.height - ds.height) / 2) + 'px',
								left : Math.round(LighterBox.scrollPos.left + (LighterBox.wndSize.width - ds.width) / 2) + 'px',
								width : ds.width + 'px',
								height : ds.height + 'px',
								opacity : 1
							},
							firstChild :
							{
								style :
								{
									marginLeft : '0px',
									marginTop : '0px'
								}
							}
						},
						1
					)
				],
				function () { Animation.animate(LighterBox.status.animation); }
			)
		);
	},
	
	hideIEOpacityBugs : function ()
	{
		LighterBox.hidden = [];
		var tmp = document.getElementsByTagName('select');
		for (var i = 0; i < tmp.length; ++i)
		{
			LighterBox.hidden[LighterBox.hidden.length] = tmp[i];
			tmp[i].style.visibility = 'hidden';
		}
		tmp = document.getElementsByTagName('object');
		for (var i = 0; i < tmp.length; ++i)
		{
			LighterBox.hidden[LighterBox.hidden.length] = tmp[i];
			tmp[i].style.visibility = 'hidden';
		}
	},
	
	start : function (e)
	{
		var ex;
		try
		{
			LighterBox.hideIEOpacityBugs();
			LighterBox.scrollPos = LighterBox.getScrollPos();
			LighterBox.wndSize = LighterBox.getWindowSize();
			LighterBox.pageSize = LighterBox.getPageSize();
			Animation.setState(
				LighterBox.background,
				{
					style :
					{
						top : '0px',
						left : '0px',
						width : '100%',
						height : LighterBox.pageSize.height + 'px',
						display : 'block',
						zIndex : 1000
					}
				}
			);
			Animation.setState(
				LighterBox.loading,
				{
					style :
					{
						top : (LighterBox.scrollPos.top + Math.round(LighterBox.wndSize.height / 2 - 16)) + 'px',
						left : (LighterBox.scrollPos.left + Math.round(LighterBox.wndSize.width / 2 - 16)) + 'px',
						display : 'block',
						zIndex : 1001
					}
				}
			);
			Animation.animate(
				LighterBox.background.animation = Animation.attach(
					LighterBox.background,
					[
						new AnimationKeyFrame(
							{ style : { opacity : 0 } },
							Math.round(1.5 * LighterBox.animationRunTime)
						),
						new AnimationKeyFrame(
							{ style : { opacity : LighterBox.backgroundOpacity } },
							1
						)
					]				
				)
			);
			LighterBox.gallery = [];
			LighterBox.loader = [];
			LighterBox.positions = [];
			LighterBox.active = -1;
			LighterBox.activateFirst = 0;
			LighterBox.loaded = 0;
			var galleryId = '' + (this.rel ? this.rel : this.getAttribute('rel'));
			var active = '' + (this.href ? this.href : this.getAttribute('href'));
			var links = document.getElementsByTagName('a');
			var current = 0;
			var docBody = document.getElementsByTagName('body')[0];
			LighterBox.maxSize = {
				width : LighterBox.wndSize.width * 0.9,
				height : LighterBox.wndSize.height * 0.9
			}
			var imgs = [];
			for (var i = 0; i < links.length; ++i)
			{
				var rel = links[i].rel ? links[i].rel : links[i].getAttribute('rel');
				if (rel == galleryId)
				{
					imgs[imgs.length] = links[i];
				}
			}
			LighterBox.imgCount = imgs.length;
			for (var i = 0; i < imgs.length; ++i)
			{
				var img = new Image();
				img.gid = current;
				img.title = imgs[i].title ? imgs[i].title : imgs[i].getAttribute('title');
				img.onload = function ()
				{
					var ds = LighterBox.imgDisplaySize(this.width, this.height);
					var d = document.createElement('div');
					d.innerHTML = '<img src="' + this.src + '" alt="" />';
					Animation.setState(
						d,
						{
							style :
							{
								position : 'absolute',
								display : 'none',
								width : ds.width + 'px',
								height : ds.height + 'px',
								margin : '0px',
								overflow : 'hidden',
								borderStyle : 'solid',
								borderColor : LighterBox.imageBorderColor,
								borderWidth : LighterBox.imageBorderWidth + 'px',
								zIndex : 1009
							}
						}
					);
					Animation.setState(
						d.childNodes[0],
						{
							style :
							{
								position : 'absolute',
								display : 'block',
								width : ds.width + 'px',
								height : ds.height + 'px',
								margin : '0px',
								zIndex : 1010,
								cursor : 'pointer'
							},
							onclick : LighterBox.show
						}
					);
					LighterBox.gallery[this.gid] = {
						loaded : true,
						title : this.title,
						src : this.src, 
						width : this.width,
						height : this.height,
						displaySize : ds,
						domObj : docBody.appendChild(d),
						animation : false
					};
					setTimeout('LighterBox.imgLoaded()', 1000);
				}
				LighterBox.loader[current] = img;
				var href = '' + (imgs[i].href ? imgs[i].href : imgs[i].getAttribute('href'));
				if (active == href)
				{
					LighterBox.activateFirst = current;
				}
				LighterBox.loader[current++].src = href;
			}
			if (!e) e = window.event;
			if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
			return false;
		}
		catch (ex)
		{
			LighterBox.stop();
			e.returnValue = true;
			return true;
		}
	},
	
	initialize : function ()
	{
		if (1 > LighterBox.animationRunTime) LighterBox.animationRunTime = 2;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; ++i)
		{
			var rel = '' + (links[i].rel ? links[i].rel : links[i].getAttribute('rel'));
			if (rel.match(/light(er)?box.*/))
			{
				links[i].onclick = LighterBox.start;
			}
		}
		var l = document.createElement('div');
		l.innerHTML = '<img src="' + LighterBox.loadingImage + '" alt="Loading..." />';
		Animation.setState(
			l,
			{
				style :
				{
					position : 'absolute',
					display : 'none'
				}
			}
		);
		var bg = document.createElement('div');
		Animation.setState(
			bg,
			{
				style :
				{
					position : 'absolute',
					backgroundColor : LighterBox.backgroundColor,
					display : 'none',
					cursor : 'crosshair'
				},
				onclick : LighterBox.stop
			}
		);
		var b = document.getElementsByTagName('body')[0];
		var s = document.createElement('div');
		Animation.setState(
			s,
			{
				style :
				{
					position : 'absolute',
					display : 'none',
					backgroundColor : LighterBox.statusBackgroundColor,
					color : LighterBox.statusColor,
					fontSize : '12px',
					textAlign : 'center',
					overflow : 'hidden',
					height : '34px',
					lineHeight : '15px',
					padding : '2px',
					margin : '0px',
					zIndex : 1020
				}
			}
		);
		LighterBox.status = b.appendChild(s);
		LighterBox.background = b.appendChild(bg);
		LighterBox.loading = b.appendChild(l);
		if (LighterBox.linkBoxEnabled) LinkBox.initialize(links);
	}
}

var LinkBox = {
	browserWindow : null,
	browser : null,

	close : function ()
	{
		Animation.setState(
			LinkBox.browser,
			{
				style :
				{
					display : 'none'
				}
			}
		);
	},
	
	stop : function ()
	{
		setTimeout('LinkBox.close()', LighterBox.animationRunTime);
		Animation.setState(
			LinkBox.browserWindow,
			{
				location : { href : 'about:blank' },
				src : 'about:blank',
				style :
				{
					display : 'none'
				}
			}
		);
		if (LinkBox.browser.animation) Animation.clear(LinkBox.browser.animation);
		Animation.animate(
			LinkBox.browser.animation = Animation.attach(
				LinkBox.browser,
				[
					new AnimationKeyFrame(
						{ style : { opacity : 0.8 } },
						Math.round(LighterBox.animationRunTime / 3) + 1
					),
					new AnimationKeyFrame(
						{ style : { opacity : 0 } },
						1
					)
				],
				LinkBox.close
			)
		);
	},
	
	start : function (e)
	{
		var ex;
		try
		{
			var url = '' + (this.href ? this.href : this.getAttribute('href'));
			if (LinkBox.browser.animation) Animation.clear(LinkBox.browser.animation);
			LighterBox.hideIEOpacityBugs();
			LighterBox.scrollPos = LighterBox.getScrollPos();
			LighterBox.wndSize = LighterBox.getWindowSize();
			LighterBox.pageSize = LighterBox.getPageSize();
			Animation.setState(
				LighterBox.background,
				{
					style :
					{
						top : '0px',
						left : '0px',
						width : '100%',
						height : LighterBox.pageSize.height + 'px',
						display : 'block',
						zIndex : 1000
					}
				}
			);
			Animation.setState(
				LinkBox.browser,
				{
					style :
					{
						display : 'block',
						top : (LighterBox.scrollPos.top + 40 - LighterBox.imageBorderWidth) + 'px',
						left : (LighterBox.scrollPos.left + 40 - LighterBox.imageBorderWidth) + 'px',
						width : (LighterBox.wndSize.width - 80 + 2 * LighterBox.imageBorderWidth) + 'px',
						height : (LighterBox.wndSize.height - 80 + 2 * LighterBox.imageBorderWidth) + 'px'
					}
				}
			);
			Animation.setState(
				LinkBox.browserWindow,
				{
					style :
					{
						display : 'none',
						top : (LighterBox.scrollPos.top + 40) + 'px',
						left : (LighterBox.scrollPos.left + 40) + 'px',
						width : (LighterBox.wndSize.width - 80) + 'px',
						height : (LighterBox.wndSize.height - 80) + 'px'
					},
					location : { href : url },
					src : url
				}
			);
			Animation.animate(
				LinkBox.browser.animation = Animation.attach(
					LinkBox.browser,
					[
						new AnimationKeyFrame(
							{ style : { opacity : 0.2 } },
							Math.round(LighterBox.animationRunTime / 2)
						),
						new AnimationKeyFrame(
							{ style : { opacity : 1 } },
							1
						)
					],
					function () { Animation.setState(LinkBox.browserWindow, { style : { display: 'block' } }); }					
				)
				
			);
			Animation.animate(
				LighterBox.background.animation = Animation.attach(
					LighterBox.background,
					[
						new AnimationKeyFrame(
							{ style : { opacity : 0 } },
							Math.round(LighterBox.animationRunTime)
						),
						new AnimationKeyFrame(
							{ style : { opacity : LighterBox.backgroundOpacity } },
							1
						)
					]				
				)
			);
			if (!e) e = window.event;
			if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
			return false;
		}
		catch (ex)
		{
			LighterBox.stop();
			e.returnValue = true;
			return true;
		}
	},
	
	initialize : function (links)
	{
		for (var i = 0; i < links.length; ++i)
		{
			var rel = '' + (links[i].rel ? links[i].rel : links[i].getAttribute('rel'));
			if (rel.match(/linkbox/))
			{
				links[i].onclick = LinkBox.start;
			}
		}
		var b = document.createElement('div');
		Animation.setState(
			b,
			{
				style :
				{
					borderStyle : 'none',
					backgroundColor : LighterBox.imageBorderColor,
					padding : LighterBox.imageBorderWidth,
					position : 'absolute',
					zIndex : 1001,
					display : 'none'
				}
			}
		);
		var w = document.createElement('iframe');
		Animation.setState(
			w,
			{
				style :
				{
					display : 'none',
					borderStyle : 'none',
					borderWidth : '0px',
					backgroundColor : LighterBox.imageBorderColor,
					position : 'absolute',
					zIndex : 1002
				}
			}
		);
		var bd = document.getElementsByTagName('body')[0];
		LinkBox.browserWindow = bd.appendChild(w);
		LinkBox.browser = bd.appendChild(b);
	}

}

Math.dec2hex = function (nDecimal, nPadding)
{
	var digits = '0123456789abcdef';
	var ret = '';
	dec = Math.floor(nDecimal);
	while (0 < nDecimal)
	{
		var m = nDecimal % 16;
		nDecimal -= m;
		nDecimal = nDecimal / 16;
		ret = digits.charAt(m) + ret;
	}
	if (nPadding && ((nPadding *= 1) > ret.length))
	{
		while (nPadding > ret.length)
		{
			ret = '0' + ret;
		}
	}
	return ret.length ? ret : '0';
}

Math.hex2dec = function (sHexadecimal)
{
	var digits = '0123456789abcdef';
	sHexadecimal += '';
	sHexadecimal = sHexadecimal.toLowerCase();
	var ret = 0;
	for (var i = 0; i < sHexadecimal.length; ++i)
	{
		var v = digits.indexOf(sHexadecimal.charAt(i));
		if (-1 < v)
		{
			ret *= 16;
			ret += v;
		}
		else i = sHexadecimal.length;
	}
	return ret;
}

function Color(sHexColor)
{
	if ('#' == (sHexColor = new String(sHexColor)).charAt(0)) sHexColor = sHexColor.substr(1);
	this.red = Math.hex2dec(sHexColor.substr(0, 2));
	this.green = Math.hex2dec(sHexColor.substr(2, 2));
	this.blue = Math.hex2dec(sHexColor.substr(4, 2));
}

Color.prototype.hex = function ()
{
	return '#' + Math.dec2hex(this.red, 2) + Math.dec2hex(this.green, 2) + Math.dec2hex(this.blue, 2);
}

Color.prototype.toString = function ()
{
	return this.hex();
}

function AnimationKeyFrame(oState, nRunTime, nResolution, fInterpolation, fCallback)
{
	if (!(nRunTime && (0 < nRunTime))) nRunTime = 3000;
	if (!fInterpolation) fInterpolation = Animation.Interpolations.linear;

	this.state = oState;
	this.runTime = nRunTime;
	if ((!nResolution) || (1 > nResolution))
	{
		this.interval = 20;
		this.resolution = Animation.MAXRESOLUTION;
	}
	else
	{
		this.resolution = nResolution;
		this.interval = Math.max(1, Math.floor(nRunTime / nResolution));
	}
	this.interpolation = fInterpolation;
	this.callback = ('function' == typeof(fCallback)) ? fCallback : false;
}

var Animation = {
	$ : [],
	
	MAXRESOLUTION : -1,

	Interpolations : {
		linear : function (nStep, nSteps)
		{
			return (0 < nSteps) ? nStep / nSteps : 1;
		},
		exp2 : function (nStep, nSteps)
		{
			return (0 < nSteps) ? Math.exp((1 - nStep / nSteps) * Math.log(1 / 2)) : 1;
		},
		exp10 : function (nStep, nSteps)
		{
			return (0 < nSteps) ? Math.exp((1 - nStep / nSteps) * Math.log(1 / 10)) : 1;
		}
	},
	
	getState : function (nRatio, oCurrState, oNextState)
	{
		if (!oNextState) return oCurrState;
		var state = {};
		if (1 < nRatio) nRatio = 1;
		if (0 > nRatio) nRatio = 0;
		for (var i in oNextState)
		{
			if (null != oCurrState[i])
			{
				if ("opacity" == i)
				{
					state[i] = oCurrState[i] * (1 - nRatio) + oNextState[i] * nRatio;
					state.filter = 'alpha(opacity=' + Math.floor(100 * state[i]) + ')';
				}
				else if ("zIndex" == i)
				{
					state[i] = Math.round(oCurrState[i] * (1 - nRatio) + oNextState[i] * nRatio);
				}
				else
				{
					switch (typeof(oNextState[i]))
					{
						case 'object' :
							state[i] = Animation.getState(nRatio, oCurrState[i], oNextState[i]);
							break;
						case 'number' :
							state[i] = oCurrState[i] * (1 - nRatio) + oNextState[i] * nRatio;
							break;
						case 'string' :
							if ('#' == oNextState[i].charAt(0))
							{
								state[i] = Animation.getState(nRatio, new Color(oCurrState[i]), new Color(oNextState[i])).hex();
							}
							else if ('%' == oNextState[i].substr(oNextState[i].length - 1, 1))
							{
								var c = oCurrState[i].substr(0, oCurrState[i].length - 1) * 1;
								var n = oNextState[i].substr(0, oNextState[i].length - 1) * 1;
								state[i] = Math.round(c * (1 - nRatio) + n * nRatio) + '%';
							}
							else if ('px' == oNextState[i].substr(oNextState[i].length - 2, 2))
							{
								var c = oCurrState[i].substr(0, oCurrState[i].length - 2) * 1;
								var n = oNextState[i].substr(0, oNextState[i].length - 2) * 1;
								state[i] = Math.round(c * (1 - nRatio) + n * nRatio) + 'px';
							}
							else state[i] = oNextState[i];
							break;
						case 'function' :
						default :
							state[i] = oNextState[i];
							break;					
					}
				}
			}
		}
		return state;
	},
	
	getAnimationData : function (nAnimation)
	{
		return Animation.$[nAnimation] ? Animation.$[nAnimation] : false;
	},

	clear : function (nAnimation)
	{
		if (Animation.$[nAnimation] && Animation.$[nAnimation].timer) clearInterval(Animation.$[nAnimation].timer);
		Animation.$[nAnimation] = false;
	},

	setState : function (oObject, oAttributes)
	{
		var ex;
		for (var i in oAttributes)
		{
			if (('object' == typeof(oAttributes[i])) && ('object' == typeof(oObject[i])))
			{
				Animation.setState(oObject[i], oAttributes[i]);
			}
			else
			{
				try
				{
					oObject[i] = oAttributes[i];
				}
				catch (ex)
				{
					throw oObject + '.' + i + ' could not be set to ' + oAttributes[i] + '. (' + ex + ')';
				}
			}
		}
	},
	
	stop : function (nAnimation)
	{
		var animation;
		if (animation = Animation.$[nAnimation])
		{
			clearInterval(animation.timer);
		}
	},
	
	isAnimating : function (nAnimation)
	{
		var animation = Animation.$[nAnimation];
		if (animation && animation.timer)
		{
			return true
		}
		else return false;
	},
	
	animate : function (nAnimation)
	{
		var animation = Animation.$[nAnimation];
		if (animation.busy) return;
		animation.busy = true;
		if (animation && animation.keyFrames[animation.keyFrameIndex])
		{
			var res = animation.keyFrames[animation.keyFrameIndex].resolution;
			var steps;
			var step;
			if (1 > res)
			{
				var timestamp = (new Date()).getTime();
				steps = animation.keyFrames[animation.keyFrameIndex].runTime;
				step = timestamp - animation.frameIndex;
			}
			else
			{
				steps = res;
				step = animation.frameIndex;
				++animation.frameIndex;
			}
			var ratio = animation.keyFrames[animation.keyFrameIndex].interpolation(step, steps);
			var state = Animation.getState(ratio, animation.keyFrames[animation.keyFrameIndex].state, animation.keyFrames[animation.keyFrameIndex + 1] ? animation.keyFrames[animation.keyFrameIndex + 1] .state : false);
			Animation.setState(animation.object, state);
			if ((!animation.timer) && (animation.keyFrames[animation.keyFrameIndex + 1]))
			{
				animation.timer = setInterval('Animation.animate(' + nAnimation + ')', animation.keyFrames[animation.keyFrameIndex].interval);
			}
			if ((step >= steps) || (!animation.keyFrames[animation.keyFrameIndex + 1]))
			{
				animation.frameIndex = (1 > animation.keyFrames[animation.keyFrameIndex].resolution) ? (new Date()).getTime() : 0;
				clearInterval(animation.timer);
				if (animation.keyFrames[++animation.keyFrameIndex])
				{
					animation.timer = setInterval('Animation.animate(' + nAnimation + ')', animation.keyFrames[animation.keyFrameIndex].interval);
				}
				else
				{
					if (animation.callback) animation.callback(nAnimation);
					Animation.clear(nAnimation);
					return;
				}
			}
			if (animation.keyFrames[animation.keyFrameIndex].callback) animation.keyFrames[animation.keyFrameIndex].callback(nAnimation);
			animation.busy = false;
		}
		else
		{
			Animation.clear(nAnimation);
		}
	},

	attach : function (oObject, aKeyFrames, fCallback)
	{
		if (oObject && aKeyFrames && aKeyFrames.length && (1 < aKeyFrames.length))
		{
			if (!fCallback) fCallback = false;
			for (var i = 0; ((i < Animation.$.length) && Animation.$[i]); ++i);
			Animation.$[i] = {
				object : oObject,
				keyFrames : aKeyFrames,
				keyFrameIndex : 0,
				frameIndex : (1 > aKeyFrames[0].resolution) ? (new Date()).getTime() : 0,
				callback : fCallback,
				timer : false,
				busy : false
			};
			return i;
		}
		else return false;
	}
}

if (LighterBox.autoInitialize)
{
	if (window.addEventListener)
	{
		window.addEventListener('load', LighterBox.initialize, false);
	}
	else
	{
		if (window.attachEvent)
		{
			window.attachEvent('onload', LighterBox.initialize);
		}
		else
		{
			var onld = function () {};
			if (window.onload) onld = window.onload;
			window.onload = function () { LighterBox.initialize(); onld(); };
		}
	}
}
