$(document).ready(function() {
	/*
	alert($("ul#navigation-menu li a").attr("href"));
	$("ul#navigation-menu li a").attr({
									  href: "page?id=462&word=%F8&index=1"
									  });
	
	*/
	
	/*$("#searchForm").submit(function() {
		//alert(location.href.split("/site/")[1].split("/")[0]); //language code
		//alert("/site/"+location.href.split("/site/")[1].split("/")[0]+"/search?word="+escape($("#searchForm input.input").val())+"&search_submit=Search");
		location.href="/site/"+location.href.split("/site/")[1].split("/")[0]+"/search?word="+escape($("#searchForm input.input").val())+"&search_submit=Search";
		return false;	
	});*/
	
	$(".jsbtn").click(function() {
		//alert($(this).text());
		$("#site-menu a.selected").removeClass("selected");
		$("#site-menu .submenu").removeClass("selected").hide();
		$(this).addClass("selected");
		
		// show in Firefox
		$(this).parent().next(".submenu").addClass("selected").show();
		// show in IE
		$(this).next(".submenu").addClass("selected").show();
		
	});
	
	function newsTicker() {
		
		
		if($("#newsticker-box").find("div:last").hasClass("selected")) {
			
			$("#newsticker-box").find(".newsticker-item:first").fadeTo(1000, 0, function() {
				
				$(this).fadeTo(1000,0);
				$(this).addClass("selected");
				$(this).fadeTo(1000,1, function() {
					$(this).fadeTo(6000,1, function() {
						newsTicker();
					});
				});
				//$(this).fadeTo(10000,1);
			});
			
		} else {
			$("#newsticker-box").find(".selected:first").next(".newsticker-item").fadeTo(1000, 0, function() {
				$(this).fadeTo(1000,0);
				$(this).addClass("selected");
				$(this).fadeTo(1000,1, function() {
					$(this).fadeTo(6000,1, function() {
						newsTicker();
					});
				});
				
			});
		}
		
		
		$("#newsticker-box").find(".selected:first").fadeTo(1000, 0, function() {
			$(this).removeClass("selected");
		});	
	} // end function
	
	

	
	//newsticker
	$("#newsticker-box .newsticker-item:first").addClass("selected");
	/*
	$("#frontpage-ticker").find(".selected:first").fadeTo(1000, 0, function() {
		$(this).removeClass("selected");
	});*/

	

	
	var countF = $("#frontpage-ticker").children().size(); 
	var countN = $("#newsticker-box").children().size();
	var randItem = Math.floor(Math.random() * countF)+1;
	//frontpage ticker function
	$("#frontpage-ticker #item-"+randItem).addClass("selected");
	
	if(countN == 1 || countN == 0) {
	// do nothing for now
	} else {
		newsTicker();
	} 
	
	
	
	
	// frontpage ticker
	var countI = $("#frontpage-ticker").children().size();
		
	function imageTicker() {
		
		$("#frontpage-ticker").find("div.selected:first").fadeTo(8000, 1).fadeOut("normal", function() {
			
			if($("#frontpage-ticker").find("div.front-item:last").hasClass("selected")) {
				
				$("#frontpage-ticker").find("div.selected:first").removeClass("selected");
				$("#frontpage-ticker").find("div.front-item:first").fadeIn().addClass("selected");
				
			} else {
				
				$("#frontpage-ticker").find("div.selected:first").removeClass("selected").next("#frontpage-ticker > div").fadeIn().addClass("selected");
				
			}
			
			imageTicker();
		});
		
	} // end function
	
	if(countI > 1) {
		imageTicker();
	} // end
	
	
	$('.gallery-button').click(function(event) {
	   var clickedPosition = Number($(this).attr('rel').substring(5));
	   //alert(clickedPosition);
	   $(this).prevAll('h3').find('.currentlyShowing').text(clickedPosition);
	   
	   //$(this).prevAll('h3').hide();
	   $(this).prevAll('h3').find('.prev, .next').show();
	   
	   if (clickedPosition == 1) {
	       $(this).prevAll('h3').find('.prev').hide();
	   }
	   
	   if (clickedPosition == Number($(this).prevAll('h3').find('.currentlyShowingTotal:first').text())) {
	       $(this).prevAll('h3').find('.next').hide();
	   }
	   
	   $('#gallery-images .selected').hide().removeClass('selected');
	   $('#' + $(this).attr('rel')).fadeIn("slow", function() {
			$(this).addClass("selected");
	   });
	   event.preventDefault();
	   
	});
	
	/*
	$('.galleryNavigation a.prev').click(function(event) {
	   if ($(this).next('.currentlyShowing').text() != '1') {
	       alert('test');
	   }
	   event.preventDefault();
	});
	*/
	
	$('.galleryNavigation a.next, .galleryNavigation a.prev').click(function(event) {
	   var navigationType = $(this).closest('.galleryNavigation').hasClass('video') ? 'video' : 'image';
	   var currentlyShowing = Number($(this).siblings('.currentlyShowing:first').text());
	   var currentlyShowingTotal = Number($(this).siblings('.currentlyShowingTotal:first').text());
	   
	   var toShow = $(this).hasClass('next') ? currentlyShowing+1 : currentlyShowing-1;
	   
	   $('#btn-' + navigationType + '-' + toShow).trigger('click');
	   
	   event.preventDefault();
	});
	
	
	// GALLERY FUNCTIONS  
	$("#gallery-images .gallery-image:first").addClass("selected");
	
	var countImages = $("#gallery-images").children(".gallery-image").size(); 
	
	var currentImageID = $("#gallery-images .selected").attr("id");
	if(currentImageID) {
		var currentImage = currentImageID.replace("image-", "");
	}
	//alert(currentResult);
	//var nextIMG = "&lt;img src='images/arrow_right.gif' alt='&gt;'/&gt;";
	//var prevIMG
	
	
	$("#image-btns #image-btns-text #btn-counter span").text(currentImage + " / "+countImages);
	
	$("#image-btns #image-btns-text #btn-counter a img").hide();
	if(countImages == 1 || countImages == 0) {
		// do nothing
	} else {
		//$("#image-btns #image-btns-text #btn-counter a.next").append("&gt;");
		//$("#image-btns #image-btns-text #btn-counter a.next").html(nextIMG);
		$("#image-btns #image-btns-text #btn-counter a.next img").show();
	}
	
	
	//next btn
	$("#image-btns #image-btns-text #btn-counter a.next").click(function() {
		
		//alert($("#gallery-images .selected").next().html());
		var nextImageID = $("#gallery-images .selected").next(".gallery-image").attr("id");
		var nextImage = nextImageID.replace("image-", "");
		var currentImageID = $("#gallery-images .selected").attr("id");
		var currentImage = currentImageID.replace("image-", "");
		
		
		if(currentImage == countImages-1) {
			//$("#image-btns #image-btns-text #btn-counter a.next").html("");
			$("#image-btns #image-btns-text #btn-counter a.next img").hide();
		}
		
	
		if(nextImage) {
			// show next image
			$("#gallery-images .selected:first").next().fadeIn(function() {
				$(this).addClass("selected");
				//$("#image-btns #image-btns-text #btn-counter a.prev").html("&lt;");
				$("#image-btns #image-btns-text #btn-counter a.prev img").show();
				$("#image-btns #image-btns-text #btn-counter span").text(nextImage + " / "+countImages);
						
			});
			
			$("#gallery-images .selected:first").hide().removeClass("selected");
			
		} 
		
	});
	
	// prev btn
	$("#image-btns #image-btns-text #btn-counter a.prev").click(function() {
		
		//alert($("#gallery-images .selected").next().html());
		var prevImageID = $("#gallery-images .selected").prev(".gallery-image").attr("id");
		var prevImage = prevImageID.replace("image-", "");
		var currentImageID = $("#gallery-images .selected").attr("id");
		var currentImage = currentImageID.replace("image-", "");
		
		if(currentImage-1 == 1) {
			//$("#image-btns #image-btns-text #btn-counter a.prev").html("");
			$("#image-btns #image-btns-text #btn-counter a.prev img").hide();
		}
		
		if(prevImage) {
			
			/*
			$("#gallery-images .selected").fadeOut(function() {
				$(this).removeClass("selected");
			});
			*/
			$("#gallery-images .selected").hide().removeClass("selected");
			
			$("#gallery-images .selected:first").prev().fadeIn(function() {
				$(this).addClass("selected");
				$("#image-btns #image-btns-text #btn-counter a.next img").show();
				$("#image-btns #image-btns-text #btn-counter span").text(prevImage + " / "+countImages);
						
			});
			
		} 
		
	});
	
	
	
	// buttons
	$("#gallery-images a.gallery-btn").click(function() {
	
		var btnID = $(this).attr("id");
		var btnResult = btnID.replace("btn-", "");
		
		var currentID = $("#gallery-images .selected").attr("id");
		var currentResult = currentID.replace("image-", "");
		
		if(btnResult == countImages) {
			//$("#image-btns #image-btns-text #btn-counter a.next").text("");
			$("#image-btns #image-btns-text #btn-counter a.next img").hide();
		} else {
			$("#image-btns #image-btns-text #btn-counter a.next img").show();
			//$("#image-btns #image-btns-text #btn-counter a.next").html("&gt;");
		}
		
		if(btnResult == 1) {
			//$("#image-btns #image-btns-text #btn-counter a.prev").text("");
			$("#image-btns #image-btns-text #btn-counter a.prev img").hide();
		} else {
			//$("#image-btns #image-btns-text #btn-counter a.prev").html("&lt;");
			$("#image-btns #image-btns-text #btn-counter a.prev img").show();
		}
		
		
		$("#image-btns #image-btns-text #btn-counter span").text(btnResult + " / "+countImages);
		if(currentResult != btnResult) {
		
		      /*
			$("#gallery-images .gallery-image").fadeOut("slow", function() {
				$(this).removeClass("selected");
			});
			*/
			
			$("#gallery-images .gallery-image").hide().removeClass('selected');
			
			$("#gallery-images #image-"+btnResult).fadeIn("slow", function() {
				$(this).addClass("selected");
				
			});
		}
		
	});
	
	// end gallery
	
	//if contact form, assign some handlers to clear fields:
	
	$(".contactformfield").bind("focus", function(e){
		if(this.prevalue==null) this.prevalue=this.value;
		if(this.value==this.prevalue) {this.value="";}		
    });
	$(".contactformfield").bind("blur", function(e){
		if(this.value=="") {this.value=this.prevalue;}		
    });
	$("#register_form").bind("submit", function(e){
		var errors=false;
		var invalidStyles={'background-color' : '#c86274', 'color' : '#ffffff'};
		var validStyles={'background-color' : '#ffffff', 'color' : '#000000'};
		$('#Email').css(validStyles);
		$('#Name').css(validStyles);
		$('#Mitteilung').css(validStyles);
		$('.notificationbox').hide(500);
		
		
		if($('#Email').val()=="*Email") {
			$('#Email').css(invalidStyles);
			errors=true;
		}
		if($('#Name').val()=="*Name") {
			$('#Name').css(invalidStyles);
			errors=true;
		}
		if($('#Mitteilung').val()=="*Mitteilung") {
			$('#Mitteilung').css(invalidStyles);
			errors=true;
		}
		if(errors) {
			$('.notificationbox').show(500);
			return false;
		}
    });
	
	//if page has news list to the right, then make sure only 2 random is displayed
	if($(".news-box").length>0) {
		var items=$(".news-box");
		var item1=Math.floor(Math.random()*items.length);
		var item2=Math.floor(Math.random()*items.length);
		while(item1==item2) {item2=Math.floor(Math.random()*items.length);}
		$(".news-box").css("display", "none");
		$(".news-box:eq("+item1+")").css("display", "block");
		$(".news-box:eq("+item2+")").css("display", "block");
	}
	
	
	//if there is only one gallery image, hide the gallery navigation regardless of multiple related files.
	if($("div.gallery-image").length==1) {
		$("#image-btns").css("display", "none");
	}
});

