$(function (){
	// easing setting
	$.easing.def = "easeInOutQuad";
	
	// external linking
	$('a[rel="external"]').attr('target','_blank');
	
	// 'content' and ':before' fallback for IE6 en IE7
	$('.home aside .intro').each(function() {
		if ($.browser.msie && $.browser.msie < 8) {
			$('li', this).prepend("- ");
		}
	});
	
	// CONTACT - Google Maps
	if ($('#google-map').length > 0) {
		$('#google-map').css({height: $('#google-map').height(), width: $('#google-map').width()});
		$(window).load(function() {
			var mapLatlng = new google.maps.LatLng(53.219602, 6.565704);
			var mapStatic = $('#google-map').get(0);
			var mapOptions = {
				zoom: 17,
				center: mapLatlng,
				mapTypeId: google.maps.MapTypeId.SATELLITE

			};
		    var mapDynamic = new google.maps.Map(mapStatic, mapOptions);
		 	var mapMarker = new google.maps.Marker({
				position: mapLatlng, 
				map: mapDynamic, 
				title:"Elzo Smid Ontwerp"
			});
		});
	};
	

	// HOME, PORTFOLIO & INSPIRATION - hover figure captions
	$('.slideshow, .category, .grid').find('figure').hover(function() {
		$('figcaption', this).fadeIn(250);
	}, function() {
		$('figcaption', this).fadeOut(250);
	});


	// PORTFOLIO - carousel
	$('.portfolio .category .carousel').find('a:gt(3)').hide();
	$(window).load(function() {
		if ($.browser.msie && $.browser.version < 7 ) { return; };
		
		$('.portfolio .category').each(function() {
			var $wrapper = $('.carousel', this);
			var $first = $('a.more:first', this);
			var $all = $('a.more', this);
			var oh = $first.outerHeight(true);
			var ow = $first.outerWidth(true);
			var i = $all.length;
			var pos = 0;

			$('<a class="prev">Vorige</a>').appendTo($(this)).click(function() {
				playCarousel("prev");
				setBtns($(this).parent(), i);
			});
			$('<a class="next">Volgende</a>').appendTo($(this)).click(function() {
				playCarousel("next");
				setBtns($(this).parent(), i);
			});

			$wrapper.css({ width: (ow*i), overflow: "hidden", display: "block"});

			function playCarousel(dir) {
				if (dir == "prev" && pos > 0) {
					pos--;
					$all.animate({
						left: pos*ow*-1
					}, 400);
				} else if (dir == "next" && pos < (i-4)) {
					pos++;
					$all.animate({
						left: pos*ow*-1
					}, 400);
				}
			}

			function setBtns(c, i) {
				$('.prev-disable', c).removeClass('prev-disable').addClass('prev');
				$('.next-disable', c).removeClass('next-disable').addClass('next');
				if (pos == 0) {
					$('.prev', c).removeClass('prev').addClass('prev-disable');
				}
				if (pos == i-4 || i<5) {
					$('.next', c).removeClass('next').addClass('next-disable');
				}
			}

			setBtns(this, i);
			
			$('.portfolio .category .carousel a').show();
		});
	});


	// HOME - slideshow
	$('.showreel').css("visibility", "hidden");
	$(window).load(function() {
		$('.showreel').each(function() {
			var $wrapper = $('.slideshow', this),
			$first = $('figure:first', this),
			$all = $('figure', this),
			w = $first.width(),
			i = $all.length,
			c = 0,
			pos = 0,
			currentMarginLeft = 0,
			play = true;
		
			$wrapper.css({width: (w*i), overflow: "hidden", display: "block"});
		
			// create navigation
			var $nav = $('<ul class="nav-slideshow">').insertAfter($wrapper);
			for (var j=0; j < i; j++) {
				$($all[j]).attr("id", "slide" + j);
				$nav.append('<li><a href="#slide' + j + '" title="' + $('.title', $all[j]).text() + '" alt="' + $('.title', $all[j]).text() + '">' + $('.title', $all[j]).text() + '</a></li>');
				if (j == 0) { $('li', $nav).addClass("active"); };
			};
			$nav.css("left", $nav.width()/-2);
		
			// naviagtion events
			$('a', $nav).click(function(e) {
				animateTo(e.target.hash.split("#slide")[1]);
				return false;
			});
		
			$wrapper.hover(function() {
				clearInterval(timer);
			}, function() {
				clearInterval(timer);
				if (play) {
					timer = setInterval(animateTo, 5000);
				};
			});
		
			// animation
			function animateTo(t) {
				if (!t && play) {
					if (c < i-1) {
						c++;
					} else {
						c = 0;
						play = false;
					};
				} else if (t) {
					c = parseInt(t);
				} else {
					return;
				}
				var marginLeft = c * -w;
			
				$wrapper.animate({
					marginLeft: marginLeft + 'px'
				}, 500);
			
				$('li', $nav).removeClass('active').filter(':eq(' + c + ')').addClass('active');
			}
		
			// autoplay
			timer = setInterval(animateTo, 5000);
			
			// show
			$(this).css("visibility", "visible");
		});
	});
	
	
	// ARCHIVE, INSPIRATION & TESTIMONIAL - Masonry
	$(window).resize(function() {
		if ($('body').hasClass('archive')) {
			$('body.archive #content').masonry({
				singleMode: true
			});
		};
		if ($('body').hasClass('inspiration')) {
			$('body.inspiration #content .grid').masonry({
				singleMode: true
			});
		};
		if ($('body').hasClass('testimonial')) {
			$('body.testimonial #content .grid').masonry({
				singleMode: true
			});
		};
	});
});


// INIT
WebFont.load({
	typekit: {id: 'uwc8pcm'},
	active: function() {
		$(window).trigger('resize');
	}
});

$(window).load(function() {
	$(window).trigger('resize');
	clearInterval(masonryTimer);
});

var masonryTimer = setInterval( function() { $(window).trigger('resize'); }, 500);
