$(document).ready(function(){
	initPopupFunction();
});

function initPopupFunction(){
	var change_speed = 1000; 
	var _fader = $('<div id="jquery-overlay"></div>');
	$('body').append(_fader);
	var _height = 0;
	var _width = 0;
	var _page = $('body > div:eq(0)');
	var _minWidth = _page.outerWidth();
	if (window.innerHeight) {
		_height = window.innerHeight;
		_width = window.innerWidth;
	}
	else {
		_height = document.documentElement.clientHeight;
		_width = document.documentElement.clientWidth;
	}
	if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
	if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
	_fader.css({
		'position':'absolute',
		'top':0,
		'left':0,
		'backgroundColor':'#000',
		'zIndex':'999',
		'opacity':'0'
	 });
	_fader.hide();


	$('a.enlarge').each(function(){
		var _href = $(this).attr('href');
		$(this).click(function(){
			if(!$(this).hasClass('opened')){
				$('a.enlarge').removeClass('opened');
				$(this).addClass('opened');
				$('div.popup-frame').hide();
				var _popup = $(_href);
				var _prev_html = _popup.html();
				
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				}
				else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				//_fader.css('opacity', 0).show().animate({opacity: 0.6}, {queue:false, duration:change_speed});
				_fader.css('opacity', 0.6).show();
				_popup.show();
				var _boxH = _popup.outerHeight();
				var scroll = $(window).scrollTop();
				_popup.css('top', (_height - _boxH)/2 + scroll);
			}
			else{
				var _popup = $(_href);
				var _prev_html = _popup.html();
				$(_popup).empty().html(_prev_html).hide();
				$(this).removeClass('opened');
				_fader.fadeOut();
			}
			var _closer = $(_href).find('a.close');
			_closer.click(function(){
				$(_popup).empty().html(_prev_html).hide();
				$('a.enlarge').removeClass('opened');
				_fader.fadeOut();
				return false;
			});
			return false;
		});
	});
};
