/*
 * Marc Breitung (http://www.marcbreitung.de)
 * 
 * Version 1.0 - 11.12.2011
 * 
 */

var totalImagesLighbox = 0;
var activeImage = null;
var activePosition = null;
var hasOverlay = false;
var lightboxXMLPath = null;
var lightboxIsLoading = false;

var slideshowXML = null;
var slideshowImage = null;
var slideshowDelay = 5000;

$(document).ready(function() {

	$('noscript').remove();

	lightboxLoadXML('lightbox_01.xml');

	$('#lighboxButton').addClass('active');
	$('.gallery_01').addClass('active');

	$('#lighboxButton').on('click', function(event) {
		event.preventDefault();
		if(!$(this).hasClass('active')) {
			$('*').stop(true, true);
			$('#navigation a').removeClass('active');
			$(this).addClass('active');
			showLighbox();
		}
	});

	$('#slideshowButton').on('click', function(event) {
		event.preventDefault();
		if(!$(this).hasClass('active')) {
			$('*').stop(true, true);
			$('#navigation a').removeClass('active');
			$(this).addClass('active');
			showSlideshow();
		}
	});

	$('#infoButton').on('click', function(event) {
		event.preventDefault();
		if(!$(this).hasClass('active')) {
			$('*').stop(true, true);
			$('#navigation a').removeClass('active');
			$(this).addClass('active');
			showInfo();
		}
	});

	$('#overlay').on('click', function(event) {
		event.preventDefault();
		$('#overlay img').animate({
			width : 110,
			height : 85,
			left : activePosition.left,
			top : activePosition.top
		}, 500, 'linear', function() {
			$('#overlay').empty();
			$('#overlay').hide();
			$(activeImage).css('visibility', 'visible');
			hasOverlay = false;
		});
	});

	$('.gallery').on('click', function(event) {
		event.preventDefault();
		if(!$(this).hasClass('active') && !lightboxIsLoading) {
			$('*').stop(true, true);
			$('.gallery').removeClass('active');
			$(this).addClass('active');
			slideshowXML = null;
			slideshowImage = null;
			$('#overlay, #loader, #lighbox, #slideshow, #infoText, #content img, #overlay img').fadeOut();
			lightboxLoadXML($(this).attr("rel"));
		}
	});
	
});

function showLighbox() {
	slideshowXML = null;
	slideshowImage = null;
	$('#overlay, #loader, #lighbox, #slideshow, #infoText, #content img, #overlay img').fadeOut();
	$('#subnavigation').fadeIn();
	lightboxLoadXML(lightboxXMLPath);

}

function showSlideshow() {
	slideshowXML = null;
	slideshowImage = null;
	$('#overlay, #loader, #lighbox, #slideshow, #infoText, #content img, #overlay img').fadeOut();
	$('#subnavigation').fadeOut();
	slideshowLoadXML('slideshow_01.xml');

}

function showInfo() {
	slideshowXML = null;
	slideshowImage = null;
	$('#overlay, #loader, #lighbox, #slideshow, #content img, #overlay img').fadeOut();
	$('#subnavigation').fadeOut();
	$('#infoText').fadeIn(function() {
		$('#contentWrap').animate({
			height : 400
		}, 500, 'linear');
	});
}

/*
 * Intro
 */
function introImageLoad(xml) {
	$('#contentWrap').animate({
		height : 450
	}, 500, 'easeInQuad', function() {
		var introImageXML = $(xml).find('image').get(0);
		var introImage = $(introImageXML).find('path').get(1);
		var img = new Image();
		$(img).load(function() {
			$("#loader").fadeOut();
			$(this).css('top', -45);
			$('#overlay').append(this);
			$('#overlay').fadeIn();
			introImagFadeOut();
		}).error(function() {
		}).attr('src', 'images/' + $(introImage).text());
	});
}

function introImagFadeOut() {
	$('#lighbox a').sort(sortAlpha).appendTo('#lighbox');
	$('#lighbox').delay(2000).fadeIn();
	$('.nav').delay(2000).fadeIn();
	$('#contentWrap').delay(2000).animate({
		height : 400
	}, 500, 'linear');
	$('#overlay img').delay(2000).animate({
		width : 110,
		height : 85,
		left : 20,
		opacity : 0,
		top : 0
	}, 1000, 'easeInQuad', function() {
		$('#overlay').hide();
		lightboxIsLoading = false;
		$('.gallery').animate({opacity : 1}, 100, 'linear');
	});
}

