/**
 * An improved advert handling script
 * @author Dean McGill
 */

if (!HPI) {
	var HPI = {};
}

HPI.marketing = {
		
	/**
	 * set this flag to true to replace the top advert with the notice image.
	 */	
	showNotice: false,
	
	/**
	 * filename of the banner image to show
	 */
	
	noticeFileName: '/images/banner-notice.jpg',
	
	/**
	 * Configuration object for openX adverts
	 * @property url - absolute URL to the openX server script
	 * @property rand - A random number for a cache buster
	 * @property charset - Character set extracted from the DOM
	 * @property referrer - The referring page extracted from the DOM
	 * @property context - The document context extracted from the DOM
	 * @property src - The location of the document to use as the IFRAME source
	 */
	openxConfig: {
			url : (location.protocol=='https:'?'https://openx.hpicheck.com/openx/delivery/ajs.php':'http://openx.hpicheck.com/openx/delivery/ajs.php'),
			rand : Math.floor(Math.random()*99999999999),
			charset: document.charset ? document.charset : document.characterSet,
			location: escape(window.location),
			referrer: escape(document.referrer),
			context: document.context ? escape(document.context) : null,
			src: '/includes/adframe_openx.html'
	},
	
	/**
	 * General advert configuration object
	 * 
	 * One each for the top, right & bottom adevrts
	 * 
	 * @property el - The DOM id of the DIV to inject the advert IFRAMES into
	 * @property width - Width of the IFRAME (same as advert image)
	 * @property height - Height of the IFRAME (same as advert image)
	 * @property key - An array key used to reference the advert type (top/right/bottom)
	 * @property src - The location of the document to use as the IFRAME source. by default
	 *                 this is set to HPIs own adverts. This is usually replaced with the
	 *                 openX src property.
	 * 
	 */
	adConfig : {
		top : {
			el : 'topadvert',
			width : 728,
			height : 90,
			key : 'top',
			src: '/includes/adframe_hpi.html'  // by default, use HPI src
		},
		right : {
			el : 'rightadvert',
			width : 120,
			height : 600,
			key: 'right',
			src: '/includes/adframe_hpi.html'
		},
		bottom : {
			el : 'bottomadvert',
			width : 468,
			height : 60,
			key: 'bottom',
			src: '/includes/adframe_hpi.html'
		}
	},
	
	/**
	 * create an IFRAME element and append to the document
	 */
	renderAdvert : function (cfg)
	{
		// if element id not found return without error
		if (!document.getElementById(cfg.el)) {
			return;
		}
		
		// if show notice is enabled, bypass the top adverts
		if (this.showNotice && cfg.el == 'topadvert') {
			this.renderNotice();
			return;
		}
		
		if (!document.getElementById(cfg.el)) {
			return;
		}
		
		var frame = document.createElement('IFRAME');
		
		frame.src = cfg.src + '?' + $.param(cfg);
		frame.width = cfg.width;
		frame.height = cfg.height;
		frame.scrolling = 'no';
		frame.frameBorder = 0;
		frame.align = 'center';
		frame.valign='top';
		frame.marginwidth = 0;
		frame.marginheight = 0;
		frame.hspace = 0;
		frame.vspace = 0;
		document.getElementById(cfg.el).appendChild(frame);
	},
	
	/**
	 * a helper method to show the openX adverts
	 *
	 * @param response - The response object from the openX servlet request
	 */
	showOpenxAdverts: function (response)
	{
		var zones = response.zones.split(",");
		if (window.isHome == true)
		{
			// adverts for home page
			$.extend(this.adConfig.top, this.openxConfig, {zoneId: zones[0]});
			$.extend(this.adConfig.right, this.openxConfig, {zoneId: zones[1]});
			$.extend(this.adConfig.bottom, this.openxConfig, {zoneId: zones[2]});
		}
		else
		{
			// adverts for other pages
			$.extend(this.adConfig.top, this.openxConfig, {zoneId: zones[3]});
			$.extend(this.adConfig.right, this.openxConfig, {zoneId: zones[4]});
			$.extend(this.adConfig.bottom, this.openxConfig, {zoneId: zones[5]});
		}

		// render the adverts
		this.renderAdvert(this.adConfig.top);
		this.renderAdvert(this.adConfig.right);
		this.renderAdvert(this.adConfig.bottom);
	},
	
	/**
	 * helper function to display HPI adverts
	 */
	showHpiAdverts : function ()
	{
		this.renderAdvert(this.adConfig.top);
		this.renderAdvert(this.adConfig.right);
		this.renderAdvert(this.adConfig.bottom);
	},
	
	/**
	 * render the notice
	 */
	renderNotice : function ()
	{
		var img = document.createElement('IMG');
		
		img.src = this.noticeFileName;
		img.width = 728;
		img.height = 90;
		img.alt = 'notice';
		document.getElementById('topadvert').appendChild(img);
	}
};


$(function() {
	
	if (window.hpiAds == true)
	{
		// use static hpi adverts
		HPI.marketing.showHpiAdverts();
	}
	else
	{
		// process openx adverts	
		$.ajax({
			url: '/consumer/marketing',
			type: 'GET',
			dataType: 'json',
			timeout: 5000,
			success: function (response)
			{
				if (response.status == true || document.domain == 'consumerdev') {
					HPI.marketing.showOpenxAdverts(response);
				} else {
					HPI.marketing.showHpiAdverts();
				}
			},
			error: function ()
			{
				HPI.marketing.showHpiAdverts();
			}
		});
	}
});
