$(document).ready(function(){
   tooltip();
   
    var detect = navigator.userAgent.toLowerCase();
	//ToolTip
	$('div.image-box').hover(function(){
		$(this).addClass('hover')
		$(this).parent().parent().addClass('hover')
	}, function(){
		$(this).removeClass('hover');
		$(this).parent().parent().removeClass('hover')
	});

	// Popup
	function popup(){
		var _options = {
			lightboxContentBlock: '#popup',
			faderOpacity: .5,
			faderBackground: '#000000'
		};

		var _fader;
		var _this = $('a.popup-link');

		_this.lightboxContentBlock = _options.lightboxContentBlock;
		_this.faderOpacity = _options.faderOpacity;
		_this.faderBackground = _options.faderBackground;

		var _lightbox = $(_this.lightboxContentBlock);
		if (!jQuery('div.fader').length) _fader = $('body').append('<div class="fader"></div>');

		_fader = $('div.fader');
		_fader.css({
			opacity:_this.faderOpacity,
			backgroundColor:_this.faderBackground
		}).text('&nbsp;');

		_fader.click(function(){
			_lightbox.fadeOut(200, function(){
				_fader.fadeOut(100);
			});
			return false;
		});
		
		_this.live('click', function(){
			_lightbox.css('zIndex',999);
			_return = $(this).attr('rel')
			_product = $(this).attr('product')
			_position = $(this).attr('position')
			_cat = $(this).attr('cat')
			
			if (_return) _lightbox.html($('#'+_return).html());

			_fader.fadeIn(100, function(){
				_lightbox.fadeIn(200);
				positionLightbox();
			});
			
			$('a.close-button').click(function(){
				_lightbox.fadeOut(200, function(){
					_fader.fadeOut(100);
				});
				return false;
			});
			
			
			$('div.image-box').hover(function(){
				$('div.image-box').addClass('other')
				$(this).addClass('hover')
				$(this).parent().parent().addClass('hover')
				$(this).removeClass('other')
				
			}, function(){
				$(this).removeClass('hover');
				$(this).parent().parent().removeClass('hover')
				$('div.image-box').removeClass('other')
			});
			
			//tooltip();
			
			document.getElementById('mainpopup').src="/product_popup.php?id="+_product+'&position='+_position+'&cat='+_cat;			

			return false;
		});

		function positionLightbox() {
			var _height = 0;
			var _width = 0;
			var _minWidth = 610;
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			} else {
				_height = document.documentElement.clientHeight;
				_width = document.documentElement.clientWidth;
			}
			var _page = $('body');
			if (_lightbox.length) {
				if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
				if (_width < _minWidth) _fader.css('width',_minWidth); else {_fader.css('width','100%');}
				if (_height > _lightbox.innerHeight()) {
					if (!window.innerHeight) {
						_lightbox.css({
							position:'absolute',
							top: (document.documentElement.scrollTop + (_height - _lightbox.outerHeight()) / 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});
			}
		}

		jQuery(window).resize(positionLightbox());
		positionLightbox();
	}

	popup();
});