$(function() {

	$("#splash a").hover(function() {

		$("strong", this).stop(true, false).animate({height: 215});

	}, function() {

		$("strong", this).animate({height: 29});

	});
	
	var slidePos = 0;
	var slideSize = $("#slideshow ul li").size();
	var slideWidth = $("#slideshow").width();
	var slideInterval;
	function nextSlide() {
	
		if(slidePos > slideSize - 1)
			slidePos = 0;
		
		$("#slideshow ol li").removeClass("active");
		$("#slideshow ol li:eq(" + slidePos + ")").addClass("active");
		$("#slideshow ul").stop(true, false).animate({left: -slideWidth * slidePos});
	
	}
	
	$("#slideshow").each(function() {
	
		slideInterval = setInterval(function() { slidePos++; nextSlide(); }, 4000);
	
	});
	
	$("#slideshow ol a").click(function() {
	
		clearInterval(slideInterval);
		slideInterval = setInterval(function() { slidePos++; nextSlide(); }, 4000);
		
		slidePos = $(this).parent().parent().find("> li").index($(this).parent());
		nextSlide();
		
		return false;
	
	});
	
	var logosDirection = "left";
	var logosWidth;
	var logosInterval;
	var logosSpeed = 2;
	function moveLogos(el) {
		
		if(logosGo && logosDirection == "left") {
		
			left = parseInt(el.find("ul").css("left"));
			el.find("ul").css("left", left - 1);
			
			var logosWidth = 0;
			el.find("ul > li").each(function() {
				logosWidth += $(this).width();
			});
			
			if(left *-1 == logosWidth/4)
				el.find("ul").css("left", 0);
				
			if(logosSpeed == 2) {
		
				left = parseInt(el.find("ul").css("left"));
				el.find("ul").css("left", left - 1);
				
				if(left *-1 == logosWidth/4)
					el.find("ul").css("left", 0);
			
			}
	
		} else if(logosGo && logosDirection == "right") {
		
			left = parseInt(el.find("ul").css("left"));
			el.find("ul").css("left", left + 1);
			
			var logosWidth = 0;
			el.find("ul > li").each(function() {
				logosWidth += $(this).width();
			});
			
			if(left == 0)
				el.find("ul").css("left", -logosWidth/4 + 1);
				
			if(logosSpeed == 2) {
		
				left = parseInt(el.find("ul").css("left"));
				el.find("ul").css("left", left + 1);
				
				if(left == 0)
					el.find("ul").css("left", -logosWidth/4 + 1);
			
			}
		
		}
	
	}
	
	var logosGo = true;
	$("#slider").each(function() {
	
		$("ul", this).append($("ul", this).html()+$("ul", this).html()+$("ul", this).html());
		
		el = $(this);
		logosInterval = setInterval(function() { moveLogos(el); }, 30);
		
		$("body").append('<div id="tooltip"></div>');
		
		el.find("ul a", this).hover(function() {
		
			logosGo = false;
			
			left = Math.max($(this).offset().left + 3, $("#slider").offset().left);
		
		});
		
		$("#slider").mouseleave(function() {
		
			logosGo = true;
		
		});
		
		el.find("ul a", this).click(function() { return false; });
		
		$(".left", this).hover(function() {
		
		}, function() {
		
			logosGo = true;
		
		});
		
		$(".left", this).hover(function() {
		
			logosGo = true;
			logosDirection = "right";
			logosSpeed = 2;
		
		}, function() {
		
			logosSpeed = 2;
		
		});
		
		$(".right", this).hover(function() {
		
			logosGo = true;
			logosDirection = "left";
			logosSpeed = 2;
			
		}, function() {
		
			logosSpeed = 2;
		
		});
	
	});

	$("select").each(function() {
		list = '';
		for(var i = 0; i < $("option", this).size(); i++) {
			list += '<li';
			if(i + 1 == $("option", this).size())
				list += ' class="last"';
			list += '>' + $("option:eq(" + i + ")", this).text() + '</li>';
		}
		
		$(this).css("position", "absolute").css("left", "-99999px")
			.before('<span class="select">' + $(":selected", this).text() + '<ul>' + list + '</ul></span>')
			.prev().click(function() {
			
			$(".select ul:visible").slideUp(300, function() { $(this).parent().css("z-index", 2); }).css("z-index", 2);
			$(document).click(function() {
				
				$(".select ul:visible").slideUp(300, function() { $(this).parent().css("z-index", 2); }).css("z-index", 2);
				$(this).unbind("click");
				
			});
			
			if($("ul:visible", this).size()) {
				
				$("ul", this).slideUp(300, function() { $(this).parent().css("z-index", 2); });
			
			}
			else {
				$(this).css("z-index", 4).next().focus();
				$("ul", this).slideDown(300, function() { $(this).parent().css("z-index", 3); }).css("z-index", 3).find(">li").unbind("click").click(function() {
					$(this).removeClass("hover");
					list = $(this).parent().parent();
					list.next().find("option:eq(" + $(this).parent().find(">li").index(this) + ")").attr("selected", true);
					list.html(list.next().find(":selected").text() + '<ul>' + $(this).parent().html() + '</ul>').find(">ul").slideUp(300, function() { $(this).parent().css("z-index", 2); }).css("z-index", 2);
					
                    var value = list.next().find(":selected").text().toLowerCase();
                    if(value == 'emmen' || value == 'zwolle' || value == 'groningen'){
                        window.location.href = '' + base_url + '' + value;
                    }
                    
                    if($(".overviewSearch").size())
						AJAXdo();
					return false;
				}).unbind("hover").hover(function() {
					$(this).addClass("hover");
				}, function() {
					$(this).removeClass("hover");
				});
			
			}
			
			return false;
		
		}).find("ul").hide();
		
		$(this).focus(function() {
			
			$(this).prev().addClass("select-active");
			
		}).blur(function() {
			$(this).prev().removeClass("select-active");
		
		});
	
	});

});
