// the following should be moved to it's own js file...

var bUC = false;
var bUCActive = false;
var oUCClosing = null;
var oUCStarting = null;
var oUCDiv = null;
var bNoFollowBtn = false;

function UCard_Init(obj) {
	
	if (oUCClosing) { 
		bUC = false;
		clearTimeout(oUCClosing);
		oUCClosing = null;
		UCard_Cleanup();
	}

	if (oUCStarting) { 
		bUC = false;
		clearTimeout(oUCStarting);
		oUCStarting = null;
	}


	if (bUC) {
		UCard_Cleanup();
	}
	
	
	bUC = true;

	oUCStarting = setTimeout(function() {
		UCard_Open(obj);
		}, 750);

}


function UCard_Open(obj) {
	
	if (bUC) {
		
		if (!!oUCDiv) {
			UCard_Cleanup();
		}
		
		var iUid = obj.readAttribute('data-uid');
		
		// setup the div
		oUCDiv = document.createElement('div');
		oUCDiv.id = 'dvUCard';
		
		oUCDiv.innerHTML = '<div class="Contents" style="color: #777; height: 170px; font-size: 13px;">Loading user\'s info...</div>';
		oUCDiv.style.left = ((obj.readAttribute('data-ucard-offset-x') > 0) ? obj.readAttribute('data-ucard-offset-x') : '50') + 'px';
		if (!!obj.readAttribute('data-ucard-offset-y')) {
			oUCDiv.style.top = obj.readAttribute('data-ucard-offset-y') + 'px';
		}
		
		oUCDiv.style.opacity = '0.00';
		//oUCDiv.style.height = '180px';
		
		bNoFollowBtn = (obj.readAttribute('data-ucard-no-follow') > 0);
		
		obj.parentNode.insertBefore(oUCDiv, obj);
	
		Event.observe(oUCDiv, 'mouseover', function(event) { bUCActive=true; });
		Event.observe(oUCDiv, 'mouseout', function(event) { bUCActive=false; UCard_Close(); });
		

		//alert('here');
		
		if (!is_ie) {
			// no soup for ie, too choppy
			Effect.Fade(oUCDiv, { duration: 0.5, from: 0, to: 1, afterFinish: function() { UCard_AjaxIn(iUid) }  } );
		} else {
			UCard_AjaxIn(iUid);
		}
		
		
	}
}






function UCard_AjaxIn(iUid) {
	// AJAX IN the content		
	new Ajax.Request('/clog-ajax.php',
	  {
	    method:'get',
	    parameters: {ajaxaction: 'ajax-ucard', uid: iUid }, 
	    onSuccess: function(transport) {
		    UCard_ParseReturn(iUid, transport.responseText.evalJSON());
		    if (!is_ie) {
			    // no soup for ie, too choppy
			    //Effect.Fade(oUCDiv, { duration: 0.7, from: 0, to: 1 } );
		    }
	    },
	    onFailure: function(){ alert('Something went wrong...') }
	  });

}


