/*-------------------------------------------------------------------------
imgRotator.js
Author: Andy Sherman
Company: Visual Blaze
Client: EverEffect
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
-----------------------------------------------------------------------*/

//initialize variables
var imgPath_prefix = "/images/infographics/";
var xmlPath = "../xml/infoGraphicRotator.xml.php";
var imgParent = "#infoGraphic";
var textParent = "#infoGraphic";
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();
		
	//parse xml to obtain data and run postParse function
	  $.ajax({
	    type: "GET",
	    url: xmlPath,
	    dataType: "xml",
	    success: parseXml
	  });	
});

function parseXml(xml) {
	//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 -> 2d Associative array
	//declare parent array
	var dataArray = new Array();
	
	//iterate through all image nodes
	$(xml).find("banner").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
	var interval = setInterval(function(){switchText(dataArray); parameter = null},9000);
	
	//asssign event handlers for clicking menu items
	$("p.infoMenu a span").each(function(i) {
		//append number to span as class to use for button id'ing on menu
		$(this).addClass(""+i);
	});
	$("p.infoMenu a")
	.attr('href', 'javascript:void(0);')
	.bind('click',function(event){
		count = $(this).children('span').attr("class");
		switchText(dataArray)
		clearInterval(interval)
		interval = setInterval(function(){switchText(dataArray); parameter = null},9000)
	});
}

function switchText(dataArray) {
	//traverse to each text element and change with array element on count var
	
	//run sIFR method replacement to 'redraw' the title with new text -> otherwise reverts to reg font
	sIFR.replacements[textParent+" h3"][0].replaceText(dataArray[count]["title"]);
	$(textParent+" p.subText").text(dataArray[count]["text"]);
	$(textParent+" p.readmore").append('<a href="'+dataArray[count]["linkURL"]+'">'+dataArray[count]["linkText"]+'</a>');
	$(textParent+" img").attr("src",imgPath_prefix+dataArray[count]["file"]).attr("alt",dataArray[count]["title"]);
	
	//change menu to new element -> need an initialization element?
	$("p.infoMenu a").removeClass("active");//remove all active classes from menu elements
	var actMenu = $("p.infoMenu a")[count];//get new active menu item
	$(actMenu).addClass("active");//assign class to new active element
	
	//increment or reset count node
	if(count != (dataArray.length -1)) {
		count++;
	}else{
		count=0;
	}
}
