﻿(function() 
{
	var adkConfig = 
	{
		pid: typeof(pid) == 'undefined' ? 0 : pid,
		plid: typeof(plid) == 'undefined' ? 15965 : plid,
		channel: typeof(channel) == 'undefined' ? '' : channel,
		excAdv: typeof(excAdv) == 'undefined' ? '' : excAdv,
		appid: typeof(appId) == 'undefined' ? 0 : appId,
		adsize: typeof(adSize) == 'undefined' ? '728x90' : adSize,
		backC: typeof(backC) == 'undefined' ? '' : backC.replace("#", "%23"),
		textC: typeof(textC) == 'undefined' ? '' : textC.replace("#", "%23"),
		linkC: typeof(linkC) == 'undefined' ? '' : linkC.replace("#", "%23")
	};

	var frame = adkConfig.adsize.split("x");
	var adId = Math.floor(Math.random() * 9999999);

	// Write ad container
	document.write("<div id='adk-frame-" + adId + "' style='width:" + frame[0] + "px;height:" + frame[1] + "px;'></div>");
	
	// Dependancy Adk.Ad.min.js
	var hElement = document.getElementsByTagName('head').item(0);
	var adkFound = false;
	var envProtocol = document.location.protocol;
	var adkSrc = envProtocol + '//social.bidsystem.com/client/Adk.Ad.js';
	for(var i = 0; i < hElement.childNodes.length; i++)
	{
		if(hElement.childNodes[i].src && hElement.childNodes[i].src.indexOf(adkSrc) == 0) adkFound = true;
	}
	if(!adkFound)
	{
		var now = new Date();
		now.setMinutes(now.getMinutes() - (now.getMinutes() % 5));
		var cacheBuster = now.getFullYear().toString() + (now.getMonth() + 1).toString() + now.getDay().toString() + "-" + now.getHours().toString() + now.getMinutes().toString();
	
		var jElement = document.createElement('script');
		jElement.type = 'text/javascript';
		jElement.src = adkSrc + '?' + cacheBuster;
		var x = document.getElementsByTagName('head')[0].appendChild(jElement);
	}
	
	var timeout = 5000;
	var elapsed = 0;
	var loadInterval = window.setInterval(function()
	{
		if(typeof(Adk) == 'object')
		{
			window.clearInterval(loadInterval);
			Adk.ShowAd('adk-frame-' + adId, adkConfig);
		}
		else if(elapsed >= timeout)
		{
			window.clearInterval(loadInterval);
		}
		elapsed += 200;
	}, 200);
})();