/*
 * Lightbox
 */
function lightboxLoadXML(xml) {
	
	lightboxIsLoading = true;
	$('.gallery').animate({opacity : 0.2}, 100, 'linear');
	
	$('#lighbox').empty();
	lightboxXMLPath = xml;
	$('#loader').fadeIn();
	$.ajax({
		type : 'GET',
		url : 'xml/' + xml,
		dataType : 'xml',
		success : function(xml) {
			lightboxReadFromXML(xml);
		}
	});
}

function lightboxReadFromXML(xml) {
	$('#lighbox').empty();
	totalImagesLighbox = $(xml).find('image').length;
	$(xml).find('image').each(function() {
		var id = $(this).attr('id');
		var path = $(this).find('path').get(0);
		var fullpath = $(this).find('path').get(1);
		fullpath = $(fullpath).text();
		var img = new Image();
		$(img).load(function() {
			var imageItem = $('<a href="#" style="background-image: url(' + $(this).attr("src") + ')" class="lighboxImageShow" id="' + id + '"><span></span></a>').appendTo('#lighbox');
			$(imageItem).on("click", {
				fullimage : fullpath
			}, function(event) {
				if(!hasOverlay) {
					event.preventDefault();
					activePosition = $(this).position();
					activeImage = $(this);
					hasOverlay = true;
					overlayImageAdd(event.data.fullimage);
				}
			});
			totalImagesLighbox--;
			if(totalImagesLighbox == 0) {
				introImageLoad(xml);
			}
		}).error(function() {
		}).attr('src', 'images/' + $(path).text());
	});
}

/*
 * Overlay Image
 */
function overlayImageAdd(image) {
	$('#overlay').hide();
	$('#overlay').empty();
	var img = new Image();
	$(img).load(function() {
		var imageWidth = this.width;
		var imageHeight = this.height;
		$(this).css('left', activePosition.left);
		$(this).css('top', activePosition.top);
		$(this).css('width', 110);
		$(this).css('height', 85);
		$('#overlay').append(this);
		$(activeImage).css('visibility', 'hidden');
		overlayImageShow(imageWidth, imageHeight);
	}).error(function() {
	}).attr('src', 'images/' + image);
}

function overlayImageShow(width, height) {
	$('#overlay').show();
	var left = getRandom(-20, 40);
	var top = getRandom(-20, 20);
	$('#overlay img').animate({
		width : width,
		height : height,
		opacity : 1,
		left : left,
		top : top
	}, 500, 'linear');
}

/*
 * Slideshow
 */
function slideshowLoadXML(xml) {
	$('#contentWrap').animate({
		height : 450
	}, 500, 'linear', function() {
		$('#loader').fadeIn();
		$.ajax({
			type : 'GET',
			url : 'xml/' + xml,
			dataType : 'xml',
			success : function(xml) {
				slideshowReadFromXML(xml);
			}
		});
	});
}

function slideshowReadFromXML(xml) {
	$("#slideshow").empty();
	$("#slideshow").show();
	slideshowXML = xml;
	slideshowImage = 0;
	slideshowShowImage(slideshowImage, 0);
}

function slideshowShowImage(image, delay) {
	var items = $(slideshowXML).find('image').length;
	var imagePathXML = $(slideshowXML).find('image').get(image);
	var imagePath = $(imagePathXML).find('path').get(0);
	var img = new Image();
	$(img).load(function() {
		if(slideshowXML != null) {
			$('#slideshow img').addClass('old');
			$("#loader").fadeOut();
			slideshowImage = (slideshowImage == (items - 1)) ? 0 : slideshowImage + 1;
			$(this).hide();
			$(this).removeClass('old');
			$('#slideshow').append(this);
			$(this).delay(delay).fadeIn(1000, function() {
				if(slideshowXML != null) {
					$('.old').remove();
					slideshowShowImage(slideshowImage, slideshowDelay);
				}
			});
		} else {
			$("#slideshow").fadeOut();
		}
	}).error(function() {
	}).attr('src', 'images/' + $(imagePath).text());
}