function UCard_ParseReturn(iUid, obj) {
	
	var sHTML = '<div class="Contents">';

	if (!!obj.avatar) {
		sHTML += '<div style="float: right; width: 128px; text-align: right; margin-left: 4px;"><img src="' + obj.avatar + '" class="PPic" alt="" border="0"' 
			+ (obj.is_site_admin == 'true' ? ' ondblclick="DMFormOpen(0, ' + iUid + ', \'' + obj.display_name.replace("'", "\\\'") + '\', \'Direct Message from ' + obj.my_display_name.replace("'", "\\\'") + '\'); return DMFauxTb();"' : '')  + ' />';
		if (!!obj.display_name) {
			if (obj.current_user_id == 0) {
				// not logged in
			} else if (obj.current_user_id != iUid && !bNoFollowBtn && obj.following_blocked == 'false') {
				sHTML += '<div style="margin-bottom:-6px;">';
				sHTML += FriendsFollowBtn(iUid, obj.current_user_id, obj.friends_is_following, obj.display_name, false, false, false, obj.follows_require_approval);
				sHTML += '</div>';
			}
			if (obj.current_user_id != 0 && obj.current_user_id != iUid) {
				sHTML += '<div style="text-align:center;width:126px;font-size:11px;margin-bottom:6px;">';
				if (obj.friends_is_following_me == 1) {
					sHTML += '<a href="/#TB_inline?height=224&width=360&inlineId=dvDMForm" class="thickbox" onclick="DMFormOpen(0, ' + iUid + ', \'' + obj.display_name.replace("'", "\\\'") + '\', \'Direct Message from ' + obj.my_display_name.replace("'", "\\\'") + '\'); return DMFauxTb();" style="text-decoration:none;font-size:11px;' + (obj.friends_is_following == 0 ? 'margin-right:20px;' : '') + '">Message</a>';
				}
				if (obj.friends_is_following == 0) {
					sHTML += '<a href="#" onclick="return ShowBlockLinks();" style="text-decoration:none;font-size:11px;">' + (obj.block_commenting == 'true' || obj.block_follows == 'true' ? 'Blocked' : 'Block') + '</a>';
				}
				sHTML += '</div>';
			}
		}
		sHTML += '</div>';
	}

	
	if (!!obj.display_name) {
		sHTML += '<div class="Name"><a href="' + obj.user_url + '" style="text-decoration: none;">' + obj.display_name + '</a></div>';
	}
	
	if (!!obj.location) {
		sHTML += '<div class="F"><span>Location:</span> ' + obj.location + '</div>';
	} else {
		sHTML += '<div class="F"><span>Location:</span> <span class="Empty">Unknown</span></div>';
	}
	
	
	if (!!obj.twitter_username) {
		sHTML += '<div class="F"><span>Twitter:</span> <a href="http://twitter.com/' + obj.twitter_username + '" target="_blank">@' + obj.twitter_username + '</a></div>';
	}
	
	if (!!obj.web_url) {
		var web_url_pr = obj.web_url;
		sHTML += '<div class="F"><span>Web:</span> <a href="' + obj.web_url + '" target="_blank">' + (web_url_pr.length > 21 ? web_url_pr.substr(0, 19) + '...' : web_url_pr)  + '</a></div>';
	}
	
	
	if (!!obj.bio) {
		sHTML += '<div class="F"><span>Bio:</span> ' + obj.bio + '</div>';
	} else {
		sHTML += '<div class="F"><span>Bio:</span> <span class="Empty">None provided</span></div>';
	}
	
	
	if (!!obj.total_followers && !!obj.total_sources) {
		sHTML += '<div class="Tots" style="position: relative; height: 30px;width:148px;">';
		
		sHTML += '<span id="spnFriendsFollowers-' + iUid + '" style="position: absolute; font-size: 11px; text-align: center; display: block; width: 48px;">';
		if (obj.total_followers > 0) {
			sHTML += '<span id="spnFollowersTot-' + iUid + '" class="Tot">' + obj.total_followers + '</span><div><a id="aFollowersNoun-' + iUid + '" href="http://amplify.com/' + obj.user_login + '/followers/" style="font-size: 10px;">Follower' + ((obj.total_followers == 1) ? '' : 's') + '</a></div>';
		} else {
			sHTML += '<span id="spnFollowersTot-' + iUid + '" class="Tot"></span><div><a id="aFollowersNoun-' + iUid + '" href="http://amplify.com/' + obj.user_login + '/sources/" style="font-size: 10px;"></a></div>';
		}
		sHTML += '</span>';
		
		
		sHTML += '<span id="spnFriendsSources-' + iUid + '" style="position: absolute; font-size: 11px; text-align: center; display: block; width: 50px; left: 53px;">';
		if (obj.total_sources > 0) {
			sHTML += '<span id="spnSourcesTot-' + iUid + '" class="Tot">' + obj.total_sources + '</span><div><a id="aSourcesNoun-' + iUid + '" href="http://amplify.com/' + obj.user_login + '/sources/" style="font-size: 10px;">Source' + ((obj.total_sources == 1) ? '' : 's') + '</a></div>';
		} else {
			sHTML += '<span id="spnSourcesTot-' + iUid + '" class="Tot"></span><div><a id="aSourcesNoun-' + iUid + '" href="http://amplify.com/' + obj.user_login + '/sources/" style="font-size: 10px;"></a></div>';
		}
		sHTML += '</span>';

		
		sHTML += '<span id="spnFriendsPosts-' + iUid + '" style="position: absolute; font-size: 11px; text-align: center; display: block; width: 50px; left: 98px;">';
		if (obj.total_posts > 0) {
			sHTML += '<span id="spnPostsTot-' + iUid + '" class="Tot">' + obj.total_posts + '</span><div><a id="aPostsNoun-' + iUid + '" href="' + obj.user_url + '" style="font-size: 10px;">Post' + ((obj.total_posts == 1) ? '' : 's') + '</a></div>';
		} else {
			sHTML += '<span id="spnPostsTot-' + iUid + '" class="Tot"></span><div><a id="aPostsNoun-' + iUid + '" href="' + obj.user_url + '" style="font-size: 10px;"></a></div>';
		}
		sHTML += '</span>';

		sHTML += '</div>';
	}

	if (obj.current_user_id != 0) {
		sHTML += '<div id="dvBlockLinks" style="display:none;margin-top:15px;clear:both;"><div style="text-align: center; font-size: 11px; margin-bottom: 6px;" align="center"><center>';
		sHTML += '<a href="#" onclick="return UCard_block_commenting(this, ' + iUid + ', false);" style="' + (obj.block_commenting == 'true' ? '' : 'display:none;') + 'text-decoration: none; font-size: 11px;">Allow to comment on my posts</a>';
		sHTML += '<a href="#" onclick="return UCard_block_commenting(this, ' + iUid + ', true);" style="' + (obj.block_commenting == 'false' ? '' : 'display:none;') + 'text-decoration: none; font-size: 11px;">Block from commenting on my posts</a>';
		sHTML += '</center></div>';
		sHTML += '<div style="text-align: center; font-size: 11px; margin-bottom: 6px;" align="center"><center>';
		sHTML += '<a href="#" onclick="return UCard_block_follows(this, ' + iUid + ', false);" style="' + (obj.block_follows == 'true' ? '' : 'display:none;') + 'text-decoration: none; font-size: 11px;">Allow to follow me</a>';
		sHTML += '<a href="#" onclick="return UCard_block_follows(this, ' + iUid + ', true);" style="' + (obj.block_follows == 'false' ? '' : 'display:none;') + 'text-decoration: none; font-size: 11px;">Block from following me</a>';
		sHTML += '</center></div></div>';
	}

	
	if (!!obj.latest_clips && obj.latest_clips != 'false') {
		sHTML += '<div class="LatestClips"><div class="LatestClipsHdr">Check out ' + (!!obj.display_name ? obj.display_name : '') + '\'s Latest Amps...  </div>' + obj.latest_clips + '</div>';
		sHTML += '<div class="SeeClog"><a href="' + obj.user_url + '">see all amps &raquo;</a></div>';
	} else {
		sHTML += '<div style="clear:both;margin-top:8px;></div>';
	}
	
	sHTML += '</div>';
	
	oUCDiv.style.height = 'auto';
	
	oUCDiv.innerHTML = sHTML;

}

