/*!
 * Simple JSON fed Adswap
 *
 * Copyright 2010, Bannerview.com
 *
 * Dependency: jQuery
 *
 * Date: Wed Apr 14 09:34 2010 -0800
 */

var https = '';

var adObj = {
	boothData: '',
	campaignData: '',
	boothHandle: Array(),
	campaignHandle: Array(),
	boothStep: 0,
	campaignStep: 0,
	boothBanner: 0,
	campaignBanner: 0,
	boothLength: 0,
	campaignLength: 0,
	gotBooth:
		function (data)
		{
			this.boothData = data;
			if (this.campaignData != '')
				this.startSeq();
		}
	,
	gotCampaign:
		function (data)
		{
			this.campaignData = data;
			if (this.boothData != '')
				this.startSeq();
		}
	,
	startSeq:
		function()
		{
			if (this.boothData != '' && this.campaignData != '')
			{
				// Grab all current ad slots
				$("a.boothBannerSlot").each(
					function () {
						adObj.boothHandle.push($(this));
					}
				);
				$("a.campaignBannerSlot").each(
					function () {
						adObj.campaignHandle.push($(this));
					}
				);
				for (var i in this.boothData)
				{
					this.boothLength++;
				}
				for (var i in this.campaignData)
				{
					this.campaignLength++;
				}
				setInterval("adObj.swap()",900);
			}
		}
	,
	swap:
		function()
		{
			if (this.boothStep >= this.boothLength)
				this.boothStep = 0;

			if (this.campaignStep >= this.campaignLength)
				this.campaignStep = 0;

			if (this.boothBanner >= this.boothHandle.length)
				this.boothBanner = 0;

			if (this.campaignBanner >= this.campaignHandle.length)
				this.campaignBanner = 0;

			this.boothHandle[this.boothBanner].attr(
				{
					'href':https+this.boothData[this.boothStep].url,
					'title':https+this.boothData[this.boothStep].name
				}
			);
			this.boothHandle[this.boothBanner].css(
				{
					'background':'url('+https+this.boothData[this.boothStep].image+') no-repeat center center'
				}
			);

			this.campaignHandle[this.campaignBanner].attr(
				{
					'href':https+this.campaignData[this.campaignStep].url,
					'title':https+this.campaignData[this.campaignStep].name
				}
			);
			this.campaignHandle[this.campaignBanner].css(
				{
					'background':'url('+https+this.campaignData[this.campaignStep].image+') no-repeat center center'
				}
			);

			this.boothStep++;
			this.campaignStep++;
			this.boothBanner++;
			this.campaignBanner++;

		}
};

$(window).load(
	function ()
	{
		var httpsReg = /^https:\/\/([a-z0-9_.-]*)\/([a-z0-9_.~-]*)/
		https = window.location.href.match(httpsReg,'i');
		if (https != null)
			https = https[0];
		else
			https = '';

		// Trigger requests
		$.getJSON(https+'/resources/boothjson.php',
			function (data)
			{
				adObj.gotBooth(data);
			}
		);
		$.getJSON(https+'/resources/campaignjson.php',
			function (data)
			{
				adObj.gotCampaign(data);
			}
		);
	}
);