/**
 * JavaScript to replace Flash banner panels
 * dependent on
 * jQuery 1.4.2+ (jquery-1.4.2.min.js)
 * cufon 1.09+ (cufon-yui.js)
 * cufon generated font file (*.font.js)
 * jQuery Cycle plugin 2.88+ (jquery.cycle.all.min.js)
 **/
var jQuery = jQuery.noConflict();
//init get XML
jQuery(function(){
	jQuery.ajax({ 
		type: 'GET', 
		url: slideShowParams.ssXMLPath, // path to XML file 
		dataType: 'xml', 
		success: function(xml){
			buildPanels(xml);
		}
	 });
});


//add text nodes and main image src in HTML from XML
function buildPanels(xml){
	var bannerNode, i, slideshowImage, slideshowTitle, slideshowDescription, rootPathToImages;
	var whichPage =  findPage(xml);
	var pageNode = whichPage.page;
	var pageChildrenLength = jQuery(xml).find(pageNode).children().length;
	
	for(i=0; i < pageChildrenLength; i++){		
		buildPanelHTML(i,pageNode);
					
		bannerNode = pageNode + ' banner';
		
		bannerNode = bannerNode + i;
		
		slideshowImage = jQuery(xml).find(bannerNode).children('background');
		slideshowTitle = jQuery(xml).find(bannerNode).children('title');
		slideshowDescription = jQuery(xml).find(bannerNode).children('description');
		rootPathToImages = slideShowParams.ssImagePath + pageNode + '/'; //build path to images
	
		jQuery('#panel_' + i + ' img').attr('src', rootPathToImages + slideshowImage.text());
		jQuery('#panel_' + i + ' .slideshowTitle').text(slideshowTitle.text());
		jQuery('#panel_' + i + ' .slideshowDescription').text(slideshowDescription.text());		
	}
	
	Cufon.replace('.slideshowText h1', {color: slideShowParams.ssTextTitleColor});
	Cufon.replace('.slideshowText span', {color: slideShowParams.ssTextDescColor});
	buildSlideShow(rootPathToImages,pageNode);
}


//determine page based on class name in body tag
//and attributes set in XML pageName nodes
function findPage(xml){
	var folderName, foundPage, searchVar; 
	var pageClassNames = jQuery(xml).find('pageName').length;
	var bodyClasses = jQuery('body').attr('class');
	var classArray = bodyClasses.split(' ');
		
		for (i = 0; i < classArray.length; i++) {		
			jQuery(xml).find('pageName').each(function(){	
				searchVar = jQuery(this).attr('bodyClassSearchVar');
				folderName = jQuery(this).attr('mapToFolder');
				
				if(classArray[i].match(searchVar)){
					foundPage = true;
					return false; 
				}		
			});
			
			if (foundPage){break;}
		}
		
		return {page: folderName}
}


//build panels dynamically based on number of child nodes each page has in XML
function buildPanelHTML(i,pageNode){
	var slideshowParent = document.getElementById('slideshow');
	var panelDiv = document.createElement('div');
	
	jQuery('#slideshow').attr('class',pageNode);
	panelDiv.setAttribute('id','panel_' + i);
	panelDiv.innerHTML = '<img /><span class="slideshowText"><h1 class="slideshowTitle"></h1><span class="slideshowDescription"></span></span>';
	slideshowParent.appendChild(panelDiv);
}


//slideshow functionality
function buildSlideShow(rootPathToImages,pageNode){
	var thumbRootPath = rootPathToImages + pageNode + '_thumb';  //path to thumbs
	jQuery('#slideshow').cycle({ 
	    fx: slideShowParams.ssCycleProps.fx, 
	    speed: slideShowParams.ssCycleProps.speed,
		timeout: slideShowParams.ssCycleProps.timeout,
	    pager: '.slideshowNav', 
	    slideExpr: 'div',
		cleartype: 1,
		cleartypeNoBg: 1,		
	    pagerAnchorBuilder: function(idx, slide) {
			idx = idx + 1;
	        return '<span><a href="#"><img src="' + thumbRootPath + idx + '.gif" /></a></span>';
	    } 
	});
}
