var element = 0;
var hidding = true;
var requestingFriend = false;
var tempX = 0;
var tempY = 0;

$(document).ready(function() {
	createElement();
	
	var myBody = document.body;
	var myDiv = document.createElement('div');
	myDiv.id = "contextualUserMenuLoadingDiv";
	myDiv.style.position = "absolute";
	myDiv.style.display = "none";
	myDiv.style.zIndex = "1";
	myDiv.innerHTML = "<img src='/images/ajax-loading.gif' width='20' height='20'>";
	myBody.appendChild(myDiv);
	
	// Background DIV
	myDiv = document.createElement("div");
	myDiv.id = "contextualMenuBackgroundTransparent";
	myDiv.innerHTML = "&nbsp;";
	myDiv.style.position = "absolute";
	myDiv.style.zIndex = "98";
	myDiv.style.backgroundImage = "url('/images/dot.gif')";
	myDiv.style.filter = "alpha(opacity=0)";
	myDiv.style.opacity = "0";
	myDiv.style.left = "0px";
	myDiv.style.top = "0px";
	myDiv.style.width = "100%";
	myDiv.style.display = "none";
	myBody.appendChild(myDiv);
	document.getElementById("contextualMenuBackgroundTransparent").style.height = document.documentElement.scrollHeight + "px";
	jQuery('#contextualMenuBackgroundTransparent').click( function() {closeUserMenu();});
});

function createElement() {
	var myBody = document.body;
	var myDiv = document.createElement('div');
	myDiv.id = "contextualUserMenu";
	myDiv.style.position = "absolute";
	myDiv.style.display = "none";
	myDiv.style.zIndex = "99";
	myBody.appendChild(myDiv);
	element++;
}

function removeElement(elemento) {
	var myBody = document.body;
	var myDiv = document.getElementById(elemento);
	myBody.removeChild(myDiv);
}

function hideUserMenu() {
	if (document.getElementById("ContextualMenuAddAsFriendLink") == null || !document.getElementById("ContextualMenuAddAsFriendLink").hasFocus) {
		
	}
}

function closeUserMenu() {
	hidding = true;
	elemento = element - 1;
	$("#contextualUserMenu" + elemento).fadeOut();
	document.getElementById("contextualMenuBackgroundTransparent").style.display = "none";
	setTimeout("removeElement('contextualUserMenu' + (elemento))", 300);
}

function showUserMenu(evento) {
	if (hidding) {
		hidding = false;
	} else {
		closeUserMenu();
	}
	moveContent(document.getElementById("contextualUserMenu"), 10);
	var myDiv = document.getElementById("contextualUserMenu");
	myDiv.id = "contextualUserMenu" + element;
	$("#contextualUserMenu" + element).fadeIn();
	createElement();
	document.getElementById("contextualMenuBackgroundTransparent").style.display = "block";
}

function showLoadingGif(e) {
	assignPosition(e, document.getElementById("contextualUserMenuLoadingDiv"), 15);
	document.getElementById("contextualUserMenuLoadingDiv").style.display = "block";
}

function hideLoadingGif() {
	document.getElementById("contextualUserMenuLoadingDiv").style.display = "none";
}

function assignPosition(e, capa, margin) {
	//La variable IE determina si estamos utilizando IE
	var IE = document.all?true:false;
	if (IE) {
		e = jQuery.event.fix(e);
	}
	
	tempX = e.pageX;
	tempY = e.pageY;
	
	moveContent(capa, margin);
}

function moveContent(capa, margin) {
	if (tempX < 0) {tempX = 0;}
	if (tempY < 0) {tempY = 0;}
	
	if (capa.id == "contextualUserMenu") {
		if ((tempX + 410 + margin) > $(window).width()) {
			tempX = tempX - 410 - margin;
		}
		
		if ((tempY + 140 + margin) > ($(window).height() + $(window).scrollTop())) {
			tempY = tempY - 140 - margin;
		}
	}
	
	capa.style.top = (tempY + margin) + "px";
	capa.style.left = (tempX + margin) + "px";
}

function removeHTMLTags(htmlString){
	if(htmlString){
		htmlString = htmlString.replace("<br>", "")
		var mydiv = document.createElement("div");
		mydiv.innerHTML = htmlString;
		if (document.all) {
			return mydiv.innerText;
		} else {
			return mydiv.textContent;
		}
	}
}
