

var sCurRefID;

$(document).ready(function(){
	
	//Referenzen
	$(".refbutton").bind('click', function(){
											
											sCurRefID = $(this).attr('id') + '-view';	
											
											});
	
	
	$(".refbutton").overlay({ top: '7%',  
					expose: { 
						color: '#000000', 
						loadSpeed: 200, 
						opacity: 0.55
					},
					onBeforeLoad: function(){
						setReference(sCurRefID, 'next')
					}, 
					close: '#reference-close-button'
					});
});

function setReference(sReferenceID, sDirection) {
	$('div.simple-overlay div.reference-view').hide();
	
	$('#' + sReferenceID).show();
	$('div.simple-overlay img').removeClass('active');
	$('div.simple-overlay img').hide();
	
	if(sDirection == 'next') {
		iImageID = 0;
	} else {
		iImageID = $('#' + sReferenceID + ' div.gallery img').length - 1;
	}
	
	setCounter();
	$('#' + sReferenceID + ' div.gallery img').eq(iImageID).fadeIn(500, function(){
																	$(this).addClass('active');
																	setCounter();
																				 });
	
	sCurRefID = sReferenceID;
}

function nextImage() {
	if($('#' + sCurRefID + ' img.active').next().length != 0) {
	
		$('#' + sCurRefID + ' img.active').next().fadeIn(500, function(){
																	$(this).prev().hide().removeClass('active');
																	$(this).addClass('active');
																	setCounter();
																});
	} else {
		$('#' + sCurRefID + ' img.active').hide();
		if($('#' + sCurRefID).next('.reference-view').length != 0) {
			setReference($('#' + sCurRefID).next('.reference-view').attr('id'), 'next');
		} else {
			setReference($('div.simple-overlay .reference-view').eq(0).attr('id'), 'next');
		}
	}
}

function prevImage() {
	if($('#' + sCurRefID + ' img.active').prev().length != 0) {
	
		$('#' + sCurRefID + ' img.active').prev().fadeIn().addClass('active').next().fadeOut(500, function(){
																	$(this).removeClass('active');
																	$(this).hide();
																	setCounter();
																});
	} else {
		if($('#' + sCurRefID).prev('.reference-view').length != 0) {
			setReference($('#' + sCurRefID).prev('.reference-view').attr('id'), 'prev');
		} else {
			setReference($('div.simple-overlay .reference-view').eq($('div.simple-overlay .reference-view').length - 1).attr('id'), 'prev');
		}
	}
}

function setCounter() {
	iNumber = $('#' + sCurRefID + ' div.gallery img').length;
	iCount = parseInt($('#' + sCurRefID + ' div.gallery img.active').attr('name'));
	sImgName =  $('#' + sCurRefID + ' div.gallery img.active').attr('imgcnt');

	if(isNaN(iCount)) {
		iCount = 1;
	}
	if (sImgName != null) {
		$('#imgcnt').html('Bildnummer: ' + sImgName);
	} else {
		$('#imgcnt').html();
	}
	$('#reference-navi-status').html(iCount + ' von ' + iNumber);
	

}
	