// ads.js
// Supporting functions for Banner ads, referrals, downloads, event tracking
// Note: This script requires Analytics tracking script to be installed a page
// according to Google instructions.

var served_advertisers = [];

function get_hostname_from_url(url) 
// Utility function to retrieve domain name from full URL
// Example: get_hostname_from_url('http://domain.co.uk') returns domain.co.uk 
// Example: get_hostname_from_url('https://www.domain.com/') returns domain.com
// Example: get_hostname_from_url('http://blog.domain.com/1/2/whatever.htm') returns blog.domain.com
{
    // return url.match(/:\/\/(.[^/]+)/)[1];
    // return url.split(/\/+/g)[1];
    var str;
    // Covert url to lowercase
    str = url.toLowerCase();
    // Extract www.domain.com
    str = str.match(/:\/\/(.[^/]+)/)[1]
    str = str.replace('www.','');
    return str;
}

function track_referral( advertiser_domain )
// Track referral link
// Example html: <a href="http://www.foo.com/sale" onclick="javascript:track_referral( 'foo.com' );" >
{
	try {	
		// For testing purposes, change 'Referrals' to 'Test' 
		pageTracker._trackEvent('Referrals', advertiser_domain, location.pathname);
		// alert( "Thank you for visiting our sponsor. Click OK to continue to the " + advertiser_domain + " site." );
	} catch(err) {}
}


function track_ad_impression( advertiser_domain ) 
// Track the ad impression
// This event needs to be deferred until the page has been completely loaded
// and GA code executed.
{
// Add new ad impression to the served_advertisers array

	var str;
	// Check if ad impression for this advertiser hasn't been already recorded on this page.
	str = served_advertisers.toString()
	if (str.search( advertiser_domain ) == -1)
	{
		served_advertisers[ served_advertisers.length ] = advertiser_domain;
	}
} 

function post_tracker_events()
// At the end of page, report all recorded ad impressions 
// (or other suitable events not yet reported to GA)
// This founction must be called at the end of each page, after the GA tracking code.
// Example: insert this just before the </body> tag: <script type='text/javascript'>post_tracker_events()</script>
{
	try {
		var i;
		for ( i=0; i < served_advertisers.length; i++ ) 
		{
			// For testing purposes, change 'Ad Impressions' to 'Test' 
			pageTracker._trackEvent('Ad Impressions', served_advertisers[i], location.pathname);
		}
	} catch(err) {}	
}

function track_ad_click( advertiser_domain )
{
	try {	
		// For testing purposes, change 'Ad Clicks' to 'Test' 
		pageTracker._trackEvent('Ad Clicks', advertiser_domain, location.pathname);
		// alert( "Thank you for visiting our sponsor. Click OK to continue to the " + advertiser_domain + " site." );
	} catch(err) {}
}


function get_banner_html( advertiser_domain, imgURL, targetURL )
// Generic function to create standardised html for a 468x60 banner ad.
// The html comes complete with impression and click tracking.
{
    var html = "";
	html = html + '<a href="' + targetURL + '" onClick="javascript:track_ad_click(\'' + advertiser_domain + '\');" >';
    html = html + '<img border="0" src="' + imgURL + '" width="468" height="60">';
	html = html + '</a>';
	
	track_ad_impression( advertiser_domain );
	
    return html;
} 


function display_banner_ad( advertiser_domain )
// This is the main function to call whenever an ad banner is to be displayed on the site.
// New ads on the site should be set up inside this function. 
// Valid advertiser domain name (e.g. "foo.com", no www) must be provided for the ad to be displayed.
// Example: display_banner_ad( "domain.com" );
// Example html: <script type='text/javascript'>display_banner_ad( "domain.com" );</script>

// Whenever the ad contract end, the corresponding ad may simply be removed from this function, 
// thus causing the banner to automatically disappear from all locations at the site.
// Date checks for contract termination etc. may also be validated.
{
	var html = "";
	// treat the situation when function is called without parameters
	if (typeof advertiser_domain == 'undefined' ) advertiser_domain = "";

	// hlj.com
	if ( advertiser_domain == "hlj.com")
	{
		html = get_banner_html( advertiser_domain, "/ad-banners/hlj-banner-animated-2.gif", "http://www.hlj.com/scripts/hljlist.cgi?x=0&y=0&Word=spitfire&range=nameonly&SeriTxt1=&GenreCode=All&MacroType=All&Maker1=All&Scale=All&Skill1=0&Skill2=5&MinP=0&MaxP=999999&Dae=All&Dis=-2&Code=" );
	}
	// delphic.com
	else if ( advertiser_domain == "delphic.com")
	{
		html = get_banner_html( advertiser_domain, "/ad-banners/delphic-banner-animated.gif", "http://www.delphic.com/spitfire-mark-ix.html" );
	}
	// goactionstations.co.uk
	else if ( advertiser_domain == "goactionstations.co.uk")
	{
		html = get_banner_html( advertiser_domain, "/ad-banners/action-stations.gif", "http://goactionstations.co.uk/" );
	}
	// spitfiresite.com
	else if ( advertiser_domain == "spitfiresite.com")
	{
		html = get_banner_html( advertiser_domain, "/ad-banners/free-ad-space.gif", "http://spitfiresite.com/" );
		// Modify this to real URL later
	}
	
	// default choice	
	else {
	
		// Randomize the choice of ad
		var random_no;
		random_no = Math.random();

		if ( random_no < 0.40 )
		{
			 // 40% probability	
			 // Run delphic.com ad
			 display_banner_ad( "delphic.com" );
		}
		else
		{
			 display_banner_ad( "goactionstations.co.uk" );
			// In all other cases, fill the space with a Tradedoubler ad
			// var uri = 'http://impgb.tradedoubler.com/imp?type(js)pool(355127)a(1717895)' + new String (Math.random()).substring (2, 11);
			// html = '<sc'+'ript type="text/javascript" src="'+uri+'" charset="ISO-8859-1"></sc'+'ript>';
		}
	}
	
	document.write( html );
}
	
