$(window).load(function() {$("#main").animate({paddingTop: "0", opacity:1},500).css({"background":"none"});});

$(".kunden a.filter").live("click", function(){
	$("body, html").animate({scrollTop: "0px"}, 400 , false);
})

$(".impressum-filter").click(function() {
	$(".impressum").show();
});

$(".teaser a, .kontakt a, .teaser-hover a").live("click", function() {
	closeActiveContent();
	$(this).parent().parent().parent().addClass("element-aktiv");
	
	$.ajax({
		url: $(this).attr("href"),
		context: document.body,
		success: function(data){
			
			$(".element-aktiv").append("<ul id='aktiv-content'>" + data + "</ul>");

			$(".element-aktiv div.teaser").hide();
			
			$("#aktiv-content .content").each(function() {
				var factorHeight = Math.ceil($(this).outerHeight(true)/136);
				$(this).height(136*factorHeight);
			});
			
			$container.isotope( 'reLayout', function(){} );
			var $innerContainer = $('#aktiv-content'); 
			
			$innerContainer.isotope({
				itemSelector: '.element',
				animationEngine : 'best-available',
				masonry: {
				    columnWidth: 242,
				    gutterWidth: 136
				  }
			});
			
			$("video").each(function() {
				var videoWidth = $(this).width();
				var videoHeight = $(this).height();
				$(this).parent().css({"width":videoWidth + "px", "height":videoHeight + "px"});
			});
			
			$container.isotope( 'reLayout', function(){} );
			$innerContainer.isotope( 'reLayout', function(){
				var position = $(".element").has("#aktiv-content").data('isotope-item-position');
				$("body, html").animate({scrollTop: position.y +"px"}, 400 , false);
			} );
			
			if($(".slideshow").length > 0) {
				$('.slideshow div').cycle({fx: 'scrollHorz'});
			}
		}
	});
	
	return false;
});

var $container = $('#main ul'); 

$(function() { // on ready
	
	$("img.lazy").show().lazyload({
		effect : "fadeIn",
		threshold : 200
	});
	
	$("#main .teaser h2 a").animate({opacity: 0});

	var tuerchen = 0;
	$(".adventskalendertuer").each(function(){
		$(this).attr("id", "tuerchen-" + tuerchen);
		tuerchen = tuerchen+1;
	});
	$(".adventskalendertuer .open").removeClass("hidden").hide();
	$("img").removeAttr("title");
	$("#slideshow-titel").cycle({
		fx: 'wipe',
		clip:'l2r' 
	});
	
	$container.isotope({
		itemSelector: '.element',
		animationEngine : 'best-available',
		
		masonry: {
		    columnWidth: 242,
		    gutterWidth: 136
		  },
		itemPositionDataEnabled: true
	});
	

});


/* Frohe Weihnachten ihr Javascriptbesucher */
$(".adventskalendertuer.clickbar .gutter").click(function() {
	$(this).find(".close").hide();
	$(this).find(".open").show();
});


$(window).bind( 'hashchange', function( event ){
	// get options object from hash
	var hashOptions = $.deparam.fragment();
	//var option = $.deparam(hashchange, true);
	// console.log(hashOptions.filter);
	$(".selected").removeClass("selected");
	$("a" + hashOptions.filter + "-filter").addClass("selected");
	if(hashOptions.length > 0) {
		//alert("jetzt" + hashOptions.filter.length);
		
		var text = hashOptions.filter;
		var v = text.replace(/./,'');

		$("body").attr("id", v);
	}
	
	// apply options from hash
	$container.isotope( hashOptions );
	
	
}).trigger('hashchange');  // trigger hashchange to capture any hash data on init
  

$('a.filter').click(function(){
	closeActiveContent();
	
	$('html,body').animate({scrollTop: 0}, 400 , false);

	// get href attr, remove leading #
	var href = $(this).attr('href').replace( /^#/, '' ),
	// convert href into object
	// i.e. 'filter=.inner-transition' -> { filter: '.inner-transition' }
	option = $.deparam( href, true );
	// set hash, triggers hashchange on window
	$.bbq.pushState( option );

	var selector = $(this).attr('data-filter');
	//alert(option.filter + "");
	$container.isotope({ filter: option.filter });
	
	$(".selected").removeClass("selected");
	var text = option.filter;
	var v = text.replace(/./,'');
	
	$("body").attr("id", v);
	$("a" + option.filter + "-filter").addClass("selected");
	
	return false;
});

// Box Hover State


$(".overlayer").live({
	mouseover: function() {
	$(this).stop().animate({opacity: .85}, 350);
},  mouseout: function() {
	$(this).stop().animate({opacity: 0},500);
}});

$(".projekt .teaser").hover(function() {
	$(this).find("h2 a").animate({opacity: .85}, 350);
	$(".teaser").addClass("teaser-hover");
}, function() {
	$(this).find("h2 a").animate({opacity: 0},500);
	$(".teaser").removeClass("teaser-hover");
});

$(".unternehmen .teaser").hover(function() {
	$(this).find(".teaser").addClass("teaser-hover");
	$(this).find("img").animate({opacity: 1},0);
	$(this).find("h3 a").animate({opacity: 1}, 0);
		
}, function() {
	$(this).find(".teaser").removeClass("teaser-hover");
	$(this).find("h3 a").animate({opacity: 1}, 350);
	$(this).find("img").animate({opacity: 0.3}, 350);

});



$(window).resize(function() {
	reloadBoxes();
});

function closeActiveContent(){
	$("#aktiv-content").remove();
	$(".element-aktiv div.teaser").show();
	$(".element-aktiv").removeClass("element-aktiv");
	reloadBoxes();
}

function reloadBoxes() {
	$container.isotope( 'reLayout', function(){} );
	if($('#aktiv-content').length > 0) {$('#aktiv-content').isotope( 'reLayout', function(){} );}
	$container.isotope( 'reLayout', function(){} );
}
