$(document).ready(function(){
	$(window).load(function () {
		$(".carrousel").each(function() {
				new Carousel($(this));
		})
	});

	// Externe links loggen
	$('a').click(function() {
		if( window.location.hostname != this.hostname ){
			_gaq.push(['GaTracker1._trackPageview', '/uitgaand/' + this.href]);
		}
		return true;
	});

	// Aantal reacties uit Disqus weergeven
	$('a[href$=#disqus_thread]').bind('DOMNodeInserted DOMNodeRemoved',function(event){
		if(event.type == 'DOMNodeInserted'){
			if( $(this).html() > 0 ){
				$(this).show();
			}
		}
    });
	
	// GA Social links meten
	// Facebook
	if( FB ){
		FB.Event.subscribe('edge.create', function(targetUrl) {
		  _gaq.push(['GaTracker1._trackSocial', 'Facebook', 'Like', targetUrl]);
		});
	 	FB.Event.subscribe('edge.remove', function(targetUrl) {
		  _gaq.push(['GaTracker1._trackSocial', 'Facebook', 'Unlike', targetUrl]);
		});
	}
	// Twitter
	if( twttr ){
		function extractParamFromUri(uri, paramName) {
			if (!uri) {
					return;
			}
			var uri = uri.split('#')[0];  // Remove anchor.
			var parts = uri.split('?');  // Check for query params.
			if (parts.length == 1) {
				return;
			}
			var query = decodeURI(parts[1]);
			// Find url param.
			paramName += '=';
			var params = query.split('&');
			for (var i = 0, param; param = params[i]; ++i) {
				if (param.indexOf(paramName) === 0) {
					return unescape(param.split('=')[1]);
				}
			}
		}
		twttr.events.bind('tweet', function(event) {
		  if (event) {
		    var targetUrl;
		    if (event.target && event.target.nodeName == 'IFRAME') {
		      targetUrl = extractParamFromUri(event.target.src, 'url');
		    }
		    _gaq.push(['GaTracker1._trackSocial', 'Twitter', 'Tweet', targetUrl]);
		  }
		});
	}
	// LinkedIn
	function LinkedInShare(){
		_gaq.push(['GaTracker1._trackSocial', 'LinkedIn', 'Share']);
	}
	
	//placeholder polyfill
	if(!Modernizr.input.placeholder) {
		$('input[placeholder]').each(function () {
			var elem = $(this),
			    val = elem.attr('placeholder');
			elem.val(val);
			elem.focus(function () { 
				elem.val() == val && elem.val('') 
			}).blur(function () { 
				elem.val().length || elem.val(val) 
			});
		});
	}
})

function Carousel (selector) { 
    var thisClass = this;
	this.collection  = selector;
	this.numElements = 0;
	this.currentIndex = 0;
	this.interval = 0;

	this.init = function() { 
		this.numElements = this.collection.find(".slide img").length;
		
		if(this.numElements > 1) {
			this.collection.find(".first").removeClass("first");
			this.collection.find(".slide img:eq(0)").clone().appendTo(this.collection.find(".slide"));
			this.collection.find(".highlight:eq(0)").clone().appendTo(this.collection.find(".visual"));
			
			this.collection.find(".slide img:eq(0)").addClass('active');
			this.collection.find(".highlight:eq(0)").addClass('active').show();
			this.collection.find("ul li:eq(0)").addClass('active').hide();
	
			w = 0; 	
			this.collection.find(".slide img").each(function() { 
				w = $(this).outerWidth() + w; 
			});
			this.collection.find(".slide").css({ width: w });	
			this.interval = window.setTimeout(function() { thisClass.autoslide() }, 5000 );
		}
	}


	this.autoslide = function() { 
		clearTimeout(this.interval);
		this.collection  = $(this.collection);
		idx = this.collection.find('.slide img').index(this.collection.find('img.active'))+1;
		
		if(idx == this.collection.find('.slide img').length) {
			idx = 1;
			 this.collection.find('.slide').css({marginLeft : 0});
		}
		
		this.slide(idx);
		this.interval = window.setTimeout(function() { thisClass.autoslide() }, 5000 );
	}

	this.slide = function(idx) {

		this.collection.find('.highlight.active').fadeOut(300).removeClass('active');
		this.collection.find('.highlight:eq('+idx+')').addClass('active').fadeOut(0).delay(500).fadeIn(500);
		this.collection.find('ul li:first').show();
		this.collection.find('ul li:first').appendTo(this.collection.find('ul'));
		this.collection.find('ul li:first').hide();

		this.collection.find('.slide img').removeClass('active');
		this.collection.find('.slide img:eq('+idx+')').addClass('active');
		var p = 558 * idx;
		this.collection.find('.slide').stop().animate({ marginLeft: 0 -p  },500);
	}

	this.init();
}



