/*
 (C) ERIK PANZER
 for patentblau 2010

*/

$(document).ready(function() {
	// rollover logo div
		$('#hortkind').hover(function(){
			$('#hortkind_txt').animate({top:'20px'},{queue:false,duration:500});
			}, 
			function(){
			$('#hortkind_txt').animate({top:'-20px'},{queue:false,duration:500});
		});
		
	// RANDOM BACKGROUND PICTURE
	
	var r = 1 + (4 * Math.random())
	r = Math.round(r)
	var tag = '<img id="back" class="backimg" src="http://www.hortkind.com/images/bg/bg_0' + r + '.jpg" />'
	$('#background').append(tag)
	// Speichern zum Wiederherstellen des Hintergrundbildes
	var back = 'http://www.hortkind.com/hortkind/images/bg/bg_0' + r + '.jpg'
	
	
	// NAVIGATION
	
	$("#menue").mouseup(function() {
		// Mouseover über Menüpunkt
		if ($("#gallery").next().is(':visible')) {
			// Fotogalerie ist geöffnet und wird ausgeblendet, andere Menupunkte werden wieder sichtbar
			$("#gallery").next().slideUp("slow")
			$('.slidable').slideDown("slow");
		}
		else {
			// Menü wird ein- oder ausgeblendet
			$('#prev').fadeOut("slow")
			$('#next').fadeOut("slow")
			$("#content").toggle("slide", { direction: "up" }, 500, function() {
				// if ($("#back").attr('src') != back) {
					// Fotoansicht ist geöffnet, Hintergrundbild wird wiederhergestellt (Fade-Effekt)
					// $('#background').append('<img id="photoview" class="backimg" src="../images/uploads' + back + '" />')
					// $(window).trigger('resize')
					// $('#photoview').fadeIn("slow", function() {
						// $('#back').attr('src', back)
						// $('#photoview').remove()
					// })
				// }
			});
		}
	});
	
	// Alle aufklappbaren Bereiche beim Laden der Seite ausblenden
	$("#nav dd").hide()
	
	$("#nav dt a.submenu").click(function() {
		var openedBefore = false
		if ($(this).parent().next().is(':visible')) {
			// Bereich dieses Menüpunktes ist ausgeklappt
			openedBefore = true
		}
		if ($(this).parent().attr('id') == 'rueckblick') {
			// Angeklickter Menüpunkt ist "Rückblick" - alle anderen Menüpunkte ausblenden
			$('.slidable').toggle("slow")
		}		
		var visible = $("#nav dd:visible")
		if (visible.length == 0) {
			// noch kein Bereich aufgeklappt -> diesen Bereich einblenden
			$(this).parent().next().slideDown("slow")
		}
		else {
			// bereits ein Bereich aufgeklappt -> zuerst ausblenden, dann einblenden (falls nicht gerade ausgeblendet)
			var clicked = $(this)
			$("#nav dd:visible").slideUp("slow", function() {
				if (!openedBefore) {
					clicked.parent().next().slideDown("slow")
				}
			});
		}
		$(this).blur()
		return false
	});
	

	
	// LEFT PANEL
		
	$('.toggle').click(function() {
		var $lefty = $("#left")
		$lefty.animate({
			left: parseInt($lefty.css('left'), 10) == -$lefty.outerWidth() ? 0 : -$lefty.outerWidth()
		});
		$(".versatile").animate({
			left: parseInt($lefty.css('left'), 10) == 0 ? 0 : $lefty.outerWidth()
		});
		$("#prev").animate({
			left: parseInt($lefty.css('left'), 10) == 0 ? 44 : $lefty.outerWidth() + 44
		});
		$("#next").animate({
			right: parseInt($lefty.css('left'), 10) == 0 ? 44 : 44 - $lefty.outerWidth()
		});
		$(this).blur()
		return false
	});

	
	// PHOTO GALLERY
	
	$('.photo').click(function() {
		// $('#loader').append('<img id="loading" src="/patentblau/images/patentblau/loading.gif" />')
		var src = $(this).attr('src')
		src = src.replace('small', 'images')
		var prev = $(this).attr('prev')
		var next = $(this).attr('next')
		$('#prev').attr('index', prev)
		$('#next').attr('index', next)
		$('#prev').fadeIn("slow")
		$('#next').fadeIn("slow")
		$("#content").toggle("slide", { direction: "up" }, 750, function() {
			var photo = new Image()
			$(photo).attr('id', 'photoview')
			$(photo).addClass('backimg')
			photo.src = src
			photo.onload = function() {
				$('#background').append(photo)
				$(window).trigger('resize')
				$('#photoview').fadeIn("slow", function() {
					$('#back').attr('src', src)
					// $('#loading').remove()
					$('#photoview').remove()
				});
			}
		});
	});
	
	$('.change').click(function() {
		// $('#loader').append('<img id="loading" src="/patentblau/images/patentblau/loading.gif" />')
		var index = $(this).attr('index')
		$.getJSON('http://www.hortkind.com/hortkind/page/photo?pidx=' + index, function(data) {
			var photo = new Image()
			$(photo).attr('id', 'photoview')
			$(photo).addClass('backimg')
			var src = '/images/uploads/large/' + data.fileName
			photo.src = src
			photo.onload = function() {
				$('#background').append(photo)
				$(window).trigger('resize')
				$('#photoview').fadeIn("slow", function() {
					$('#back').attr('src', src)
					// $('#loading').remove()
					$('#photoview').remove()
				})
			}
			$('#prev').attr('index', data.prev)
			$('#next').attr('index', data.next)
		});
	});

});


