//get top groups
function getTopEvents(compid, path) {
	if (compid == "National") compid = '';
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top10=event&comp="+escape(compid)+"&callback=?",function(data){
		var html = "<ul>";
		$.each(data.data, function(index, group){
			//html += "<li><label>"+(index+1)+". <a href='"+group.groupurl+"' target='_new'>"+((group.groupname.length>15) ? (group.groupname.substr(0,15)+"...") : group.groupname) +"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";			
			if (compid != '') {
				html += "<li><label>"+formatIndex(index+1)+". <a href='"+group.groupurl+"' target='_new'>"+((group.eventname.length>(22+13)) ? (group.eventname.substr(21,13)+"...") : group.eventname.substr(21,99)) +"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";						
			} else {
				html += "<li><label>"+formatIndex(index+1)+". <a href='"+group.eventurl+"' target='_new'>"+((group.eventname.length>(22+13)) ? (group.eventname.substr(21,13)+"...") : group.eventname.substr(21,99)) +"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";						
			}
			var tgid = group.compid;
			var eventid = group.sessionid;
		});
		html += "</ul>";
		if (path == undefined) {
			html += '<br/><a href="http://www.kintera.org/site/c.rjJ0J6MHIoE/b.5795495/k.73F/View_All_Events_for.htm?comp='+compid+'"><img alt="View Top 100" src="http://www.kintera.org/atf/cf/{9dac21a8-151b-4260-a32e-387894e52ef8}/BTN_VIEW_ALL.GIF" /></a.';
		} else {
			html += '<br/><a href="'+path+'"><img alt="View all" src="/atf/cf/{9dac21a8-151b-4260-a32e-387894e52ef8}/BTN_VIEW_ALL.GIF" /></a.';			
		}
		$(".data_table_hp:eq(0)").html(html);
	});
}
function getTopTeams(compid) {
	if (compid == "National") compid = '';	
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top10=team&comp="+compid+"&callback=?",function(data2){
		var html = "<ul>";
		$.each(data2.data, function(index, team){
			var city = '';
			if (compid == 'Community Teams') {city = "("+titleCaps(team.eventname.substr(21,99))+")"}
			html += "<li><label>"+formatIndex(index+1)+". <a href='"+team.teamurl+"' target='_new'>"+((team.teamname.length>15) ? (team.teamname.substr(0,15)+"...") : team.teamname) +" "+((city.length>15) ? (city.substr(0,15)+"...") : city)+"</a></label><span class='aligned_right'>"+formatCurrency(team.raised)+"</span></li>";			
			var tgid = team.compid;
			var eventid = team.sessionid;
		});
		html += "</ul>";
		html += '<br/><a href="/site/c.rjJ0J6MHIoE/b.5795501/k.909D/View_All_Teams.htm?comp='+compid+'"><img alt="View all" src="/atf/cf/{9dac21a8-151b-4260-a32e-387894e52ef8}/BTN_VIEW_ALL.GIF" /></a>';
		$(".data_table_hp:eq(1)").html(html);
	});
}
function getTopGroups(compid) {
	if (compid == "National") compid = '';
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top10=participant&comp="+compid+"&callback=?",function(data3){
		var html = "<ul>";
		$.each(data3.data, function(index, member){
			var city = '';
			if (compid == 'Community Teams') {city = "("+titleCaps(member.eventname.substr(21,99))+")"}
			html += "<li><label>"+formatIndex(index+1)+". <a href='"+member.memberurl+"' target='_new'>"+((member.membername.length>15) ? (member.membername.substr(0,15)+"...") : member.membername) +" "+((city.length>15) ? (city.substr(0,15)+"...") : city)+"</a></label><span class='aligned_right'>"+formatCurrency(member.raised)+"</span></li>";			
			var tgid = member.compid;
			var eventid = member.sessionid;
		});
		html += "</ul>";
		//html += '<br/><img alt="View all" src="/atf/cf/{9dac21a8-151b-4260-a32e-387894e52ef8}/BTN_VIEW_ALL.GIF" />'
		$(".data_table_hp:eq(2)").html(html);
	});
	
}

function getVirtual(compid) {
	$.getJSON("http://www.kinteratools.com/ahanew/getmappedevents.php?virtual=true&compid="+compid+"&callback=?",function(datav){
		var html = "";
		$.each(datav.data, function(index, evt){
			if (compid=='National') {
				html += "<b><a href='"+evt.groupurl+"' target='_new'>"+ titleCaps(evt.eventname) +"</a></b>";
			} else {
				html += "<b><a href='"+evt.groupurl+"' target='_new'>"+ titleCaps((evt.eventname.length>(22+35)) ? (evt.eventname.substr(21,35)+"...") : evt.eventname.substr(21,99)) +"</a></b>";
			}
		});
		if (html != '') {
			$("#virtualtxt").show();			
			$("#virtual").html(html);
	  	} else {
			$("#virtualtxt").hide();
		}
	});
}

