var iCCWidth = 310;
var iCCHeight = 228;

var bCC = false;
var bCCActive = false;
var sContentClass;
var oCCClosing = null;
var oCCStarting = null;

var bCCUnload = false;

var bMacDhtml = false;

function OpenCC(sUser, bRight, bFromDhtml) {
	
	// uses mousex and mousey set in layer_mover.js
	
	if (bCC) {
		CloseCCDone();
	}
	
	bCC = true;

	if (!bRight) { bRight = false; }	
	if (bFromDhtml) { bMacDhtml = true; } else { bMacDhtml = false; }
	
	oCCStarting = setTimeout('StartCC(\'' + sUser + '\', ' + bRight + ')', 700);
}


function StartCC(sUser, bRight) {
	var oCC = document.getElementById('dvCCPanel');
	
	var iLeft = mouseX - iCCWidth;
	var iTop = mouseY - iCCHeight;
	if (iTop < 4) { iTop = 4; } 
	oCC.style.background='url(/images/cc/bg-left.png)';
	sContentClass = 'ContentL';

	if (bRight) { 
		iLeft = mouseX + 30;
		oCC.style.background='url(/images/cc/bg-right.png)';
		sContentClass = 'ContentR';
	} 
		
	if (is_ie && is_major < 7) {
		oCC.style.background='url(/images/cc/spacer.gif)';
		sContentClass += 'IE';
	}	
	
	if (bCC && !bCCUnload) {
		if (!bMacDhtml && !is_ie) {
			HideScrollbars();
		}
		
		oCC.style.top = iTop + 'px';
		oCC.style.left = iLeft + 'px';
		oCC.style.display = 'block';
		oCC.innerHTML = '<div class="' + sContentClass + '"><div class="Loading" align="center"><img src="/images/blue-load.gif" style="margin: 50px 10px 10px 10px;" /><br/>Loading clipper: <b>' + sUser + '</b>...</div></div>';
	
		cc_open(sUser);
	} else {

		if (!bMacDhtml && !is_ie) {
			ShowScrollbars();
		}
		
		oCC.style.display = 'none';
	}
}

function cc_open(sUser) {
	bDialogLoading = true;
	
	xmlDialogHTTP = CreateXMLHTTP();
	
	if (!xmlDialogHTTP) {
		alert('Browser Not Supported');
		return;
	}

	xmlDialogHTTP.open('GET', '/feed_card.aspx?u=' + sUser, true);
	xmlDialogHTTP.onreadystatechange = cc_XMLStateChange;
	xmlDialogHTTP.send(null);
}

function cc_XMLStateChange() {
	if (xmlDialogHTTP.readyState != 4) {
		return;
	}

	if (!xmlDialogHTTP.responseXML) { 
		return; 
	}	
	
	if (xmlDialogHTTP.responseXML && xmlDialogHTTP.responseXML.documentElement) {
		xmlDialog = xmlDialogHTTP.responseXML.documentElement;
		cc_load();
		bDialogLoading = false;
	} else {
	}
	
}

