/*
 * jQuery simpleLightbox v1.0.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
jQuery.fn.simpleLightbox = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: .0,
		faderBackground: '#000000',
		closeLink:'a.close'
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		_this.lightboxContentBlock = _options.lightboxContentBlock;
		_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');
		
		_this.click(function(){
			_fader.fadeIn(300, function(){
				jQuery('.lightbox').fadeOut(400);
				_lightbox.fadeIn(400);
				jQuery.fn.simpleLightbox.positionLightbox();
				if(typeof(hideSelectBoxes) == 'function') hideSelectBoxes(_lightbox);
			});
			return false;
		});
		jQuery(_this.closeLink).click(function(){
			_lightbox.fadeOut(400, function(){
				_fader.fadeOut(300);
				if(typeof(showSelectBoxes) == 'function') showSelectBoxes(_lightbox);
			});
			return false;
		});
		
		jQuery.fn.simpleLightbox.positionLightbox = function () {
			var _height = 0;
			var _width = 0;
			var _minWidth = $('body > div').outerWidth();
			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(function(){
			jQuery.fn.simpleLightbox.positionLightbox();
		});
		if (!window.innerHeight) {
			jQuery(window).scroll(function(){
				jQuery.fn.simpleLightbox.positionLightbox();
			});
		}
		
		jQuery.fn.simpleLightbox.positionLightbox();
		
	});
}
$(document).ready(function(){
	$('a.open-calls').simpleLightbox({
		lightboxContentBlock:'.popup-calls',
		closeLink:'a.close'
	});
	$('a.open-messages').simpleLightbox({
		lightboxContentBlock:'.popup-messages',
		closeLink:'a.close'
	});
	$('a.open-write').simpleLightbox({
		lightboxContentBlock:'.popup-write',
		closeLink:'a.close'
	});
});
