$(document).ready(function(){

	var titles 					= [];
	var captions 				= [];
	var urls 					= [];
	var images 					= [];
	
	var currentImgNo			= 0;
	var preImgNo;
	
	var $imageHolder = $('#imageHolder');
	var $bulletNavigation = $('#bulletNavigation');
	
	var url = "";
	//url = "http://w3svc982.staging2-sl.netstarter.com.au:81";
	
	
	
	$.loadXML(url, xmlCallback, pageId);
	function xmlCallback(xml)
	{
		

		if(!(xml instanceof jQuery))
		{
			xml = $(xml);
		}
		noOfImages = xml.find('TemporaryTable').length;
		//currentImgNo = noOfImages;
		
		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 ="";

			if(urls[i] == ""){
				imageChilds = "<li><img src='"+ images[i] +"' border='0' /><div class='caption'><h4>"+ titles[i] +"</h4><p>"+ captions[i] +"</p></div></li>";
			}else{
				imageChilds = "<li><a href='"+ urls[i] +"'><img src='"+ images[i] +"' border='0' /></a><div class='caption'><h4>"+ titles[i] +"</h4><p>"+ captions[i] +"</p><div class='readMoreLink'><a href='"+ urls[i] +"'><img src='Images/read-more.png' border ='0' /></a><div></div></li>";
			}
			
			var bulletChilds = "<li><a class='changeSlide' href='javascript:void(0);' rel='"+ i +"'><img src='Images/blank.png' border='0' /></a></li>";
			
			
			$imageHolder.append(imageChilds);
			$bulletNavigation.append(bulletChilds);
			
			$bulletNavigation.children().eq(0).addClass('selected')
			
			if(i!=0){
				//$('#imageHolder li:eq('+ i +')').animate({
				/*$imageHolder.children().eq(i).animate({
					opacity:0,
					top:252
				},10);	*/
				$imageHolder.children().eq(i).hide();
				/*$imageHolder.children().eq(i).css('top',252);*/	
			}
		}
		
		/*Cufon.replace('h4', { hover: true, fontFamily: 'FSAlbertBold' });
		Cufon.replace('div.caption p', { hover: true, fontFamily: 'StoneSans' });*/
		Cufon.replace('h4', { fontFamily: 'TitilliumMaps29L 400', color:'#fff'});
		
		var documentWidth = $(window).width();

		$bulletNavigation.css("left",700 - 20*noOfImages);
		$bulletNavigation.css("width",23*noOfImages);
		
		var refreshIntervalId;
		var varName = function(){
			preImgNo = currentImgNo;
			changeSlideNumber();
			animateNext();
		};
		defineInterval();
		function defineInterval(){
			refreshIntervalId = setInterval(varName, 5000);
		}
		
		
		$('.changeSlide').click(function(){
			preImgNo = currentImgNo;
			currentImgNo = $(this).attr('rel');
			if(preImgNo != currentImgNo){
				animateNext();
				clearInterval(refreshIntervalId);
				defineInterval();
			}
				
		});


		function animateNext(){
			
			$imageHolder.children().eq(preImgNo).css("z-index",10);
			$imageHolder.children().eq(preImgNo).fadeOut('slow');
			$imageHolder.children().eq(currentImgNo).css("z-index",0);
			$imageHolder.children().eq(currentImgNo).show();
			
			//$('#imageHolder li:eq('+ preImgNo +')').animate({
			//$imageHolder.children().eq(preImgNo).fadeOut()
			/*$imageHolder.children().eq(preImgNo).fadeOut('slow',function(){
				//$imageHolder.children().eq(preImgNo).css("top","-252px");
				console.log('anim comp')
				$imageHolder.children().eq(preImgNo).css("z-index",0);
			});*/
			
			/*$('#bulletNavigation li:eq('+ preImgNo +')').removeClass();
			$('#bulletNavigation li:eq('+ currentImgNo +')').addClass('selected');*/

			$bulletNavigation.children().eq(preImgNo).removeClass();
			$bulletNavigation.children().eq(currentImgNo).addClass('selected');
			
			//$imageHolder.children().eq(currentImgNo).css("top","0");
			//$imageHolder.children().eq(currentImgNo).css("z-index",999);
			/*$imageHolder.children().eq(currentImgNo).fadeIn('slow',function(){
				$imageHolder.children().eq(preImgNo).hide()
			});*/
			
		}
		
		function changeSlideNumber(){
			if(currentImgNo < noOfImages-1){
				currentImgNo++;
			}else{
				currentImgNo = 0;
			}
		}
		

	}



//coloboxInitialize();


	});
