//HOAL 23.01.2009
//Um Konflikte mit anderen JS Librarys zu vermeiden anstatt $(...) -> jQuery(...) verwenden
//Ansonsten Probleme mit Perfect Lightbox
jQuery.noConflict();

jQuery(document).ready(function(){
	init(0);
	
	jQuery(".cur_pg").css("color","#353535");
	
	
	 
	//Lightbox blättern
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is('#nextLink') || jQuery(event.target).is('#coll_next')) {
			var id = jQuery("#thumbs .zoom_active").attr("id");
			
			jQuery("#thumbs .zoom_active").removeClass("zoom_active");
			
			var elem = jQuery("."+id).parent();
			var next_elem = jQuery(elem).next().next();
			var url = jQuery(next_elem).attr("href");
			//alert(url);
			
			var next_id = jQuery(next_elem).children(":first").attr("class");
			
			jQuery("#"+next_id).attr("class","zoom_active");
			jQuery("#"+next_id).addClass("zoom_active");
			
			getContent(url,"#coll_content","50");
			
			jQuery("#thumbs .active").removeClass("active");
			jQuery(next_elem).children(":first").addClass("active");
			
			var pos = jQuery(next_elem).children(":first").position();
			
			//alert(jQuery("#thumbs_container").height() + " / " + pos.top);
			
			var elem_num = jQuery("img").index(jQuery("#thumbs a .active"));
			
			var check = elem_num % 6;
			
			//alert(check);
			
			if(check==0) {
				jQuery("#thumbs").animate(
						{top: "-"+pos.top + "px"}, 1000
				);
				
				var next_pg = jQuery(".cur_pg").next();
				
				jQuery(".page").css("color","");
				jQuery(next_pg).css("color","#353535"); 
				jQuery(".cur_pg").removeClass("cur_pg");
				jQuery(next_pg).addClass("cur_pg");
				
			}
			
			jQuery("#filter_mask").animate
			(
				{right: "420px"}, 1000, function() { jQuery("#coll_filter").hide(); }
			);
			
			return false;
		}
		if (jQuery(event.target).is('#prevLink') || jQuery(event.target).is('#coll_prev')) {
			var id = jQuery("#thumbs .zoom_active").attr("id");
			
			jQuery("#thumbs .zoom_active").removeClass("zoom_active");
			
			var elem = jQuery("."+id).parent();
			var prev_elem = jQuery(elem).prev().prev();
			var url = jQuery(prev_elem).attr("href");
			//alert(url);
			
			var next_id = jQuery(prev_elem).children(":first").attr("class");
			
			jQuery("#"+next_id).attr("class","zoom_active");
			jQuery("#"+next_id).addClass("zoom_active");
			
			getContent(url,"#coll_content","50");
			
			jQuery("#thumbs .active").removeClass("active");
			jQuery(prev_elem).children(":first").addClass("active");
			
			var pos = jQuery(prev_elem).children(":first").position();
		//	alert(pos.top);
			//alert(jQuery("#thumbs_container").height() + " / " + pos.top);
			
			var elem_num = jQuery("img").index(jQuery("#thumbs a .active"))+1;
			//alert(elem_num);
			var check = elem_num % 6;
			
			//alert(check);
			
			if(check==0) {
				//alert(pos.top);
				thumbpos = pos.top-204;
				jQuery("#thumbs").animate(
						{top: "-"+thumbpos + "px"}, 1000
				);
				
				var prev_pg = jQuery(".cur_pg").prev();
				
				jQuery(".page").css("color","");
				jQuery(prev_pg).css("color","#353535"); 
				jQuery(".cur_pg").removeClass("cur_pg");
				jQuery(prev_pg).addClass("cur_pg");
				
			}
			
			jQuery("#filter_mask").animate
			(
				{right: "420px"}, 1000, function() { jQuery("#coll_filter").hide(); }
			);
			
			return false;
		}
	});
	
	 
	jQuery("#col2").bind("ajaxComplete", function(){
	 jQuery(this).css("display","none");
	});
	
	jQuery(".filter_input ul").css("display","none");
	
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is('.filter_input') || jQuery(event.target).is('.filter_input span.active')) {
			jQuery(".filter_input ul:visible").fadeOut("fast");
			if(jQuery(event.target).is('.filter_input')) {
				jQuery(event.target).children(":last").toggle();
			} else {
				jQuery(event.target).parent().children(":last").toggle();
			}
		} else {
			jQuery(".filter_input ul:visible").fadeOut("fast");
		}
	});
	
	
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is('.page')) {
			var pos = - (303 * (jQuery(event.target).html()-1));
			var url = jQuery(event.target).attr("href");
			
			jQuery("#thumbs .zoom_active").removeClass("zoom_active");
			href = jQuery(event.target).attr("href");
			
			id = jQuery("a[href="+href+"]").attr("class");
			
			jQuery("#"+id).attr("class","zoom_active");
			jQuery("#"+id).addClass("zoom_active");
			
			getContent(url,"#coll_content","50");
			jQuery("#coll_content").fadeOut();
			
			jQuery("#thumbs").animate
			(
				{top: pos + "px"}, 1000 
			);
			
			jQuery(".page").css("color","");
			jQuery(event.target).css("color","#353535"); 
			jQuery(".cur_pg").removeClass("cur_pg");
			jQuery(event.target).addClass("cur_pg");
			//initLightbox();
			return false;
		}
	});
	
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is('#thumbs img')) {

			jQuery("#thumbs .zoom_active").removeClass("zoom_active");
			
			id = jQuery(event.target).attr("class");
			
			jQuery("#"+id).attr("class","zoom_active");
			jQuery("#"+id).addClass("zoom_active");
			
			//var url = jQuery(this).parent().attr("href");
			var url = jQuery(event.target).parent().attr("href");
			getContent(url,"#coll_content","50");
			jQuery("#coll_content").fadeOut();
			
			jQuery("#thumbs .active").removeClass("active");
			jQuery(event.target).addClass("active");
			jQuery("#filter_mask").animate
			(
				{right: "420px"}, 1000, function() { jQuery("#coll_filter").hide(); }
			);
			return false;
		}
			
	
	});
	
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is("#coll_gal_prev")) {
						
			var pos = jQuery("#thumbs").position();
			var check = jQuery("#thumbs").height() + pos;
			pos = pos.top+303;
			
			var index = jQuery("*").index(jQuery(".cur_pg"))-1;
			elm = jQuery("*:eq("+index+")");
			
			jQuery(".page").css("color","");
			elm.css("color","#353535"); 
			
			var url = elm.attr("href");
			if(url) {
				getContent(url,"#coll_content","50");
			}
			
			jQuery(".cur_pg").removeClass("cur_pg");
			jQuery(elm).addClass("cur_pg");
			
			if(pos <= 0) {
				jQuery("#thumbs").animate
				(
					{top: pos + "px"}, 1000
				);
			}
			return false;
		}
	});
	
	jQuery("body").click(function(event) {
		if (jQuery(event.target).is("#coll_gal_next")) {				
			//var pos = jQuery("#thumbs").offset().top;
			var pos = jQuery("#thumbs").position();
			var check = -Math.ceil((jQuery("#thumbs").children().size() / 2)/6)*300;

			var elm = jQuery(".cur_pg + .page");
			
			jQuery(".page").css("color","");
			elm.css("color","#353535"); 
			
			var url = jQuery(".cur_pg + .page").attr("href");
			getContent(url,"#coll_content","50");
			
			jQuery(".cur_pg").removeClass("cur_pg");
			jQuery(elm).addClass("cur_pg");
			
			pos = pos.top-303;
						
			if(pos > check) {
				jQuery("#thumbs").animate
				(
					{top: pos+"px"}, 1000
				);
			} 
			return false;
		}
	});
		

	jQuery("body").click(function(event) {
		if (jQuery(event.target).is("#filter_result_link")) {
			jQuery("#filter_mask").animate
				(
					{right: "420px"}, 1000, function() { jQuery("#coll_filter").hide(); }
				);
			return false;
		}
		if (jQuery(event.target).is("#filter_open")) {
			jQuery("#coll_filter").show();
			jQuery("#filter_mask").animate
				(
					{right: "0px"}, 1000
				);
		}
		if (jQuery(event.target).is("#filter_close")) {
			
			jQuery("#filter_mask").animate
				(
					{right: "420px"}, 1000, function() { jQuery("#coll_filter").hide(); }
				);
		}
	});
					
});