function getTopExecEvents(compid) {
	if (compid == undefined) compid = '';
	//if (compid != '') $('#page_title h1').append(" for "+compid);
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top10=executiveevents&comp="+escape(compid)+"&callback=?",function(data){
		var html = "<ul id='topexecs'>";
		html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:220px;'><strong>Event Name</strong></label><span class='aligned_right'><strong>Amount Raised</strong></span></li>";
		html += "<hr>";
		$.each(data.data, function(index, execev){
			html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:220px;'>"+(index+1)+". <a href='"+execev.url+"' target='_new'>"+execev.event.substr(21,35)+"</a></label><span class='aligned_right'>"+formatCurrency(execev.raised)+"</span></li>";
		});
		html += "</ul>";
		$("#output").html(html);
	});
}

function getTopExecs(compid) {
	if (compid == undefined) compid = '';
	//if (compid != '') $('#page_title h1').append(" for "+compid);
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top10=executive&comp="+escape(compid)+"&callback=?",function(data){
		var html = "<ul id='topexecs'>";
		html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:220px;'><strong>Exec Name</strong></label><span class='aligned_right'><strong>Amount Raised</strong></span></li>";
		html += "<hr>";
		$.each(data.data, function(index, exec){
			html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:220px;'>"+formatIndex(index+1)+". "+exec.firstname+' '+exec.lastname+"</label><span class='aligned_right'>"+formatCurrency(exec.raised)+"</span></li>";
			html += "<li>&nbsp;&nbsp;&nbsp;&nbsp;<a href='"+exec.url+"' target='_new'>"+exec.event.substr(21,35)+"</a></li>";
		});
		html += "</ul>";
		$("#output2").html(html);
	});
}

function getTop100Events(compid) {
	if (compid == undefined) compid = '';
	if (compid != '') $('#page_title h1').append(" for "+compid);
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top100=event&comp="+escape(compid)+"&callback=?",function(data){
		var html = "<ul id='topevents'>";
		html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:400px;'><strong>Event Name</strong></label><span class='aligned_right'><strong>Amount Raised</strong></span></li>";
		html += "<hr>";
		$.each(data.data, function(index, group){
			//html += "<li><label>"+(index+1)+". <a href='"+group.groupurl+"' target='_new'>"+((group.groupname.length>15) ? (group.groupname.substr(0,15)+"...") : group.groupname) +"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";			
			html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:400px;'>"+formatIndex(index+1)+". <a href='"+group.eventurl+"' target='_new'>"+titleCaps(group.eventname.substr(21,99))+"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";						
		});
		html += "</ul>";
		html += "<br /><a href='javascript:history.go(-1);'>Back</a>";
		$("#output").html(html);
	});
}

function getTop100Teams(compid) {
	if (compid == undefined) compid = '';
	if (compid != '') $('#page_title h1').append(" for "+compid);
	$.getJSON("http://www.kinteratools.com/ahanew/geteventstats.php?top100=team&comp="+escape(compid)+"&callback=?",function(data){
		var html = "<ul id='topteams'>";
		html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:400px;'><strong>Event Name</strong></label><span class='aligned_right'><strong>Amount Raised</strong></span></li>";
		html += "<hr>";
		$.each(data.data, function(index, team){
			if (team.teamname != null) {
				//html += "<li><label>"+(index+1)+". <a href='"+group.groupurl+"' target='_new'>"+((group.groupname.length>15) ? (group.groupname.substr(0,15)+"...") : group.groupname) +"</a></label><span class='aligned_right'>"+formatCurrency(group.raised)+"</span></li>";			
				html += "<li><label style='display:inline-block;*display:inline;zoom:1;width:400px;'>"+formatIndex(index+1)+". <a href='"+team.teamurl+"' target='_new'>"+titleCaps(team.teamname.substr(0,99)) +"</a></label><span class='aligned_right'>"+((team.raised != undefined) ? formatCurrency(team.raised):0)+"</span></li>";						
			}
		});
		html += "</ul>";
		html += "<br /><a href='javascript:history.go(-1);'>Back</a>";
		$("#output").html(html);
	});
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
	return (((sign)?'':'-') + '$' + num);
}

function titleCaps(title) {
	var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
	var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  	var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
	title = lower(title);
	while (true) {
		var m = split.exec(title);
		parts.push( title.substring(index, m ? m.index : title.length)
			.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
				return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
			})
			.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
			.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
				return punct + upper(word);
			})
			.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));

		index = split.lastIndex;

		if ( m ) parts.push( m[0] );
		else break;
	}

	return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
		.replace(/(['Õ])S\b/ig, "$1s")
		.replace(/\b(AT&T|Q&A)\b/ig, function(all){
			return all.toUpperCase();
		});
}

function lower(word){
	return word.toLowerCase();
}

function upper(word){
  return word.substr(0,1).toUpperCase() + word.substr(1);
}

function formatIndex(count) {
	return (count < 10) ? ("0"+count) : count;
}
