var slideshowViewer;

function SlideshowViewer () {

	var html = '<div id="slideshow_backdrop"></div>';
	var img = '<img src="/js/slideshow/img/close.png" />';
	var ctrl = '&nbsp;<a href="#" id="slideshow_close">'+img+'</a>';
	img = '<img src="/js/slideshow/img/prev.png" />';
	ctrl += '<br /><a href="#" id="slideshow_prev">'+img+'</a>';
	img = '<img src="/js/slideshow/img/next.png" />';
	ctrl += '<a href="#" id="slideshow_next" style="margin-right:-2px;">'+img+'</a>';
	ctrl = '<div id="counter">1/7</div><div id="controls">'+ctrl+'</div>';
	ctrl = '<div id="right">'+ctrl+'</div>';
	var sf = '<div id="slideshow_captions"> </div>'+ctrl;
	sf = '<div id="slideshow_foot">'+sf+'</div>';
	var sc = '<div id="slideshow_content"><div id="slideshow_loading"></div><div id="slideshow_pictures"></div>'+sf+'</div>';
	html += '<div id="slideshow_center">'+sc+'</div>';
	
	$('body').append(html); 

	$('#slideshow_backdrop').click( function() {
		event.stopPropagation();
		slideshowViewer.hide();
	});
	$('#slideshow_center').click( function() {
		event.stopPropagation();
		slideshowViewer.hide();
	});

	$('#slideshow_content').click( function(event) {
		event.stopPropagation();
		slideshowViewer.nextSlide();
	});
	$('#slideshow_close').click( function(event) {
		event.stopPropagation();
		slideshowViewer.hide();
	});
	$('#slideshow_prev').click( function(event) {
		event.stopPropagation();
		slideshowViewer.prevSlide();
		return false;
	});
	$('#slideshow_next').click( function(event) {
		event.stopPropagation();
		slideshowViewer.nextSlide();
		return false;
	});
	
	this.currSlideshowId = "";
	this.slidesCount = 0;
	this.currSlideIdx = 0;
	this.currCaptionIdx = 0;
	this.captions = [];
	
	this.loadSlideshow = function(slideshowId, imgidx) {
		if(!imgidx) imgidx = 0;
				
		var rels = $("a[rel='slideshow["+slideshowId+"]']");
		var imghrefs = [];
		rels.each(function() {
			imghrefs.push($(this).attr('href'));
		});
		
		var pictHtml = "";
		var captHtml = "";
		var ic = imghrefs.length;
		for(var i=0; i<ic; i++) {
			var imgid = slideshowId+"_slide_"+(i+1);
			var img = '<img id="'+imgid+'" src="'+imghrefs[i]+'" />'; // class="'+imgclasses[i]+'" 
			pictHtml += '<div class="slideshow-picture"><div>'+img+'</div></div>';
			
			var captid = slideshowId+"_caption_"+(i+1);
			var capt = $("#caption_"+slideshowId+"_"+i).html();
			if(capt) {
				captHtml += '<div class="caption" id="'+captid+'" >'+capt+'</div>';
			} else {
				captHtml += '<div class="caption" id="'+captid+'" ></div>';
			}
		}

		$('#slideshow_pictures').empty();
		$('#slideshow_pictures').append(pictHtml);
		$('#slideshow_captions').empty();
		$('#slideshow_captions').append(captHtml);

		this.currSlideshowId = slideshowId;
		this.slidesCount = ic;
		this.currSlideIdx = imgidx;
		this.currCaptionIdx = imgidx;
		this.setCounter();
		
		var bhg = $("body").height();
		bhg = Math.max(bhg, $(window).height);
		$("#slideshow_backdrop").css("height", bhg+"px");

				
		imgid = slideshowId+"_slide_"+(imgidx+1);
		captid = slideshowId+"_caption_"+(imgidx+1);
		$('#'+imgid).load(function() {
			$('#'+imgid).fadeIn(500);
			$('#'+captid).fadeIn(500);
			$('#slideshow_loading').fadeOut(500);
		});
	
		$('#slideshow_backdrop').fadeIn(500);
		$('#slideshow_center').fadeIn(500);
	};
		
	this.nextSlide = function() {
		if(this.slidesCount > 1) {
			this.showSlide((this.currSlideIdx+1) % this.slidesCount);
		}
	}
	this.prevSlide = function() {
		if(this.slidesCount > 1) {
			this.showSlide((this.currSlideIdx-1+this.slidesCount) % this.slidesCount);
		}
	}
	
	this.showSlide = function(newSlideIdx) {
		var oldIdx = this.currSlideIdx;
		this.currSlideIdx = newSlideIdx;
		$("#"+this.currSlideshowId+"_slide_"+(oldIdx+1)).fadeOut(500);
		var oldCaption = $("#"+this.currSlideshowId+"_caption_"+(this.currCaptionIdx+1)).html();
		var newCaption = $("#"+this.currSlideshowId+"_caption_"+(this.currSlideIdx+1)).html();
		if(oldCaption != newCaption) {
			$("#"+this.currSlideshowId+"_caption_"+(oldIdx+1)).fadeOut(75);
			this.currCaptionIdx = this.currSlideIdx;
		}
		var code = '$("#'+this.currSlideshowId+'_slide_'+(this.currSlideIdx+1)+'").fadeIn(500);';
		if(oldCaption != newCaption) {
			code += '$("#'+this.currSlideshowId+'_caption_'+(this.currSlideIdx+1)+'").fadeIn(1500);';
		}
		window.setTimeout(code, 50);
		this.setCounter();
	}

	this.setCounter = function() {
		$('#slideshow_foot #counter').empty();
		if(this.slidesCount > 1) {
			$('#slideshow_foot #counter').append((this.currSlideIdx+1)+"/"+this.slidesCount);
		}
	};
	
	
	this.hide = function() {
		$('#slideshow_backdrop').fadeOut(800);
		$('#slideshow_center').fadeOut(300, function() {
			slideshowViewer.unload();
		});
	};
	
	this.unload = function() {
		$('#slideshow_pictures').empty();
		$('#slideshow_captions').empty();
		this.currSlideshowId = "";
		this.slidesCount = 0;
		this.currSlideIdx = -1;
		this.captions = [];
		$('#slideshow_loading').fadeIn(0);
	};
	
}


