$(document).ready(function(){
	initPopup();
});

function initPopup(){
	var _opener = $('a.popup-opener');
	_opener.each(function(){
		var _btn = $(this);
		var _hoverFlag = false;
		var _box = $(this).parents('div.popup-holder').find('div.popup');
		$(this).click(function(){
			if(!$(this).hasClass('opened')){
				$(this).addClass('opened');
				_box.show();
			}
			else{
				$(this).removeClass('opened');
				_box.hide();
			}
			var _closer = _box.find('a.close');
			_closer.click(function(){
				_btn.removeClass('opened');
				_box.hide();
				return false;
			});
			return false;
		});
		_box.mouseenter(function(){
			_hoverFlag = false;
		}).mouseleave(function(){
			_hoverFlag = true;
		});
		$('body').click(function(){
			if(_hoverFlag){
				_box.hide();
				_btn.removeClass('opened');
			}
		});
	});
}

