/**
 * Click Counter demo javascript module
  *
 * LICENSE: This source file is subject to the BSD license
 * that is available through the world-wide-web at the following URI:
 * http://www.opensource.org/licenses/bsd-license.php.
 *
 * @author     Michael P. Shipley <michael@michaelpshipley.com>
 * @copyright  2008 Michael P. Shipley
 * @license    http://www.opensource.org/licenses/bsd-license.php BSD
 * @version    1.0
 * @link       http://www.michaelpshipley.com Michael Shipley
 */


/**
	Add click monitors to all links with a class of 'count' 
*/
cc_add_click_monitor("count");



/**	
* 	function to return click counts by ajaxing click_counter.php
 */
function cc_demo_get_clicks()
{
	// make url of php script that displays clicks by taking the document url, and extracting the path.
	var url = document.URL;
	var path = url.substr(0,url.lastIndexOf("/")+1);	
	var display_click_counter = path + 'display_click_counts.php';	
	
	var http;
		
	if (window.XMLHttpRequest)
	{
		http = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		http = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else
	{
		alert('browser doesn\'t support javascript http connections');	
		return true;
	}
	http.open("GET", display_click_counter, false);
	http.send(null);

	return http.responseText;	
}


/**
	function to display click counts
*/
function cc_demo_show_clicks()
{

	// get clicks
	var clicks = cc_demo_get_clicks();

	// show clicks
	var clicks_display_div = document.getElementById('clicks_display_div');
	clicks_display_div.innerHTML = clicks;
}

/**
	tell click counter to call cc_demo_show_clicks every time it finishes processing a click
*/
cc_http_response_callback = cc_demo_show_clicks;
