var sharethis_user = 0;
$(document).ready(function () {

	//blok klikbaar maken
	$('div.uitgelicht div.inhoud').click(function() { 
	   var href = $(this).find('a.perma').attr('href');
	   window.location = href;
	});

	$('ul.artikelen div.inhoud').click(function() { 
	   var href = $(this).find('a.perma').attr('href');
	   window.location = href;
	});

	$('div.aanbevolen div.inhoud ul li').click(function() { 
	   var href = $(this).find('a.perma').attr('href');
	   window.location = href;
	});

	//font-sizes goedzetten met JS
	$('div.uitgelicht h2').css('font-size','2.4em').css('line-height','35px');
	
	Cufon.replace('.font', { fontFamily: 'Webanalistenfont' });
	Cufon.replace('.date-single', { fontFamily: 'Webanalistenfont' });
	//alle h1, h2 en h3's moeten automatisch vervangen worden
	Cufon.replace('div.inhoud h1', { fontFamily: 'Webanalistenfont' });
	Cufon.replace('div.inhoud h2', { fontFamily: 'Webanalistenfont' });
	Cufon.replace('div.inhoud h3', { fontFamily: 'Webanalistenfont' });
	Cufon.replace('div.inhoud h4', { fontFamily: 'Webanalistenfont' });
	Cufon.replace('div.inhoud h5', { fontFamily: 'Webanalistenfont' });
	
	//zoekresultaten
	//$('#frmSubmitZoeken').click(function() {
		//getSearchResults();
		//return false;
	//});
	
	//$('#frmZoeken').keypress(function() {
		//getSearchResults();
	//});
	
	//ook nog plakken afvangen
	$('#frmZoeken').focusin(function() {
		if ($(this).val() == 'Zoek door de website') {$(this).val('');}
	});
	
	//ook nog plakken afvangen
	$('#frmZoeken').focusout(function() {
		if ($(this).val() == '') {$(this).val('Zoek door de website');resetSearchResults();}
	});
	
			
	//INLOGGEN
	//click op a. Als div met class in <li> bestaat dan die tonen. Op deze manier dynamisch.
	$('div.submenu ul li a').click(function() {
		if ($(this).is('.active')) {
		} else {
			$('div.submenu ul li a').removeClass('active');
			if ($(this).parent().find('div.popup').length) {
				$(this).parent().find('div.popup').show();
				$(this).addClass('active');
				var valueNow = $(this).html();
				$(this).attr('rel',valueNow);
				if (valueNow == 'inloggen')
					$(this).html('sluiten');
				return false;	
			}
		}
		return true;
	});
	
	$('div.submenu ul li a.active').live('click',function() {
		var valueHistory = $(this).attr('rel');
		$('div.submenu ul li a').removeClass('active');
		$('div.popup').hide();
		$(this).html(valueHistory);
		return false;
	});

	//CLICK ON INPUT/TEXTAREA WITH CLASS TEXT
	$('input.text').focusout(function() {							  
		focusOut($(this));
	});
	$('input.text').focusin(function() {
		focusIn($(this));
	});
	$('textarea.text').focusout(function() {							  
		focusOut($(this));
	});
	$('textarea.text').focusin(function() {
		focusIn($(this));
	});
	//MANAGEMENTSAMENVATTING
	$('a.managementsamenvatting').click(function() {
		switchManagementSamenvatting();
		return false;
	});
	
	$('a.btnSummaryClose').click(function() {
		switchManagementSamenvatting();
		return false;
	});
	
	//TOOLTIPS
	tooltip();
	
	$('#filter').click(function () {
		if ($('div.verbergen').is(':visible')) {
			$(this).removeClass('filtermin').addClass('filterplus');
			$('div.filtergedeelte div.verbergen').hide();			
		} else {
			$(this).removeClass('filterplus').addClass('filtermin');
			$('div.filtergedeelte div.verbergen').show();
		}
		return false;
	});	
	
	$('a.meerberichten').click(function() {
		if ($('div.loading').length == 0) {
			$(this).after('<div class="loading"><img src="'+templateurl+'/images/loading.gif" /></div>');
		} else {
			$('div.loading').fadeIn();
		}
		$(this).addClass('laden');
		showArticles();
		//setTimeout(function() {}, 1000);
		
		return false;								   
	});
	
	//SHARETHIS
	
	if ($('div.share').length > 0) {
		$('div.share').stickyfloat({lockBottom:false,openShare:180});
		$('div.share a.sharethis').click(function(event) {
			//if user clicked once, the stickyfloat wil not auto open and close anymore
			//console.log('check1:'+sharethis_user);
			sharethis_user = 1;
			//console.log('check2:'+sharethis_user);
			event.preventDefault();
			if ($('div.options').is(':visible')) {
				$(this).parent().find('div.options').stop().animate({ 'height' : 'toggle'},500,function(){});
				$(this).removeClass('sharethishide').addClass('sharethisshow');
			} else {
				$(this).parent().find('div.options').stop().animate({ 'height' : 'toggle'},500,function(){});
				$(this).removeClass('sharethisshow').addClass('sharethishide');
			}
			return false;
		});
	}
	
	//TWITTER AND FACEBOOK LIKES FIX
	setTimeout( "tfFix()", 5000 );
});

