var pop = {

	/**
	 * popBox
	 * Pops up a content block
	 * @param Object settings
	 * @author contato@skatista.net
	 */
	popBox: function(settings) {
		if (typeof(settings) == "object") {
			// Defaults interval to 60 seconds
			settings.interval = (isNaN(settings.interval) ? 60000 : settings.interval);
			
			// Verify whether block exists and run timer once document is ready
			$(document).ready(function() {
				var el = "#" + settings.id;
				if ($(el).length == 1 && pop.read_cookie("skatista_pbox") === false) {
					// Fix IE6
					if ($.browser.msie && $.browser.version == "6.0") {
						$(document.body).css({width: "100%", height: "100%"});
						$(el).css({position: "absolute"});
					}
					
					var t = window.setTimeout(function() {
						// Retrieve block's position
						var pos = calcPosition(el);
						// Center and display block
						$(el).fadeIn();
						$(el + "_content").css({left: pos.left, top: pos.top}).fadeIn("slow");
						// Set a cookie so it won't show up on every single request
						pop.set_cookie("skatista_pbox", "1", 7);
					}, settings.interval);
					
					// Center block on window resize events
					$(window).resize(function() {
						var pos = calcPosition(el);
						$(el + "_content").css({left: pos.left, top: pos.top});
					});
					
					// Close box
					$("." + settings.id + "_close").click(function(e) {
						pop.close_box(settings.id);
					});
				}
			});
		}
		
		// Center block in the viewport
		var calcPosition = function(el) {
			var width = $(el).outerWidth();
			var height = $(el).outerHeight();
			var block_width = $(el + "_content").outerWidth();
			var block_height = $(el + "_content").outerHeight();
			var pos_left = Math.abs((width / 2) - (block_width / 2));
			var pos_top = Math.abs((height / 2) - (block_height / 2));
			
			return new Object({left: pos_left, top: pos_top});
		};
	},
	
	close_box: function(id) {
		if ($.browser.msie && $.browser.version == "6.0") {
			$(document.body).css({width: "auto", height: "auto"});
		}
		$("#" + id).hide();
	},
	
	set_cookie: function(n, v, ed) {
		var vdate = new Date();
		vdate.setDate(vdate.getDate() + ed);
		document.cookie = n + '=' + v + ((ed == '' || ed == 0) ? '' : ';expires=' + vdate.toUTCString() + ';path=/');
    },
	
	read_cookie: function(n) {
		if (document.cookie.length > 0) {
			cstrt = document.cookie.indexOf(n + '=');
			if (cstrt != -1) {
				cstrt = cstrt + n.length + 1;
				cend = document.cookie.indexOf(';', cstrt);
				if (cend == -1) cend = document.cookie.length;
				return unescape(document.cookie.substring(cstrt, cend));
			} else {
				return false;
			}
		} else {
			return false;
		}
    }

};
