var bTimer = null;
var onBubble = null;

function showBubble(obj)
{
	var bubble = document.getElementById('gnuBubble');
	var props = obj.rel.split('-');
	var section = props[0];
	var objId = props[1];
	var imageName = 0;
	var justDelete = false;
	if(props.length > 2) {
		imageName = props[2];
		if(imageName == "justDelete") {
			justDelete = true;
		}
	}
	var bubbleContent = document.getElementById('bubbleContent');
	bubbleContent.innerHTML = createBubbleContent(section, objId, imageName, justDelete);
	
	bubble.style.visibility = 'visible';
	setLyr(obj,'gnuBubble');
}

function createBubbleContent(section, objId, imageName, justDelete) {
	var lc_section = section.toLowerCase();
	var maybeName = objId;
	if(imageName != 0) {
		maybeName = imageName;
	}
	var bubbleContent = '<ul class="noListStyle">';
	if(!justDelete) {
		bubbleContent += '<li><a href="/painter.html?'+lc_section+'='+maybeName+'">Paint with this '+lc_section+'</a></li>';
	}
	bubbleContent += '<li><a onclick="deleteFromNoteBook(this);" class="deleteAjaxLink" href="javascript: void(0);" ajax="/Ajax/DeleteUser'+section+'/'+objId+'">Delete from Idea Notebook</a>';
	bubbleContent += '</ul>';

	return bubbleContent;
}

function deleteFromNoteBook(link) {
	var ajaxURL = link.getAttribute("ajax");
	sendGetReq(ajaxURL, deleteFromNoteBookResponse);
}
function deleteFromNoteBookResponse(response) {
	window.location = window.location;
}
function setLyr(obj,lyr)
{
	var coors = findPos(obj);
	coors[1] -= 50;
	coors[0] += 20;
	var x = document.getElementById(lyr);
	x.style.top = coors[1] + 'px';
	x.style.left = coors[0] + 'px';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			if (obj.className != 'pageContainer') {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
	}
	return [curleft,curtop];
}

function hideBubble() { 
	var bubble = document.getElementById('gnuBubble');
	bubble.style.visibility = 'hidden';
}

function bubbleInit() {
	var bubble = document.getElementById('gnuBubble');
	if (!bubble) return;
	bubble.style.visibility = 'hidden';

	bubble.onmouseover = function() {
		onBubble = true;
		clearTimeout(bTimer);
		return false;
	}
	bubble.onmouseout = function() {
		onBubble = false;
		bTimer = setTimeout('hideBubble()', 1000);
		return false;
	}
	var arrLinks = document.getElementsByTagName('a');
	if (arrLinks.length <= 0) return;
	for (var i = 0; i < arrLinks.length; i++) {
		if (arrLinks[i].className.indexOf('bubbles') > -1) {
			arrLinks[i].onmouseover = function() {
				clearTimeout(bTimer);
				showBubble(this);
				return false;
			}
			arrLinks[i].onmouseout = function() {
				if (!onBubble) {
					bTimer = setTimeout('hideBubble()', 1000);
				}
				return false;
			}
		}
	}
}

// initialize
windowObject.addLoadFunction(bubbleInit);