// $Id$

var buddyCards_lastRequest = new Array;
var buddyCards_loadedUsers = new Array;
var buddyCards_hoverDivs = new Array();
var buddyCards_missingData = new Array();

function buddyCards_updateMissingData()
{
	if (!buddyCards_fetchedScores)
		return; // internal failure - shouldn't happen
	for (var i=0; i<buddyCards_missingData.length; i++)
	{
		commentId = buddyCards_missingData[i];
		score = buddyCards_fetchedScores['id_'+commentId];
		if (score)
			buddyCards_getEl('buddyCardsUserScore_'+commentId).innerHTML = score;

		avatar = buddyCards_fetchedAvatars['id_'+commentId];
		if (avatar)
			buddyCards_getEl('buddyCardsAvatar_'+commentId).src = avatar;
	}
}

function buddyCards_getMissingUserData()
{
	missing = buddyCards_missingData.toString();
	if (!missing.length)
		return;
	buddyCards_MakeXmlHttpRequest('getMissingUserData', 'ids='+missing)
}

function buddyCards_showProfile(commentId)
{
	str = buddyCards_loadedUsers['u'+commentId];
	buddyCards_getEl('buddyCardsProfileContent').innerHTML = str;
}

function buddyCards_getProfile(commentId)
{
	buddyCards_lastRequest['commentId']=commentId;

	str = buddyCards_loadedUsers['u'+commentId];
	if (str && str.length)
	{
		buddyCards_showProfile(commentId);
	}
	else
	{
		buddyCards_MakeXmlHttpRequest('getProfile', 'comment='+commentId)
		buddyCards_getEl('buddyCardsProfileContent').innerHTML = '<div id="wait"><br /><br /><img src="'+gsBlogUrl+'/wp-content/plugins/buddyCards/wait.gif" /> &nbsp;Searching...<br /><br /></div>';		
	}
	
	elProfile = buddyCards_getEl('buddyCardsProfileWindow');
	elUser = buddyCards_getEl('buddyCardsUser_'+commentId);
	if (!elUser)
		return;
	pos = buddyCards_getElPos(elUser);
	pos.x += elUser.offsetWidth -8;
	buddyCards_setElPos(elProfile, pos);

	buddyCards_displayEl(elProfile, 1);
}

function buddyCards_MakeXmlHttpRequest(action, args)
{
	buddyCards_lastRequest['action']=action;
    if (window.XMLHttpRequest)
    	req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req = new ActiveXObject("Microsoft.XMLHTTP");
    if (!req)
	{
//		alert("Your browser is outdated (missing XMLHttpRequest). This feature will not work on your broswer.");
    	return;
    }

	url = gsBlogUrl+'/wp-content/plugins/buddyCards/buddyCardsRequestHandler.php?action='+action+'&'+args;
//document.location.href=url;
//return;
    req.onreadystatechange = buddyCards_ProcessReqChange;
    req.open("GET", url, true);
    req.send(null);
}

function buddyCards_ProcessReqChange()
{
	// only if req shows "loaded"
    if (req.readyState == 4)
    {
        if (req.status == 200) // only if "OK"
        {
        	str = req.responseText;
        	if (buddyCards_lastRequest['action']=='getProfile')
        	{
        		commentId = buddyCards_lastRequest['commentId'];
        		buddyCards_loadedUsers['u'+commentId] = str;
        		buddyCards_showProfile(commentId);
        	}
        	else if (buddyCards_lastRequest['action']== 'getMissingUserData')
        	{
        		eval(str);
        		buddyCards_updateMissingData();
        	}
        	else
        	{
        		alert(buddyCards_lastRequest['action'] + ' does not have a handler');
        	}
        }
//        else
//        	alert("There was a problem retrieving the data from buddyCards:" + req.statusText);
    }
}

function buddyCards_getEl(id)
{
	return document.getElementById(id);
}

function buddyCards_getElPos(el)
{
  var coords = {x: 0, y: 0}; 
  do { 
    coords.x += el.offsetLeft; 
    coords.y += el.offsetTop; 
  } 
  while ((el = el.offsetParent)); 
  return coords; 
}

function buddyCards_setElPos(el, coords)
{
	el.style.left = coords.x+"px";
	el.style.top = coords.y+"px";
}

function buddyCards_getWinWidth()
{
    if (window.innerWidth) return window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		return document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		return document.body.clientWidth;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientWidth) 
 		return document.body.parentNode.clientWidth;
}

function buddyCards_getWinHeight()
{
    if (window.innerHeight) return window.innerHeight;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		return document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		return document.body.clientHeight;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientHeight) 
  		return document.body.parentNode.clientHeight;
}

