function linkHoverForThisCandidate(candidateID) {
	$('#' + candidateID).mouseover(linkCandidateHoverDetails);
}

function linkCandidateHoverDetails () {
/*	trigger = $("#" + candidateID);
	targetID = candidateID;//trigger.attr('id');
*/
	trigger = $(this); //event.target
	targetID = trigger.attr('id');
	targetDiv = $("div." + targetID);
	if(targetDiv.length == 0) {
		url = "http://ilcampaign.org/sunshine/candidatesDetail.json.php?CandidateID="+targetID+"&jsoncallback=?";
		$.getJSON(url, function(individualData) { 
			x = trigger.offset().left + 20;
			y = trigger.offset().top + 20;
			$("body").append("<div style='display: none;position:absolute; left:"+x+"px; top:"+y+"px; z-index: 9999; padding: 5px 10px; background:url(http://ilcampaign.org/sunshine/images/BrownGrad.png) repeat-x;background-color: white; width: 275px; border: 1px solid #666;' class='" + targetID + "'><h4 style='float:left; margin: 0px'>"+individualData['candidateName']+"</h4><img style='float:right' src='http://ilcampaign.org/sunshine/images/close-button.png' class='sunshine-close' title='close' /><div style='clear:both'><img style='float: left; width: 87px; height: 115px; padding-bottom: 20px' src='http://ilcampaign.org/sunshine/images/politicians/" + targetID + ".jpg' /><div style='float: right;'><img style='float: left; padding-right: 10px; height: 30px' src='http://ilcampaign.org/sunshine/images/"+ individualData['partyImage'] +"' />"+individualData['officeHeld']+"</div><div style='float:right;text-align: right;'><h6 style='margin: 0px 0px 0px 50px'>Beginning Balance</h6><span  style='margin: 0px 0px 0px 50px'>"+individualData['beginningBalance']+ "</span><h6 style='margin: 0px 0px 0px 50px'>Ending Balance</h6><span  style='margin: 0px 0px 0px 50px'>"+individualData['endingBalance']+"</span><a style='display:block;font-size: 13px;' href='http://ilcampaign.org/sunshine/SunshineDetail.php?committeeID="+individualData['candidateID']+"&cycle1=20092010&cycle2=20092010'>Get More Info</a></div></div></div>");
			$("div." + targetID).show();//.draggable();
			
			$("div." + targetID +" img.sunshine-close").click(function() {
				$(this).parent().hide();
			});
			//if($("div." + targetID).show().draggable()) {
			//	$("div." + targetID).show().draggable();
			//}
		});
	} else {
		x = trigger.offset().left + 20;
		y = trigger.offset().top + 20;
		targetDiv.css('left', x+'px');
		targetDiv.css('top', y+'px');
		targetDiv.show();
	}

}

function runCandidateExpression (candidateName, candidateID, targetID) {
	replaceTarget = $("#" + targetID);
	candidateLink = candidateName + " <a href=\"http://ilcampaign.org/sunshine/SunshineDetail.php?committeeID="  + candidateID + "&cycle1=20072008&cycle2=20092010\">";
	candidateLink += "<img class=\"sunshine-candidate\" id=\"" + candidateID + "\" src=\"http://ilcampaign2.org/sunshine/images/sun.png\" height=\"25\" width=\"25\" border=\"0\" /></a>";
	regexString = candidateName; //"("+candidateName+")(?!(?:\\s*\\w*)*<\/a>)";
	regex = new RegExp(regexString, "gi");
	if(replaceTarget.html().match(regex)) {
		//alert(replaceTarget.html(replaceTarget.html().replace(regex, candidateLink)));
		replaceTarget.html(replaceTarget.html().replace(regex, candidateLink));
		setTimeout("linkHoverForThisCandidate("+candidateID+")", 2000);
	}	
}


(function($) {
  	$.fn.linkCandidates = function() {
		/*$(".sunshine-candidate").live('mouseover', function(event){
			trigger = $(event.target);
			targetID = trigger.attr('id');
			targetDiv = $("div." + targetID);
			if(targetDiv.length == 0) {
				url = "http://ilcampaign.org/sunshine/candidatesDetail.json.php?CandidateID="+targetID+"&jsoncallback=?";
				$.getJSON(url, function(individualData) { 
					x = trigger.offset().left + 20;
					y = trigger.offset().top + 20;
					$("body").append("<div style='display: none;position:absolute; left:"+x+"px; top:"+y+"px; z-index: 9999; padding: 5px 10px; background:url(http://ilcampaign.org/sunshine/images/BrownGrad.png) repeat-x;background-color: white; width: 250px; border: 1px solid #666;' class='" + targetID + "'><h4 style='float:left; margin: 0px'>"+individualData['candidateName']+"</h4><img style='float:right' src='http://ilcampaign.org/sunshine/images/close-button.png' class='sunshine-close' title='close' /><div style='clear:both'><img style='float: left; width: 87px; height: 115px;' src='http://ilcampaign.org/sunshine/images/politicians/" + targetID + ".jpg' /><div style='float: right;'><img style='float: left; padding-right: 10px' src='http://ilcampaign.org/sunshine/images/"+ individualData['partyImage'] +"' />"+individualData['officeHeld']+"</div><div style='float:right;text-align: right;'><h6 style='margin: 0px 0px 0px 50px'>Beginning Balance</h6><span  style='margin: 0px 0px 0px 50px'>$"+individualData['beginningBalance']+ ".00</span><h6 style='margin: 0px 0px 0px 50px'>Ending Balance</h6><span  style='margin: 0px 0px 0px 50px'>$"+individualData['endingBalance']+".00</span></div></div><a style='display:block;font-size: 13px;width: 120px;margin-top: 120px' href='http://ilcampaign.org/sunshine/SunshineDetail.php?stateID="+individualData['candidateID']+"&cycle1=20092010&cycle2=20092010'>Get More Info...</a></div>");
					$("div." + targetID).show().draggable();
					//if($("div." + targetID).show().draggable()) {
					//	$("div." + targetID).show().draggable();
					//}
				});
			} else {
				x = trigger.offset().left + 20;
				y = trigger.offset().top + 20;
				targetDiv.css('left', x+'px');
				targetDiv.css('top', y+'px');
				targetDiv.show();
			}
		});
		$(".sunshine-close").live('click', function(event) {
			$(event.target).parent().hide();
		});*/
		var moddedThis = this;
		$.getJSON("http://ilcampaign.org/sunshine/candidates.json.php?jsoncallback=?", function(data) { 
			var ilData = data;
			moddedThis.each(function() {
				for(i=0;i<ilData.length;i++) { 
					/*fullName = ilData[i]['CandidateName'];
					candidateLink = fullName + " <a href=\"http://ilcampaign.org/sunshine/SunshineDetail.php?stateID="  + ilData[i]['CandidateID'] + "&cycle1=20072008&cycle2=20092010\">";
					candidateLink += "<img class=\"sunshine-candidate\" id=\"" + ilData[i]['CandidateID'] + "\" src=\"http://ilcampaign2.org/sunshine/images/sun.png\" height=\"25\" width=\"25\" border=\"0\" /></a>";
					regexString = "("+fullName+")(?!(?:\\s*\\w*)*<\/a>)";
					regex = new RegExp(regexString, "gi");
					$(this).html($(this).html().replace(regex, candidateLink));*/
					setTimeout("runCandidateExpression(\"" + ilData[i]['CandidateName'] + "\", \"" +ilData[i]['CandidateID'] + "\", \"" + $(this).attr('id') + "\");");
				}
			});
		});
		return this;
	};
})(jQuery);
