jQuery.fn.lightBox = function(_options){
	var _options = jQuery.extend({
		lightboxContentBlock: '.pop-up',
		opener: 'a.openLightbox',
		faderOpacity: 0.65,
		faderBackground: '#000',
		closeLink:'a.close',
		href:true,
		onClick: null
	},_options);

	var _this = jQuery(_this);
			_this.lightboxContentBlock = _options.lightboxContentBlock;
			if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
				_this.faderOpacity = _options.faderOpacity;
				_this.faderBackground = _options.faderBackground;
				_this.closeLink = _options.closeLink;

				var _fader;
				var _lightbox = $(_this.lightboxContentBlock);
				if (!jQuery('div.lightbox-fader').length)
					_fader = $('body').append('<div class="lightbox-fader"></div>');
				
				_fader = jQuery('div.lightbox-fader');
				_lightbox.css('zIndex',999);
				_fader.css({
					opacity:_this.faderOpacity,
					backgroundColor:_this.faderBackground,
					display:'none',
					position:'absolute',
					top:0,
					left:0,
					zIndex:998,
					textIndent: -9999
				}).text('$nbsp');
				if ($.browser.msie && $.browser.version >6 ) {
					_fader.css({
						position:'fixed'
					}).text('$nbsp');
				}
			}
			Overlay();
			var _opener = $(_options.opener);
			var _lbox = $(_this.lightboxContentBlock);
			var _LightboxImage = _lbox.find("div.standart");
			var _LightboxText = _lbox.find(".LightboxText p");
			var _LightBoxNum = _lbox.find("span.curItem");
			var _LightBoxNumLength = _lbox.find("span.itemLength");
			var _curItem;
			var _next = $("a.next",_lbox);
			var _prev = $("a.prev",_lbox);
			var _Lvisible=false;
			
			
			//fader start
			function Overlay(){
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				var _thisHeight = $('body > div:eq(0)').outerHeight();
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
				if (_height > _thisHeight) {_fader.css('height',_height);} else {_fader.css('height',_thisHeight);}
			}

			function startdLightBox(_href, _text,_curNum){
				var _imgWidth =0;

				setTimeout(function(){
					_LightboxImage.find("param[name='movie']").attr("value",_href);
					_LightboxImage.find("embed").attr("src",_href);
					_fader.show();
					_lbox.css({
						width: 876+"px",
						display: "none"
					});
					lightboxPosition();
					_lbox.show(100);
					setTimeout(function(){
						_LightboxText.html(_text);
						_LightBoxNum.html(_curNum);
						_LightBoxNumLength.html(_opener.length);
					},20)
				},10);
				return false;
			}

			_opener.click(function(){
				_Lvisible= true;
				var _href = $(this).attr("href");
				var _text = $(this).attr("title")
				_curNum = _opener.index($(this))+1;
				startdLightBox(_href, _text, _curNum);
				return false;
			});

			function lightboxPosition(){
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lightbox.outerHeight();
				var _page = $('body > div:eq(0)');
				if (_height > _thisHeight) {
					if (!window.innerHeight) {
						_lightbox.css({
							position:'absolute',
							top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
						});
					} else {
						_lightbox.css({
							position:'fixed',
							top: ((_height - _lightbox.outerHeight()) / 2)+"px"
						});
					}
				}
				else {
					_lightbox.css({
						position:'absolute',
						top: 0
					});
				}
				if (_width > _lightbox.outerWidth()) _lightbox.css({left:(_width - _lightbox.outerWidth()) / 2 + "px"});
				else _lightbox.css({position:'absolute',left: 0});
				_fader.bgiframe();
			}
			
			
			_next.click(function(){
				if (_curNum >= _opener.length) _curNum=0;
				_opener.eq(_curNum).trigger("click");
				return false;
			});
			_prev.click(function(){
				if (_curNum == 1) _curNum=_opener.length-1;
				else _curNum-=2;
				_opener.eq(_curNum).trigger("click");
				return false;
			});
			
			jQuery(window).scroll(function(){
				if (_Lvisible) {
					lightboxPosition();
					Overlay();
				};
			});
			jQuery(window).resize(function(){
				if (_Lvisible) {
					lightboxPosition();
					Overlay();
				};
			});
			//close
			$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_Lvisible=false;
					_lightbox.hide(10, function(){
						_fader.hide(20);
					});
				}
			});
			$(_options.closeLink).click(function(){
				_Lvisible=false;
				_lightbox.hide(20, function(){
					_fader.hide(10);
				});
				return false;
			});
}

$(document).ready(function(){
	$("a.openLightbox").lightBox();
})