BROWSER = '';
var S3PATH = "http://localhost:8888/sites/sprint_center/www/themes/default/s3/";

function loadSlideshow(id,target,opts){
	//Setup loader and target container
	$(target).append('<div class="slideshow"><div class="loading"></div></div>');

	//Load JSON data for slideshow
	$.ajax({
		//url: ROOT+'ajax/slideshow/'+id,
		//local testing //url: 'http://localhost:8888/sites/constantcenter/www/index.php/ajax/slideshow/'+id,
		url: 'http://constantcenter.carbonhouse.com/ajax/slideshow/'+id,
		data: {callback:'parseSlideshowData'},
		dataType: 'jsonp'
	});
}

function parseSlideshowData(json){
	buildSlideshow(json,'#slideshow_'+json.slideshow.id+' .slideshow');
}


function buildSlideshow(data,target,opts){
	var slide_html = '',slides = '', img_url = '';
	var opts = (!opts)? {} : opts;
	var bol_thumbs = (!opts || !opts.thumbs)? false : true;
	var bol_captions = (!opts || !opts.captions)? false : true;

	var slides = data.slides;
	opts.autoplay = (slides.length > 1)? (null != opts.autoplay)? opts.autoplay : true : false;

	//Loop through slides to set variables
	for (var i=0; i < slides.length; i++) {
		img_url = (slides[i].media_file_name != '')? slides[i].media_file_name : 'img/placeholder.jpg';
		link = (slides[i].media_link != '')? slides[i].media_link : '';
		var thumb_url = (slides[i].media_file_custom_thumb != '')? slides[i].media_file_custom_thumb : (slides[i].media_file_thumb != '')? slides[i].media_file_thumb : 'img/thumb.jpg';
		var thumb_html = (bol_thumbs)? '<img src="'+thumb_url+'" alt="'+slides[i].media_title+'/>' : slides[i].media_title;
		slide_html += '<li>';
		slide_html += '<a class="thumb thumb_'+slides[i].media_file_type+'" href="'+img_url+'" title="'+slides[i].media_title+'">'+
								thumb_html+
							'</a>';

		if (slides[i].media_link != '') slide_html += '<a href="'+slides[i].media_link+'" rel="imglink" class="slide_link">Link</a>';
		slide_html += '</div>';
		slide_html += '</li>';
	};
		$(target).append('<div class="slide"><a href="'+link+'"><img src="'+img_url+'" /></a></div>');
		$(target).append('<div class="imglink"></div>');
		$(target).append(
			'<div class="thumbs_holder">'+
				'<ul class="thumbs noscript">'+
					slide_html +
				'</ul>'+
			'</div>'
		);

		// setupSlideshow(target,opts.autoplay,7000,slides.length);
}


function setupSlideshow(slideshow,autoplay,delay,slideCount){
	var gallery = $(slideshow+' '+'.thumbs_holder').galleriffic({
	    delay:                     delay, // in milliseconds
	    imageContainerSel:         slideshow+' .slide', // The CSS selector for the element within which the main slideshow image should be rendered
	    controlsContainerSel:      slideshow+' .controls', // The CSS selector for the element within which the slideshow controls should be rendered
	    captionContainerSel:       slideshow+' .caption_holder', // The CSS selector for the element within which the captions should be rendered
			videoContainerSel:       	 slideshow+' .video', // The CSS selector for the element within which should be shown when an image is loading
			linkContainerSel:       	 slideshow+' .imglink', // The CSS selector for the element within which should be shown when an image is loading
	    loadingContainerSel:       slideshow+' .loading', // The CSS selector for the element within which should be shown when an image is loading
	    renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
	    renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
	    enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	    enableKeyboardNavigation:  false, // Specifies whether keyboard navigation is enabled
	    autoStart:                 autoplay, // Specifies whether the slideshow should be playing or paused when the page first loads
	    syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	    defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
	    onSlideChange: function(prevIndex, nextIndex) {
											if(this.data[nextIndex].caption.find('a[rel="imglink"]').attr('href')){
												var linkURL = this.data[nextIndex].caption.find('a[rel="imglink"]').attr('href');
												var linkTarget = this.data[nextIndex].caption.find('a[rel="imglink"]').attr('target');
												displaySlideLink(this.linkContainerSel,linkURL,linkTarget,true);
											} else {
												displaySlideLink(this.linkContainerSel,'',linkTarget,false);
											}
									 } // accepts a delegate like such: function(prevIndex, nextIndex) { ... }

	});
}

function displaySlideLink(holder,url,target,show){
	//alert(holder);
	$(holder).toggle(show);
	$(holder).html('');
	if(show && url) {
		$(holder).html('<a href="'+url+'" id="imglink" target="'+target+'"></a>');
	}
}


