var GFB = GFB || {};

(function($){

	$(function(){
		GFB.news.init();
	});
	
	GFB.news = {
		
		open: false,
		
		cache: null,
		
		init: function(){
			var obj = this;
			$('#archiveLink').click(function(){
				if (!obj.open) {
					
					if (obj.cache) {
						obj.create(obj.cache);
					}
					else {
						$.get('?page_id=13', function(res){
							obj.cache = res;
							obj.create(res);
						});
					}
					obj.open = true;
				}
				return false;
			});
		},
		
		html: '<div id="overlay"><div id="overlay00"><div id="overlayClose"></div></div></div>',
		
		position: function(overlay){
			var l = ($(window).width() - overlay.width()) / 2;
			var t = (($(window).height() - overlay.height()) / 2) + $(window).scrollTop();
			overlay.css({
				left: l+'px',
				top: t+'px'
			})
		},
		
		create: function(res){
			var obj = this;
			res = res.replace(/<script.*?>/g).replace(/<\/script>/g);
			var overlay = $(document.body).append(this.html).find('#overlay');
			var container = $(document.createElement('div')).html(res);
			overlay.children().append(container.find('li#monthly-archives h3'));
			overlay.children().append(container.find('li#monthly-archives ul'));
			overlay.find('#overlayClose').click(function(){
				overlay.remove();
				obj.open = false;
			})
			this.position(overlay);
		}
		
		
	}

})(jQuery);



