$.noConflict();
jQuery(document).ready(function($)
{
	$(window).load(function() {
		$("body").css("background-image","none");
		$("body").css("overflow","auto");
		if( ($.browser.mozilla == true && $.browser.version.slice(0,1) > 1) || $.browser.webkit == true ) $("body").css("overflow-y","scroll");
		$("#wrapper").css("visibility","visible");
	});
		
	/* fancybox: start */
	$("a[rel=media_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'centerOnScroll'	: true,
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	/* fancybox: ende */
	
	
	/* fancy cycle start */
	var fancyDefW = 700;
	var gImagePath = _ThemeDir+'images/';
	var gImages = new Array();
	
	function preloading() { for (i=0; i<preloading.arguments.length; i++){ gImages[i] = new Image(); gImages[i].src = preloading.arguments[i]; }}
	preloading(
			gImagePath+'us-gallery-slide-prev.png',
			gImagePath+'us-gallery-slide-next.png',
			gImagePath+'us-gallery-scroll-prev.png',
			gImagePath+'us-gallery-scroll-next.png'
	);
	
	
	$(".ajax-call").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'centerOnScroll'	: true,
			'autoDimensions'	: false,
			'width'				: fancyDefW,
			'height'			: 471,
			'onComplete'		: function () {
				
				/* cycle */
				var notFirst = false;
				var firstObj = $("#g > *:first-child");
				var defSlideH = 400;
				var scrollStripW = 0;
				var scrollSpeed = 200;
				var scrollTime = 800;
				var thumbHeight = 70;
				var thumbMarginRight = 2;
				var thumbVideoW = 110;
				var fancyW = new Array();
				var newFancyWrapW = fancyDefW; /* default */
				
				$(document).pngFix(); /* talk to best friend IE */
				$('#g')
					.after('<div id="g-nav"><ul id="g-nav-inside">')
					.cycle({
					fx: 'fade',
					speed:  'fast', 
					timeout: 0,
					prev:   '#g-slide-prev',
					next:   '.g-slide-next',
					pager:  '#g-nav-inside',
					pagerAnchorBuilder: function(idx, slide) { 
						
						if(slide.tagName == "IMG") {
							scrollStripW = scrollStripW + ( Math.ceil( slide.width / (slide.height/thumbHeight) ) + thumbMarginRight );
							return '<li><a href="#" title="' + slide.title + '"><img height="'+thumbHeight+'" src="' + slide.alt + '" /></a></li>'; 
						} else {
							scrollStripW = scrollStripW + ( thumbVideoW + thumbMarginRight );
							return '<li><a href="#" title="' + slide.title + '"><img height="'+thumbHeight+'" src="'+_ThemeDir+'images/us-gallery-video-thumb.gif" /></a></li>'; 
						}	
						
						fancyW.push( getWidth( $(slide) ) );
					},
					before: onBefore,
					after: onAfter
				});
				
				$('#g-scroll-next').click(function(){
					$("#g-nav").scrollTo( "+="+scrollSpeed+"px", scrollTime, {axis:'x'} );
					return false;
				});
				
				$('#g-scroll-prev').click(function(){
					$("#g-nav").scrollTo( "-="+scrollSpeed+"px", scrollTime, {axis:'x'} );
					return false;
				});
				
				/* start cycle */
				fancyW.sort(function(a,b){ return b - a; });
				
				$("#g-nav-inside").css("width", scrollStripW+ "px" );
				if( $('#g').children().length <= 1 ) { $('#g-scroll-prev, #g-scroll-next, #g-slide-prev, #g-slide-next').css("display","none"); }
				
				centerImg( firstObj );
				
				
				/* while sliding */
				
				function onBefore() { 
					if(notFirst) $(this).fadeOut( 800, function(){ $(this).css("visibility", "hidden"); } );
				}

				function onAfter( curr, next, opts ) { 
					var caption1 = (opts.currSlide +1) + '/' + opts.slideCount;
					$('#g-slide-title').html("<span class='caption'>"+caption1+"</span><span class='description'>"+this.title+"</span><span class='clear'></span>");
					centerImg( $(this) );
					$(this).fadeOut(0);
					$(this).css("visibility", "visible");
					$(this).fadeIn( 800 );
					notFirst = true;
				}
				
				function centerImg ( obj ) {
					var objW = getWidth( obj );
					var objH = getHeight( obj );
					
					newFancyWrapW = objW;
					resizeBox();
					
					if(objH < defSlideH) {
						var posTop = Math.ceil( (defSlideH - objH) / 2 );
						obj.css("top", posTop+"px" );
					}
				}
				
				function resizeBox ( ) {
					
					if( newFancyWrapW == "" || newFancyWrapW == 0 || newFancyWrapW > fancyW[0] ) newFancyWrapW = fancyDefW;
					
					$("#fancybox-wrap").css("width", ( newFancyWrapW+20 )+"px" );
					$("#fancybox-content").css("width", newFancyWrapW+"px" );
					$("#fancybox-content div").css("width", newFancyWrapW+"px" );
					$("#g-nav").css("width", ( ( newFancyWrapW/100 ) * 92 ) + "px" );
					$("#g-nav").css("margin", "0 "+ ( ( newFancyWrapW/100 ) * 4 ) + "px" );
					
					/*
					if($(".activeSlide").length > 0){
						var activeP = $(".activeSlide").position();
						var navScrollTo = activeP.left;
					}
					else
						var navScrollTo = 0;
					
					$("#g-nav").scrollTo( navScrollTo+"px", scrollTime, {axis:'x'} );
					*/
					
					$.fancybox.center(0);
				}
				
				function getWidth ( slide ) {
					var slideW = 0;
					if (slide.attr("width") != "" ) slideW = slide.attr("width");
					else if ( slide.css("width") != "" ) slideW = slide.css("width").replace("px",""); 
					else slideW = slide.width();
					if ( slideW == 0 || slideW == "" ) slideW = fancyDefW;
					return slideW*1;
				}
				
				function getHeight ( slide ) {
					var slideH = 0;
					if (slide.attr("height") != "" ) slideH = slide.attr("height");
					else if ( slide.css("height") != "" ) slideH = slide.css("height").replace("px",""); 
					else slideH = slide.height();
					if ( slideH == 0 || slideH == "" ) slideH = defSlideH;
					return slideH*1;
				}
			}
	});
		
	/* fancy cycle ende */
	
	
	
	$(function(){
		$("#navigation h3").each(function(){ 
			if( $(this).parent().attr("class") != "section" && $(this).parent().attr("class") != "section first" ) $(this).next().hide(); else $(this).next().show();
		});
	});
	
	$("#navigation h3").click(function(){
		if( $(this).next().is(":visible") )
		{
			$(this).next().slideUp();
			$(this).removeClass("underscore");
		} else {
			$(this).next().slideDown();
			$(this).addClass("underscore");
		}
		return false;
	});
	
	$("#searchbox a").click(function(){
		$(this).css("display","none");
		$("#searchform").css("display","inline");
		$("#searchfield").focus();
		return false;
	});

	$("#searchfield").blur(function(){
		setTimeout(function(){
			$("#searchform").css("display","none");
			$("#searchbox a").css("display","block"); 
		}, 200);
	});
	
	if($(".content-container-overview").length > 0) 
	{
		$(".content-container-overview").hover(function(){
			$(this).addClass("content-container-overview-hover");
			$(this).find(".content-container-overview-link").addClass("overview-link-hover");
		}, function(){
			$(this).removeClass("content-container-overview-hover");
			$(this).find(".content-container-overview-link").removeClass("overview-link-hover");
		})	
	
		$(".content-container-overview").click(function(){
			if( $(this).attr("class").search('ajax-call') != -1) { 
				return false;
			}
			else if($(this).find(".content-container-overview-link").length > 0) {
				var url = $(this).find(".content-container-overview-link").attr("href");
				window.location.href = url;
			}	
		});
		
	}
	
	if($(".marginal-container").length > 0) 
	{
		$(".marginal-container").hover(function(){
			$(this).addClass("content-container-overview-hover");
			$(this).find(".marginal-container-link").addClass("overview-link-hover");
		}, function(){
			$(this).removeClass("content-container-overview-hover");
			$(this).find(".marginal-container-link").removeClass("overview-link-hover");
		});
	
		$(".marginal-container").click(function(){
			if($(this).find(".marginal-container-link").length > 0){
				var url = $(this).find(".marginal-container-link").attr("href");
				window.location.href = url;
			}
		});
	}
	
	/* forms */
	$('#Form_filter_kuenstler').change(function() { $('#Form_filter_kuenstler').submit(); });
	$('#Form_filter_jahr').change(function() { $('#Form_filter_jahr').submit(); });
	$('#Form_filter_kategorie').change(function() { $('#Form_filter_kategorie').submit(); });
	
	
	
	var inputVal = "";
	var inputVals = [];
	var isError = false;
	
	$("form ul input.text, form ul textarea").focus(function(){
		inputVal = $(this).attr("value");
		$(this).attr("value","");
	});

	$("form ul input.text, form ul textarea").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value",inputVal);
		inputVal = "";
	});

	$(function(){
		$("form ul input.text").each(function(){
			inputVals.push( $(this).attr("value") );
		})
	});

	$("#Form_AnfrageForm_action_SendAnfrageForm").click(function(){

		var pflichtFelder = new Array("Form_AnfrageForm_Nachname","Form_AnfrageForm_Email","Form_AnfrageForm_Email2");
		
		$("form ul input.text").each(function(index){ 
			if( $(this).hasClass("form-error") ) $(this).removeClass("form-error");
			var value = $(this).attr("value");
			if(pflichtFelder.length > 0) {
				for(var i=0;i<pflichtFelder.length;i++) {
					if ( pflichtFelder[i] == $(this).attr("id") ) {
						if ( value == "" || value == inputVals[index] ){ $(this).addClass("form-error"); isError = true; }
					}
				}
			} 
			else
				( value == "" || value == inputVals[index] ) ? $(this).addClass("form-error") : isError = true; 
		});

		if($("#Form_AnfrageForm_Email").length > 0 && $("#Form_AnfrageForm_Email2").length > 0 ){
			var e1 = $("#Form_AnfrageForm_Email");
			var e2 = $("#Form_AnfrageForm_Email2");
			if( e1.attr("value").search('@') == -1){ e1.addClass("form-error"); isError = true; }
			if( e1.attr("value").search('.') == -1){ e1.addClass("form-error"); isError = true; }
			if( e2.attr("value").search('@') == -1){ e2.addClass("form-error"); isError = true; }
			if( e2.attr("value").search('.') == -1){ e2.addClass("form-error"); isError = true; }
			if( e1.attr("value") != e2.attr("value") ){ e2.addClass("form-error"); isError = true; }
		}
		else if($("#Form_AnfrageForm_Email").length > 0) {
			var e1 = $("#Form_ContactForm_Email");
			if( e1.attr("value").search('@') == -1){ e1.addClass("form-error"); isError = true; }
			if( e1.attr("value").search('.') == -1){ e1.addClass("form-error"); isError = true; }
		}

		if(isError == true){ $(document).scrollTo(0, 800, {queue:true}); return false; } 
	});
	/* forms ende */
});

