$(document).ready(function() {
	
	$('#opening_monday').append(' <span style="cursor: pointer; color: #cc0000;" onclick="mondayforall()"> (f&uuml;r alle &uuml;bernehmen)</span>');
	
	//$('#name,#street,#postcode,#city,#state').attr("autocomplete","off");
	$('#name,#street,#postcode,#city').attr("autocomplete","off");

	//$('#name,#street,#postcode,#city,#state').keyup(function() {
	$('#name,#street,#postcode,#city').keyup(function() {
		var aval = $(this).val();
		var aname = $('#name').val();
		var astreet = $('#street').val();
		var apostcode = $('#postcode').val();
		var acity = $('#city').val();
		var astate = $('#state').val();
		
		if (aval.length >= 2) {
			var aid = $(this).attr('id');
			$('#forminfo').load('/ajax.php',{'name':aname,'street':astreet,'postcode':apostcode,'city':acity,'state':astate,'field':aid},function() {
				if ($('#forminfo').html() != '') {
					var offset = $('#'+aid).offset();
					var aheight = $('#'+aid).height();
					var awidth = $('#'+aid).width();
					$('#forminfo').css({top: offset.top+aheight, left: offset.left, width: awidth});
					$('#forminfo').show();
				}
			});
		} else {
			$('#forminfo').hide();
		}
	});
	$('#name').blur(function() { $('#forminfo').hide(); } );
	//$('#street,#postcode,#city,#state').blur(function() {
	$('#street,#postcode,#city').blur(function() {
		$('#forminfo').hide();
		var street = $('#street').val();
		var postcode = $('#postcode').val();
		var city = $('#city').val();
		var state = $('#state').val();
		var country = 'Deutschland';
		var address = '';
		var zoom = 14;
		if (country != '') {
			address = country;
			zoom = 5;
		}
		if (country != '' && state != '') {
			address = state+" "+country;
			zoom = 7;
		}
		if (country != '' && city != '') {
			address = city+" "+country;
			zoom = 10;
		}
		if (country != '' && city != '' && postcode != '') {
			address = postcode+" "+street+" "+country;
			zoom = 12;
		}
		if (country != '' && city != '' && postcode != '' && street != '') {
			address = street+" "+postcode+" "+city+" "+country;
			zoom = 16;
		}
		if (address!='') {
			geocodeAddress(address,'/images/map/wdmnr_search.png',32,32,zoom,'maperror');
		}
	} );
	
	$('#state').change(function() { $('#city').blur(); });
	
	var add_p = $('<p align="right" class="continue"><span style="color: #cc0000; cursor: pointer;">weiter...</span></p>');
	$("dd[class^='continue_to_']").append(add_p);
	$("dd[class^='continue_to_']").children('.continue').click(function() {
		var parent = $(this).parent().get(0);
		var box = $(parent).attr('class').substr(12);
		$(this).hide();
		if (box == 'contact') {
			$('#street').blur();
		}
		$('#box_'+box).show("slow");
	});;
	
	var add_p = $('<p align="right" class="closer"><span style="color: #cc0000; cursor: pointer;">Hinweis schlie&szlig;en...</span></p>');
	$("dd.closebox").append(add_p);
	$("dd.closebox").children('.closer').click(function() {
		var parent = $(this).parent().get(0);
		var parent2 = $(parent).parent().get(0);
		$(parent2).hide("slow");
	});;
	
	$("select.selectfrom").each(function() { 
		select_enable_disable(this);
	});
	$("select.selectfrom").click(function() {
		select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	$("select.selectfrom").change(function() {
		select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	$("select.selectfrom").keyup(function() {
		select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	$("select.selectto").click(function() {
		//select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	$("select.selectto").change(function() {
		//select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	$("select.selectto").keyup(function() {
		//select_enable_disable(this);
		document.getElementById('opening_unknown').checked = false;
	});
	
	
	
});

function mondayforall() {
	var monf = document.getElementById('monday_from').selectedIndex;
	var mont = document.getElementById('monday_to').selectedIndex;
	
	document.getElementById('tuesday_from').selectedIndex = monf;
	document.getElementById('wednesday_from').selectedIndex = monf;
	document.getElementById('thursday_from').selectedIndex = monf;
	document.getElementById('friday_from').selectedIndex = monf;
	document.getElementById('saturday_from').selectedIndex = monf;
	document.getElementById('sunday_from').selectedIndex = monf;
	
	document.getElementById('tuesday_to').selectedIndex = mont;
	document.getElementById('wednesday_to').selectedIndex = mont;
	document.getElementById('thursday_to').selectedIndex = mont;
	document.getElementById('friday_to').selectedIndex = mont;
	document.getElementById('saturday_to').selectedIndex = mont;
	document.getElementById('sunday_to').selectedIndex = mont;
	
	select_enable_disable($('#tuesday_from'));
	select_enable_disable($('#wednesday_from'));
	select_enable_disable($('#thursday_from'));
	select_enable_disable($('#friday_from'));
	select_enable_disable($('#saturday_from'));
	select_enable_disable($('#sunday_from'));

}

function select_enable_disable(obj) {
	//alert($(obj).val());
	if ($(obj).val()=='-1') {
		var parent = $(obj).parent().get(0);
		var sibling = $(parent).children("select.selectto");
		$(sibling).attr("disabled","disabled");
	} else {
		var parent = $(obj).parent().get(0);
		var sibling = $(parent).children("select.selectto");
		$(sibling).removeAttr("disabled");
	}
	
	
}


function move_to_next(obj,event) {
	if (event.keyCode == 13 || event.keyCode == 9) {
		
		switch ($(obj).attr("name")) {
			case "name":
				$('#box_address').show();
				$('#state').focus();
				
				return false;
			break;
			case "street":
				$('#street').blur();
				$('#box_contact').show();
				$('#internet').focus();
				
				return false;
			break;
			case "opening_hours":
				$('#box_description').show();
				$('#description').focus();
				return false;
			break;
		}
		
	}
	if (event.keyCode == 9) {
		switch ($(obj).attr("name")) {
			case "contact":
				$('#box_opening').show();
				return false;
			break;
			case "description":
				$('#box_submit').show();
				$('#user_email').focus();
				return false;
			break;
		}
		
	}
}
