// Use it when DOM is ready or if some new inputs are appended
// Huh, now DOM is ready!
$(document).ready(function() {
	$(".default-value").click(function() {
		$(this).children("span").css("display", "none");
		$(this).children("input.text").focus();
	})
	
	$("input.with-default-value").focus(function() {
		if ($(this).attr("value") == "") {
			$(this).parent().children("span").css("display", "none");
		}
	})
	
	$("input.with-default-value").blur(function() {
		if ($(this).attr("value") == "")
			$(this).parent().children("span").removeAttr("style");
	})
	
	np_add_css("\
		div.nice-popup {\
			position: fixed;\
			top: 2px;\
			right: 2px;\
			border: 1px solid #d49b75;\
			background-color: #503c27;\
			cursor: pointer;\
			padding: 5px 10px;\
			z-index: 9999;\
			color: #e7bd86;\
		}\
		\
		div.nice-popup.error {\
			background-color: #fffacd;\
			color: #d40c0c;\
		}\
		\
		* html div.nice-popup {\
			position: absolute;\
		}\
	");
})

var	np_prefix = "nice-popup-",
	np_border_size = 1,
	np_margin = 2,
	
	np_popups = [],
	stop = false,
	
	np_get_popup_height = function(index) {
		return $("#" + np_prefix + np_popups[index].id)[0].clientHeight;
	},
	
	np_search_by_id = function(id) {
		for (var i = 0; i < np_popups.length; i++) 
			if (np_popups[i].id == id) return i;
		
		return -1;
	},
	
	np_get_popup_heights = function(last_popup) {
		if (typeof last_popup == "undefined")
			var last_popup = np_popups.length ||  0
		
		for (var i = 0, heights = 0; i < last_popup; i++)
			heights += np_get_popup_height(i);
		
		return heights;
	},
	
	np_show_nice_popup = function(html) {
		if (np_popups.length != 0) {
			var popup_id = np_popups[np_popups.length - 1].id + 1,
				popup_position = np_get_popup_heights() + np_popups.length * (np_margin + np_border_size * 2) + np_margin;
		}
		else {
			var popup_id = 0,
				popup_position = np_margin;
		}
		
		$("body").append("<div class=\"nice-popup\" id=\"" + np_prefix + popup_id + "\" style=\"top: " + popup_position + "px;\">" + html + "</div>");
		$("#" + np_prefix + popup_id).click(function() { np_hide_nice_popup(popup_id) });
		
		np_popups.push({
			"id": popup_id,
			"timeout": setTimeout(function() { np_hide_nice_popup(popup_id) }, 4000)
		});
		
		return false;
	},
	
	np_hide_nice_popup = function(popup_id) {
		if (typeof popup_id == "undefined")
			popup_id = np_popups[np_popups.length - 1].id;
		
		var index = np_search_by_id(popup_id),
			popup = $("#" + np_prefix + popup_id),
			popup_height = popup[0].clientHeight;
		
		clearTimeout(np_popups[index].timeout);
		np_popups.splice(index, 1);
		popup.remove();
		
		np_move_up_popups(index, popup_height + np_margin + np_border_size * 2);
	},
	
	np_move_up_popups = function(index, shift) {
		for (var i = index; i < np_popups.length; i++) {
			var popup = $("#" + np_prefix + np_popups[i].id),
				new_popup_position = parseInt(popup.css("top")) - shift;
			
			popup.animate({ top: new_popup_position + "px" }, shift * 10);
		}
	},
	
	np_add_css = function(css) {
		$("head").append("<style type=\"text/css\">" + css + "</style>");
	}