function getContent(url,content,type) {
	if(window.location.search.substring(1)) {
		jQuery.ajax({ 
    //erweitere aufzurufenden Link 
    url: url+"&type="+type,
    //wenn es geklappt hat 
    success: 		function(html) {
        //Inhalt reinschreiben und anzeigen 
        jQuery(content).html(html);
        jQuery(content).fadeIn(); 
				init(type);
				//initLightbox();
        //location.hash = url; 
    } 
    });
	} else {
    jQuery.ajax({ 
    //erweitere aufzurufenden Link 
    url: url+"/type/"+type,
    //wenn es geklappt hat 
    success: 		function(html) {
        //Inhalt reinschreiben und anzeigen 
        jQuery(content).html(html);
        jQuery(content).fadeIn(); 
				init(type);
				//initLightbox();
        //location.hash = url; 
    } 
    });
	}
}


function init(type) {
	
	jQuery("#filter_open").hover(
		function() {
			jQuery(this).css("background-position","0 -28px");
		},
		function() {
			jQuery(this).css("background-position","0 0");
		}
	);
	
	jQuery("#coll_filter a").click(function() {
		if(jQuery(this).attr("id") != "filter_result_link") {
			var url = jQuery(this).attr("href");
			jQuery("#col2").css("display","block");
			getContent(url,".tx-collection-pi1","60");
			//jQuery(".tx-collection-pi1").fadeOut();
			//jQuery("h2").fadeOut();
			return false;
		}
	});
	
	jQuery("#thumbs_container").css("overflow","hidden");
	jQuery("#text_container").css("overflow","hidden");
	jQuery("#text_down").css("display","none");
	jQuery("#text_up").css("display","none");
	
	cl = jQuery("#coll_img img").attr("class");
	
	jQuery("#thumbs .active").removeClass("active");
	jQuery("#thumbs ."+cl).addClass("active");
	jQuery("#thumbs .zoom_active").removeClass("zoom_active");
	jQuery("#thumbs #"+cl).addClass("zoom_active");
	
	
	jQuery("#coll_zoom").click(function() {
	
		jQuery(".zoom_active").click();
				
		return false;
	});
	
	jQuery(".single_zoom").click(function() {
			
		jQuery(".zoom_active").click();
		
		return false;
	});
	
	if(jQuery(".filter_active").length > 0 && type != 50) {
		
		jQuery("#coll_filter").show();
		jQuery("#filter_mask").css("right","0px");
	}
	
	if(type==60) {
		initLightbox();
	}
	
	if((jQuery("#text").height() > jQuery("#text_container").height())) {
		jQuery("#text_down").css("display","block");
		jQuery("#text_up").css("display","block");
	}
	
	
	
	jQuery("#text_down").click(function() {
		
		var pos = jQuery("#text").position();
		var check = jQuery("#text").height();

		pos = pos.top-150;
		
		check = check + pos;
		
		//alert(check + " " +pos);
		if(check > 0) {
			jQuery("#text").animate
			(
				{top: pos+"px"}, 1000
			);
		} 
		return false;

	});
	
	jQuery("#text_up").click(function() {
		
		var pos = jQuery("#text").position();
		var check = jQuery("#text").height() + pos;
		pos = pos.top+150;
		
		
		if(pos <= 0) {
			jQuery("#text").animate
			(
				{top: pos + "px"}, 1000
			);
		}
		
		return false;

	});
	
	jQuery("#merkmale .content img").mouseover(function() {
		jQuery(this).fadeTo("fast", 0.5);

		jQuery(this).parent().find(".text").show("normal");
	});
	
	jQuery("#merkmale .content img").mouseout(function() {
		jQuery(this).fadeTo("fast", 1);
		jQuery(this).parent().find(".text").hide("normal");
	});
	
	jQuery("#menu2").mouseover(function() {
		jQuery("#menu2 ul li ul li ul").css("display","block");
	});
	
	jQuery("#menu2").mouseout(function() {
		jQuery("#menu2 ul li ul li ul").css("display","none");
	});
}