$(document).ready(function(){

	// jQuery Cycle Plugin - Option Reference
	// http://malsup.com/jquery/cycle/options.html
	$(function() { 
		
		$('#caption > a').html($('#slideshow img').attr("alt") + '&nbsp;<span>&raquo;</span>'); 
		$('#caption > a').attr("href",$('#slideshow img').attr("goto"));
		
		// retrieve list of slides from server 
		//var remoteUrl = 'http://ihconcept.com/develop/slides.json' //path to json file
		var remoteUrl = '/slides.txt' //path to json file
		
		$.getJSON(remoteUrl, function(data){ 
			
			response = {
					urls: [],
					capt: [],
					hrefs:[]
				};
			
			$.each(data.results.items,function(i,item) {
					if (item.url != "") {
						response.urls[i] = item.url;
					}
					response.capt[i] = item.caption;
					response.hrefs[i] = item.href;
					
				});
			startSlideshow(response);
		});
		
		function startSlideshow(response) {	 
			var totalSlideCount = 1 + response.urls.length; 
			//alert(totalSlideCount);
			 
			var $slideshow = $('#slideshow'); 
			 
			// markup contains only a single slide; before starting the slideshow we  
			// append one slide and prepend one slide (to account for prev/next behavior) 
			//$slideshow.prepend('<img src="'+response.urls.pop()+'" alt="'+response.capt.pop()+'" goto="'+response.hrefs.pop()+'" />'); 
			for (v=0; v < totalSlideCount-1; v++){
			    $slideshow.append('<img src="'+response.urls.shift()+'" alt="'+response.capt.shift()+'" goto="'+response.hrefs.shift()+'"  />'); 
	        }
			// start slideshow 
			$('#slideshow').cycle({ 
				fx: 'fade', 
				startingSlide: 0,  // start on the slide that was in the markup 
				timeout:  5000, 
				speed:    1000, 
				prev:    '#prev', 
				next:    '#next', 
				before:   function() {
					$('#caption > a').html(this.alt + '&nbsp;<span>&raquo;</span>'); 
					$('#caption > a').attr("href",$(this).attr("goto"));
					onBefore;
				}
				/*
				after:     function() {
					$('#caption').html(this.alt);
				} 
				*/
			}); 
			 
	 
			function onBefore(curr, next, opts, fwd) { 
				//alert(next.src);
				// on Before arguments: 
				//  curr == DOM element for the slide that is currently being displayed 
				//  next == DOM element for the slide that is about to be displayed 
				//  opts == slideshow options 
				//  fwd  == true if cycling forward, false if cycling backward 
					 
				// on the first pass, addSlide is undefined (plugin hasn't yet created the fn yet) 
				if (!opts.addSlide) 
					return; 
					 
				// have we added all our slides? 
				if (opts.slideCount == totalSlideCount) 
					return; 
				
				// shift or pop from our slide array  
				var nextSlideSrc = fwd ? slides.shift() : slides.pop(); 
				 
				// add our next slide 
				
				opts.addSlide('<img src="'+nextSlideSrc+'" />', fwd == false); 
			}; 
		}; 
		
	}); 
		
});
