/**
 * Простая либа по созданию простеньких попапов
 * @author Gisma (dmitrey.schevchenko@gmail.com)
 */
var simplePopup = {
		popupVisible : false,
		init : function (config) {
			var self = this;
			var standartConfig = {
				container : '',
				onShow : {},
				dragable : false,
				useOverlay: false,
				useCenterAlignment : true
			};
			var $ = jQuery;
			var result = jQuery.extend(standartConfig,config);
			this.config = config;			
			// Подключаем обработчики
			jQuery('.popupLink').live('click',function (event) {
				// Определяем активный попап
				var id = jQuery(this).attr('rel');
				if (jQuery('#' + id).css('display') == 'none')
				{
					simplePopup.show(jQuery('#' + id));
				} else {
					simplePopup.hide(jQuery('#' + id));
				}
				// 
				return false;
			});
			// Закрытие
			jQuery('.popup .close').live('click',function (event) {
				simplePopup.hide(jQuery(this).parents('.popup'));
				return false;
			});
			// Отправка формы
			jQuery('.popup .submit').live('click',function (event) {
				simplePopup.submit(jQuery(this).parents('.popup'));
				return false;
			});
			$('*').live('click',function (event) {
				if (event.currentTarget == event.target)
				{
					var isPopupParent = $(this).parents('.popup').length > 0;
					var hasClass = $(this).hasClass('popupLink');
					var hasClassInParents = $(this).parents('.popupLink').length > 0;
					var condition = isPopupParent || ( hasClass || hasClassInParents ) ;
					if (!condition)
					{
						$('.popup').each(function () {
							simplePopup.hide(jQuery(this));
						});

					}
				}
			});
			if (config.useCenterAlignment)
			{
				$(window).resize(function () {
					self.moveCenter($('.popup:visible'));
				});
			}
			
			
		},
		/**
		 * Открывает попап
		 */
		show : function (dialog) {
			dialog.addClass('simple-popup-active').addClass('popup');
			jQuery('.popup').each(function () {
				simplePopup.hide(jQuery(this));
			});
			if (this.config.container) {
				jQuery(this.config.container).show();
			}
			
			var id = dialog.attr('id');
			if ( this.config.onShow[id] !== undefined)
			{
				this.config.onShow[id].call(dialog.get(0));
			} else {
				dialog.show();
			}
			
			if (this.config.draggable) {
				dialog.draggable();
			}
			// Если указано использовать оверлей, то он создается
			this.createOverlay();
			simplePopup.popupVisible = true;
			if (this.config.useCenterAlignment)
			{
				simplePopup.moveCenter(dialog);
			}
			
		},
		/**
		 * Скрывает попап
		 */
		hide : function (dialog) {

			simplePopup.popupVisible = false;
			this.hideOverlay();
			dialog.removeClass('simple-popup-active');
			dialog.hide();
			// Если есть контейнер, то его тоже скрываемы 
			if (this.config.container) {
				jQuery(this.config.container).hide();
			}
			
		},
		/**
		 * Смещает указанный объект в центр
		 */
		moveCenter : function (dialog) {
			var offsetLeft = $(window).scrollLeft() + ($(window).width() - dialog.width()) / 2;
			var offsetTop = $(window).scrollTop() + ($(window).height() - 80 - dialog.height()) / 2;
			dialog.css({
				position : 'absolute',
				left : offsetLeft + 'px',
				top : offsetTop + 'px'
			})
		},
		/**
		 * Отсылает форму
		 */
		submit : function (dialog) {
			dialog.find('form').submit();
			
		},
		createOverlay : function () {
			if (this.config.useOverlay)
			{
				this.hideOverlay();
				var overlay = $('<div/>').addClass('simple-popup-overlay');
				overlay.css('height','100%');
				overlay.css('width','100%');
				overlay.css('left','0');
				overlay.css('top','0');
				overlay.css('position','fixed');
				overlay.click(function () {
					simplePopup.hide($('.simple-popup-active'));
					return false;
				});
				$('body').prepend(overlay);

			}
			
		},
		hideOverlay : function () {
			if (this.config.useOverlay)
			{
				$('.simple-popup-overlay').remove();
			}
		}		
	
}
