var as_highlight = -1;
var as_size=-1;
var nav = false;


$(document).ready(function() {
	if ( jQuery('#zz_place').hasClass("large") ) {
		focusZipCode();
	}

	equalHeight($(".moreinfoAnkeilerLarge p"));
	
	$(document.body).addClass('js');

	$("ul.overview h3, ol.overview h3").click(function () {
		var jThis = $(this); 			
		var next = jThis.next();
		
		$("ul.overview h3:visible, ol.overview h3:visible").next().slideUp("normal");
		$("ul.overview h3:visible, ol.overview h3:visible").parent().removeClass('open');

		if (next.css('display') == 'none') {
			next.slideDown("normal");
			jThis.parent().addClass('open');
		} else {
			next.slideUp("normal");
			jThis.parent().removeClass('open');
		}
	});
	
	$("ul.overview h3, ol.overview h3").mouseover(function () {
		$(this).addClass('mouseover');
	});
	
	$("ul.overview h3, ol.overview h3").mouseout(function () {
		$(this).removeClass('mouseover'); 
	});	
	
 
	

	$('#zz_type').keydown(function(e) {	
		if ( e.keyCode == 40 || e.keyCode == 38 ) { //down resp. up
			nav=true;
			e.preventDefault();
			e.stopPropagation();
			if ( as_size > 0 ) {
				//$("#as_ulwrapper li").mouseout();
				if (  as_highlight>=0 ) {
					$("#as_ulwrapper li:eq("+ as_highlight+")").removeClass("highlight");
				}
				
				if (e.keyCode == 38) {-- as_highlight;}
				if (e.keyCode == 40) {++ as_highlight;}
				if (  as_highlight<0 )  {as_highlight=0;}
				if (  as_highlight>=as_size )  {as_highlight=as_size-1;}						
				
				$("#as_ulwrapper li:eq("+ as_highlight+")").addClass("highlight");	
				$("#as_ulwrapper").addClass("highlighted");	
			}
			return false;
		} else if ( e.keyCode == 13) {
			nav=true;
			e.preventDefault();
			e.stopPropagation();
			if (  as_highlight>=0 ) {
				var a = $("#as_ul li:eq("+as_highlight+") a");
				zz_typeid = $(a[0]).attr("hash").substr(1);
				var term = a.children("span").html();
				$('#zz_typecode').attr('value', zz_typeid);
				$('#zz_type').attr('value', term);	
				$('div#as_zz_type').hide();	
				as_highlight = -1;			
			}	
			return false;			
		}
	});
	
	$('#zz_type').keyup(function(e) {	

		val = e.currentTarget.value;
		if ( nav ) {
			nav =false;
		} else { // no up/down: request list
			$.get(baseDir + "ZoekSpecialisaties.axd?", { q: val }, function(data) {
				$('#as_ulwrapper').html(data);
				var total = $("#as_ul span.sub").size();
				as_size = $("#as_ul li").size();
				as_highlight = -1;
				if ( as_size == 0 && $("div.moreInfo").size() == 0 ) {
					$('#as_ulwrapper').html('<ul id="as_ul">geen specialisme gevonden</ul>');
					$('#zz_typecode').attr('value', '');
				} else if (total == 1) {
					zz_typeid = $($("#as_ul span.sub").closest("a")[0]).attr("hash").substr(1);
					$('#zz_typecode').attr('value', zz_typeid);
				}
				$("#as_ulwrapper li").hover(function(e) {
					if (  as_highlight>=0 ) {
						$("#as_ulwrapper li:eq("+ as_highlight+")").removeClass("highlight");
						$("#as_ulwrapper").removeClass("highlighted");	
					}
					as_highlight=-1;
				});
				

				$('div#as_zz_type').show();
			});
		}
		
		
	});

	$('div:not(#as_zz_type)').click(function() { $('div#as_zz_type').hide() });

	$('#as_ulwrapper').click(function(event) {
		var elem = $(event.target);
		var term = elem.html();
		term=term.replace(/<[\/]?span[^>]*>/gi,''); // sometimes IE6 has a span tag around the term, remove it
		aelem = elem.closest("a");
		zz_typeid = $(aelem[0]).attr("hash").substr(1);
		$('#zz_typecode').attr('value', zz_typeid);
		$('#zz_type').attr('value', term);
		as_highlight = -1;
		
	});

	$('.suggest_spec').click(function() {  // clicked on a searchsuggestion
		as_highlight = -1;
		$('#zz_typecode').attr('value', '');
		$('#zz_type').focus();
		$('#zz_type').attr('value', this.innerHTML.replace(/^\s+|\s+$/, ''));	
		$('#zz_type').trigger('keyup');		
		return false;
	});

});



