(function($) {
	$(document).ready(function(){
		if($("input#ville").length || $("input#vid_ville").length) {
			$.ajax({
				url: '/lib/inc/ajax/tags.ajax.php',
				success: function(tags) {
					$("input#ville, input#vid_ville").autocomplete(tags.split("~"));
				}
			});
		}
		/*
		if($("input#search_box").length) {
			$.ajax({
				url: '/lib/inc/ajax/tags.ajax.php',
				success: function(tags) {
					$("input#search_box").autocomplete(tags.split("~"));
				}
			});
		}
		*/
		if($(".slideshow").length) {
			$(".sous_menu").hide();
			StartSlideShow();
			/*$(".slideshow").mouseover(function() {
				StopSlideShow();	
			});
			$(".slideshow").mouseout(function() {
				StartSlideShow();	
			});*/
		}

		$("#ddlPays, #ddlOrderBy").change(function() {
			var continent = "";
			if($("#ddlContinents").length > 0)
				continent = $.base64Encode($("#ddlContinents").val());
			var pays = "";
			if($("#ddlPays").length > 0)
				pays = $.base64Encode($("#ddlPays").val());
			var orderBy = $.base64Encode($("#ddlOrderBy").val());
			var params = { continent:continent, pays:pays, orderby:orderBy };
			params = jQuery.param(params);

			var url = location.pathname;
			if (url.indexOf("page-"))
				window.location.href = './?' + params;
			else
				window.location.href = '?' + params; 
		});

		$("#ddlContinents").change(function() {
			var continent = "";
			if($("#ddlContinents").length > 0)
				continent = $.base64Encode($("#ddlContinents").val());
			var pays = "";
			var orderBy = $.base64Encode($("#ddlOrderBy").val());
			var params = { continent:continent, pays:pays, orderby:orderBy };
			params = jQuery.param(params);

			var url = location.pathname;
			if (url.indexOf("page-"))
				window.location.href = './?' + params;
			else
				window.location.href = '?' + params; 
		});

		$("#playPauseButton").click(function() {
			if(isRunning) {
				StopSlideShow();
				$("#playPauseButton").html("<div id='playButtonImage'>&nbsp;</div>");
			}
			else {
				StartSlideShow();
				$("#playPauseButton").html("<div id='stopButtonImage'>&nbsp;</div>");
			}
			return false;
		});

		$(".retour").click(function() {
			if(history.length > 0) {
				history.go(-1);
				return false;
			}
			return true;
		});

		$(document).keydown(function(e){
			if (e.keyCode == 37) { 
			   StopSlideShow();
			   PreviousPicture();
			   return false;
			}
			if (e.keyCode == 39) { 
			   StopSlideShow();
			   NextPicture();
			   return false;
			}
		});

	});
})(jQuery);

var timer;
var isRunning = false;

function BindAlbumLinks() {
	(function($) {
		$("#albumListLoad a").each(function() {
			$(this).click(function() {
				StopSlideShow();
				$("#playPauseButton").html("<div id='playButtonImage'>&nbsp;</div>");
				$(".slideshow.On").hide().removeClass("On");
				$(".slideshow." + $(this).attr("class")).addClass("On").show();
				return false;
			});
		});
	})(jQuery);
}

function StartSlideShow() {
	timer = setInterval("NextPicture()", 3000);
	isRunning = true;
}

function StopSlideShow() {
	clearInterval(timer);
	isRunning = false;
}

function NextPicture() {
	(function($) {
		var currentPictureClass = $(".slideshow.On").attr("class");
		var currentPictureNum = currentPictureClass.split("picture-");
		currentPictureNum = currentPictureNum[1];
		currentPictureNum = currentPictureNum.split(" ");
		currentPictureNum = currentPictureNum[0];
		var nextPictureNum = parseInt(currentPictureNum) + 1;
		if(!$(".slideshow.picture-" + nextPictureNum).length)
			nextPictureNum = 1;
		$(".slideshow.On").hide().removeClass("On");
		$(".slideshow.picture-" + nextPictureNum).show().addClass("On");
	})(jQuery);
}

function PreviousPicture() {
	(function($) {
		var currentPictureClass = $(".slideshow.On").attr("class");
		var currentPictureNum = currentPictureClass.split("picture-");
		currentPictureNum = currentPictureNum[1];
		currentPictureNum = currentPictureNum.split(" ");
		currentPictureNum = currentPictureNum[0];
		var nextPictureNum = parseInt(currentPictureNum) -1;
		if(!$(".slideshow.picture-" + nextPictureNum).length)
			nextPictureNum = 1;
		$(".slideshow.On").hide().removeClass("On");
		$(".slideshow.picture-" + nextPictureNum).show().addClass("On");
	})(jQuery);
}