function tfFix() {
	twitterFix();
	facebookFix();
}

function twitterFix() {
	if ($('div.meta li.WPliveData_tweets span.font').length > 0) {
		var current = $('div.meta li.WPliveData_tweets span.font').html();
		var current2 = $('div.meta li.WPliveData_tweets span.font cufon').attr('alt');
		//console.log('Twitter:'+current);
		console.log('Twitter:'+current2);
		if (current != '') $('span.t-count').html(current);
		if (current2 != '') $('span.t-count').html(current2);
	}
}
function facebookFix() {
	if ($('div.meta li.WPliveData_facebook_likes span.font').length > 0) {
		var current = $('div.meta li.WPliveData_facebook_likes span.font').html();
		var current2 = $('div.meta li.WPliveData_facebook_likes span.font cufon').attr('alt');
		//console.log('FB:'+current);
		console.log('FB:'+current2);
		if (current != '') $('span.connect_widget_not_connected_text').html(current);
		if (current2 != '') $('span.connect_widget_not_connected_text').html(current2);
		$('div.connect_widget_number_cloud').hide();
	}
}
	
function focusOut(e){
	if($(e).val() == '') {
		if ($(e).attr('base') != 'none' && $(e).attr('base') != '') {
			$(e).val($(e).attr('base'));
			$(e).attr('base','');
		} else {
			//$(this).parent().find('label').show();	
		}
	}
}
function focusIn(e){
//$(this).parent().find('label').hide();
	if ($(e).attr('base') == '' || $(e).attr('base') == undefined) {
		if($(e).val() != '' && $(e).attr('base') != 'none') {
			$(e).attr('base',$(e).val());
			$(e).val('');
		} else {
			$(e).attr('base','none');
		}
	}	
}


this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 1;
		yOffset = 1;
		breedtePopup = 200;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */
	
	$("a.tip").hover(function(e){	
		this.t = this.title;
		//
		content = "<div id='tooltip' rel=\""+this.title+"\">"+ this.t +"</div>";
		console.log('content:'+content);
		$(this).addClass('tooltipactive');
		this.title = "";
		$("body").append(content);
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX - xOffset - breedtePopup) + "px")
			.fadeIn("fast")
			.hover(function() { /*tijdens hover niets doen*/},function() {
				title = $(this).attr('rel');
				//console.log('title:'+title);
				$('a.tooltipactive').attr('title',title);
				$('a.tooltipactive').removeClass('tooltipactive');
				$('#tooltip').remove();
			});
    },
	function(){
		this.title = this.t;	
		//$("#tooltip").remove();
    });
	
};

function switchManagementSamenvatting() {
	if ($('a.managementsamenvatting').hasClass('managementsamenvattingactive')) {
		//console.log('verbergen');
		$('a.managementsamenvatting').removeClass('managementsamenvattingactive');
		$('div.managementsamenvatting').hide();
	} else {
		$('a.managementsamenvatting').addClass('managementsamenvattingactive');
		$('div.managementsamenvatting').show();
	}
}

function getSearchResults() {
	
	$('div.zoeken ul.resultaten').hide();
	$('#header div.zoeken span.frmZoeken').css('height','31px');
	$('#header div.zoeken span.frmZoeken span.driehoek').css('display','block'); //hoekje in zoekveld tonen
	//query opvragen
	var q = $('#frmZoeken').val();
	
	if (q != 'Zoek door de website') {
		//TYPERESULT DING DOEN		
		$('div.zoeken ul.resultaten').show();
	} else {
		
	}
}

function resetSearchResults() {
	$('div.zoeken ul.resultaten').hide();
	$('#header div.zoeken span.frmZoeken span.driehoek').css('display','none'); //hoekje in zoekveld tonen
	$('#header div.zoeken span.frmZoeken').css('height','auto');
}