function cc_load() {
	var oCC = document.getElementById('dvCCPanel');
	var oChannel = xmlDialog;
	var oClipper;
	var sHTML = '';

	bKeepDialogOpen = true;

	var sLocation = '<i>unknown</i>';
	var sUser = oChannel.getAttribute("username");
	var sFollowUrl = 'PostFollowUser(\'' + sUser + '\', true, \'' +  oChannel.getAttribute("is-followed") + '\');';
	var sFollowImg = 'add-clipper-right.gif';
	var sFollowLabel = 'Add to';

	if (oChannel.getAttribute("is-followed") == '1') {
		sFollowUrl = 'ShowDialog(\'<br/>You are already following <b>' + sUser + '</b>.\')';
		sFollowImg = 'in-clippers-right.gif';
		sFollowLabel = 'In';
	} else if (!bAuth) {
		sFollowUrl = 'CloseCC();LoginPrompt(\'follow\', ExchangeUrl())';
		sFollowLabel = 'Login to add to';
	}

	if (oChannel.getAttribute("location") != '') { sLocation = oChannel.getAttribute("location"); }	
	
	var sImg = '/images/user-no-photo-small-trans.gif';

	if (oChannel.getAttribute('has-photo') == 1) {
		sImg = '/profile/small/' + oChannel.getAttribute('guid') + '.png';
	}

	sHTML += '<div class="CCWrap">';
	sHTML += '<div class="CCHeader"><table cellpadding="0" cellpadding="0" width="98%"><tr><td width="40" height="32" align="right"><a href="/clipper/' + sUser.replace(/ /mig, '+') + '/"><img src="' + sImg + '" alt="" border="0" /></a></td>';
	sHTML += '<td class="CCCardHeaderUser"><a href="/clipper/' + sUser.replace(/ /mig, '+') + '/" style="font-size: 18px;" class="CLB">' + sUser + '</a>';
	if  (oChannel.getAttribute('auth-allow-watch') == 1) {
		sHTML += '<br /><a href="javascript:user_nipsa(\'' + oChannel.getAttribute('guid') + '\');" style="font-size:9px;" class="CLB">nipsa</a> <small>-</small> <a href="javascript:user_ban(\'' + oChannel.getAttribute('guid') + '\');" style="font-size:9px;" class="CLB">ban</a> <small>-</small> <a href="/admin/user.aspx?guid=' + oChannel.getAttribute('guid') + '" style="font-size:9px;" class="CLB" target="_blank">admin</a>';
	}
	sHTML += '</td></tr></table></div>';
	sHTML += '<table cellpadding="0" cellpadding="0" width="100%" class="CCInfo">';
	sHTML += '<tr><td class="Lab">Real Name:</td><td class="Dat">' + (oChannel.getAttribute('name-full') == '' ? '<span class="NA">n/a</span>' : oChannel.getAttribute('name-full')) + '</td></tr>';
	sHTML += '<tr><td class="Lab">Location:</td><td class="Dat">' + sLocation + '</td></tr>';
	sHTML += '<tr><td class="Lab"><nobr>Last action:</nobr></td><td class="Dat"><nobr>';
	
	if (oChannel.getAttribute("action-type") == '1' || oChannel.getAttribute("action-type") == '2' || oChannel.getAttribute("action-type") == '3') { 
		if (oChannel.getAttribute("action-type") == '1') {
			sHTML += '<img src="/images/header/ic_clip.gif" border="0" alt="clip" width="18" height="15" style="margin: 0px 3px -6px 0px" />';
			sHTML += '<a href="/clipper/' + sUser.replace(/ /mig, '+') + '/clip/' + oChannel.getAttribute("action-guid") + '/" title="Clipped ' + oChannel.getAttribute("action-date") + '" class="CLB">' + oChannel.getAttribute("action-title") + '</a>';
		} else if (oChannel.getAttribute("action-type") == '2') {
			sHTML += '<img src="/images/header/ic_pop.gif" border="0" alt="pop" width="18" height="15"  style="margin: 0px 3px -6px 0px" />';
			sHTML += '<a href="/clipper/' + sUser.replace(/ /mig, '+') + '/pops/clip/' + oChannel.getAttribute("action-guid") + '/" title="Popped ' + oChannel.getAttribute("action-date") + '" class="CLB">' + oChannel.getAttribute("action-title") + '</a>';
		} else if (oChannel.getAttribute("action-type") == '3') {
			sHTML += '<img src="/images/header/ic_com.gif" border="0" alt="comment" width="18" height="15" style="margin: 0px 3px -6px 0px" />';
			sHTML += '<a href="/clipper/' + sUser.replace(/ /mig, '+') + '/comments/clip/' + oChannel.getAttribute("action-guid") + '/" title="Commented ' + oChannel.getAttribute("action-date") + '" class="CLB">' + oChannel.getAttribute("action-title") + '</a>';
		}
		
	} else {
		sHTML += 'N/A'; 
	}
	sHTML += '</nobr></td></tr>';
	sHTML += '</table>';
	sHTML += '<table cellpadding="0" cellpadding="2" width="100%"><tr><td width="50%" align="center" style="border-right: dotted 1px #dcdcdc;" valign="top">';
	sHTML += '<table cellpadding="0" cellpadding="0" width="100%" class="CCStats">';	
	sHTML += '<tr><td width="20"><img src="/images/header/ic_clip.gif" border="0" /></td><td><a href="/clipper/' + sUser.replace(/ /mig, '+') + '/"><b>' + oChannel.getAttribute("clip-count-total")  + '</b> clips created</td></tr>';
	sHTML += '<tr><td width="20"><img src="/images/header/ic_pop.gif" border="0" /></td><td><a href="/clipper/' + sUser.replace(/ /mig, '+') + '/pops/"><b>' + oChannel.getAttribute("pops-total")  + '</b> pops</td></tr>';
	sHTML += '<tr><td width="20"><img src="/images/header/ic_com.gif" border="0" /></td><td><a href="/clipper/' + sUser.replace(/ /mig, '+') + '/comments/"><b>' + oChannel.getAttribute("comments-total")  + '</b> comments</td></tr>';
	sHTML += '</table>';
	sHTML += '</td><td width="50%" valign="top" style="padding-left: 4px;" align="center">';

    if (oChannel.getAttribute("followers") > 0) { 
        sHTML += '<div class="CCFollowers"><img src="/images/icons/followers.gif" border="0" width="15" height="15" style="margin: 0px 3px -2px 0px;" /><a href="javascript:OpenClippers(\'' + sUser + '\', 1, 50)" title="# people who are following ' + sUser + '. Click to see who." class="CLB"><span id="spnTotalFollowersCard">' + oChannel.getAttribute("followers") + ' follower';
        if (oChannel.getAttribute("followers") > 1) {
			sHTML += 's';
		}                    
		sHTML += '</span>';
		sHTML += '</a></div>';
	} else {
         sHTML += '<div class="CCFollowers"><a href="javascript:OpenClippers(\'' + sUser  + '\', 1, 50)" class="CLB"><span id="spnTotalFollowersCard"></span></a>&nbsp;</div>';
	}
	if (sUser != sAuthName) {
		sHTML += '<a href="javascript:' + sFollowUrl + '"><img src="/images/clipper/' + sFollowImg + '" border="0" id="imgFollowCard" /></a>';
	}
	sHTML += '</td></tr></table>';
	sHTML += '</div>';
	oCC.innerHTML = '<div class="' + sContentClass + '" onmouseover="bCCActive=true;" onmouseout="bCCActive=false;CloseCC();"><div class="Loading" align="center">' + sHTML + '</div>';
}

function CloseCC() {
	bCC = false;
	oCCClosing = setTimeout('CloseCCDone()', 750);
}

function CloseCCDone() {
	if (!bCCActive) {
		var oCC = document.getElementById('dvCCPanel');
		oCC.style.display = 'none';
		oCC.innerHTML = '';
		if (oCCClosing) { 
			clearTimeout(oCCClosing);
			oCCClosing = null;
		}
		
		if (!bMacDhtml && !is_ie) {
			ShowScrollbars();
		}
	}
}

function UnloadCC() {
	var oCC = document.getElementById('dvCCPanel');
	oCC.style.display = 'none';
	bCCUnload = true;
	if (oCCStarting) { 
		clearTimeout(oCCStarting);
		oCCStarting = null;
	}
	return true;
}

