/*-------------------------------------------------------------------------
imgRotator.js
Author: Andy Sherman
Company: Visual Blaze
Client: Dekalb
Descr: Index page image rotator
- pulls all images + data from xml file
- assigns <img> with JS to specific container
- rotates images
- seperate rotator rotates text in banner from xml file

resources:
http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery
http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/

-----------------------------------------------------------------------*/

//initialize variables
var imgPath_prefix = "/images/flash/";
var xmlPath = "../xml/imgRotator.xml.php";
var imgParent = "#flash p:first-child";
var textParent = "#flash div";
var count = 0;//initialie count var for text rotation

$(document).ready(function() { 
	
	//removing default image so isn't duplicate with corresponding xml based image
	$(imgParent + ' img').remove();
	
	//set opacity on title banner bg
	$("#flash div").animate({ opacity: 0.85 }, 1 ); //Set Opacity
	
	//parse xml to obtain data and run postParse function
	  $.ajax({
	    type: "GET",
	    url: xmlPath,
	    dataType: "xml",
	    success: parseXml
	  });	
});

function parseXml(xml) {
	//loop through xml to obtain images and append to image container
	$(xml).find("image").each(function() {
		//append images to imagecontainer
		$(imgParent).append('<img src="'+imgPath_prefix+$(this).find("file").text()+'" alt="'+$(this).find("title").text()+'" />');
	});
	//call function to animate images
	imgAnimate(imgParent);
	//call function to animate text
	textAnimate(xml)
}

function imgAnimate(selector) {
	//rotation code for images
	$(selector).innerfade({ 
		speed: 'slow', 
		timeout: 9000, 
		type: 'sequence', 
		containerheight: '271px'
	});
}

function textAnimate(xml) {
	//build 2d array of information
	//declare parent array
	var dataArray = new Array();
	
	//iterate through all image nodes
	$(xml).find("image").each(function(i) {
		//tempVars from xml
		var file = $(this).find("file").text();
		var title = $(this).find("title").text();
		var text = $(this).find("text").text();
		var linkURL = $(this).find("link").attr("url");
		var linkText = $(this).find("link").text();
		
		//push associative array [object] into dataArray
		dataArray[i]= {file: file, title: title, text: text,linkURL: linkURL,linkText: linkText};
	});
	
	//call text function to initialize first first from xml file
	switchText(dataArray);
	//set timer to rotate all subsequent text changes on interval
	setInterval(function(){switchText(dataArray); parameter = null},9000);
}

function switchText(dataArray) {
	//traverse to each text element and change with array element on count var
	$(textParent+" h3").text(dataArray[count]["title"]);
	//$(textParent+" p").text(dataArray[count]["text"]);
	$(textParent+" p").text("");
	$(textParent+" p").append('<a href="'+dataArray[count]["linkURL"]+'">'+dataArray[count]["linkText"]+'</a>');
	//increment or reset count node
	if(count != (dataArray.length -1)) {
		count++;
	}else{
		count=0;
	}
}