function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}




//google geo info

var _submitbuttonid = "";

function gogglecustom(intZorgtype, strPlaats,checkzorgtype) {

    $('#zz_typecode').val(intZorgtype);
    $('#zz_place').val(strPlaats);
    return goggle(checkzorgtype);

}

function goggle(checkzorgtype) {

    if (checkzorgtype) {
		if( as_highlight == -1 ) {
			var inv2 = document.getElementById('zz_typecode').value;
			if (inv2 == '') {
				showError($('#suggestspec'), 'Voer een zorgtype in.');
				return false;
			}
		} else {
			return false;
		}
    }
	
    
    inv = document.getElementById('zz_place').value;

    if (inv != '' && inv != 'Postcode of woonplaats' && validPostcodePlaats(inv) ) {
		$('.error').remove();
		GetGeoCode(inv)
	} else {
		showError($('#suggestplace'),'Vul hier een plaatsnaam of postcode in, in de vorm: 1051HL.');
	}
	return false;
}

function validPostcodePlaats(s) {
	// begin met 3 cijfers of meer, check dan of begin valide postcode is
	if ( (s.match(/^[0-9]{3,}/) != null) && (s.match(/^[0-9]{4}[ ]*[A-Za-z]{2}/) == null) ) {
		return false;
	}
	return true;
}

function getLonLat(response) {

	if (!response || response.Status.code != 200) {
		showError($('#suggestplace'),'Niet gevonden. Vul hier een plaatsnaam of postcode in, in de vorm: 1051HL.');
		return false;
		//niet op kunnen halen

	} else {

		place = response.Placemark[0];
		var latitude = place.Point.coordinates[1];
		var longitude = place.Point.coordinates[0];
		$('#latitude').val(latitude);
		$('#longitude').val(longitude);

		var _zorgtype = document.getElementById("zz_type").value;
		
		try {
		    pageTracker._trackPageview('/zoekactie/' + _zorgtype);
		} catch (err) {
		    alert(err);
		}

		if (_submitbuttonid == "" )
		    document.getElementById('zoekform').submit();
		else {
		    __doPostBack('searchbar1$btnSearch', '')
		}
		
		
	}
}

function GetGeoCode(input) {
	var geocoder = new GClientGeocoder();
	geocoder.getLocations(input + " netherlands", getLonLat);
}

function showError(inp, txt) {
    var l = inp.prev();

    if (!l.hasClass('error')) {
		if (inp.is('#suggestspec') ) {
			inp.before('<div class="error lefte"><img src="img/structure/error_arrow.png" alt="" /> <div>' + txt + '</div></div>');
		} else {
			inp.before('<div class="error"><img src="img/structure/error_arrow.png" alt="" /> <div>' + txt + '</div></div>');
		}
    }
}


function focusZipCode () {
	var jInput = jQuery('#zz_place'),
		input = jInput[0],
		def = input.title,
		prefilled = 'prefilled';
	input.value = def;
	jInput.addClass(prefilled);
	jInput.focus(
		function (e) {
			jThis = jQuery(this);
			if (this.value == def) {
				this.value = '';
				jThis.removeClass(prefilled);
			}
			
		}
	);
	jInput.blur(
		function (e) {
			jThis = jQuery(this);
			if (!this.value) {
				this.value = def;
				jThis.addClass(prefilled);
			}
		}
	);
}