function buddyCards_getWinLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function buddyCards_getWinTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function buddyCards_getWinRight() {return buddyCards_getWinLeft()+buddyCards_getWinWidth();}
function buddyCards_getWinBottom() {return buddyCards_getWinTop()+buddyCards_getWinHeight();}

function buddyCards_displayEl(el, display)
{
	if (display)
		el.style.display = 'block';
	else
		el.style.display = 'none';
}

buddyCards_hoverDiv = function(buttonId, popupId, align, extraOnShowCode, extraOnHideCode)
{
	this.timer = 0;
	this.buttonId = buttonId;
	this.popupId=popupId;
	this.align=align;
	this.extraOnShowCode=extraOnShowCode;
	this.extraOnHideCode=extraOnHideCode;
}

function buddyCards_createHover(buttonId, popupId, align, extraOnShowCode, extraOnHideCode)
{
	i=buddyCards_hoverDivs.length;
	buddyCards_hoverDivs[i] = new buddyCards_hoverDiv(buttonId, popupId, align, extraOnShowCode, extraOnHideCode);
	
	elButton = document.getElementById(buttonId);
	elButton.onmouseover = new Function("buddyCards_showHoverDiv(arguments[0],'"+i+"');");
	elButton.onmouseout = new Function("buddyCards_hideHoverDiv(arguments[0],'"+i+"');");

	elPopup = document.getElementById(popupId);
	elPopup.onmouseover = new Function("buddyCards_showHoverDiv(arguments[0],'"+i+"');");
	elPopup.onmouseout = new Function("buddyCards_hideHoverDiv(arguments[0],'"+i+"');");
}

function buddyCards_hideHoverDiv(evt, i)
{
	hover = buddyCards_hoverDivs[i];
	evt = (evt) ? evt : event;
	elPopup = document.getElementById(hover.popupId);

	// don't hide if we're on a child window
	var elTarget = evt.relatedTarget ? evt.relatedTarget : evt.toElement;
	if (!elTarget)
		return;
	while ( elTarget = elTarget.parentNode )
		if ( elTarget == elPopup )
			return;

	hover.timer = setTimeout("buddyCards_onHideHoverTimer("+i+");",100);
}

function buddyCards_onHideHoverTimer(i)
{
	hover = buddyCards_hoverDivs[i];
	elPopup = document.getElementById(hover.popupId);
	elPopup.style.visibility = 'hidden';
	eval(hover.extraOnHideCode);
}

function buddyCards_showHoverDiv(evt, i)
{
	hover = buddyCards_hoverDivs[i];
	if (hover.timer)
	{
		clearTimeout(hover.timer);
		hover.timer=0;
	}
	evt ? evt : window.event;

	elPopup = document.getElementById(hover.popupId);
	if (elPopup.style.visibility == 'visible')
		return;
	
	eval(hover.extraOnShowCode);
	
	elButton = document.getElementById(hover.buttonId);
	coords = buddyCards_getElPos(elButton);
	coords.y += elButton.offsetHeight - 14;
	coords.x += elButton.offsetWidth - 20;
	if (hover.align == 'rightEdges')
	{
		coords.x += elButton.offsetWidth - elPopup.offsetWidth;
	}
	else // bottomLeft
	{
	}
	
	buddyCards_setElPos(elPopup, coords);
	elPopup.style.visibility = 'visible';
}

function buddyCards_toggleDisplayOff(id)
{
	el = document.getElementById(id);
	el.style.display = 'none';
}

function buddyCards_showWindowShade(id, displayMode) // block or none
{
	el = document.getElementById(id);

	if (el.style.display == displayMode)
		return;
	
	if (displayMode == 'block')
	{
		var biggestHeight = document.body.scrollHeight;
		if (buddyCards_getWinHeight() > biggestHeight)
			biggestHeight = buddyCards_getWinHeight();

		el.style.width=document.body.scrollWidth+'px';
		el.style.height=biggestHeight+'px';
	}
	
	el.style.display = displayMode;
}


function buddyCards_displayBox(id)
{
	el = document.getElementById(id);

	el.style.display = 'inline';
	el.style.top = document.body.scrollTop + 80 + 'px';
	el.style.left= (buddyCards_getWinWidth()/2 - el.offsetWidth/2) +'px';
	el.style.visibility = 'visible';
}

function buddyCards_getWinWidth()
{
    if (window.innerWidth) return window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		return document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		return document.body.clientWidth;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientWidth) 
 		return document.body.parentNode.clientWidth;
}

function buddyCards_getWinHeight()
{
    if (window.innerHeight) return window.innerHeight;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		return document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		return document.body.clientHeight;
  	else if (document.body && document.body.parentNode && document.body.parentNode.clientHeight) 
  		return document.body.parentNode.clientHeight;
}