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

(function($) 
{

	var enableRotateBanner = true;
	
	var banData = new Object();
	var banTop, banBot, banBuf;

	var retValBanner = "";			// Return value from AJAX
	var picsBanner = new Array();		// Array that holds picture names
	var randBanner = new Array();		// Array that contains random ad numbers
	var amtPicsBanner = 0;
	var currentPic = 0;
	var displayPic = 0;

	$.fn.InitializeBannerRotator = function(callerSettings) 
	{
		var x, banId, imgIdBaseBanner;
		
		var bannerSettings = $.extend(
		{
			top: null,			bottom: null,			left: null,			right: null,
			width: 200,			height: 150,			zIndex: 5000,
			border: 1,			borderColor: "#000000",			borderStyle: "solid",
			initialPic: null,
			pauseLength: 10000,	fadeTime:2000,
			lastControl: null
		},callerSettings||{});

		banId = this[0].id;		imgIdBaseBanner = banId + "Img";	

		GetBannerRotatorInfo(imgIdBaseBanner);

		// Make three images containers
		for(x = 0; x< 3; x++)
		{
			// The img tag doesn't exist. So we'll create it here and apply some basic styles.
			imgIdBanner = imgIdBaseBanner + x; 	banIdEl = "#" + banId;
		
			// Create img tag
			$("#" + banId).append($('<img id="' + imgIdBanner + '">'));

			// Set position, top, left and z-index of newly created  image tag
			$("#" + imgIdBanner).css("position", "absolute").css("left", "0px").css("top", "0px");

			// Set width tag of newly created  image tag
			$("#" + imgIdBanner).css("height", bannerSettings.height + "px");

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

			// Set the initial picture into each img container			
			$("#" + imgIdBanner).attr("src", "/images/topBanners/" + bannerSettings.initialPic);
		}
		// Make image id holder variables
		banTop = "#" + imgIdBaseBanner + "0";		banBot = "#" + imgIdBaseBanner + "1";		banBuf = "#" + imgIdBaseBanner + "2";

		// Set z=index for top and bottom 
		$(banBot).css("z-index", bannerSettings.zIndex+0);
		$(banTop).css("z-index", bannerSettings.zIndex+1);

		// Set Containers to non visible except bottom
		$(banBuf).hide();
		$(banTop).fadeTo(0, 1);
		$(banBot).fadeTo(0, 1);

		// Set Bottom image to default image
//		$(banBot).attr("src", "/images/topBanners/top4.jpg");		
		$(banTop).attr("src", "/images/topBanners/" + bannerSettings.initialPic);		
		$(banBot).attr("src", "/images/topBanners/" + bannerSettings.initialPic);		
//		$(banBuf).attr("src", "/images/topBanners/" + bannerSettings.initialPic);		

		// Set Misc CSS properties
		$(banIdEl).css("height", (bannerSettings.height * bannerSettings.qty) + "px");
		$(banIdEl).css("width", bannerSettings.width + "px");
		$(banIdEl).css("border", bannerSettings.border + "px");
		$(banIdEl).css("border-color", bannerSettings.borderColor);
		$(banIdEl).css("border-style", bannerSettings.borderStyle);
		
		// Set position CSS if specified
		if (bannerSettings.top != null)		$(banIdEl).css("top", bannerSettings.top + "px");
		if (bannerSettings.bottom != null)	$(banIdEl).css("bottom", bannerSettings.bottom + "px");
		if (bannerSettings.left != null)		$(banIdEl).css("left", bannerSettings.left + "px");
		if (bannerSettings.right != null)		$(banIdEl).css("right", bannerSettings.right + "px");

		// Setup the timer event handeler
		setInterval(function() { RotateBanners(imgIdBaseBanner, bannerSettings); }, bannerSettings.pauseLength);

	}

	

	//-----------------------------------------------------
	// RotateBanners
	//-----------------------------------------------------
	function RotateBanners(imgIdBaseBanner, bannerSettings)
	{

		var randomPicBanner, banTmp;

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

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

		// Copy buffered image into the bottom frame
		newPicName = $(banBuf).attr("src");
		$(banBot).attr("src", $(banBuf).attr("src"));

		// Fade out the Top picture
		$(banTop).fadeTo(bannerSettings.fadeTime, .01, function()
		{

			// bring the bottom to the top
			banTmp = banTop;	banTop = banBot;	banBot = banTmp;

			// Change bottom image z-index above top image
			$(banBot).css("z-index", bannerSettings.zIndex + 0);
			$(banTop).css("z-index", bannerSettings.zIndex + 1);

			// Fade in the New bottom Pic
			$(banBot).fadeTo(0, 1);

			// Pick random Banner to display next and make sure is is different from the current pic
			randomPicBanner = GetNextRandomPicture(currentPic, banData.length);
	
			// Set the current pic to the random pic
			currentPic = randomPicBanner;
	
			// Load picture into buffer (image 2)
			$(banBuf).attr("src", "/images/topBanners/"+ banData[randomPicBanner].topImgPicture);

		});
	}


	//-----------------------------------------------------
	// GetNextRandomPicture
	//-----------------------------------------------------
	function GetNextRandomPicture(oldPic, amtPics)
	{
		var tmpRandomPic;

		// Pick random Banner to display next and make sure is is different from the current pic
		while(true)
		{
			// Get the random number
			tmpRandomPic = parseInt(Math.random() * amtPics);		
			
			// Check to see if random picture selected is out of range
			if (tmpRandomPic >= amtPics) tmpRandomPic = amtPics-1;

			// Break out of loop if randomly selected pic is not the same as the current pic
			if (oldPic != tmpRandomPic) break;
		}
		return tmpRandomPic;
	}


	//-----------------------------------------------------
	// GetBannerRotatorInfo
	//-----------------------------------------------------
	function GetBannerRotatorInfo(imgIdBaseBanner)
	{
		var banUrl, banStatus, banBuf, rndTmp;
		
		banBuf = "#" + imgIdBaseBanner + "2";

		// Get the url that the data is at
		banUrl = "/js/bannerRotator/!bannerRotator_ajax.php";
		$(function()
		{
			$.getJSON(banUrl, function(banDataTmp, banStatus)
				{
					// Put picture object array in global variable
					banData = banDataTmp;

					// Pick random Banner to display next and make sure is is different from the current pic
					rndTmp = GetNextRandomPicture(-1, banData.length);

					// Put random picture in picture buffer
					$(banBuf).attr("src", "/images/topBanners/"+ banData[rndTmp].topImgPicture);
				}
			);              
		});

	}	





})(jQuery);
