var ua = navigator.userAgent.toLowerCase();
var isOpera = ua.indexOf("opera") > -1;
var isSafari = (/webkit|khtml/).test(ua);
var isIE = !isOpera && ua.indexOf("msie") > -1;
var isIE7 = !isOpera && ua.indexOf("msie 7") > -1;
var isGecko = !isSafari && ua.indexOf("gecko") > -1;
var offsetFromX = 12;
var offsetFromY = 10;


function displayQTip(el, e) {
	var msg = el.getAttribute('message');
	var dlg = document.getElementById("qtip_container");
	var messBox = document.getElementById("help_qtip_message");

	if (!dlg || !msg.length || !messBox) {
		return;
	}

	messBox.innerHTML = msg;

	var b = document.body;

	var mouseX = getMouseX(e);
	var mouseY = getMouseY(e);
	var dlgSize = getSize(dlg);

	dlg.style.left = getXPosition(mouseX, dlgSize.width);
	dlg.style.top = getYPosition(mouseY, dlgSize.height);

	dlg.style.visibility = 'visible';
}

function hideQTip(el) {
	var messBox = document.getElementById("help_qtip_message");
	var dlg = document.getElementById("qtip_container");
	if (!dlg || !messBox) {
		return;
	}
	messBox.innerHTML = '';
	dlg.style.visibility = 'hidden';
}


function displayRecipeDetails(divId, e) {
	var dlg = document.getElementById(divId);
	if (!dlg) {
		return;
	}

	var mouseX = getMouseX(e);
	var mouseY = getMouseY(e);
	var dlgSize = getSize(dlg);

	if (navigator.appName == 'Microsoft Internet Explorer'){
		dlg.style.left = 0;
		//dlg.style.top = mouseX;
	}else{
		dlg.style.left = 200;
		//dlg.style.top = mouseX;
	}
	dlg.style.visibility = 'visible';
}

function hideRecipeDetails(divId, e) {
	var dlg = document.getElementById(divId);

	if (!dlg) {
		return;
	}

	dlg.style.visibility = 'hidden';
}

function getSize(el) {
	var size = {
		width: el.clientWidth,
		height: el.clientHeight
	};

	return size;
}

function getXPosition(mX, width) {
	var x = 10;
	if ( document.body.clientWidth < (mX + width + offsetFromX) ) {
		x = document.body.clientWidth - width;
	} else {
		x = mX + offsetFromX;
	}

	return x;
}

function getYPosition(mY, height){
	var y = 10;
	if (document.body.clientHeight < (mY + height + offsetFromY)) {
		y = mY + document.body.scrollTop - height - offsetFromY;
	} else {
		y = mY + offsetFromY + document.body.scrollTop;
	}

	return y;
}

function getMouseX(e) {
	return e.clientX;
}

function getMouseY(e) {
	return e.clientY;
}

function displayConfirm(e, div, msgEl, form, action, id) {
	div = document.getElementById(div);
	msgEl = document.getElementById(msgEl);
	form = document.getElementById(form);

	if (!div || !msgEl || !form || !action || !id) {
		return;
	}

	switch (action) {
		case 'accept':
			msgEl.innerHTML = 'You wish to accept the invitation about friendship?';
			break;
		case 'decline':
			msgEl.innerHTML = 'You wish to decline the invitation about friendship?';
			break;
		case 'delete':
			msgEl.innerHTML = 'Are you sure to delete friend from your circle?';
			break;
		case 'del_invite':
			msgEl.innerHTML = 'Are you sure to delete the invitation to friendship?';
			break;
	}
	form.cmd.value = action;
	form.fid.value = id;
	
	var mouseX = getMouseX(e) - 250;
	var mouseY = getMouseY(e);
	var dlgSize = getSize(div);
	div.style.width = "400";

	if (navigator.appName == 'Microsoft Internet Explorer'){
		div.style.left = 0;
		//div.style.top = mouseX;
	}else{
		div.style.left = 200;
		//div.style.top = mouseX;
	}

//	div.style.left = getXPosition(mouseX, dlgSize.width);
//	div.style.top = getYPosition(mouseY, dlgSize.height);
	div.style.top = getYPosition(mouseY, 400);

	div.style.display = '';
}

function hideConfirm(div) {
	div = document.getElementById(div);

	if (!div) {
		return;
	} else {
		div.style.display = 'none';
	}
}