/**
 * Apply replacment to DOM lements
 */
Cufon.replace('h1')('h2')('h3')('blockquote');


$(document).ready(function(){
	
	// CONFIG 
	var teaPerDay = 165000000;				// NUMBER OF CUPS PER DAY
	var updateFrequency = 500;				// UPDATE COUNTER EVERY.. (milliseconds)
	
	// Mad font stuff
	// Do Cufon Replacement
	Cufon.now();
	$('#catSelect').change(function(){
		document.location.href = '/research_documents/browse/' + $('#catSelect').val();		
	}); 

	var secondsInDay = 24 * 60 * 60;		// const
	setInterval (function(){
		now = new Date();
		secsOnClock = (((now.getHours() * 60) + now.getMinutes()) * 60) + now.getSeconds();
		teaPerSecond = teaPerDay / secondsInDay;
		teacounter = Math.floor(secsOnClock * teaPerSecond);
		// Update the onscreen counter
		$('#tea-counter').text(addCommas(teacounter));
	}, updateFrequency );
	
	// Intro paragraphs
	$("div#main_wide p:first").addClass("intro");

	// Health warning
	show_health_warning();
});

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function registerVisitorType(type){
	$.ajax({
	  url: "/users/registerVisitorType/" + type,
	  success: function(responder){
	    eval(responder); // cookie is set via ajax response
	  }
	});
}

function show_health_warning() {
	if(!$.cookie('health_warning') && document.URL.search(/registerVisitorType/) == -1) {
		var content = "<h2>Welcome to www.teaadvisorypanel.com.</h2>";	
		content += "<p>This site has been set up by the Tea Advisory Panel to provide journalists and health professionals with the latest scientific research and nutritional information on tea. Please tick one of the boxes below to indicate whether you are a journalist or health professional.</p>";		
		content += "<ul style='list-style:none'>";
		content += "	<li><input type='checkbox' onclick='registerVisitorType(this.value)' name='userType' value='journalist'> Health and Lifestyle journalist</li>";
		content += "	<li><input type='checkbox' onclick='registerVisitorType(this.value)' name='userType' value='gpnurse'> GP / Nurse</li>";
		content += "	<li><input type='checkbox' onclick='registerVisitorType(this.value)' name='userType' value='nutritionadvisor'> Nutrition Advisor</li>";
		<!--content += "	<li><input type='checkbox' onclick='registerVisitorType(this.value)' name='userType' value='Other'> Other</li>";-->
		content += "</ul>";
		$.prompt(content);
	}
}

