// JavaScript Document

$(document).ready(function() {
	
	var titles 					= [];
	var captions 				= [];
	var urls 					= [];
	var images 					= [];
	
	var currentImgNo			= 0;
	
	var url = "";

	$.loadXML(url, xmlCallback, pageId);
	
	function xmlCallback(xml)
	{
		
		if(!(xml instanceof jQuery))
		{
			xml = $(xml);
		}
	
		noOfImages = xml.find('TemporaryTable').length;
		
		xml.find('TemporaryTable').each(function(){
			titles.push($(this).find('Title').text());
			captions.push($(this).find('Caption').text());
			urls.push($(this).find('URL').text());
			images.push($(this).find('LargeFileName').text());
		});

		for(var i = 0; i <noOfImages ; i++){
			
			var imageChilds ="";
			imageChilds = "<li><img class='imgSet' src='"+ images[i] +"' title='<p>"+ titles[i] +"</p><h4>"+ captions[i] +"</h4>' border='0' /></a> </li>";
			//<div class='caption'><h2>"+ titles[i] +"</h2><h4>"+ captions[i] +"</h4></div>
			
			$('#imageHolder').append(imageChilds);
			
			
			
			/*if(i!=0){
				$('#imageHolder li:eq('+ i +')').animate({
					opacity:0
				},10);	
			}*/
		}
		
		$('#imageHolder li:eq(0)').css('top',0)
		
		$('#imageHolder').nivoSlider({
			pauseTime:5000,
			effect:'fold',
			pauseOnHover:false,
			directionNav:false,
			controlNav:false,
			beforeChange: function(){
					changeURL();
				}/*,
			beforeTitleChange: function(){
					cufonCreation();
				}*/
			
		});
		
		/*$('#imageHolder').children().prepend("<a href='"+ urls[i] +"'>");
		$('#imageHolder').children().prepend("</a>");*/
		
		changeURL();
		function changeURL(){
			$('#galleryURL').attr('href',urls[currentImgNo]);
			
			if(currentImgNo >= noOfImages-1){
				currentImgNo = 0;
			}else{
				currentImgNo++;	
			}
		}
		
		//cufonCreation();
		function cufonCreation(){
			
			Cufon.replace('.nivo-caption p p', { hover: false, fontFamily: 'Gill Sans MT'  });
			//Cufon.replace('.nivo-caption p h4', { hover: false, fontFamily: 'Klavika Rg'/*, textShadow: '1px 1px #666'*/ });
			
		}
		
		
		
		/*var content = "<h2>"+ titles[0] +"</h2><h4>"+ captions[0] +"</h4>";
		$('#imageHolder li:eq('+ 0 +')').children('.caption').append(content);
		$('#imageHolder').children().removeClass();*/
		
		
		//addText();
		/*
		var firstTitle = "<span class='heading01'>"+ titles[1] +"<br/> </span>"; 
		var secondTitle = "<span class='heading02'>"+ captions[1] +"</span>";
		
		Cufon.replace('#captions .heading01', { hover: true, fontFamily: 'Austin Roman' });
		Cufon.replace('#captions .heading02', { hover: true, fontFamily: 'Austin Roman' });
		*/
		
		$('.caption').animate({
				opacity:0.7
			},{queue:false});
			
		/*$('.caption').children().animate({
				opacity:1
			});*/
		
		var refreshIntervalId;
		
		var varName = function(){
			animateNext();
		};
		
		//defineInterval();
		function defineInterval(){
			refreshIntervalId = setInterval(varName, 5000);
		}
		
		
		


		function animateNext(){
			var preImgNo = currentImgNo;
			
			$('#imageHolder li:eq('+ preImgNo +')').animate({
				opacity:0
			},500,function(){
				$('#imageHolder li:eq('+ preImgNo +')').css("top","488px");
			});
			
			if(currentImgNo == 0){
				currentImgNo = noOfImages-1;
			}else{
				currentImgNo--;
			}
			
			$('#imageHolder li:eq('+ currentImgNo +')').css("top","0");
			$('#imageHolder li:eq('+ currentImgNo +')').animate({
				opacity:1
			},500);
			
		}
		

	}

});
