// JavaScript Document

var myXMLImages ="_xml/animalFacts_images.xml";

$(document).ready(function() {
	var loadXMLImages =  new LoadXMLImages(myXMLImages);
	loadXMLImages.ajaxGet();
});

function LoadXMLImages(myXMLImages) {
	var self = this;
	//load xml file
	
	this.ajaxGet = function() { 
		$.get(myXMLImages, function(dataImages){ self.parseXML(dataImages); });
	}
	
	this.parseXML = function(dataImages) {
		var urlText = new Array();
		//[0].childNodes[num].length;
		var i = 0;
		var num = Math.floor((Math.random() * 4));
		
		$(dataImages).find('urlGroup').each(function() {
			urlText[i] =  new Array();
			var j = 0;
			
			$(this).find('url').each(function() {
				urlText[i][j] = $(this).text();
				j++;
			});
			
			i++;
		});
		
		self.display(urlText, num);
		
		
		$('.cycleImages').bind('mouseenter', function() {
			$(this).css({ color: '#0093ff', background: 'url(images/cycleImages_o.gif) no-repeat' });
		});
		
		$('.cycleImages').bind('mouseleave',function() {
			$(this).css({ color: '#2D80BD', background: 'url(images/cycleImages.gif) no-repeat' });
		});
		
		$('.cycleImages').click(function() {
		
			if(num < urlText.length-1){
				num++;
			} else {
				num = 0;
			}
			
			self.display(urlText, num);
		});
	}
	
	this.display = function(urlText, num) {
		//write to the thumbs div
		var xmlImages = $("#xmlImages");
		xmlImages.stop().hide();
		xmlImages.stop().slideDown(2500).html(
			'<img src="' + urlText[num][0] + '" alt="animal pictures" />' +
			'<img src="' + urlText[num][1] + '" alt="animal pictures" />' +
			'<img src="' + urlText[num][2] + '" alt="animal pictures" />' +
			'<img src="' + urlText[num][3] + '" alt="animal pictures" />' +
			'<img src="' + urlText[num][4] + '" alt="animal pictures" />'+
			'<img src="' + urlText[num][5] + '" alt="animal pictures" />'
		); 
	}
	
}