$(document).ready(function () {

	var $slide = $('.slide-right');
	var slide_index = 0;
	
	if ($slide.length){
	
	
		var $uls = $slide.find('.slide-thumbs');
		
		if ($uls.length>1)
			$slide.find('p').html('1 van ' + $uls.length);
		else{
			$slide.find('p,.arrows').css('display', 'none');
		}	

		$uls.css('display', 'none');
		$($uls.get(0)).css('display', 'block');

		$slide.find('.arrow-right').click(function(){
			slide_index++;
			
			if (slide_index >= $uls.length){
				slide_index = $uls.length - 1;
				return false;
			}
			
			if (slide_index >= $uls.length-1)
				$(this).removeClass('more-right');
			
			if (slide_index>0)
				$slide.find('.arrow-left').addClass('more-left');
			
			$slide.find('p').html((slide_index + 1) + ' van ' + $uls.length);
			
			$uls.css('display', 'none');
			$($uls.get(slide_index)).fadeIn(500);
			return false;
		});
	
		$slide.find('.arrow-left').click(function(){
			slide_index--;
			
			if (slide_index < 0){
				slide_index = 0;
				return false;
			}
			
			if (slide_index < 1)
				$(this).removeClass('more-left');
			
			if (slide_index <= $uls.length-1)
				$slide.find('.arrow-right').addClass('more-right');
			
			$slide.find('p').html((slide_index + 1) + ' van ' + $uls.length);
			
			$uls.css('display', 'none');
			$($uls.get(slide_index)).fadeIn(500);
			return false;
		});
	

	}

	// No background on the last item in the service-menu
	$("#service-menu li:last-child").addClass("last-item");
	$("#social dl:first-child").addClass("first-social");
	$(".thumb-overview .slide-thumbs li:nth-child(5n+5)").addClass("slide-thumb-last");
	$(".slide-thumbs-small li:nth-child(5n+5)").addClass("slide-thumb-last");
	$(".sidebar-block p:last-child").addClass("last-p");
	
    // Slide "more sites"
		$("#slide-content").hide()
		$(".showhide-content").click(function() {
			$("#slide-content").slideToggle("slow");
			return false;
	});
	
    // Hover and click the brandlist
	$("#slide-content dl, .content-block, .ecard").click(function(){
		window.location=$("base").attr("href")+$(this).find("a").attr("href");return false;
	});
	$("#slide-content dl, .content-block, .ecard").hover(function(){
		$(this).addClass('over-pointer'); // Plaats een class op een hover
	},function(){
	$(this).removeClass('over-pointer'); // Verwijder de class op een mouseout
	});
		
	// Give the current menu-item an arrow
	$("#main-navigation li.current a").append("<img src='images/backgrounds/arrow-current.gif' class='current-arrow' alt='' />");
	
	// Write the "tip icon" when an item has a "tip class"
	$("dd.tip a").append("<img src='images/bullets-icons/tip.png' alt='Tip!' />");
	$(".relative-social").prepend("<img src='images/globals/pump.png' alt='the pump' id='the-pump' />");
	$("#progress li span").prepend("<img src='images/globals/corners-progress.png' alt='Rondingen over afbeelding' class='corners-progress' />");
	$("#home .set-relative").append("<img src='images/globals/label-anders.png' alt='Assepoester feestkleding, gewoon anders' id='label-anders' />");
	$(".set-relative").append("<img src='images/globals/corner-lt.png' alt='' id='corner-lt' />");
	$(".set-relative").append("<img src='images/globals/corner-rt.png' alt='' id='corner-rt' />");
	$(".set-relative").append("<img src='images/globals/corner-rb.png' alt='' id='corner-rb' />");
	$(".set-relative").append("<img src='images/globals/corner-lb.png' alt='' id='corner-lb' />");
	$(".content-block").append("<img src='images/globals/corners-pasfoto.png' alt='' class='corners-pasfoto' />");
	$("#slideshow-content").prepend("<img src='images/globals/border-slideshow.png' alt='' class='zero-absolute' />");
	$(".slide-left").prepend("<img src='images/globals/slideshow-staand.png' alt='' class='zero-absolute' />");
	$("#content .slide-thumbs li a, .theme-thumbs li a").prepend("<img src='images/globals/borders-thumbs.png' alt='' class='zero-absolute' />");
	$("#content .slide-thumbs-small li a").prepend("<img src='images/globals/border-thumbs-small.png' alt='' class='zero-absolute' />");
	$("#theme .slide-thumbs li a").prepend("<img src='images/globals/border-themes.png' alt='' class='zero-absolute' />");
	$(".img-ecard").prepend("<img src='images/globals/borders-ecard.png' alt='' class='zero-absolute' />");
	$(".slide-thumbs li.current a").prepend("<img src='images/globals/current-slide.png' alt='' class='current-slide' />");
	$(".ecard:nth-child(odd)").addClass("margin-right10");
	$(".slide-thumbs-small li.video a").prepend("<img src='images/globals/play-video.png' alt='' class='play-video' />");
	$(".slide-thumbs li.video a").prepend("<img src='images/globals/play-video.png' alt='' class='play-video-big' />");


	// Accordion
	$(".accordion dt:first").addClass("active");
		$(".accordion dd:not(:first)").hide();
		$(".accordion dt").click(function(){
			$(this).next("dd").slideToggle("slow")
			.siblings("dd:visible").slideUp("slow");
			$(this).toggleClass("active");
			$(this).siblings("dt").removeClass("active");
	});	
	// Clear inputs on blur
	$("input").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// Write print link when javascript is enabled
  	$('.page-options').prepend('<li class="print"><a href="#print">Print pagina</a></li>');
	$('.page-options li.print a').click(function() {
		window.print();
		return false;
	});
	
	// Tabs slideshow
	$('#slide-tabs li a')
		.css( {backgroundPosition: "0 8px"} )
		.mouseover(function(){
			if (this.parentNode.className!='current')
				$(this).stop().animate({backgroundPosition:"(0 -53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 8px)"}, {duration:300, complete:function(){
				$(this).css({backgroundPosition: "0 8px"})
			}})
		})
	
	var imgWrapper = $('#slideshow span > img, #slideshow div > img, #home #slideshow > div, .hold-slideshow > img, .hold-slideshow > div, .slide-left > div');
		// only show the first image, hide the rest
		imgWrapper.hide().filter(':first').show();

		$('#slide-tabs li a, .slide-thumbs li a, .slide-thumbs-small li a, .test-slide li').not('a[rel^="prettyPhoto"]').click(function () {

			// check if this item doesn't have class "current"
			// if it has class "current" it must not execute the script again
			if (this.className.indexOf('current') == -1){
				imgWrapper.hide();
				imgWrapper.filter(this.hash).fadeIn(500);
				$('#slide-tabs li').removeClass('current');
				$(this.parentNode).addClass('current');
				$('.current-slide').remove();
      			$(this).prepend('<img src="images/globals/current-slide.png" class="current-slide" />');

			}
			return false;
		});


	// Ordered list
	$('.commandments').each(function(){
		children = $(this).children();
		for (var i=0; i<children.length; i++){
			$(children[i]).prepend('<span>' + (i+1) + '</span>');
		}
	})
		
	// Animate links in usp
	$('#usp-right dd a, #doormat dd a').hover(function() {
		$(this).stop().animate({ paddingLeft: '10px' }, 200);
	}, function() {
		$(this).stop().animate({ paddingLeft: 0 }, 200);
	});
	
	// Highlight form
	$('.contactform input, .contactform textarea').focus(function(){
		$(this).parents('p.row').addClass("current-form");
	}).blur(function(){
		$(this).parents('p.row').removeClass("current-form");
	});
	
	// mailchimp
	$('input[name=newsletter]').parents('form').submit(function() {
		
		if ($('input[name=newsletter]').val() != $('input[name=newsletter]').attr('defaultValue')) {
			
			var email = $('input[name=newsletter]').val();			
			$('input[name=newsletter]').val('').prev('label').text('Even geduld ...');
			
			$.post(document.location.href, { newsletter: email }, function(data) {
				$('input[name=newsletter]').prev('label').text( data.status );
			}, 'json');
			
		}
		
		return false;
		
	});
	
	// Pretty photo
	// var selector = $("a[rel^='prettyPhoto']");
	//if (selector.prettyPhoto)
	//	selector.prettyPhoto({
	//		animationSpeed: 'slow', /* fast/slow/normal */
	//		padding: 40, /* padding for each side of the picture */
	//		opacity: 0.7, /* Value betwee 0 and 1 */
	//	});


});

