$(document).ready(function(){
	$('.back').css({
		cursor:'pointer'
	});
	$('.back').click(function(){
		history.back();
	});
	$('.slideshow').each(function(){
		var data = eval('('+$(this).attr('ssData')+')');
		$('.ssImage').css({visibility:'visible'});
		$(this).cycle(data);
		$(this).height(data.height);
		$(this).width(data.width);
	});

	$('.googleMap').each(function(){
		var opts = eval('('+$(this).attr('mapOptions')+')');
		var center = eval('('+opts.center+')');
		opts.center = new google.maps.LatLng(center.b,center.c);
		opts.zoom = parseFloat(opts.zoom);
		var el = document.getElementById($(this).attr('id'));
		// Markers ophalen
		var markers = [];
		$(this).find('.marker').each(function(){
			markers[markers.length] = {
				data:eval('('+$(this).attr('markerOptions')+')'),
				description:$(this).html()
			}
		});
		var map = null;
		if(el){
			map = new google.maps.Map(el,opts);
		}
		if(map != null && markers.length > 0){
			for(var i = 0; i < markers.length; i++){
				var opts = markers[i].data;
				if(opts.cmsDraggable != undefined){
					opts.draggable = opts.cmsDraggable;
				}
				if(opts.cmsClickable != undefined){
					opts.clickable = opts.cmsClickable;
				}
				var position = eval('('+opts.position+')');
				opts.position = new google.maps.LatLng(position.b,position.c);
				opts.map = map;
				var m = new google.maps.Marker(opts);
				if(markers[i].description != ''){
					var w = new google.maps.InfoWindow({
						content:markers[i].description,
						position:opts.position
					});
					w.open(map);
					google.maps.event.addListener(m, 'click', function(e){
						w.open(map);
					});
				}
			}
		}
	});
	$('#navigatie li').hover(
		function() {
		$(this).find('.dropNav').animate({height:'toggle'}, 600);
		$('.schaduw').show();
	}, function() {
		$(this).find('.dropNav').animate({height:'toggle'}, 600);
	});

	$('.subItem').hover(
		function() {
		$(this).find('.subDropNav').animate({width:'toggle'}, 600);
	}, function() {
		$(this).find('.subDropNav').animate({width:'toggle'}, 600);
	});
	
	$('.inputDate').attr('readonly', 'true');
	if($('.inputDate').length > 0) {
		$('.inputDate').datepicker({
			minDate:'+5d',
			dayNames: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
			dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
			monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
			dateFormat: 'DD dd MM yy'
		});
	}

	$(function(){
		$("ul#ticker01").liScroll({travelocity: 0.045}); //0.045
	});
	
	$('.spaansland').show();
	$('#extra').change(function(){
		var option = $(this).find(':selected');
		var cl = option.attr('class').replace('merk_', '');
		var cls = [
			'elsmorsweg',
			'spaansland',
			'essenlaan'
		];
		for(var i = 0; i < cls.length; i++){
			if(cl == cls[i]){
				$('.'+cls[i]).show();
			} else {
				$('.'+cls[i]).hide();
			}
		}
	});
	var vestiging = {
		old:null,
		current:null,
		valid:false,
		useAutoscroll:false,
		time:3000,
		setCurrent:function(cl){
			this.old = this.current;
			this.current = cl;
			this.showCurrent();
		},
		showCurrent:function(){
			if(this.current === null){
				var t = $('#vestiging');
				if(t.length > 0){
					this.valid = true;
					t.find('.vp').css({
						opacity:0
					});
					var i = $('#vestigingLijst').find('.vl').first();
					this.current = i.attr('id').replace('vl_', '');
					i.addClass('active');
					var vid = i.attr('id').replace('vl_', '');
					var v = $('#vestiging').find('#vp_'+vid);
					v.css({
						display:'block',
						opacity:1
					});
					v.addClass('actief');
				}
			} else {
				$('#vestigingLijst').find('#vl_'+this.current).addClass('active');
				$('#vestigingLijst').find('#vl_'+this.old).removeClass('active');
				var cvp = $('#vestiging').find('#vp_'+this.current);
				cvp.addClass('actief');
				cvp.animate({
					opacity:1
				},250,'swing', function(){
					$(this).css({
						display:'block'
					});
				});
				var ovp = $('#vestiging').find('#vp_'+this.old);
				ovp.removeClass('actief');
				ovp.animate({
					opacity:0
				},250,'swing', function(){
					$(this).css({
						display:'none'	
					});
				});
			}
		},
		over:false,
		timer:null,
		autoscroll:function(time){
			if(this.useAutoscroll === true){
				var v = this;
				if(v.valid){
					v.timer = setTimeout(function(){
						var n = $('#vestigingLijst').find('#vl_'+v.current).next();
						if(n.length == 0){
							n = $('#vestigingLijst').find('.vl').first();
						}
						v.setCurrent(n.attr('id').replace('vl_', ''));
						if(!v.over){
							v.autoscroll(time);
						}
					},time);
				}
			}
		}
	};

	$('.vp').mouseenter(function(){
		vestiging.over = true;
		clearTimeout(vestiging.timer);
	});
	$('.vp').mouseleave(function(){
		vestiging.over = false;
		vestiging.autoscroll(vestiging.time);
	});
	$('.vl').mouseenter(function(){
		vestiging.over = true;
		clearTimeout(vestiging.timer);
		if(!$(this).hasClass('active')){
			vestiging.setCurrent($(this).attr('id').replace('vl_', ''));
		}
	});
	$('.vl').mouseleave(function(){
		vestiging.over = false;
		vestiging.autoscroll(vestiging.time);
	});
	vestiging.showCurrent();
	vestiging.autoscroll(vestiging.time);

});
