$(document).ready(function(){
	$('a.edit').nyroModal();
	$('#gallery a').nyroModal();
	$('.edit a').nyroModal();

	$(".radio").buttonset();
	$("input[type='submit']").button();
	$("a.button").button();

	$('#actionform #searchfield').keyup(function(e) {
		var n = $(this).val().length;
		
		if(n >= 2){
			$('#searchoutput').slideDown('fast');
			$.ajax({
				type: "GET",
				url: "/autocomplete/search/",
				data: "s="+$(this).val(),
				success: function(msg){
					$('#searchoutput').html(msg);
				}
			});
		}else{
			$('#searchoutput').hide();
			$('#searchoutput').html('');
		}
		
		if(e.keyCode == 13) {
			e.preventDefault();
			if($(this).val() !== ''){
				window.location = 'http://www.heerlijk.nl/restaurant/zoek/'+$(this).val()+'/';
			}
		}
		
	});
	$('#actionform #searchfield').blur(function(e) {
		setTimeout(function() {
			$('#searchoutput').slideUp('fast');
		}, 2500);
		
		if($(this).val() == ''){
			$(this).css('color','#999').val($(this).attr('old'));
		}
	});
	$('#actionform #searchfield').focus(function(e) {
		if($(this).val() == $(this).attr('old')){
			$(this).val('').css('color','#333');
		}
		$(this).trigger('keyup');
	});
	
	$('.restlist').click(function(e) {
		window.location = $(this).find("a").attr("href"); return false;
	});
	$('.restlist').mouseover(function(e) {
		$(this).css('cursor','pointer');
	});
	
	
//STAR
	$('img#favme').mouseover(function(e) {
		$(this).attr('src','http://www.heerlijk.nl/img/icons/star_fav.png');
	});
	$('img#favme').mouseout(function(e) {
		if( $(this).attr('fav') == '0'){
			$(this).attr('src','http://www.heerlijk.nl/img/icons/star_nfav.png');
		}
	});
	$("img#favme").click(function (e) {
		var fav = $(this).attr('fav');
		var cid = $(this).attr('cid');
		var rid = $(this).attr('rid');
		var cnt = parseInt($("span#fav").html());
		
		$.ajax({
			type: "GET",
			cache: false,
			url: "http://www.heerlijk.nl/autocomplete/favourite/"+fav+"/"+cid+"/"+rid+"/",
			success: function(msg) {
				if(fav == '1'){
					$("img#favme").attr('src','http://www.heerlijk.nl/img/icons/star_nfav.png');
					$("img#favme").attr('fav','0');
					$("span#fav").html((cnt-1));
				}else{
					$("img#favme").attr('src','http://www.heerlijk.nl/img/icons/star_fav.png');
					$("img#favme").attr('fav','1');
					$("span#fav").html((cnt+1));
				}
			}
		});
	});
//END STAR



// GIFTTICKET
	$('#actionform .amount').click(function(e) {
		var amount = $(this).val();
		$('#image-gift').attr('src','https://www.heerlijk.nl/include/cb/cb-mail-'+amount+'-small.jpg');
	});
// END GIFTTICKET	
	
	$("div.clickable").click(function(){
		window.location = $(this).find("a").attr("href"); return false;
	});
	
	
	$('#actionform #line1').keyup(function(e) {
		$('#auto-line1').html( $(this).val() );
	});
	$('#actionform #line2').keyup(function(e) {
		$('#auto-line2').html( $(this).val() );
	});
	$('#actionform #line3').keyup(function(e) {
		$('#auto-line3').html( $(this).val() );
	});
	
	
	
	$('#startdate option').each(function(e, selected){
		var a = $(selected).val();
		if( a.search(/been/i) != -1 ){
			$(this).attr('disabled','disabled');
			$(this).append(' - Al een reservering');
			$(this).css('color','red');
		}
	});
	
	
	$(".display a").click(function (e) {
		e.preventDefault();
		var href = $(this).attr('href');
		var target = $(href).attr('transition');
		
		if(target=='up'){
			$(href).slideDown('fast');
			$(href).attr('transition','down');
			$(this).html('[-]');
		}else{
			$(href).slideUp('fast');
			$(href).attr('transition','up');
			$(this).html('[+]');
		}
	});
	$("input.confirm").click(function (e) {
		$('.confirmdata').css('background','#FF9');
		var mes = $(this).attr('confirm');
		if(!mes){
			mes = 'Kloppen de ingevoerde gegevens?';
		}
		
		if (!confirm(mes)) {
			e.preventDefault();
		}
	});
	$("input.redirect").click(function (e) {
		var b = $(this).attr('backlink');
		window.location = b;
	});

	$("#likenotlike a").click(function (e) {
		var nid = parseInt($(this).attr('nid'));
		var t = parseInt($(this).attr('t'));
		
		$("#likenotlike").html('<img src="http://www.heerlijk.nl/img/default/lb-loading.gif" border="0" width="16" height="16">');
		
		$.ajax({
			type: "GET",
			cache: false,
			url: "http://www.heerlijk.nl/autocomplete/newslike/"+nid+"/"+t+"/",
			success: function(msg) {  
				$("#likenotlike").html(msg);
			}
		});
	});
	
	
	
//BUTTONS PREVIOUS NEXT
	$('span#previous').click(function() {
		var nr = parseInt($("span#news_total").html());
		var s = parseInt($("span#news_start").html());
		var l = parseInt($("span#news_limit").html());
		var ns = s+l;
		
		if(ns>=nr){
			ns = ns-l;
			$("span#previous").css('display','none');
		}
		
		$("ul#news").html('<p><img src="http://www.heerlijk.nl/img/default/lb-loading.gif" border="0" width="16" height="16" /> bezig met laden van nieuws</p>');
		
		$.ajax({
			type: "GET",
			cache: false,
			url: "http://www.heerlijk.nl/ajax/loadnews/"+ns+"/",
			success: function(msg) {  
				$("ul#news").html(msg);
				$("span#news_start").html(ns);
				$("span#next").css('display','block');
			}
		});
	});
	$('span#next').click(function() {
		var nr = parseInt($("span#news_total").html());
		var s = parseInt($("span#news_start").html());
		var l = parseInt($("span#news_limit").html());
		var ns = s-l;
		
		if(ns<=0){
			$("span#next").css('display','none');
			ns = 0;
		}
		
		$("ul#news").html('<p><img src="http://www.heerlijk.nl/img/default/lb-loading.gif" border="0" width="16" height="16" /> bezig met laden van nieuws</p>');
		
		$.ajax({
			type: "GET",
			cache: false,
			url: "http://www.heerlijk.nl/ajax/loadnews/"+ns+"/",
			success: function(msg) {  
				$("ul#news").html(msg);
				$("span#news_start").html(ns);
				$("span#previous").css('display','block');
			}
		});
	});
//ENDBUTTONS PREVIOUS NEXT

	
	
	setTimeout(function() {
		$('#message').animate({ height: $('#message').height()+10}, 150 );
		$('#message').slideUp('slow');
    }, 4500);
	
	
//SLIDER & POSTALCODE MAINPAGE
	$("#slider-range").slider({
		range: "min",
		min: 0,
		max: 75,
		value: $("#kmend").val(),
		slide: function(event, ui) {
			$("#amount").html('tussen 0 km en ' + ui.value + ' km');
			$("#kmend").val(ui.value);
		}
	});
	
	$("#kmend").val($("#slider-range").slider("values", 1));
	$("#amount").html('tussen 0 km en ' + $("#kmend").val() + ' km');
	$('#actionform2 #field_postalcode').keyup(function(e) {
		var n = $(this).val().length;
		if(n >= 2){
			$('#searchoutput2').slideDown('fast');
			$.ajax({
				type: "GET",
				url: "/autocomplete/postal/",
				data: "s="+$(this).val(),
				success: function(msg){
					$('#searchoutput2').html(msg);
				}
			});
		}else{
			$('#searchoutput2').hide();
			$('#searchoutput2').html('');
		}
		
		if(e.keyCode == 13) {
			e.preventDefault();
		}		
	});
	$('#actionform2 #field_postalcode').focus(function(e) {
		$(this).trigger('keyup');
	});
//END SLIDER & POSTALCODE MAINPAGE









	$('#checkSubmit input#submit').click(function(e){
		var gogo = parseInt($(this).attr('go'));
		if(gogo == 0){
			e.preventDefault();
		}
	});

	


	var site = 'http://www.heerlijk.nl/';
	var ssite = 'https://www.heerlijk.nl/';
	
	function check_restaurants (){
		var check 		= false;
		var startdate 	= $("#actionform select#startdate").val();
		var kmbegin		= $("#actionform input#kmbegin").val();
		var kmend 		= $("#actionform input#kmend").val();
		var blocks 		= $("#actionform input[name='blocks']:checked").val();
		
		if( (startdate > 0) && (kmend > 0) && (blocks > 0) ){
			var check = true;
			$("#actionform #check").html('<img src="'+site+'img/default/lb-loading.gif" border="0" height="16" /> bezig met laden van restaurants');
		}
		
		setTimeout(function() {
		var postalcode 	= $("#actionform input#postalcode").val();
		var persons 	= $("#actionform input[name='persons']:checked").val();

		
		if(startdate == '' || startdate == 'undefined') startdate=0; 
		if(postalcode == '' || postalcode == 'undefined' || postalcode == 'Vul een postcode of plaats in') postalcode=0; 
		if(kmbegin == '' || kmbegin == 'undefined') kmbegin=0; 
		if(kmend == '' || kmend == 'undefined') kmend=0; 
		if(blocks == '' || blocks == 'undefined') blocks=1; 
		if(persons == '' || persons == 'undefined') persons=2; 
				
		if(check){
			$.ajax({
				type: "GET",
				cache: false,
				url: site+"autocomplete/check_restaurants/"+startdate+'/'+postalcode+"/"+kmbegin+"/"+kmend+"/"+persons+"/"+blocks+"/look/",
				success: function(msg) {
					$("#actionform #check").html(msg);
					
					var se = $('#check:contains("Er is geen restaurant gevonden")').text();
					if(se){
						$('input#submit').attr('go','0');
					}else{
						$('input#submit').attr('go','1');
					}
				}
			});
		}
		}, 1000);
	}

	$("#actionform a.ui-slider-handle").mouseup(function() {
		check_restaurants();
	});
	$("#actionform select[changer='true']").change(function() {
		check_restaurants();
	});
	$("#actionform input[changer='true']").change(function() {
														   
		check_restaurants();
	});
	$("#actionform label[changer='true']").click(function() {
		check_restaurants();
	});
	
	$("#actionform #postalcode").change(function() {
		$(this).css('background', 'url('+site+'img/default/lb-loading-16.gif) no-repeat right');
		$.ajax({
			type: "GET",
			cache: false,
			url: site+"autocomplete/check_postalcode/"+ $(this).val() +"/",
			success: function(msg) {
				$("#actionform #postalcode").val(msg);
				$("#actionform #postalcode").css('background', 'none');
			}
		});

	});
	
	$(window).ready(function() {
		check_restaurants();
	});
});

function fixIeBug(btn) {
	$(btn).attr('checked', 'checked');
}

