var oldHtml = '';
$(document).ready(function(){
	$('.hidden').hide();
	$('a.toggle').click(function(){
		$(this).parent('div').parent('li').children('ul').slideToggle('slow');
    return false;
	});
  
  
	autoComplete();
	enableRemovers();
	addPlace();
	$("#email").focus();
	$("#password").focus();
	$("#name").focus();
	
	$('.colorpick').keyup(function(){ $(this).change()});
	
	setFontstyleCss('body_fontstyle','body_example','true');
	setFontstyleCss('header_fontstyle','header_example td','true');
	setFontstyleCss('subtitle_fontstyle','subtitle_example','true');
	setFontstyleCss('subtitle_fontstyle','subtitle_example2','true');
	
	$('#body_bordercolor').change(function(){
		$('#body_example').css('border-color',$(this).val());
		$('#inner_example').css('border-color',this.value);
		$('#header_example').css('border-color',this.value);
		$('#kader_example').css('border-color',this.value);
	});
	
});

function loadExampleCss() {
	
}
function updChkbx(example,id) {
	var cb,hidd;
	hidd = '1';
	$('.' + id).each(function(){
		cb = $(this).val();
		if ($(this).attr('checked')) hidd = hidd + cb;
	});
	setFontstyleCss(hidd,example);
	$('#' + id).val(hidd);
}

function setFontstyleCss(hidd,example,set) {
	if (set != null) {
		var id = hidd;
		hidd = $('#' + id).val();
	}
	$('#' + example).css('text-decoration','none').css('font-weight','normal').css('font-style','normal');
	if (hidd.match('u')) {
		$('#' + example).css('text-decoration','underline');
		if (set=='true') $('#' + id + '_u').attr('checked',true);
	}	
	if (hidd.match('b')) {
		$('#' + example).css('font-weight','bold');
		if (set=='true') $('#' + id + '_b').attr('checked',true);
	}
	if (hidd.match('i')) {
		$('#' + example).css('font-style','italic');
		if (set=='true') $('#' + id + '_i').attr('checked',true);		
	}
	//console.log(hidd);
}
function addPlace() {
	$('.add a').click(function () {
		var countInputs = $(".search-houses").size();
		var newDiv = countInputs + 1;
		var formHtml = $('.hidden-place-input').html();
		var html = '<div class="clearfix" id="places-wrap-' + newDiv + '">' + formHtml;
		if(countInputs < 6) {
				$('#extraplaces').append(html);
				setTimeout(autoComplete, 100);
			}
	    if($(".search-houses").size() == 6) {
	    	oldHtml = $('.add').html();
	    	$('.add').html('U heeft het maximale aantal locaties toegevoegd');
		  }
		  enableRemovers();
	});
}

function enableRemovers() {
 	$('a.field-remove').click(function(){
		 if($(".search-houses").size() == 6) {
		 	$('.add').html(oldHtml);
		 	addPlace();
		 }
   		$(this).parent('div').remove();
  	});
}
/*
function autoComplete() {
	$(".search-houses").each(function() {
		$(this).autocomplete(
			"./js/cities.php",
			{
				delay:15,
				minChars:3,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});
}
*/
var autoComplete = function() {
	$(".search-houses").each(function() {
		$(this).autocomplete(
			"./js/cities.php",
			{
				delay:15,
				minChars:3,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});
}

function SendTestMail(userid) {
	var html = '<p class="msg-suc" id="send-msg" >Testmail wordt verstuurd...</p>';
	$(".content").prepend(html);
	$.get("./consultant-testmail.php?id=" + userid, function(data){
		var html = 'Uw testmail is verstuurd';
	   $("#send-msg").html(html);

	});
}

function formatItem(row) {
		return row[0] + " (" + row[1] + ")";
	}

function ToggleInfo(userid) {
	$('#' + userid + '-moreinfo').slideToggle('slow');
}