function ShowBlockLinks() {
	if ($('dvBlockLinks').style.display == 'none') {
		Effect.SlideDown('dvBlockLinks', { duration: 0.3 } );
	} else {
		Effect.SlideUp('dvBlockLinks', { duration: 0.3 } );
	}
	return false;
}

function UCard_block_commenting(aLink, iUid, bBlock) {
	new Ajax.Request('/clog-ajax.php',
	  {
	    method:'get',
	    parameters: {ajaxaction: 'ajax-block-commenting', uid: iUid, block: (bBlock ? 1 : 0) }, 
	    onSuccess: function(transport) {
		    //alert(transport.responseText);
		    aLink.style.display = 'none';
		    if (bBlock) {
			    aLink.previousSibling.style.display = 'block';
		    } else {
			    aLink.nextSibling.style.display = 'block';
		    }
	    },
	    onFailure: function(){ alert('Something went wrong...') }
	  });

	return false
}

function UCard_block_follows(aLink, iUid, bBlock) {
	new Ajax.Request('/clog-ajax.php',
	  {
	    method:'get',
	    parameters: {ajaxaction: 'ajax-block-follows', uid: iUid, block: (bBlock ? 1 : 0) }, 
	    onSuccess: function(transport) {
		    //alert(transport.responseText);
		    aLink.style.display = 'none';
		    if (bBlock) {
			    aLink.previousSibling.style.display = 'block';
		    } else {
			    aLink.nextSibling.style.display = 'block';
		    }
	    },
	    onFailure: function(){ alert('Something went wrong...') }
	  });

	return false
}

function UCard_Close() {
	//alert('wanna close it');
	bUC = false;
	if (!!oUCDiv) {
		oUCClosing = setTimeout('UCard_CloseDone()', 500);
	}
}


function UCard_CloseDone() {
	if (!bUCActive && !!oUCDiv) {
		//alert('made it here, i can make it anywhere');		
		Effect.Fade(oUCDiv, { duration: 0.3, from: 1, to: 0, afterFinish: UCard_Cleanup } );
		if (oUCClosing) { 
			clearTimeout(oUCClosing);
			oUCClosing = null;
		}
	}
}


function UCard_Cleanup() {
	if (!!oUCDiv) {
		oUCDiv.parentNode.removeChild(oUCDiv);
		oUCDiv = null;
	}
	
	clearTimeout(oUCStarting);
	oUCStarting = null;
}


function UCard_Unload() {
	UCard_Cleanup();
	return true;
}



function UCard_ReParse(sDiv) {
			    
    $$('#' + sDiv + ' a[data-uid]').each(function(link) {
	Event.observe(link, 'mouseover', function(event) { UCard_Init(link); });
	Event.observe(link, 'mouseout',  function(event) { UCard_Close(); });
	Event.observe(link, 'click',  function(event) { UCard_Unload(); });
    });
	
}


