$(document).ready(function() {
	// functie
	autoFill('snelzoeken_functie', 'Functie of referentienummer...', '.');
	
	// plaats
	autoFill('snelzoeken_plaats', 'Welke plaats...', '.');
	
	// nieuwsbrief
	autoFill('nieuwsbrief_email', 'Vul je e-mailadres in...', '#');
	
	$("#snelzoeken_plaats").autocomplete({
		source: "ajax/?action=autocomplete&field=plaats",
		minLength: 2,
		select: function(event, ui) {
		}
	});	
	// activeer tooltips
	$("#kaartklein a[title]").tooltip({ position: "center right"});
	
	// plaats een info-icoon achter het inputveld
	$(":input[title]").each(function() {
		if ($(this).parent().attr('nodeName') == 'DD') {
			$(this).after('<a style="margin-left: 5px;" class="info" title="' + $(this).attr('title') + '">&nbsp;</a>');
		} else {
			$(this).before('<a class="info" title="' + $(this).attr('title') + '">&nbsp;</a>');
		}
	});
	
	// select all desired input fields and attach tooltips to them
	$(":input[title]").tooltip({

		// place tooltip on the right edge
		position: "center right",

		// a little tweaking of the position
		offset: [-2, 10],

		// use the built-in fadeIn/fadeOut effect
		effect: "fade",

		// custom opacity setting
		opacity: 0.7,
		
		tipClass: 'tip'

	});
	
	// select all desired input fields and attach tooltips to them
	$(".info").tooltip({

		// place tooltip on the right edge
		position: "center right",

		// a little tweaking of the position
		offset: [-2, 10],

		// use the built-in fadeIn/fadeOut effect
		effect: "fade",

		// custom opacity setting
		opacity: 0.7,
		
		tipClass: 'tip'

	});
		
	// select all desired input fields and attach tooltips to them
	$(".info-left").tooltip({

		// place tooltip on the right edge
		position: "center left",

		// a little tweaking of the position
		offset: [10, -2],

		// use the built-in fadeIn/fadeOut effect
		effect: "fade",

		// custom opacity setting
		opacity: 0.7,
		
		tipClass: 'tip'

	});
	
	setBodyTag();
	
	$('a').each(function() {
		$(this).click(function() {
				meetDoel(this);
			})
	});
	
});

function setBodyTag() {
	var level = $('[class^=menu level]').length;
	if (level > 0) {
		$('body').addClass('menu' + level);
	}
}

function autoFill(veld, invoer, type) {
	if (type != '.') {
		type = '#';
	}
	
	$(type+veld).each(function() {
		// standaard tekst
		if ($(this).val() == invoer || $(this).val() == '') {
			$(this).val(invoer).css('color','gray');
		}
		
		// onfocus
		$(this).focus(function() {
			if ($(this).val() == invoer) {
				$(this).val('').css('color','black');
			}
		});
		
		// onblur
		$(this).blur(function() {
			if ($(this).val() == '') {
				$(this).val(invoer).css('color','gray');
			}
		});
	});
}

function writeCookie(name, basket, time) {
	jQuery.jCookie(name, '#' + basket, time, { path: '/' });
}

function sorteerVacatures() {
	// declare vars
	var url = window.location.search.substring(1);
	var foundSorteer = 0;

	// haal huidige value op van de select
	var select_value = $('#sorteer_vacatures').val();

	url_array = url.split('&');
	
	for (var sorteer in url_array) {
		// splitten van het '=' teken
		var split = url_array[sorteer].split('=');
		
		if (split[0] == 'sorteer') {
			url_array[sorteer] = split[0] + '=' + select_value;
			foundSorteer = 1;
		}
		if (split[0] == 'sortering') {
			url_array[sorteer] = split[0] + '=aflopend';
			foundSorteer = 1;
		}
		
		if (foundSorteer == 1)
			break;
	}
	
	if (foundSorteer == 0) {
		url_array.push('sorteer=' + select_value);
		url_array.push('sortering=aflopend');
	}
	
	var new_url = url_array.join('&');

	return new_url;
}

function filterOptie(checkbox) {
	// declare vars
	var url = window.location.search.substring(1);
	var foundFacet = false;
	var checkBox = $(checkbox);
	
	if (checkBox.parent().prev().prev().attr('nodeName') == 'DIV') {
		facet = checkBox.parent().prev().prev().prev().html();
	} else {
		facet = checkBox.parent().prev().prev().html();
	}
	
	//facet = facet.replace(/\s/g, '+');
	
	// items voor in de url
	var value = checkBox.attr('name');
	var set = checkBox.is(':checked');

//	facet = facet.replace(/\s/g, '+');
//	value = encodeURIComponent(value);
	facet = URLnicing(facet);
	value = URLnicing(decodeURIComponent(value));

	url_array = url.split('&');
	if (set) {
		url_array.push(facet + '=' + value);
	} else {
		for (var sorteer in url_array) {
			if ((facet + '=' + value) == url_array[sorteer]) {
				url_array.splice(sorteer, 1);
			}
		}
	}
	
	for (var sorteer in url_array) {
		var facetArray = url_array[sorteer].split('=');
		if (facetArray[0] == 'facet') {
			url_array.splice(sorteer, 1);
		}
	}
	var new_url = url_array.join('&') + '&facet=' + facet;

	return new_url;
}

function toggleVerfijn(obj) {
	
	var _parent;
	var _label;
	
	obj = $(obj);
	_label = obj.parent().prev();
	_parent = obj.parent().next().next();
	
	if (_parent.is(':visible')) {
		_parent.hide();
		obj.html('+');
	} else {
		_parent.show();
		obj.html('-');
	}
}

function meetDoel(obj) {

	try {
		if (obj.href) {
			if (obj.href.match(/^http:\/\/.*vcard\/mailto:*/)) {
				var link = obj.href.match(/mailto:(.*)\//);
				pageTracker._trackPageview('vcard:' + link[1]);
			} else if (obj.href.match(/^.*@.*$/)) {
				pageTracker._trackPageview(obj.href);
			} else if (obj.href.match(/^http:\/\/.*twitter.*/)) {
				pageTracker._trackPageview('twitter:' + obj.href);
			} else if (obj.href.match(/^http:\/\/.*linked.*/)) {
				pageTracker._trackPageview('linkedin: ' + obj.href);
			}
		}
	} catch (e) {}

}

function URLnicing (value) {
	return value.replace(/[^a-z0-9]/gi, '-').replace(/-{2,}/g, '-').replace(/^-/, '').replace(/-$/, '');
/*
	return preg_replace(
    		'/-{2,}/', '-',
    		preg_replace(
    			'/[^a-z0-9]/i', '-',
    			iconv(
    				'UTF-8',
    				'ASCII//TRANSLIT',
    				$value
    			)
    		)
    	);
*/	
}

/*
function utf8_to_cp1251($s)
{
    for ($c=0;$c<strlen($s);$c++)
    {
       $i=ord($s[$c]);
       if ($i<=127) $out.=$s[$c];
           if ($byte2){
               $new_c2=($c1&3)*64+($i&63);
               $new_c1=($c1>>2)&5;
               $new_i=$new_c1*256+$new_c2;
           if ($new_i==1025){
               $out_i=168;
           } else {
               if ($new_i==1105){
                   $out_i=184;
               } else {
                   $out_i=$new_i-848;
               }
           }
           $out.=chr($out_i);
           $byte2=false;
           }
       if (($i>>5)==6) {
           $c1=$i;
           $byte2=true;
       }
    }
    return $out;
}
*/

