/* Author:

*/



jQuery.fn.email = function( options ){
	//default options
	var settings = $.extend({
		'pattern' : /(.+)(\s+\[at\]\s+)(.+)(\s+\[dot\]\s+)(.+)/i,
		'replace' : '$1@$3.$5'
	}, options);

	return this.each(function(){
		$this = $(this);

		var email = $this.text().replace(settings.pattern, settings.replace);
		$this.attr({
			'href': 'mailto:'+email
		}).text(email);
	});
};

jQuery.fn.fadeIn = function(speed, callback) {
	return this.animate({
		opacity: 'show'
	}, speed, function() {
		if (jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};

jQuery.fn.fadeOut = function(speed, callback) {
	return this.animate({
		opacity: 'hide'
	}, speed, function() {
		if (jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};

jQuery.fn.labelToggle = function() {

	return this.each(function () {

		jQuery(this).focus(function () {
			if ( !jQuery(this).attr('data-label') ) {
				jQuery(this).attr('data-label', jQuery(this).val() );
			}

			if ( jQuery(this).val() == jQuery(this).attr('data-label') ) {
				jQuery(this).val('');
				jQuery(this).addClass('labelinside-off').removeClass('labelinside-on');
			}
		});

		jQuery(this).blur(function () {
			if ( jQuery(this).val() == '' && jQuery(this).attr('data-label') ) {
				jQuery(this).val( jQuery(this).attr('data-label') )
				jQuery(this).removeClass('labelinside-off').addClass('labelinside-on');
			}
		});

	});

};

function responsive() {
	if ( $(window).width() < 1200) {

		if ( $('#sidebar1').length > 0) {
			$('.widget_twitter,.widget_linkedin,.widget_nieuwsbrief').hide();
		}


		$('body').addClass('small layout960');
		$('#sidebar1').detach().appendTo('#sidebar2').addClass('moved');

		if ( $('body.home').length ) {
			$('.columnagenda').hide();
		}

		var callbacks = $(document).data('responsivecallbacks');
		if($.isArray(callbacks)){
			for(var i in callbacks){
				if(callbacks.hasOwnProperty(i)){
					callbacks[i]('small');
				}
			}
		}

	} else {
		$('.widget_twitter,.widget_linkedin,.widget_nieuwsbrief').show();
		if ( $('#sidebar1').hasClass('moved') ) {
			$('#sidebar1').detach().insertAfter('div.column600');
		}

		$('body').removeClass('small layout960');

		if ( $('body.home').length ) {
			$('.columnagenda,.columnactueel').show();
			$('.column200').css({
				'float':'left'
			});
		}

		var callbacks = $(document).data('responsivecallbacks');
		if($.isArray(callbacks)){
			for(var i in callbacks){
				if(callbacks.hasOwnProperty(i)){
					callbacks[i]();
				}
			}
		}

	}
}

jQuery(function ($) {

	$('a.email').email();

	$('.labelinside').labelToggle();

	$(window).resize(function () {
		responsive();
	});

	$('.last-child > div').addClass('last-child');

	$('.clickable').on('click',function (event) {
		event.preventDefault();
		window.location=$(this).find('a:first').attr('href');
	});

	$('li.has_subnav').hover(function(){
		if ( $('div#dimmer').length ==0 ) {
			if ( $('html').hasClass('ie7') ) {
				$('#wrapper').append('<div id="dimmer"><div></div></div>');
			} else {
				$('body').append('<div id="dimmer"><div></div></div>');
			}
		}

		$('#dimmer').stop(true,true);

		if( typeof timeout != 'undefined' ){
			clearTimeout(timeout);
		}

		if ( !$('#dimmer').is('visible') ){
			if ( $('html').is('.ie7, .ie8') ) {
				$('#dimmer').show();
			}else{
				$('#dimmer').fadeIn(300);
			}
		}
	}, function(){
		if ( $('html').is('.ie7, .ie8') ) {
			$('#dimmer').stop(true, true);
			timeout = setTimeout(function(){
				$('#dimmer').hide();
			}, 350);

		}else{
			$('#dimmer').stop(true, true).delay(350).fadeOut(300);
		}
	});

	$('li.has_subnav').hoverIntent({
		over: function () {
			$(this).children('.subnav').css({
				display:'block'
			});
		},
		out: function () {
			$(this).children('.subnav').css({
				display:'none'
			});
		},
		'timeout' : 350
	});


	$('.oddeven > div').each(function (index, value) {
		var classname = '';
		if (index%2) {
			classname='odd'
		} else {
			classname='even'
		};
		$(this).addClass(classname);
	});

	$('div.oddeven>article').each(function (index, value) {
		var classname = '';
		if (index%2) {
			classname='odd'
		} else {
			classname='even'
		};
		$(this).addClass(classname);
	});

	$('ul.oddeven').each(function (index, value) {
		$(this).find('li').each(function (index, value) {
			var classname = '';
			if (index%2) {
				classname='odd'
			} else {
				classname='even'
			};
			$(this).addClass(classname);
		});
	});

	$('ul.select').click(function () {
		$(this).find('ul').show();
	});

	$('a.share-linkedin').click(function (event) {
		event.preventDefault();
		if ( $(this).attr('data-title') ) {
			title = $(this).attr('data-title');
		} else {
			title = document.title;
		}
		url = 'http://www.linkedin.com/shareArticle?related=acconavm&mini=true&title='+encodeURIComponent(title)+'&url='+encodeURIComponent(window.location);
		window.open (url, "sharelinkedin","height=400,width=575,menubar=0,location=0,toolbar=0,status=1,directories=0");
	});

	$('a.share-twitter').click(function (event) {
		event.preventDefault();

		if ( $(this).attr('data-title') ) {
			title = $(this).attr('data-title');
		} else {
			title = document.title;
		}

		url = 'https://twitter.com/share?text='+jQuery.trim(title.split('|')[0])+'&url=' + encodeURIComponent(window.location);
		window.open (url, "sharetwitter","height=400,width=575,menubar=0,location=0,toolbar=0,status=1,directories=0");
	});

	$('select.dropkick1').dropkick({
		startSpeed: 0,
		change: function (value,label) {
			window.location = value;
		}
	});

	$('select.dropkick2').dropkick({
		startSpeed: 0,
		change: function (value,label) {
			window.location = value;
		}
	});

	$('.dienst-big h2 a.title').click(function (event) {
		event.preventDefault();
		var $dienst = $(this).closest('.dienst-big');

		if ( $dienst.hasClass('open') ) {
			$dienst.find('.entry').slideUp('fast', function () {
				$dienst.removeClass('open').addClass('closed');
			});

		} else {

			$('.dienst-big.open .entry').slideUp('fast', function () {
				$(this).closest('.dienst-big').removeClass('open').addClass('closed');
			});

			$dienst.find('.entry').slideDown('fast', function () {
				$dienst.removeClass('closed').addClass('open');
			});

		}
	});

	$(".tabs li a").click(function () {
		// switch all tabs off
		$(".active").removeClass("active");

		// switch selected tab on
		$(this).addClass("active");
	});

	$(".lightbox-nieuwsbrief").colorbox({
		iframe: true,
		width: 600,
		height: 640
	});

	$(".inline").colorbox({
		inline: true
	});

	$(".lightbox").colorbox({
		opacity: 0.8
	});

	/*
	$("textarea").bind('paste', function(event) {
            event.preventDefault();
	});*/


	$(document).on('click','#stel_een_vraag button', function (event) {
		event.preventDefault();
		window.open('http://twitter.com/share?url=&text='+encodeURIComponent($('.twitter_preview div p').text()),'twitter');
	});
	$(document).on('change', 'select.twitter_hashtag', function () {
		$('.twitter_preview div p span.onderwerp').text( ' '+$(this).val() );
	});

	$(document).on('keyup change', 'textarea.twitter_vraag', function (event) {




		var maxLength = 140;
		var length = $('.word_preview').text().length;
		if( length >= maxLength ) {
			event.preventDefault();
		} else {
			$(".word_preview").html( $(this).val() );
		}

		$('p.charCount').text( 140-$('.twitter_preview div p').text().length + ' karakters over'  );
	});


	$('input.tags').change(function () {
		$.ajax({
			url: $('form.query').attr('action') + $('input.page').val() + '/?' + $('input.tags').serialize(),
			success: function (data) {
				$('.loadmore').remove();
				$('#articles').html( $('#articles', data) );
			}
		});
	});
	$('.loadmore a').live('click',function (event) {
		$.ajax({
			url: $(this).attr('href'),
			success: function (data) {

				$('.loadmore').remove();
				$('#articles').append( $('#articles', data).hide().fadeIn() );
			}
		});
		event.preventDefault();
	});

	$('.listview').click(function () {

		if ( !$('#articles').hasClass('list') ) {
			$('#articles').addClass('list');
			$(this).find('span').text('Bekijk als artikelen');
		} else {
			$('#articles').removeClass('list');
			$(this).find('span').text('Bekijk als lijst');
		}

	});

	/* We use the initCallback callback
	 * to assign functionality to the controls
	 */
	function mycarousel_initCallback(carousel) {
		jQuery('.jcarousel-control a').bind('click', function(e) {
			e.preventDefault();
			var ret = carousel.scroll(jQuery.jcarousel.intval(jQuery(this).index() + 1));
			return false;
		});
	};

	// Ride the carousel...
	jQuery(document).ready(function() {
		jQuery("#mycarousel").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {
				onBeforeAnimation: function(carousel, element, index){
					jQuery("#mycarousel").data('index', index);
				}
			},
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	});

	// 	Carousel navigation-item width based on how many items
	$navItemWidth = 100/$('.jcarousel-control a').length+'%';
	$('.jcarousel-control a').css('width', $navItemWidth);
	//	Vertical align navigation-items so they can have different heights
	$('.jcarousel-control a img').each(function(index) {
		$navItemVertical = ($('.jcarousel-control a').height()/2) - ($(this).height()/2);
		$(this).css('margin-top', $navItemVertical);
	});

	//	add the active class to the current selected item
	$('.jcarousel-control a').click(function() {
		$(".active").removeClass("active");
		$(this).addClass('active');
	});
});

jQuery(function ($) {

	var emailRegex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|xxx)\b/i;

	var twitterRegex = /^@[a-z0-9]+$/i;

	$('[required]').each(function(){
		$this = $(this);
		if ( validate($this) ){
			$this.addClass('success').after('<span class="icon success">Success</span>');
		}
	});

	$(document).on('submit', 'form', function(event) {
		$(this).find('[required]').each(function() {
			var $this = $(this);
			if ( !validate($this) ) {
				event.stopImmediatePropagation();
				event.stopPropagation();
				event.preventDefault();

				$this.removeClass('success').siblings('span.icon.success').remove();
				if ( !$this.hasClass('error') ) {
					$this.addClass('error');
					$this.after('<span class="error">Dit veld is verplicht.</span>');
					if($('#colorbox').is(':visible')){
						$.colorbox.resize();
					}

					$this.on('blur', blurfunction = function() {
						var $this = $(this);
						if ( validate($this) ) {
							$this.removeClass('error');
							$this.siblings('span.error').remove();
							$this.off('blur', blurfunction);
							if($('#colorbox').is(':visible')){
								$.colorbox.resize();
							}
						}
					});
				}
			}
		});
	});

	$(document).on('blur', '[required]', function(e){
		var $this = $(this);
		if($this.val() != '' && !$this.hasClass('success')){
			if(validate($this)){
				$this.addClass('success').after('<span class="icon success">Success</span>');
			}
		}else{
			if(!validate($this)){
				$this.removeClass('success').siblings('span.icon.success').remove();
			}
		}
	});

	function validate($el){
		var failure = false;
		switch(true){
			case $el.hasClass('email'):
				if(!emailRegex.test($el.val())){
					failure = true;
				}
				break;
			case $el.hasClass('twitter'):
				if(!twitterRegex.test($el.val())){
					failure = true;
				}
				break;
			case ($el.val() == ''):
				failure = true;
				break;
		}
		return !failure; //reverse the boolean so we return true if validated and false when not.
	}
});

jQuery(function($){
	$(document).on('submit', '.ajaxform', function(e){
		e.preventDefault();
		$form = $(e.currentTarget);

		//werkt om een IE bug heen, vervangt # door document.location
		var action = $form.attr('action') == '#' ? document.location.href : $form.attr('action');

		$.post(action, $form.serialize(), function(data, textStatus, jqXHR){
			if(textStatus == 'success' && data.success){
				switch($form.attr('id')){
					case 'downloadflyer':
					case 'twitterflyer':
					case 'offerteaanvragen':
					case 'downloadbijlage':
					case 'twitterbijlage':
						$form.closest('#cboxLoadedContent').load($form.find('input[name="Pagina"]').val(), 'approved=true', function(){
							if($('#colorbox').is(':visible')){
								$.colorbox.resize();
							}
						});
						break;

				}
			}
		}, 'json');
	});
});
