if (document.getElementById("image") !== null) {
	var aktiv=false;
	var play=false;

	var tn_width = 150;
	var number_of_tn=$("#thumbnails .thumbnail").length;
	var box_width = 600;
	var total_width = tn_width * number_of_tn;
	var opacity = 0.3;
	var out=false;
	var old_hash;

	var center = $("#image").width() / 2;

	var hash_value = window.location.hash.slice(window.location.hash.indexOf("#") + 1);

	if (document.getElementById("edit") !== null)  {
		var old_edit = document.getElementById("edit").href;
		old_edit = old_edit.substring(0,old_edit.lastIndexOf("."));
	}
	if (document.getElementById("delete") !== null){
		var old_delete = document.getElementById("delete").href
		old_delete = old_delete.substring(0,old_delete.lastIndexOf("."));
	}

	function setHash() {
		window.location.hash = $(".thumbnail").index($("#thumbnails .active"))+1;
	}	
	
	window.onhashchange = function () {
		hash_value = window.location.hash.slice(window.location.hash.indexOf("#") + 1);
		changePic(hash_value-1);
	};
	
	window.onload = function() {
		if (window.location.hash.length < 1) {
			setHash();
			hash_value = window.location.hash.slice(window.location.hash.indexOf("#") + 1);
			var id_link = document.getElementById("slide"+(hash_value)).value;
			if (document.getElementById("edit") !== null) document.getElementById("edit").href = old_edit+"-"+id_link+".html";
			if (document.getElementById("delete") !== null) document.getElementById("delete").href = old_delete+"-"+id_link+".html";
		}
		if (window.location.hash.length > 0) {
			hash_value = window.location.hash.slice(window.location.hash.indexOf("#") + 1);
			var id_link = document.getElementById("slide"+(hash_value)).value;
			
			function firstAnimation() {
				changePic(hash_value-1);
			}
			window.setTimeout(firstAnimation,300,true);
			
			if (document.getElementById("edit") !== null) document.getElementById("edit").href = old_edit+"-"+id_link+".html";
			if (document.getElementById("delete") !== null) document.getElementById("delete").href = old_delete+"-"+id_link+".html";
		}
	};

	function getPicIndex(pic) {
		var index = $(".thumbnail").index($("#thumbnails .active"));	
		if (pic == null) {
			return index;
		}
		else if (pic == "next") {
			if ($("#thumbnails .thumbnail").length == index+1) {
				return 0;
			}
			else {
				return index+1;
			}
		}
		else if (pic == "last") {
			if (index == 0) {
				return $("#thumbnails .thumbnail").length-1;
			}
			else {
				return index-1;
			}
		}
		else
			return 0;
	}
		
	function getPicPosition(picindex) {
		var pic = $('#image .imagecontainer').get(picindex);
		var position = $(pic).position();
		return position.left - center + ($(pic).innerWidth() / 2);
	}
	
	function basename(path, suffix) {
	    // Returns the filename component of the path  
	    // 
	    // version: 1102.614
	    // discuss at: http://phpjs.org/functions/basename
	    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Ash Searle (http://hexmen.com/blog/)
	    // +   improved by: Lincoln Ramsay
	    // +   improved by: djmix
	    // *     example 1: basename('/www/site/home.htm', '.htm');
	    // *     returns 1: 'home'
	    // *     example 2: basename('ecra.php?p=1');
	    // *     returns 2: 'ecra.php?p=1'
	    var b = path.replace(/^.*[\/\\]/g, '');
	 
	    if (typeof(suffix) == 'string' && b.substr(b.length - suffix.length) == suffix) {
	        b = b.substr(0, b.length - suffix.length);
	    }
	 
	    return b;
	}

	function changePic(newpic) {
		var speed = 500;
		if(basename(window.location.href, ".html").length <= 2) speed = 0;
		if (($("#image:animated").length <= 0)) {
			var thumbnail = $('#thumbnails .thumbnail').get(newpic);
			var image = $('#image .imagecontainer').get(newpic);
			$("#thumbnails .active").fadeTo(0,opacity);
			$("#thumbnails .active").removeClass("active");
			$(thumbnail).fadeTo(0,1);
			$(thumbnail).addClass("active");
			$("#image .active").removeClass("active");
			$(image).addClass("active");
			$("#image").animate(
			{
				scrollLeft : $("#image").scrollLeft() + getPicPosition(newpic)
			},speed);
			var toSlide = (getPicIndex()*tn_width);
			var slided = $("#thumbnails").scrollLeft();	
			var compareSlide = ((box_width-tn_width)+slided);
			if (((toSlide+tn_width) > (slided+box_width)) || (toSlide < slided)) {
				if (toSlide > $("#slider").slider("option", "max")) {
					toSlide = $("#slider").slider("option", "max");
				}
				$("#thumbnails").animate({
					scrollLeft : toSlide
				},speed);
				$('#slider').slider("option", "value", toSlide);
			}
			
			old_hash = hash_value;
			window.location.hash = $(".thumbnail").index($("#thumbnails .active"))+1;
			hash_value = window.location.hash.slice(window.location.hash.indexOf("#") + 1);
			var id_link = document.getElementById("slide"+(hash_value)).value;
			if (document.getElementById("edit") !== null) document.getElementById("edit").href = old_edit+"-"+id_link+".html";
			if (document.getElementById("delete") !== null) document.getElementById("delete").href = old_delete+"-"+id_link+".html";
			return true;
		}
		return false;	
	}	

	function startPlay() {
		changePic(getPicIndex("next"));
	}

	$(document).ready(function gallery() {
		if ($("#image").length > 0) {
			$(".arrow").hover(function() {
				$(this).fadeTo(100,1);
			},function() {
				$(this).fadeTo(100,0.3);
			});

			$("#right").click(function() {
				changePic(getPicIndex('next'));
				event.preventDefault();
			});

			$("#left").click(function() {
				changePic(getPicIndex('last'));
				event.preventDefault();
			});	
			
			$(".aboutImage").click(function() {
				changePic(getPicIndex('next'));
			});
			
			$("#thumbnails a").click(function(event) { event.preventDefault(); });
			
			$(".thumbnail").click(function(event) {		
				event.preventDefault();
				if (!($(this).hasClass('active')))
					changePic($(".thumbnail").index(this));
			});

			$(".thumbnail").hover(
				function() {
					if (!($(this).hasClass("active")) && !(play))
					{
						$(this).stop().fadeTo(200,1.0);
					}
				},
				function()
				{
					if (!($(this).hasClass("active")) && !(play))
						$(this).stop().fadeTo(200,opacity,function() {
					});
				}
			);
			
			$(document).keyup(function(event) {
				if (event.keyCode == '37') { //Left arrow
					event.preventDefault();
					if (changePic(getPicIndex('last'))) {
					}
				}
				if (event.keyCode == '39') { //Right arrow
					event.preventDefault();
					if (changePic(getPicIndex('next'))) {		
					}
				}
			});
			
			if (number_of_tn<=1) {
				$("#controller img").hide();
				$("#thumbnails").hide();
			}

			if (total_width > box_width) {
				$("#slider").slider({ animate: true, max: (total_width - box_width) });
				$('#slider').bind('slidechange', function(event, ui) {
					$("#thumbnails").animate({
						scrollLeft : $("#slider").slider("value")
					},500);
				});
				$('#slider').bind('slide', function(event, ui) {
					$("#thumbnails").scrollLeft($("#slider").slider("value"));
				});
			} else {
				$("#slide_wrapper img").hide();
			}
			
			var news_list_height = 0;
			var news_box_height = 0;
			var toSlide = 0;
			
			$(".news_slider").each(function(event) {
				if($(this).prev("ul").attr("scrollHeight") > $(this).prev("ul").height()) {
					$(this).show();
					toSlide = 100;
					maxNewsScroll = $(this).prev("ul").attr("scrollHeight") - $(this).prev("ul").height();
					news_box_height = $(this).prev("ul").height();
					
					$(this).find(".slider_s").slider({animate:false, value : 100, orientation:"vertical"});
					$(this).find(".slider_s").slider().height(news_box_height);
					$(this).find('.slider_s').bind('slidechange', function(event, ui) {
						$(this).parent().prev("ul").animate({
							scrollTop : maxNewsScroll - ui.value * (maxNewsScroll / 100)
						},0);
						toSlide = ui.value;
					});
					$(this).bind('slide', function(event, ui) {
						$(this).prev("ul").scrollTop(maxNewsScroll - ui.value * (maxNewsScroll / 100));
						toSlide = ui.value * (maxNewsScroll / 100);
					});
					$(this).find(".bottomarrow").click(function() {
						toSlide -= 20;
						$(this).parent().find(".slider_s").slider("option", "value", toSlide);
					});
					$(this).find(".toparrow").click(function() {
						toSlide += 20;
						$(this).parent().find(".slider_s").slider("option", "value", toSlide);
					});
				} else {
					$(this).hide();
				}
			});
			
			var content_list_height = 0;
			var cSlide = 0;
			var maxContentScroll = $(".aboutText").attr("scrollHeight") - $(".aboutText").height();
			
			$(".aboutText").each(function(event) {
				if($(this).attr("scrollHeight") > $(this).height()) {
					$(this).next(".content_slider").show();
					cSlide = 100;
					maxContentScroll = $(this).attr("scrollHeight") - $(this).height()
					$(this).next(".content_slider").find(".slider_c").slider({animate:false, value:100, orientation:"vertical"});
					$(this).next(".content_slider").find(".slider_c").slider().height($(this).height()-40);
					$(this).next(".content_slider").find(".slider_c").bind('slidechange', function(event, ui) {
						$(this).parent().prev(".aboutText").animate({
							scrollTop : maxContentScroll - ui.value * (maxContentScroll / 100)
						},0);
						cSlide = ui.value;
					});
					$(this).next(".content_slider").find(".slider_c").bind('slide', function(event, ui) {
						$(this).parent().prev(".aboutText").scrollTop(maxContentScroll - ui.value * (maxContentScroll / 100));
						cSlide = ui.value * (maxContentScroll / 100);
					});
					$(this).next(".content_slider").find(".bottomarrow").click(function() {
						cSlide -= 20;
						$(this).parent().find(".slider_c").slider("option", "value", cSlide);
					});
					$(this).next(".content_slider").find(".toparrow").click(function() {
						cSlide += 20;
						$(this).parent().find(".slider_c").slider("option", "value", cSlide);
					});
				} else {
					$(this).next(".content_slider").hide();
				}
			});
		}
	});
}