function setInhoudHover() {
	$('ul.artikelen li.artikel div.inhoud').hover(
		function() {
			$(this).addClass('inhoudhover');
			
		},
		function() {
			$(this).removeClass('inhoudhover');
	});	
}
function showArticles(e) {
		
		//function that creates posts
		var addArticles = function(postsJSON) {
			var j  = mp_desiredPosts;
			var num = 0;
			$.each(postsJSON,function(i,post) {
				var postURL 	= post.post_name;
				var id 			= 'post-' + post.ID;
				var contentLI 	= post.post_contentLI;
				$('<li></li>')
				.addClass('artikel')
				.attr('id',id)
				.html(contentLI)
				.click(function() {window.location = postURL;})
				.appendTo($('ul.artikelen'))
				.slideDown(250,function() {if(num == 0) {$.scrollTo($('li#' + id));}});
				if (j % 2 == 0) {$('li#'+id).addClass('first');}
				++num;
				--j;
			});	
			
			if (num != 6) {
				$('a.meerberichten').hide();  
			}
		};
		notinclude = notincludePages();
		$.ajax({
			url: domain+'/wp-content/plugins/morePosts/morePosts.php',
			data: {'start': mp_start,'desiredPosts': mp_desiredPosts,'notinclude':notinclude},
			type: 'get',
			dataType: 'json',
			cache: false,
			success: function(responseJSON) {mp_start += mp_desiredPosts;addArticles(responseJSON);
			},
			//failure class
			error: function() {
				//reset the message
				console.log('Oops!');
				//loadMore.text('Oops! Try Again.');
			},
			//coconsole.log(mplete event
			complete: function() {
				$('div.loading').fadeOut();
				$('div.loading').remove();
				$('a.meerberichten').removeClass('laden');
				setInhoudHover();
				Cufon.replace('div.inhoud h1', { fontFamily: 'Webanalistenfont' });
				Cufon.replace('div.inhoud h2', { fontFamily: 'Webanalistenfont' });
				Cufon.replace('div.inhoud .font', { fontFamily: 'Webanalistenfont' });
			}
		});
	}
	
	function notincludePages() {
		exclude = '';
		//uitgelicht artikel
		id = $('div.uitgelicht').attr('id');
		id =id.replace('post-','').toString();
		if (id != '')
		exclude = exclude+id;
		
		//regular articles
		$('ul.artikelen li.artikel').each(function() {
		   id = $(this).attr('id');
		   id =id.replace('post-','').toString();
		   if (exclude != '') exclude = exclude + ',' + id;
		   else exclude = exclude+id;
		});
		
		return exclude;
	}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

$(window).load(function () {

	$('iframe').each(function() {
		var url = $(this).attr("src");
		if (url.indexOf('?')) {
			character = '&';
		} else {
			character = '?';
		}
		
		$(this).attr("src",url+character+"wmode=transparent");
	});

	var vacature = $.getUrlVar('meer');
	if(vacature == 1) {
		toggleDiv('mydiv');
	}

	//img in LIVE na x seconden fadend veranderen
	//switchBackground();
	/*function switchBackground () {
		var newImg = 'bg.jpg';
		if ($('#live div.content img').attr('src') == 'images/bg.jpg') newImg = 'bg2.jpg';
		$('#live div.content img').delay(2000).fadeOut(10000, function () {						
			$(this).delay(2000).attr('src','images/'+newImg).fadeIn(10000, function () {
				switchBackground();
			});																				
      	});
	}*/
	//SHARETHIS
	$('div.share').show();
	
	$('div.uitgelicht').hover(
		function() {
			$(this).find('div.inhoud').addClass('inhoudhover');
			$(this).find('span.dezeweek').addClass('dezeweekhover');
			$(this).find('span.twinkle').addClass('twinklehover');
			
		},
		function() {
			$(this).find('div.inhoud').removeClass('inhoudhover');
			$(this).find('span.dezeweek').removeClass('dezeweekhover');
			$(this).find('span.twinkle').removeClass('twinklehover');
	});
	
	setInhoudHover();
	
	
			
	/*TABBING*/
	$('ul.tabs a').click(function() {
		//alle active classes verwijderen
		$('ul.tabs li a').removeClass('active');
		//aan geselecteerde active toekennen
		$(this).addClass('active');
		
		//achterhalen hoeveelste item in ul het is
		var index = $('ul.tabs li a').index(this);

		//alle show classes weghalen
		$(this).parent().parent().parent().find('div').removeClass('show');
		//show op corresponderende tabcontent zetten
		$(this).parent().parent().parent().find('div').eq(index).addClass('show');
		return false;
	});
		
	
	
	/* $('div.aanbevolen ul li').hover(function() {current = $(this).index(); e = $(this).parent(); hideListItems(e,current);},function() {current = $(this).index(); e = $(this).parent();showListItems(e,current);});
	
	function hideListItems(e, current) {
		$(e).find('li').each(function() {
			if ($(this).index() != current) {
				$(this).addClass('opacity30');
			}
		});
	}
	function showListItems(e, current) {
		$(e).find('li').each(function() {
			if ($(this).index() != current) {
				$(this).removeClass('opacity30');
			}
		});
	}
	*/
	//$("#doormat").fadeTo("fast", 0.5);
	//$("#doormat .onderkant").fadeTo("fast", 0.7);
	/*$("#doormat").mouseover(function () {
		//$("#doormat").fadeTo(300, 0.8);
		$("#doormat .onderkant").fadeTo(300, 1.0);
	});
	$("#doormat").mouseleave(function () {
		//$("#doormat").fadeTo(300, 0.6);
		$("#doormat .onderkant").fadeTo(300, 0.7);
	});*/

	$(window).scroll(checkScroll);
	
	function checkScroll() {
		$obj = $('#header');
		var y = $(document).scrollTop();
		if (y > 35) {
			$obj.css('position','fixed').css('margin-top','0px');
			$('.content').css('margin-top','211px');
		} else {
			$obj.css('position','relative').css('margin-top','37px');
			$('.content').css('margin-top','20px');
		}
	}
	checkScroll();
	

});

  function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }

