$(function(){
		
	//Inhalte erst anzeigen wenn alle graiken gealden sind
	$("#container").css("visibility","hidden");
	$(window).bind("load", function () { 
		$("#container").css("visibility","visible");
		});
	
	
	//Alle Links mit einem target "_blank" ausrüsten
	$("a").not(".refLink").each(function(){
		if($(this).attr("target") == undefined || $(this).attr("target") != "_new") {
			$(this).attr("target","_new")
			};
		
		})
		
	//Mouseposition festhakten
    var mouseX = 0;
    var mouseY = 0;
    $().mousemove( function(e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
    });
	
	
	var waitForFinalEvent = (function () {			
			var timers = {};
			return function (callback, ms, uniqueId) {
				
				if (!uniqueId) {
					uniqueId = "Don't call this twice without a uniqueId";
					}
				
				if (timers[uniqueId]) {
					clearTimeout (timers[uniqueId]);
					}
					
		timers[uniqueId] = setTimeout(callback, ms);
		
		};	
	
	})();	

flag_nr = 0;


var viewer = false;


//Mouseover bei Sub
$(".sub").live("mouseover",function(){
	$(this).find(".subheadline").css("display","block");
	})
.live("mouseout",function(){
	$(this).find(".subheadline").css("display","none");
	})



//Mausklicks im Fenster überwachen
$("body, #alles").click(function(e){
	
	if($(e.target).parent().hasClass("sub") == false && $("#viewer").length > 0 && viewer == true && e.target.nodeName != 'A'){
		//$(e.target).parent().parent().hasClass("refLink");
		
		//Adresse zurücksetzen
		$.address.value("");  
		return false;		
		}
		
	});

	
//Eltern von txt Klassen eine Klasse txt_ref hinzufügen
$(".txt").parent().addClass("txt_ref");
$(".txt").parent().mouseover(function(){
	$(this).css("cursor","pointer");
	$(this).find(".txt").css("text-decoration","underline")
	})
.mouseout(function(){
	$(this).css("cursor","default");
	$(this).find(".txt").css("text-decoration","none")
	});

//Bilder zum scrollen vorladen
$("[data-picScroll]").each(function(e){
	var image = $(this).attr("data-picScroll").split(",");
	for(i=0;i < image.length; i++){
		$("#search_and_load").append("<img src='"+image[i]+"' alt='' width='1px' height:'1px'/>")
		}
	})


//Bilder Scrollen Steuern auf erster Ebene
$(".picScroll").mousemove(function(e){
	 //Ausrechnen wie viele Bilder vorhanden sind
	
	
	 picScroll = $(this).next().attr("data-picScroll").split(",");	  
	 picScrollWidth = Math.round($(this).width()/picScroll.length);
	 
	 off =  $(this).offset().left;  
	 nr = 0 ; 
	 
	 //alert(picScroll);
	 for ( i = 0; i < picScroll.length; i++ ){
		//Maus links
		if( i == 0 && e.pageX < off+(picScrollWidth*(i+1)) ) {
			nr = i;
			}
		//Mouse nicht am Rand
		if( i > 0 && i+1 != picScroll.length  && e.pageX > off+(picScrollWidth*(i)) && e.pageX < off+(picScrollWidth*(i+1)) ) {
			nr = i;
			}
		//Maus rechts
		if( i+1 == picScroll.length  && e.pageX > off+(picScrollWidth*(i)) ) {
			nr = i;
			}
		}
		//Bild wechseln
		$(this).next().attr("src",picScroll[nr]);
	})


function over(obj){
		colorize = $(obj).attr("data-color");
		
		//Schatten beim schleißen ausblenden
		$(obj).not(".txt_ref").css({
			"-moz-box-shadow"	: 	"0 0 0 white",
			"-webkit-box-shadow": 	"0 0 0 white",
			"box-shadow"		: 	"0 0 0 white",
			"background"		:	"rgb(255, 254, 248)",			
			})
		
		$(obj).not(".txt_ref").mouseover(function(e){
			
			$(this).find(".cont_headline").css({
				"visibility"		:	"visible",
				"background"		:	"rgba(0,0,0,0)",
				"color"				:	"#202020",
				});
					
			$(this).css({
				"background"		:	"rgb(255, 254, 248)",
				"z-index"			:	"120",
				"cursor"			:	"pointer",
				"-moz-box-shadow"	: 	"24px 24px 0px rgba(0,0,0,0.8)",
				"-webkit-box-shadow": 	"24px 24px 0px rgba(0,0,0,0.8)",
				"box-shadow"		: 	"24px 24px 0px rgba(0,0,0,0.8)"
				});
				
			})
		.mouseout(function(){			
			$(this).find(".cont_headline").css({
				"visibility"		:	"hidden",
				"background"		:	"rgba("+colorize+",1)"
				});
				
			$(this).css({
				"background"		:	"rgb(255, 254, 248)",
				"z-index"			:	"100",
				"cursor"			:	"pointer",
				"-moz-box-shadow"	: 	"0 0 0 white",
				"-webkit-box-shadow": 	"0 0 0 white",
				"box-shadow"		: 	"0 0 0 white"
				})
			});
			
			
	}	
	over($(".item[data-sub='true']"));




	//Sobald adresse geändert, aktiv werden
	$.address.change(function(event) {  
		if(event.value != "/") {			
			
			var saver = $(".item[data-id="+event.value.split("\/")[1].split("_")[0]+"]");

			//Inhalte nachladen
			var id = $(saver).attr("data-id");
			
			//Wenn der kein Viewer da ist
			if($("#viewer").length == 0 && clicked == false){
				clicked = true;
			
			$.ajax({  
				type: "POST",  
				url: "admin/aus05.php",  
				data: "type=sub&id="+id+"&main=false",  
				success: function(data) { 
						
						colorize = $(saver).attr("data-color");
											
						//Rahmen immer an lassen
						$(saver).not(".txt_ref").unbind("mouseover");
						$(saver).not(".txt_ref").unbind("mouseout");
						
											
						//schatten direkt nach klick umdrehen
						$(saver).not(".txt_ref").css({
							"-moz-box-shadow"	: 	"-24px -24px 0px rgba(0,0,0,0.8)",
							"-webkit-box-shadow": 	"-24px -24px 0px rgba(0,0,0,0.8)",
							"box-shadow"		: 	"-24px -24px 0px rgba(0,0,0,0.8)"
							})
									
						$(saver).not(".txt_ref").mouseover(function(){
							$(this).css({
								"background"		:	"rgb("+colorize+")",
								"z-index"			:	"120",
								"cursor"			:	"pointer",
								"-moz-box-shadow"	: 	"-24px -24px 0px rgba(0,0,0,0.8)",
								"-webkit-box-shadow": 	"-24px -24px 0px rgba(0,0,0,0.8)",
								"box-shadow"		: 	"-24px -24px 0px rgba(0,0,0,0.8)"
								});
							})
						.mouseout(function(){
							$(this).css({
								"background"		:	"rgb(255, 254, 248)",
								"z-index"			:	"100",
								"cursor"			:	"pointer",
								"-moz-box-shadow"	: 	"0 0 0 white",
								"-webkit-box-shadow": 	"0 0 0 white",
								"box-shadow"		: 	"0 0 0 white"
								});
							})
						
					
						//headline Einblenden
						$(saver).find(".cont_headline").css("visibility","visible");
						
						
							//Gruppe auslesen
							var gr = $(saver).attr("class").split(" ")[1];
							
							//this -> item // angeklickte referenz mit farbig überdecken
							$(saver).find(".cont_headline").css("background","rgb("+colorize+")")
							
							//Bidposition merken
							bgpos = $(saver).attr("data-bgpos");	
							
							//Bildwiederholung merken
							bgrepeat = $(saver).attr("data-bgrepeat");	
								
							
							//Hintergrund von txt Ref ändern
							$(".txt_ref").css({"background":"rgb("+colorize+")"})
							
							
							//Bild merken
							bgimage = "bilder/database/"+$(saver).attr("data-bgimage");
							if(bgimage == ""){
								bgimage = "";
								}
								
							//Farbe auf Hintergrund anwenden
							$("#alles").css({ 
								"background":"rgb("+colorize+")" , 
								"background":"rgba("+colorize+",0.95)" , 
								"display" : "block",
								"background-image" : "url("+bgimage+")",
								"background-repeat" : bgrepeat,
								"background-position" : bgpos,
								"z-index":"100"								
								})
												
							//Viewer einbauen
							$("#container").prepend("<div id='viewer'></div>");		
							
							//Viewer mit Inhalt füllen
							
							$("#viewer").html(data);
							
							
							//Viewer Positionieren
							$("#viewer").css( "top", $(saver).position().top+$(saver).height()+50+"px" );
							
						
							var eins = $(saver).position().top-5+15;
							//if($("#viewer").position().top)
							if($("#viewer").position().top+15 > $(window).height()+$(window).scrollTop()-20)
								{
								$('html, body').animate({scrollTop:$("#viewer").position().top-$(window).height()+165}, 500);
								}
									
							//Viewer füllen und sichtbar machen
							//$("."+gr).not(this).clone().appendTo("#viewer");
							//$("#viewer > ."+gr).not(this).css("display","inline-block");	
							
							//Mit Masonry ordnen
							var $subCon = $('#viewer');
								$subCon.imagesLoaded( function(){
								  $subCon.masonry({
									// options
									itemSelector : '.sub',
									columnWidth : 1
									});
								  });
							
							
							$(".item").not(saver).each(function(e){
								
								//Inhalte hinter #alles rücken
								$(this).css({
									"z-index":"19"
									});
								bgFarbeMerken = $("body").css("background");
								$("body").css("background","rgb(255, 254, 248)");	
								});
							
							//Viewer auf an stellen
							window.setTimeout(function(){
								viewer = true;
								},500);	
						}				  
					});	
				}
			} 
			else  {
				
				
				setTimeout(function(){
					over($(".item[data-sub='true']"));
					
					$(window).scrollTop(wP);
					$(".txt").parent().css({"background":"rgba(0,0,0,0)"})		
			
					$("#alles").css("display","none");
					$("#viewer").remove();
					
					//Headline wieder ausblenden
					$(".cont_headline").css("visibility","hidden");
						
					
					//Alle Items wieder normal darstellen
					$(".item").css({"z-index":"100","border":"0px solid transparent"});
					$("body").css("background",bgFarbeMerken);
					viewer = false;
					clicked = false;
					
					//Subheadline wieder einschalten
					$(".subheadline").css("visibility","visible");
					
					//Mouseover bei Referenzen wieder herstellen
					
					},100)
					
			
			}
		}); 
			


	clicked = false;
	
	
	wP = 0;
	$("a.refLink[data-sub='true']").click(function() {  
    	wP = $(window).scrollTop();
		//$.address.value($(this).attr('href'));  
		});
	$.address.crawlable(true)
	

	  
var kontaktinview = false;		  
//Schauen ob #kontakt INVIEW IST
$('#kontakt').bind('inview', function (event, visible) {
	  if (visible == true) {
		kontaktinview = true;
		$("#kontakt_btn").hide();
		
		} else {
		kontaktinview = false;
		$("#kontakt_btn").show();
	
		}
	});
	
$("#logo").click(function(){
	window.location.href = window.location.href;
	})
.hover(function(){
	$(this).css("cursor","pointer");
	});
			
	//Höhe Kontakt festlegen
	$(window).resize(function () {
				
		waitForFinalEvent(function(){
			$(".beschr_flag").remove();
			
			var position = $(".letztes").position();
			var pW = position.left + $(".letztes").width();
			
			
			hj = 0;
			$("#multi .packen").each(function(){
				kontaktWARimbild = kontaktinview;
				
				pos = $(this).offset();
				p = pos.left;
				j = p+$(this).width();
				
				$(this).css("background-color","transparent");
				if(j > $("#kontakt").width()) {

					hj += $(this).height();

					}				
					$("#multi").height(  $(window).height()-150-$("#kontakt_btn").height()  +hj);
					$("#kontakt").height(  $(window).height()-150-$("#kontakt_btn").height()  +hj);
					
					
					if(kontaktWARimbild == true) {
						offsetK = $("#kontakt").offset().top; 
						$(window).scrollTop(offsetK-150);
						}
					
												
				});			
			
		  }, 500, "some unique string");
	  });
	  
	  $(window).resize();
	  
	//#kontakt_btn
	$("#kontakt_btn").click(function(){
		$('.sub').trigger('click');
		$(this).hide();
		
		//$('html, body').animate({scrollTop:$("#viewer").position().top-$(window).height()+300}, 500);
		$('html, body').animate({scrollTop:$("#kontakt").offset().top-150},333);		
		return false;
		})
	.mouseover(function(){
		$(this).css({
			"text-decoration":"underline",
			"cursor":"pointer"
			});
		})
	.mouseout(function(){
		$(this).css({
			"text-decoration":"none",
			"cursor":"default"
			})
		});
		
		
	var $container = $('#container');
		$container.imagesLoaded( function(){
		  $container.masonry({
			// options
			itemSelector : '.item',
			columnWidth : 1
			});
		  });
	
	
	})
