// Gallery-Einstellungen
var galleryOK = false;
var galleryPictW = 712 + 10; 	// width + margin-right
var galleryPictH = 330; 		// height
var galleryPreviewW = 100 + 2; 	// width + margin-right
var galleryGroupSize = 7;		// Anzahl sichtbare Vorschaubilder
var galleryGroupWidth;
var gallerySliderSpeed = 500;
var maxPicts; var curPict; var maxGroups; var curGroup;

$(document).ready(function() {

	if ($('div.galleryWrapper').length != 0) galleryOK = true;

	// Ermittlung Anzahl Bilder
	if (galleryOK) initGallery(); 

	// Preview buttons
	$('div.controlPreviewLeft').bind({
		click: function(){ showGroup(-1); },
		mouseenter: function(){ if (curGroup > 1) { $(this).fadeTo(0,1); $(this).css("cursor","pointer"); } },
		mouseleave: function(){ if (curGroup > 1) { $(this).fadeTo(0,0.20); $(this).css("cursor","auto"); } }
	});
	$('div.controlPreviewRight').bind({
		click: function(){ showGroup(1); },
		mouseenter: function(){ if (curGroup < maxGroups) { $(this).fadeTo(0,1); $(this).css("cursor","pointer"); } },
		mouseleave: function(){ if (curGroup < maxGroups) { $(this).fadeTo(0,0.20); $(this).css("cursor","auto"); } }
	});

	// Picture buttons
	$('div.controlPictLeft').bind({
		click: function(){ if (curPict > 1) showPictByDirection(-1); },
		mouseenter: function(){ $(this).css("cursor","pointer"); },
		mouseleave: function(){ $(this).css("cursor","auto"); }
	});
	$('div.controlPictRight').bind({
		click: function(){ if (curPict < maxPicts) showPictByDirection(1); },
		mouseenter: function(){ $(this).css("cursor","pointer"); },
		mouseleave: function(){ $(this).css("cursor","auto"); }
	});

	// Keyboard
	$(document).keyup(function(event) {
		if (event.keyCode == 37) if (curPict > 1) showPictByDirection(-1);
		if (event.keyCode == 39) if (curPict < maxPicts) showPictByDirection(1);
	});

	// Verhalten Vorschaubild
	$('div.previewWrapper div.element').bind({
		click: function(){
			tempPict = getPreviewNumOfObject($(this));
			if (tempPict != curPict) showPictByIndex(tempPict);
		},
		mouseenter: function(){	
			tempPict = getPreviewNumOfObject($(this));
			if (tempPict != curPict) {
				$(this).fadeTo(0,0.80);
				$(this).css("cursor","pointer");
			}
		},
		mouseleave: function(){
			tempPict = getPreviewNumOfObject($(this));
			if (tempPict != curPict) {
				$(this).fadeTo(0,1);
				$(this).css("cursor","auto");
			}
		}
	});

	// Link Detailsinfos
	$('div.pictWrapper div.info div.title a').bind({
		click: function(event){
			event.preventDefault();
			tempObjLink = $(this);
			tempObj = $(this).parent().parent().children('div.description');
			tempObj.slideToggle(gallerySliderSpeed/2, function(){
				updateControls();
				if (tempObj.is(":visible")) {
					tempObjLink.html('weniger');
				} else {
					tempObjLink.html('mehr');
				}
			});
		}
	});

});

function initGallery() {
	// Controls preview default
	if ($('div.controlPreview').length != 0) $('div.controlPreview').fadeTo(0,0.20);

	// Anzahl Bilder / aktuelles Bild
	maxPicts = $('div.galleryWrapper div.pictWrapper').find('div.element').length;
	curPict = 1;
	maxGroups = Math.ceil(maxPicts/galleryGroupSize);
	curGroup = 1;
	galleryGroupWidth = $('div.previewWrapper').width();
	//alert(galleryGroupWidth);

	// Breite Slider einstellen
	$('div.pictWrapper div.slider').width(maxPicts*galleryPictW);
	$('div.previewWrapper div.slider').width(maxPicts*galleryPreviewW);

	// Transparenz Beschreibung + Link
	if ($('div.pictWrapper div.info').length != 0) {
		$('div.pictWrapper div.info').fadeTo(0,0.75);
		$('div.pictWrapper div.info div.title').append(' | <a href="#">mehr</a>');
	}

	updatePreview();
	updateControls();
}

function getPreviewNumOfObject(obj) {
	return parseInt(obj.attr('class').replace('element element',''));
}

function updatePreview() {
	// Update preview image
	$('div.previewWrapper div.element').fadeTo(0,1);				// preview pictures: 100%
	$('div.previewWrapper div.element' + curPict).fadeTo(0,0.20);	// current preview picture: 20%

	// Update previewGroup
	targetGroup = Math.ceil(curPict/galleryGroupSize);
	if (targetGroup != curGroup) showGroup(targetGroup-curGroup);
}

function updateControls() {
	// Preview buttons
	if (curGroup == 1) { $('div.controlPreviewLeft').css('display','none'); } else { $('div.controlPreviewLeft').css('display','block').fadeTo(0,0.20); }
	if (curGroup == maxGroups) { $('div.controlPreviewRight').css('display','none'); } else { $('div.controlPreviewRight').css('display','block').fadeTo(0,0.20); }

	// Picture buttons - display
	if (curPict == 1) { $('div.controlPictLeft').css('display','none'); } else { $('div.controlPictLeft').css('display','block'); }
	if (curPict == maxPicts) { $('div.controlPictRight').css('display','none'); } else { $('div.controlPictRight').css('display','block'); }

	// Picture buttons - size
	tempH = $('div.pictWrapper div.element' + curPict + ' div.info').outerHeight();
	$('div.controlPict').css('height', function(index) {
		return galleryPictH - tempH;
	});
}

function showGroup(dir) {
	curGroup = curGroup + dir;
	//tempX = (curGroup-1) * galleryGroupWidth * (-1);
	tempX = (curGroup-1) * (galleryPreviewW * galleryGroupSize) * (-1);
	$("div.previewWrapper div.slider").animate({"left": tempX + "px"},gallerySliderSpeed);
	updateControls();
}

function showPictByDirection(dir) {
	curPict = curPict + dir;
	tempX = (curPict-1) * galleryPictW * (-1);
	$("div.pictWrapper div.slider").animate({"left": tempX + "px"},gallerySliderSpeed);
	updatePreview();
	updateControls();
}

function showPictByIndex(idx) {
	//alert(idx + "/" + curPict);
	if (idx != curPict) {
		tempX = (idx-1)*galleryPictW*(-1);
		//$("div#gallerySliderLayer").css({"left": tempX + "px"});
		$("div.pictWrapper div.slider").animate({"left": tempX + "px"},gallerySliderSpeed);
		curPict = idx;
		updatePreview();
		updateControls();
	}
}

