$(document).ready(function() {
	$(function() {
		$('a.new-window').click(function() {
			window.open(this.href);
			return false;
		});
	});
	var is_touch_device = 'ontouchstart' in document.documentElement ? true : false;

	if(is_touch_device) {
		$(".drop").toggleClass("click");
		$(".click").toggleClass("drop");
		
		$(".click a").click(function() {
			$(".click").not($(this).parent()).removeClass("active");
			$(this).parent().toggleClass("active");
		});
	}

	$('.ajax').click(function(e) {
		pager(e, $(this));
	});

	$("tbody tr:odd").addClass("odd");

	$(".thumb").click(function(e) {
		e.preventDefault();
		var image = $(this).attr("href");
		$('.slide img').hide();
		$('.slide').addClass('loading');
        var img = new Image();
        $(img).load(function () {
            $(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $('.slide').removeClass('loading').html(this);
            $(this).fadeIn();
        }).error(function () {
            // notify the user that the image could not be loaded
        }).attr('src', image);
		return false;
	});


	if(typeof $.validator == 'function')
	{
		$("#contactForm").validate({
			errorElement : "span",
			submitHandler : function() {
				var form = $("#contactForm");
				$.ajax({
					url : form.attr("action"),
					data: form.serialize(),
					type: form.attr("method"),
					success: function(data) {
						form.parent().hide().html(data).show();
						_gaq.push(['_trackEvent', 'Terugbelformulier', 'Verstuurd']);
					},
					error: function(xhr, ajaxOptions, thrownError) {
						_gaq.push(['_trackEvent', 'Terugbelformulier', 'Fout']);
						alert('Fout bij versturen formulier');
					}
				});
			}
		});
	}

});

if(typeof(stepcarousel) != "undefined"){
	stepcarousel.setup({
		galleryid : 'mygallery',
		beltclass : 'belt',
		panelclass : 'panel',
		panelbehavior : {
			speed : 500,
			wraparound : true,
			persist : true
		},
		autostep : {
			enable : true,
			moveby : 1,
			pause : 10000
		},
		defaultbuttons : {
			enable : false,
			moveby : 4,
			leftnav : ['', 0, 0],
			rightnav : ['', 0, 0]
		},
		statusvars : ['statusA', 'statusB', 'statusC'],
		contenttype : ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']
	
	});
}

function pager(e, el) {
	e.preventDefault();
	var url = el.attr("href");
	$('html,body').animate({
		scrollTop : el.parent().parent().parent().offset().top - 40
	}, 600);
	$.get(url, function(data) {
		el.parent().parent().parent().hide().html(data).fadeIn();
		$('.projects a.new-window').click(function() {
			window.open(this.href);
			return false;
		});
		$('.ajax').click(function(e) {
			pager(e, $(this));
		});
	});
}
