// define empty variables for use in image cycling
var itemCount = 0;
var itemList;

// Banner Loader and Rotator
$(document).ready(
	function(){
		// hide containing image <DIV>
		$('#promo .promo_wrap div').hide();
		// establish action to click and rotate image
		$('#promo_more').click(function(){							
			loadImage();
		});
		 // slide-down images on homepage
		$('#home_nav a').hover(
			function(){
					$(this).animate({top:'0px'},{queue:false,duration:500});
			}, 
			function(){
					$(this).animate({top:'245px'},{queue:false,duration:500});
			}
		);
	}
)

// function to load in array of images from specified location
function fetchBanner(url) {
	// create call to create array of random images
	$.ajax({
		type: 'GET',
		url: url, // our path to the XML file
		dataType: 'xml', // output will be in XML format
		error: function(){
			alert('Failed to load XML');
		},
		success: function(xml){ // take the result JSON object and...
			// define new array variable
			var itemArray = new Array();
			// ...loop through the object...
			$(xml).find('item').each(function(){
				// define nested array of item attributes
				var itemVals = {};
				// loop through nodes and assign array values
				itemVals.itemID = $(this).attr('id');
				itemVals.itemImage = $(this).attr('imageurl');
				itemVals.itemLinkVal = $(this).find('link').attr('href')
				itemVals.itemLinkClass = $(this).find('link').attr('class');
				itemVals.itemLinkText = $(this).find('link').text();
				itemVals.itemDesc = $(this).find('description').text();
				// add finished array to the parent array
				itemArray.push(itemVals);
				// alert(itemArray);
			});
			// assign local array values to global array values
			itemList = itemArray;
		},
		complete: function(){
			// when the array is fully populated, call the loadImage function
			loadImage();
		}
	});
}

// function to load image from populate array into page
function loadImage() {
	
	// Generate random number within range of array length
	var randomNum = Math.floor(Math.random()*(1+itemList.length))
	
	// hide rotation button action until next image has loaded
	$('#promo_more').fadeOut(500);
	// fade out related link
	$('.promo_link').fadeOut(500);

	
	// check to see if current counter exists within array
	if(!itemList[randomNum]) {
		// if not, reset to 0
		randomNum = 0;
	}
	
	// assign array values for current item to variables
	itemID = itemList[randomNum].itemID;
	itemImage = itemList[randomNum].itemImage;
	itemLinkVal = itemList[randomNum].itemLinkVal
	itemLinkClass = itemList[randomNum].itemLinkClass
	itemLinkText = itemList[randomNum].itemLinkText;
	itemDesc = itemList[randomNum].itemDesc;
	
	// build image location
	itemImageLoc = abspath + itemImage;
	
	// fadeout the containing <DIV> and subsequent related elements
	$('#promo .promo_wrap div').fadeOut(500, function(){
		// create an image object									 
		var img = new Image();
		// wrap our new image in jQuery, passing it's attribute for preloading, and then fire a function on the load event
		 $(img).attr({ 
			src: itemImageLoc,
			height: 490,
			width: 838,
			alt: itemDesc
		}).load(function(){
			// clear the containing <DIV> of whatever is inside it and append the new image
			$('#promo .promo_wrap div').empty().html(this);
			// clear the item link and append the new link, as well as a unique class
			$('.promo_link').empty().text(itemLinkText).removeClass().addClass(itemLinkClass+ ' promo_link').attr('href',baseurl+itemLinkVal).fadeIn(1000);
			// fade in the finished container
			$('#promo .promo_wrap div').fadeIn(1000, function(){
				itemCount++;
				$('#promo_more').fadeIn(500);
			});
		});									 
	});	
}
