var toshow = 0;

var intervall;

$(document).ready(function () {
	
	if ($("#slideraround").length > 0) {
		
		$("#greatgalpic").children("img").first().show();
		intervall = window.setInterval("slidenow()", 3000);
		
	}
	
	if ($("#galerietraeger").length > 0) {
		
		$("#greatgalpic").children("img").first().show();
		
		$("#smallaround .smallgalpic").hide();
		$("#smallaround .smallgalpic").eq(0).show();
		$("#smallaround .smallgalpic").eq(1).show();
		$("#smallaround .smallgalpic").eq(2).show();
		
		
		$("#smallaround .smallgalpic").each(function (index) {
			$(this).click(function () {
				$("#greatgalpic img:visible").fadeOut(1000);
				$("#greatgalpic img").eq(index).fadeIn(1000);
			});
		});
		
		check_movers();
	}
	
	if ($("#javascriptsucc").length > 0) {
		$("#javascriptsucc").delay(3000).fadeOut(500,function () {
			$(this).remove();
		});
	}
	
	if ($("#javascriptsucc, #javascripterrs").length > 0) {
		$("body, #botok").click(hide_light);
	}
	
});

function slidenow() {
	
	var oldelement = $("#greatgalpic img:visible");
	var useindex = oldelement.index("#greatgalpic img");
	useindex++;
	if (useindex == $("#greatgalpic img").length) {
		useindex = 0;
	}
	var newelement = $("#greatgalpic img:eq("+ useindex +")");
	
	oldelement.fadeOut(1000);
	newelement.delay(100).fadeIn(1000);
	
}

function check_movers() {
	if ($("#smallaround .smallgalpic").eq(0).css('display') != 'block') {
		if ($("#movearealeft #movemeleft").length <= 0) {
			$("#movearealeft").append('<a id="movemeleft" href="#"></a>');
			$("#movemeleft").click(function () {
				move_gal(-1);
				return false;
			});
		}
	} else {
		if ($("#movemeleft").length > 0) {
			$("#movemeleft").remove();
		}
	}
	
	if ($("#smallaround .smallgalpic").last().css('display') != 'block') {
		if ($("#movearearight #movemeright").length <= 0) {
			$("#movearearight").append('<a id="movemeright" href="#"></a>');
			$("#movemeright").click(function () {
				move_gal(1);
				return false;
			});
		}
	} else {
		if ($("#movemeright").length > 0) {
			$("#movemeright").remove();
		}
	}
}

function move_gal(direction) {
	toshow += direction;
	var first_picture = toshow;
	//var foruse = toshow > 0 ? ":gt("+ first_picture +")" : "";
	var secpicture = first_picture + 1;
	var thirdpicture = first_picture + 2;
	
	if (thirdpicture >= $("#smallaround .smallgalpic").length) {
		return
	}
	
	$("#smallaround .smallgalpic").hide();
	
	$("#smallaround .smallgalpic").eq(first_picture).show();
	$("#smallaround .smallgalpic").eq(secpicture).show();
	$("#smallaround .smallgalpic").eq(thirdpicture).show();

	check_movers();
}


function hide_light() {
	$("body").unbind("click");
	$("#javascriptsucc, #javascripterrs").clearQueue();
	$("#javascriptsucc, #javascripterrs").fadeOut(500, function() {
		$(this).remove();
	});
}

function add_light(type, html) {
	
	if (type == 'err') {
		$("body").append('<div id="javascripterrs">'+ html +' <div id="botok">OK</div></div>');
	} else {
		$("body").append('<div id="javascriptsucc">'+ html +' <div id="botok">OK</div></div>');
		$("#javascriptsucc").delay(5000).fadeOut(500,function () {
			$(this).remove();
		});
	}
	
	$("#botok").click(hide_light);
}

