$(document).ready(function(){
	
	// Bilder vorausladen
	$("#photoSelection a").each(function(){
		var graphic = new Image();
		graphic.src = $(this).attr("href");
	});	

	// Alle Thumbnails transparent darstellen
	$("#photoSelection a").animate({opacity: 0.5}, 0);
	$("#galleryPhoto").hide();
	
	$("#portfolioArea a").animate({opacity: 0.5}, 0);
	
	// Transparent fuer Galerie-Navigation setzen
	$("#siteNavigation a").animate({opacity: 0.5}, 0);
	$("#siteNavigation a.active").animate({opacity: 1}, 0);
	$("#siteNavigation a.semi-transparent").animate({opacity: 0.7}, 0);

	$("#displayDisclaimer a").animate({opacity: 0.5}, 0);
	
	// Titel fuer die Hyperlinks setzen
	$("#photoSelection img").each(function(){
		$(this).parent("a").attr("title", $(this).attr("alt"));
	});
	
	// Setze aktuelles Foto als aktiv in der Thumbnail-Listes
	$("#photoSelection a.active").animate({opacity: 1}, 350);
	$("#galleryPhoto").fadeIn(350);
	
	// Fototitel schreiben
	$("#galleryPhotoTitle").text(
		$("#photoSelection .active img").attr("alt")
	);
	
	// Füge ggf. Shop-Link an
	/*isBuyable = $("#photoSelection a.active").hasClass("buy-this");
	if (isBuyable) {
		$("#galleryPhotoTitle").append('<br><a href="http://shop.oliver-brandenburg.com" target="_blank"><img src="images/jetztkaufen.gif" alt="foto jetzt kaufen" /></a>');
	}*/
	
	
	$("#photoSelection a").click(function(event){
		// Standardverhalten verhindern
		event.preventDefault();
		
		// Abbrechen, falls das aktive Element ausgewaehlt wurde
		if ( $(this).attr("class") == "active" ) return 0;
		
		// Aktuell aktives Element deaktivieren
		
		$("#photoSelection a.active").removeClass("active").animate({opacity: 0.5}, 350);
		
		// Setze neues aktives Element
		$(this).animate({opycity: 1}, 350).addClass("active");
		
		// Bild wechseln
		$("#galleryPhoto").attr("src", $(this).attr("href"));
		
		$("#galleryPhotoTitle").text($(this).children().attr("alt"));
		
		/*var isBuyable = $(this).hasClass("buy-this");
		if (isBuyable) {
			$("#galleryPhotoTitle").append('<br><a href="http://shop.oliver-brandenburg.com" target="_blank"><img src="images/jetztkaufen.gif" alt="foto jetzt kaufen" /></a>');
		}*/
		
		$("#galleryPhoto").attr("title", $(this).children().attr("alt"))
						  .attr("alt", $(this).children().attr("alt"));
	});
	
	$("#photoSelection a").hover(function(){
		$(this).stop().animate({opacity: 1}, 200);
	},
	function(){
		if ($(this).attr("class").match("active") == "active") return 0;
		$(this).stop().animate({opacity: 0.5}, 200);
	});

	// Portfolio-Animation
	$("#portfolioArea a").hover(function(){
		$(this).stop().animate({opacity: 1}, 200);
		altName = $(this).children("img").attr("alt");
		
		$("#siteGalleries a").each(function(){
			if ( $(this).children("img").attr("alt") == altName ) {
				$(this).stop().animate({opacity: 1}, 200);
			};							
		});
	},
	function(){
		// Beim Verlassen des Hyperlinks
		if ($(this).attr("class").match("active") == "active") return 0;
		$(this).stop().animate({opacity: 0.5}, 200);
		
		altName = $(this).children("img").attr("alt");
		
		$("#siteGalleries a").each(function(){
			if ( $(this).children("img").attr("alt") == altName ) {
				$(this).stop().animate({opacity: 0.5}, 200);
			};							
		});
	});

	// Navigation animieren
	$("#siteNavigation a").hover(function() {
		$(this).stop().animate({opacity: 1}, 200);
		altName = $(this).children("img").attr("alt");

		if ( $(this).parents().parents("div").attr("id") == "siteGalleries" ) {
			$("#portfolioArea a").each(function(){
				if ( $(this).children("img").attr("alt") == altName ) {
					$(this).stop().animate({opacity: 1}, 200);
				};							
			});
		};
	},
	function(){
		if ($(this).attr("class").match("active") == "active") return 0;
		
		newOpacity = ( $(this).hasClass("semi-transparent") ) ? 0.7 : 0.5;
		
		$(this).stop().animate({opacity: newOpacity}, 200);

		altName = $(this).children("img").attr("alt");
		
		$("#portfolioArea a").each(function(){
			if ( $(this).children("img").attr("alt") == altName ) {
				$(this).stop().animate({opacity: 0.5}, 200);
			};							
		});
	});
	
	// disclaimer-Navigation
	$("#displayDisclaimer a").hover(function(){
		$(this).stop().animate({opacity: 1}, 200);
	},
	function(){
		if ($(this).attr("class").match("active") == "active") return 0;
		$(this).stop().animate({opacity: 0.5}, 200);
	});
	
	emailValue = "E-Mail"
	// Newsletter-Formular
	$("#newsletterForm #address").focus(function(){
		if($(this).attr("value") == emailValue ){
			$(this).attr("value", "");
		}
	});
	
	$("#newsletterForm #address").blur(function(){
		if($(this).attr("value") == ""){
			$(this).attr("value", emailValue);
		}
	});
	
	/*
		Shop-Info-Box
	*/
	$.get('shop-info.html', function(data){
		var infoButton = $('.shop-info');
		var infoButtonPos = $('.shop-info').offset();
		
		$('body').prepend('<div id="shop-info"></div>');
		$('#shop-info').css('display', 'none').append(data);
		
		var infoBox = $('#shop-info');
		var infoBoxButton = $('#siteNavigation .shop-info');
		
		var topPos = infoButtonPos.top - infoBox.height() - infoBoxButton.height() - 15;
		var leftPos = infoButtonPos.left - (infoBox.width()/2);
		
		$('#shop-info').css({
			'position': 'absolute',
			'left': leftPos,
			'top': topPos,
			'z-index': 1
		});
	});
	
	var infoButtonHover = function( event ) {
		$('#shop-info').css('display', 'block');
	}

	var infoButtonLeave = function( event ) {
		$('#shop-info').css('display', 'none');
	}

	$('#siteNavigation img.shop-info').mouseenter(infoButtonHover);
	$('#siteNavigation img.shop-info').mouseleave(infoButtonLeave);
});
