// ***************************************************
// * jQuery.dbh.components: jquery.dbh.AdRotator
// * version: 1.0
// * © 2011 Derek Hughes
// ***************************************************

(function($) 
{

	var enableRotate = true;
	
	var retVal = "";			// Return value from AJAX
	var retUrl = "";			// Return value from AJAX
	var pics = new Array();		// Array that holds picture names
	var urls = new Array();		// Array that holds URL's
	var rand = new Array();		// Array that contains random ad numbers
	var amtPics = 0;

	$.fn.InitializeAdRotator = function(callerSettings) 
	{
		var x;
		adSettings = $.extend(
		{
			top: null,
			bottom: null,
			left: null,
			right: null,
			width: 200,
			height: 150,
			border: 1,
			borderColor: "#000000",
			borderStyle: "solid",
			initialPic: null,
			pauseLength: 10000,
			qty: 1,
			layout: "V",
			lastControl: null
		},callerSettings||{});


		GetAdRotatorInfo();

		for (x = 0; x < adSettings.qty; x++)
		{
			// The img tag doesn't exist. So we'll create it here and apply some basic styles.
			adId = this[0].id;		imgIdBase = adId + "Img";	imgId = imgIdBase + x; 	adIdEl = "#" + adId;
			$("#" + adId).append($('<a id="' + imgId + 'Link" href="#" target="_blank" ><img id="' + imgId + '"></a>'));
			$("#" + imgId).css("height", adSettings.height + "px");

			// Fix for stupid non-complient IE
			if (!jQuery.support.boxModel)
			{
				$("#" + imgId).css("width", adSettings.width-( adSettings.border * 2) + "px");
			}
			else
			{
				$("#" + imgId).css("width", adSettings.width + "px");
			}

			

			$("#" + imgId).attr("src", "/images/randomAds/" + adSettings.initialPic);
		}

		$(adIdEl).css("height", (adSettings.height * adSettings.qty) + "px");
		$(adIdEl).css("width", adSettings.width + "px");
		$(adIdEl).css("border", adSettings.border + "px");
		$(adIdEl).css("border-color", adSettings.borderColor);
		$(adIdEl).css("border-style", adSettings.borderStyle);
		
		if (adSettings.top != null)		$(adIdEl).css("top", adSettings.top + "px");
		if (adSettings.bottom != null)	$(adIdEl).css("bottom", adSettings.bottom + "px");
		if (adSettings.left != null)		$(adIdEl).css("left", adSettings.left + "px");
		if (adSettings.right != null)		$(adIdEl).css("right", adSettings.right + "px");

//return;
		// Setup the timer event handeler
		setInterval(function() { RotateAds(imgIdBase); }, adSettings.pauseLength);

	}

	

	//-----------------------------------------------------
	// RotateAds
	//-----------------------------------------------------
	function RotateAds(imgIdbase)
	{
		var alreadyHere = false;

		// If picture namnes loaded, turn them into an array
		endPos = retVal.lastIndexOf("-END-");
		if (endPos != -1)
		{
			pics = retVal.split("||");			pics.length--;		amtPics = pics.length;		retVal = "";
			urls = retUrl.split("||");
			// Set array to the first picture
			for (x = 0; x < adSettings.qty; x++)	rand[x] = x;			
		}

		// if pic array not loaded return without rotating
		if (pics.length < 1) return;

		// if rotate is not enabled then return without rotating
		if (!enableRotate) return;

		// Pick random ad to display next and make sure there are no duplicates
		while(true)
		{
			// Reset flag
			alreadyHere = false;

			// Get the random number
			randomPic = parseInt(Math.random() * amtPics);		if (randomPic >= amtPics) randomPic = amtPics-1;

			// Check to see if already in the array
			for (x = 0; x < adSettings.qty; x++)
			{
				if (rand[x] == randomPic) alreadyHere = true;
			}

			// Not in array so break
			if (!alreadyHere) 
			{
				break;
			}
		}

		// Scroll the rand array up one and replace picture at the end of the array
		for (x = 0; x < adSettings.qty; x++) rand[x] = rand[x+1];
		rand[adSettings.qty-1] = randomPic;

		// Rotate the pictures
		for (x = 0; x < adSettings.qty; x++)
		{
			imgId = "#" + imgIdbase + x;			
			$(imgId).attr("src", "/images/randomAds/" + pics[rand[x]]);
			$(imgId + "Link").attr("href", "http://" + urls[rand[x]]);

//			$("#adSpace2").slideToggle("slow");	$("#adSpace2").show("slow");
		}
	}

	//-----------------------------------------------------
	// GetAdRotatorInfo
	//-----------------------------------------------------
	function GetAdRotatorInfo()
	{
		var adUrl, adData, textStatus;
//		retVal  = "arkwestgas ad.jpg||Huntsville Lumber ad.jpg||Lodge ad.jpg||azalea falls ad.jpg||beyond printing ad.jpg||";
//		retVal += "butterball ad.jpg||citywater ad.jpg||faubus motel ad.jpg||Harps ad.jpg||fnbgf ad.jpg||";
//		retVal += "-END-";
//		return;

		// Get the url that the data is at
		adUrl = "/js/adRotator/!adRotator_ajax.php";
		$(function()
		{
			$.getJSON(adUrl, function(adData, textStatus)
				{
					$.each(adData, function(index, info)
					{
						retVal += info.rotAdsPicture + "||";
						retUrl += info.rotAdsUrl + "||";
					});
					retVal += "-END-";
				}
			);              
		});

	}	





})(jQuery);
