jQuery.fn.simpleLightbox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		lightboxContentBlock: 'popup-1',
		faderOpacity: 0.3,
		faderBackground: '#000000',
		closeLink:'a.popup-close',
		href:true,
		iewidth:false,
		onClick: null
	},_options);

	return this.each(function(i, _this){
        var _this = jQuery(_this);

		//if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		//else _this.lightboxContentBlock = _this.attr('href');

		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
            var popup_name = jQuery(document.getElementById("open_popup_name"));
            var _lightbox;
            /*if(popup_name)
            {
                _lightbox = $('#'+popup_name.attr('class'));
            }
            else{*/
                _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);
			
            if ($.browser.msie && $.browser.version == '6.0')
            {
                _fader.css({
                    opacity:_this.faderOpacity,
                    backgroundColor:_this.faderBackground,
                    position:'absolute',
                    top:0,
                    left:0,
                    zIndex:998,
                    height:1000,
                    textIndent: -9999
                }).text('&nbsp;');
            }
            else{
                _fader.css({
                    opacity:_this.faderOpacity,
                    backgroundColor:_this.faderBackground,
                    display:'none',
                    position:'fixed',
                    top:0,
                    left:0,
                    zIndex:998,
                    textIndent: -9999
                }).text('&nbsp;');
            }

			_lightbox.shownFlag = false;

            playthislightbox = function(){
                if (jQuery.isFunction(_options.onClick)) {
                    _options.onClick.apply(_this);
                }
                _fader.fadeIn(300, function(){
                    _lightbox.shownFlag = true;
                    _lightbox.fadeIn(400);
                    jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
                });
                return false;
			}
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				_fader.fadeIn(300, function(){
					_lightbox.shownFlag = true;
					_lightbox.fadeIn(400);
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				});
				return false;
			});
            
            /////////////
            document.getElementsByClassName = function(cl) { 
                    var retnode = []; 
                    var myclass = new RegExp('\\b'+cl+'\\b'); 
                    var elem = this.getElementsByTagName('*'); 
                    for (var i = 0; i < elem.length; i++) { 
                    var classes = elem[i].className; 
                    if (myclass.test(classes)) retnode.push(elem[i]); 
                    } 
                    return retnode;
            };
			
            /////////////
			jQuery(_this.closeLink).click(function(){
//                 userclick = 'yes';
//                 var elems = document.getElementsByClassName('popup-hover');
//                 if(elems[0] != null)
//                 {
//                     if(elems[0].className != 'popup-hover')
//                     {
//                         newClassName = elems[0].className;
//                         newClassName = newClassName.substr(newClassName.indexOf('popup-hover'),11);
//                         elems[0].className = newClassName;
//                     }
//                 }
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
					//flashVideoRemove(_lightbox);
				});
				return false;
			});
			_fader.click(function(){
                    userclick = 'yes';
                    var elems = document.getElementsByClassName('popup-hover');
                    if(elems[0] != null)
                    {
                        if(elems[0].className != 'popup-hover')
                        {
                            newClassName = elems[0].className;
                            newClassName = newClassName.substr(newClassName.indexOf('popup-hover'),11);
                            elems[0].className = newClassName;
                        }
                    }
				if(_lightbox.css('display') == 'block')
                {
                    _lightbox.fadeOut(400, function(){
                        _fader.fadeOut(300);
						//flashVideoRemove(_lightbox);
                    });
                }
				return false;
			});

			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if (_options.iewidth == true){
					var _lwidth = $(_options.lightboxContentBlock).find('object').width();
					$(_options.lightboxContentBlock).css('width', _lwidth);
				}
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				_minWidth = 1000;
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = $('body > div:eq(0)');
				if (_lbox.length) {
					if (_height > _page.outerHeight(true)) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if (_width < _minWidth) {
						_fader.css('width',_minWidth);
					}
					else {_fader.css('width',_width);}
					if (_height > _thisHeight) {
						if (!window.innerHeight) {
							_lbox.css({
								position:'absolute',
								top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
							});
						} else {
							_lbox.css({
								position:'fixed',
								top: 52+"px"
							});
						}
					}
					else {
						_lbox.css({
							position:'absolute',
							top: 0
						});
						_fader.css({
							height:_thisHeight
						});
					}
					if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
					else _lbox.css({position:'absolute',left: 0});
				}
			}

			jQuery(window).resize(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).scroll(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).click(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			
			$('#popup-1 .tabset ul li a').click(function(){
				setTimeout(function()
				{
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				}, 300)
			});

			$('.light-box a.size1080').click(function (){
				//$(this).parent().parent().find('object').attr({width:'1005', height:'560'});
				flashVideoAdd('#lightbox-video1',1080);
				//jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			$('.light-box a.size720').click(function (){
				flashVideoAdd('#lightbox-video1',720);
			});
			$('.light-box a.size480').click(function (){
				flashVideoAdd('#lightbox-video1',480);
			});
			$('.light-box a.link-vimeo').click(function (){
				flashVideoAdd('#lightbox-video1',4);
			});
			$('.light-box a.link-youtube').click(function (){
				flashVideoAdd('#lightbox-video1',5);
			});
			
			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						_fader.fadeOut(300);
						//flashVideoRemove(_lightbox);
					});
				}
			});
                     
            if ($.browser.msie && $.browser.version == '6.0')
            {     
                $(window).scroll(function(event){
                    jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
                    _fader.css('height',$('body').height())
                });
            }
            
		}
	});
}
$(document).ready(function(){
	/*$('a.open-popup').simpleLightbox({
		faderOpacity: 0.5,
		faderBackground: '#000000',
		closeLink:'a.popup-close'
	});*/
});
