/**
 * jQuery Album
 * @version: 0.1
 * @date: 2008-05-29
 * @author: AndyB
 *
 * Based on jquery.lightbox by Warren Krewenki, Krzysztof Kotowicz
 *
 **/

(function($) {
	$.fn.album = function(options) {
		var $ = jQuery
		options = $.extend({
		//	Customizable
			//	resize images if they are bigger than window
			fitToScreen: true,
			//	Opacity of the background
			overlayOpacity: 0.6,
			//	Margin around the image/navigation
			borderSize: 10,
			//	Replace all transitions by show/hide
			fast: false,
			//	Img src for loading image
			images: {
				loading: 'img/album/loading.gif'
			},
			//	Strings for localisation
			strings: {
				help: '[\u2190] [P]: previous image, [\u2192] [N]: next image, [ESC] [X]: close image gallery',
				prevLinkTitle: 'previous image',
				nextLinkTitle: 'next image',
				closeTitle: 'close image gallery',
				shortcut: 'shortcut'
			},
			// Keyboard-Shortcuts for localisation
			keyboard: {
				//	Winamp like
				previous: 'z',
				next: 'b',
				slideshow: 'x',
				stop: 'v',
				comment: 'n',
				all: 'c',
				close: 'e'
			},
			// Layout customisation
			layout: ['image', 'all', 'comment', 'navbar', 'caption'],
			// Navigation bar customisation
			navbar: [
				{id: 'previous-button', html: '<img src="img/album/previous.png" alt="◁◁" longdesc="show previous picture" />'},
				{id: 'slideshow-button', html: '<img src="img/album/slideshow.png" alt="▷" longdesc="play slideshow" />'},
				{id: 'next-button', html: '<img src="img/album/next.png" alt="▷▷" longdesc="show next picture" />'},
				{id: 'stop-button', html: '<img src="img/album/pause.png" alt="▯▯" longdesc="pause slideshow" />'},
				{id: 'close-button', html: '<img src="img/album/close.png" alt="△" longdesc="close" />'},
				{id: 'comment-button', html: '<img src="img/album/comment.png" alt="○" longdesc="post a comment" />'},
				{id: 'all-button', html: '<img src="img/album/all.png" alt="□" longdesc="display all pictures" />'},
				{html: '<span id="album-image-number-current"></span>&nbsp;/&nbsp;<span id="album-image-number-total"></span>'}
			],
			// This function is called after the image is faded in
			onImageShow: function() {},
			// This function is called when an image is being loaded, before the container is resized
			onImageLoad: function() {},
			// This function is called before commentform is submitted
			validate: function() {},
			//	How long resizing should take (in milliseconds)
			resizeSpeed: 400,
			//	How long sliding should take (in milliseconds)
			slideSpeed: 400,
			//	How long fading should take (in milliseconds)
			fadeSpeed: 400
		}, options, {
		//	Internal variables, do not change
			//	stores all images in an album
			imageArray: [],
			//	stores the current image index
			activeImage: null,
			//	true, if loading image
			inProgress: false,
		});
		
		// Save the matched elements
		var container = this;

		/**
		 * Loads previous image
		 *
		 * @return boolean false
		 */
		function prev() {
			if(options.activeImage > 0) {
				_changeImage(options.activeImage -1);
			} else {
				stop();
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Loads next image
		 *
		 * @return boolean false
		 */
		function next() {
			if(options.activeImage < options.imageArray.length -1) {
				_changeImage(options.activeImage +1);
			} else {
				stop();
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Starts slideshow
		 *
		 * @return boolean false
		 */
		var slideshowStarted = false;
		function slideshow() {
			if(!slideshowStarted) {
				$('#album-slideshow-button').hide();
				$('#album-stop-button').show();
				slideshowStarted = window.setInterval(next, 5000);
				$('#album-previous-button').unbind('click').click(function() {
					stop();
					prev();
					return false;
				});
				$('#album-next-button').unbind('click').click(function() {
					stop();
					next();
					return false;
				});
				$('#album-comment-button').unbind('click').click(function() {
					stop();
					comment();
					return false;
				});
				$('#album-all-button').unbind('click').click(function() {
					stop();
					all();
					return false;
				});
				next();
				help(false);
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Stops slideshow
		 *
		 * @return boolean false
		 */
		function stop() {
			if(slideshowStarted) {
				window.clearInterval(slideshowStarted);
				slideshowStarted = false;
				$('#album-stop-button').hide();
				$('#album-slideshow-button').show();
				$('#album-previous-button').unbind('click').click(prev);
				$('#album-next-button').unbind('click').click(next);
				$('#album-comment-button').unbind('click').click(comment);
				$('#album-all-button').unbind('click').click(all);
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Show all images
		 *
		 * @return boolean false
		 */
		function all() {
			options.inProgress = true;
			//	Load image overview
			var newContainer = container.clone();
			newContainer.find('a').unbind('click').click(function() {
				_changeImage($(this).parent().parent().prevAll().length);
				return false;
			});
			$('#album-image, #album-video, #album-comment, #album-navbar, #album-caption, #album-all').hide();
			$('#album-all').html(newContainer).css('overflow', 'auto');
			__resizeContainer(420, 400, 'all', function() {
				if(options.fast) {
					$('#album-all').show();
				} else {
					$('#album-all').fadeIn(options.fadeSpeed);
				}
				options.inProgress = false;
			});
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Show comments
		 *
		 * @return boolean false
		 */
		var commentsShown = false; 
		function comment(doShow) {
			stop();
			if(doShow != true && doShow != false) {
				doShow = !commentsShown;
			}
			if(doShow) {
				//	Show comments
				_disableKeyboardNav(true);
				$('#album-all, #album-image, #album-video, #album-caption').hide();
				$('#album-next-button').unbind('click').click(nextComment);
				$('#album-previous-button').unbind('click').click(prevComment);
				$('#album-box').unbind('click');
				$('#album-slideshow-button, #album-stop-button').addClass('disabled').unbind('click').click(function() {});
				$('#album-all-button').unbind('click').click(function() {
					$('#album-comment').html('').hide();
					$('#album-next-button').unbind('click').click(next);
					$('#album-previous-button').unbind('click').click(prev);
					$('#album-all-button').unbind('click').click(all);
					$('#album-slideshow-button').removeClass('disabled').click(slideshow);
					$('#album-stop-button').removeClass('disabled').click(stop);
					commentsShown = false;
					all();
					return false;
				});
				_loadComment(options.activeImage);
				commentsShown = false;//({width: $('#album-image').width(), height: $('#album-image').height()});
			} else {
				//	Hide comments
				$('#album-comment').html('').hide();
				$('#album-next-button').unbind('click').click(next);
				$('#album-previous-button').unbind('click').click(prev);
				$('#album-all-button').unbind('click').click(all);
				$('#album-slideshow-button').removeClass('disabled').unbind('click').click(slideshow);
				$('#album-stop-button').removeClass('disabled').unbind('click').click(stop);
				$('#album-image, #album-caption').show(options.slideSpeed);
				commentsShown = false;
				_enableKeyboardNav();
				_changeImage(options.activeImage);
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Loads comment
		 *
		 */
		function _loadComment(id) {
			if(id >= 0 && id < options.imageArray.length) {
				options.activeImage = id;
				$.get(options.commentSrc.replace(/\{name\}/g, options.imageArray[id].url), null, function(data) {
					__resizeContainer(420, 480, 'comment');
					//	
					data += '<!--';
					$('#album-comment').html('<div style="text-align: center;"><img src="' +options.imageArray[options.activeImage].link +'" style="width: auto; height: 160px" alt="" /><p style="text-align: center;">' +options.imageArray[options.activeImage].caption +'</p></div>' +data.substring(0, data.lastIndexOf('<!--'))).show(function() {
						$(this).css({overflow: 'auto'});
					});
					$('#album-comment dl.comment dt small').prepend('schrieb am ');
					$('#album-comment dl.comment dt br').remove();
					$('#album-image-number-current').html(options.activeImage +1);
					if ((options.activeImage) > 0) {
						$('#album-previous-button').removeClass('disabled').click(prevComment);
						$('#album-previous').click(prev);
					} else {
						$('#album-previous-button').addClass('disabled').click(function(){return false;});
					}
					if ((options.activeImage + 1) < options.imageArray.length) {
						$('#album-next-button').removeClass('disabled').click(nextComment);
						$('#album-next').click(next);
					} else {
						$('#album-next-button').addClass('disabled').click(function(){return false;});
					}
					
					$('#album-comment :submit').click(function() {
						if(options.validate($('#album-comment form'))) {
							$.post($('#album-comment form').attr('action'), $('#album-comment form').serialize(), function() {
								_loadComment(options.activeImage);
							});
						}
						return false;
					});
				});
			}
		}

		/**
		 * Show previous comment
		 *
		 * @return boolean false
		 */
		function prevComment() {
			_loadComment(options.activeImage -1);
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Show next comment
		 *
		 * @return boolean false
		 */
		function nextComment() {
			_loadComment(options.activeImage +1);
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Close
		 *
		 * @return boolean false
		 */
		function close(force) {
			if(!commentsShown || force == true) {
				if(commentsShown) {
					comment(false);
				}
				//	Unbind shortcuts
				_disableKeyboardNav();
				//	Hide things
				$('#album-box').hide();
				if(options.fast) {
					$('#album-overlay').hide();
				} else {
					$('#album-overlay').fadeOut(options.fadeSpeed);
				}
				$('#album-overlay').remove();
				$('#album-box').remove();
				$('select, object, embed, applet').show();
			}
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Toggle help text
		 *
		 * @return boolean false
		 */
		var caption = false; 
		function help(hide) {
			if(!caption && !hide && $(this).attr('id') && $(this).children().attr('longdesc') != '' && options.keyboard[$(this).attr('id').substring('album-'.length, $(this).attr('id').indexOf('-button'))]) {
				caption = $('#album-caption').html();
				$('#album-caption').html($(this).children().attr('longdesc') +', ' +options.strings.shortcut +':&nbsp;' +options.keyboard[$(this).attr('id').substring('album-'.length, $(this).attr('id').indexOf('-button'))].toUpperCase());
			} else if(caption || hide == true) {
				$('#album-caption').html(caption);
				caption = false;
			}
			return false;
		}

		/**
		 * Initialisation
		 *
		 * @return boolean false
		 */
		function _initialize() {
			//	not loading anything yet
			options.inProgress = false;
			//	"this" is the element, the user clicked on
			_start(this, container);
			//	the browser should not follow the link
			return false;
		}

		/**
		 * Start the album
		 *
		 * @param object linkClicked: The link which the user has clicked
		 * @param object container: The jQuery object with all elements matched
		 */
		function _start(linkClicked, container) {
			//	Yet another IE workaround (YAIW)
			$('embed, object, select, applet').hide();

			close();

		//	Generate markup
			var layout = ({image: '', video: '', navbar: '', caption: '', comment: '', all: ''});
			layout.image = '<img src="" id="album-img" />\n'
				+'<div id="album-loading">\n'
				+'	<a href="#" id="album-loading-link">\n'
				+'		<img src="' +options.images.loading +'" />\n'
				+'	</a>\n'
				+'</div>\n';
			for(var i = 0; i < options.navbar.length; i++) {
				if(options.navbar[i].id) {
					layout.navbar += '<a href="#" id="album-' +options.navbar[i].id +'">' +options.navbar[i].html +'</a>\n';
				} else {
					layout.navbar += options.navbar[i].html +'\n';
				}
			}
			var markup = '';
			for(var i = 0; i < options.layout.length; i++) {
				markup += '<div id="album-' +options.layout[i] +'">' +layout[options.layout[i]] +'</div>\n';
			}
			//	Append markup
			$('body').append('<div id="album-overlay"></div><div id="album-box"><div id="album-container">' +markup +'</div></div>');
			//	Add close action
			$('#album-overlay, #album-box').click(close);
			$('#album-close-button').click(function() { close(true); });
			//	Add context help
			$('#album-navbar [id]').hover(help, help);
			//	Add comments action
			$('#album-comment-button').click(comment);
			//	Disable slideshow controls, they are set in _updateDetails()
			$('#album-slideshow-button, #album-all-button').addClass('disabled').click(function() {});
			$('#album-stop-button').hide();
			//	Hide help, navbar, caption and container
			$('#album-all, #album-navbar, #album-caption, #album-comment').css({paddingLeft: options.borderSize, paddingRight: options.borderSize}).add('#album-container').css({width: 320 +'px'}).hide();
			$('#album-navbar').css({width: (320-2*options.borderSize) +'px'});

			//	Format overlay and fade in
			var arrayPageSize = __getPageSize();
			$('#album-overlay').css({width: arrayPageSize[0] +'px', height: arrayPageSize[1] +'px', opacity : options.overlayOpacity});
			if(options.fast) {
				$('#album-overlay').show();
			} else {
				$('#album-overlay').fadeIn(options.fadeSpeed);
			}

			//	Calculate top and left offset for the album container
			var arrayPageScroll = __getPageScroll();
			var albumTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
			var albumLeft = arrayPageScroll[0];
			$('#album-box').css({top: albumTop +'px', left: albumLeft +'px'});
			if(options.fast) {
				$('#album-box, #album-container').show();
			} else {
				$('#album-box, #album-container').fadeIn(options.fadeSpeed);
			}

			//	Catch window resizing and adapt overlay size
			$(window).resize(function() {
				//	Format overlay
				var arrayPageSize = __getPageSize();
				$('#album-overlay').css({
					width: arrayPageSize[0],
					height: arrayPageSize[1]
				});
				//	Calculate top and left offset for the album container
				var arrayPageScroll = __getPageScroll();
				$('#album-box').css({
					top: arrayPageScroll[1] +(arrayPageSize[3] / 10),
					left: arrayPageScroll[0]
				});
			});
		//	Markup generated

		//	Feed album with data
			//	imageArray = [{link: img1.href, caption: img1.title}, ...]
			options.imageArray = new Array();
			options.activeImage = 0;
			//	Get links
			links = $('a', container);

			//	Add images to imageArray
			if(links.length == 1) {
				//	Single image
				options.imageArray.push({link: links.get(0).getAttribute('href'), caption: links.get(0).getAttribute('title'), url: links.eq(0).find('img').attr('longdesc'), isVideo: links.get(0).find('img').hasClass('video')});
			} else {
				//	Many images
				for(var i = 0; i < links.length; i++) {
					options.imageArray.push({link: links.get(i).getAttribute('href'), caption: links.get(i).getAttribute('title'), url: links.eq(i).find('img').attr('longdesc')});
				}
			}
			while(options.imageArray[options.activeImage].link != linkClicked.getAttribute('href')) {
				options.activeImage++;
			}

			_changeImage(options.activeImage);
		}

		/**
		 * Changes an image
		 *
		 * @param int imageNum: Index of image in options.imageArray
		 */
		function _changeImage(imageNum) {
			if(options.inProgress == false) {
				$('#album-navbar').show();
				$('#album-all').hide();
				options.inProgress = true;
				options.activeImage = imageNum;	// update global var

				// hide elements during transition
			//	$('#album-loading').show();
				$('#album-img, #album-help').hide();
/*
				if(options.fast) {
					$('#album-navbar, #album-caption').hide();
				} else {
					$('#album-navbar, #album-caption').slideUp(options.slideSpeed);
				}
*/
				if(options.imageArray[options.activeImage].link.indexOf('#') == -1) {
					$('#album-video').hide();
					$('#album-image').show();
					imgPreloader = new Image();

					// once image is preloaded, resize image container
					imgPreloader.onload = function() {
						var newWidth = imgPreloader.width;
						var newHeight = imgPreloader.height;

						if(options.fitToScreen) {
							var arrayPageSize = __getPageSize();
							var initialPageWidth = arrayPageSize[2] - 2 * options.borderSize;
							var initialPageHeight = arrayPageSize[3] - 200;

							if(imgPreloader.height > initialPageHeight) {
								newWidth = parseInt((initialPageHeight/imgPreloader.height) * imgPreloader.width);
								newHeight = initialPageHeight;
							} else if (imgPreloader.width > initialPageWidth) {
								newHeight = parseInt((initialPageWidth/imgPreloader.width) * imgPreloader.height);
								newWidth = initialPageWidth;
							}
						}

						$('#album-img').attr('src', options.imageArray[options.activeImage].link).width(newWidth).height(newHeight);
						__resizeContainer(newWidth, newHeight, 'image', _showImage);
						//	YAIW
						imgPreloader.onload=function(){};
					}

					imgPreloader.src = options.imageArray[options.activeImage].link;
				} else {
					$('#album-image').hide();
					$('#album-video').show();
					var _lnk = options.imageArray[options.activeImage].link;
					var _player = _lnk.replace(/.*player=(.*\.swf).*/, '$1');
					var _width = parseInt(_lnk.replace(/.*width=([1-9][0-9]*).*/, '$1'));
					var _height = parseInt(_lnk.replace(/.*height=([1-9][0-9]*).*/, '$1'));
					var _duration = parseFloat(_lnk.replace(/.*duration=([\.0-9]*).*/, '$1'));
					var _file = _lnk.replace(/.*file=(.*\.flv).*/, '$1');
					var _image = _lnk.replace(/^([^#]+).*/, '$1');
					var _autostart = false;
					if(slideshowStarted) {
						stop();
						_autostart = true;
					}
					$('#album-video').html($.flash({
						swf:_player, 
						width:_width, 
						height:_height, 
						hasVersion:9, 
						params:{
							allowfullscreen:true, 
							allowscriptaccess:"always", 
							flashvars:{
								autostart:_autostart, 
								controlbar:"over", 
								duration:_duration, 
								file:_file, 
								image:_image
							}
						}
					}));
					if(_autostart) {
						window.setTimeout(slideshow, (_duration*1000)+5000);
					}
					__resizeContainer(_width, _height, 'video', function() {
						$('#album-loading').hide();
						_updateDetails();
						_preloadNeighborImages();
						options.inProgress = false;
					});
				}
				options.onImageLoad();
			}
		}

		/**
		 * Shows image and calls onImageShow()
		 *
		 */
		function _showImage() {
			$('#album-loading').hide();
			if(options.fast) {
				$('#album-img').show(options.onImageShow);
			} else {
				$('#album-img').fadeIn(options.fadeSpeed, options.onImageShow);
			}
			_updateDetails();
			_preloadNeighborImages();

			options.inProgress = false;
		};

		/**
		 * Loads caption and fills #album-image-number-current and #album-image-number-total
		 *
		 */
		function _updateDetails() {
			//	Display caption
			if(options.imageArray[options.activeImage].caption) {
				$('#album-caption').html(options.imageArray[options.activeImage].caption).show();
			} else {
				$('#album-caption').text('');
			}
			caption = false;

			//	Fill #album-image-number-current and #album-image-number-total
			if(options.imageArray.length > 1) {
				$('#album-all-button').removeClass('disabled').click(all);
				$('#album-slideshow-button').removeClass('disabled').click(slideshow);
				$('#album-stop-button').click(stop);
				$('#album-image-number-current').text(options.activeImage + 1);
				$('#album-image-number-total').text(options.imageArray.length);

				// display previous / next text links
				if ((options.activeImage) > 0) {
					$('#album-previous-button').removeClass('disabled').click(prev);
					$('#album-previous').click(prev);
				} else {
					$('#album-previous-button').addClass('disabled').click(function(){return false;});
				}

				if ((options.activeImage + 1) < options.imageArray.length) {
					$('#album-next-button').removeClass('disabled').click(next);
					$('#album-next').click(next);
				} else {
					$('#album-next-button').addClass('disabled').click(function(){return false;});
				}
				if(options.fast) {
					$("#album-navbar").show();
				} else {
					$("#album-navbar").slideDown(options.slideSpeed);
				}
			} else {
				$('#album-previous-button, #album-next-button, #album-all-button').addClass('disabled').unbind();
			}

			var arrayPageSize = __getPageSize();
			$('#album-overlay').height(arrayPageSize[1]);
			_enableKeyboardNav();
		};

		/**
		 * Loads previous and next images
		 *
		 */
		_preloadNeighborImages = function() {
			if((options.imageArray.length - 1) > options.activeImage) {
				preloadNextImage = new Image();
				preloadNextImage.src = options.imageArray[options.activeImage + 1].link;
			}
			if(options.activeImage > 0) {
				preloadPrevImage = new Image();
				preloadPrevImage.src = options.imageArray[options.activeImage - 1].link;
			}
		};

		/**
		 * Binds shortcuts to events
		 *
		 */
		function _enableKeyboardNav() {
			document.onkeydown = __keyDown;
		};

		/**
		 * Unbinds shortcuts from events
		 *
		 */
		function _disableKeyboardNav(keepEscape) {
			if(keepEscape) {
				document.onkeydown = function(e) {
					if (e == null) { // ie
						var keycode = event.keyCode;
						var escapeKey = 27;
					} else { // mozilla
						var keycode = e.keyCode;
						var escapeKey = e.DOM_VK_ESCAPE;
					}

					var key = String.fromCharCode(keycode).toLowerCase();

					if(keycode == escapeKey) {
						close(true);
					}
				};
			} else {
				document.onkeydown = '';
			}
		};

		/**
		 * Handles keydown-events
		 *
		 */
		function __keyDown(e) {
			if (e == null) { // ie
				var keycode = event.keyCode;
				var escapeKey = 27;
			} else { // mozilla
				var keycode = e.keyCode;
				var escapeKey = e.DOM_VK_ESCAPE;
			}

			var key = String.fromCharCode(keycode).toLowerCase();

			if((options.keyboard.close.indexOf(key) > -1) || (keycode == escapeKey)) {
				close();
			} else if((options.keyboard.previous.indexOf(key) > -1) || (keycode == 37)) {
				prev()
			} else if((options.keyboard.next.indexOf(key) > -1) || (keycode == 39)){
				next()
			} else if((options.keyboard.slideshow.indexOf(key) > -1)){
				slideshow()
			} else if((options.keyboard.stop.indexOf(key) > -1)){
				stop()
			} else if((options.keyboard.all.indexOf(key) > -1)){
				all()
			}
		};

		/**
		 * Adjusts dimensions of containers
		 *
		 * @param int imgWidth: imageWidth
		 * @param int imgHeight: imageHeight
		 */
		function __resizeContainer(imgWidth, imgHeight, whatToResize, callback){
			//	get current width and height
			$('#album-video').width($('#album-image').width())
			$('#album-video').height($('#album-image').height())
			var currentWidth = $('#album-image').width();
			var currentHeight = $('#album-image').height();

			//	get new width and height
			var widthNew = (imgWidth  + (options.borderSize * 2));
			var heightNew = (imgHeight  + (options.borderSize * 2));

			$('#album-image, #album-video').css({padding: options.borderSize +'px'});
			$('#album-caption, #album-navbar, #album-help, #album-all, #album-comment').css({paddingLeft: options.borderSize +'px', paddingRight: options.borderSize +'px', width: imgWidth});

			$('#album-' +whatToResize).css({width: imgWidth, height: imgHeight}).show(callback); //.slideDown(options.resizeSpeed, callback);
			//	Prevent flickering
			if((currentWidth == imgWidth) && (currentHeight == imgHeight)){
				//	YAIW
				if(jQuery.browser.msie) {
					__pause(250);
				} else {
					__pause(100);
				}
			}

			$('#album-container').css({width: widthNew +'px'});
		};

		/**
		 * Get information about window and document
		 *
		 * @return array: [pageWidth, pageHeight, windowWidth, windowHeight]
		 */
		function __getPageSize() {
			var xScroll, yScroll;

			if (window.innerHeight && window.scrollMaxY) {
				xScroll = window.innerWidth + window.scrollMaxX;
				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
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth;
				} else {
					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 = xScroll;
			} else {
				pageWidth = windowWidth;
			}

			return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		}

		/**
		 * Get information about position in document
		 *
		 * @return array: [xScroll,yScroll]
		 */
		__getPageScroll = function(){
			var xScroll, yScroll;

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

			return new Array(xScroll,yScroll);
		}

		__pause = function(ms) {
			var date = new Date();
			var curDate = null;
			do {
				curDate = new Date();
			} while(curDate - date < ms);
		}

		//	Return jQuery object
		return this.unbind('click').find('a').click(_initialize);
	}
})(jQuery);
