
function init_google_maps() {
	if(typeof(restaurant_lat) !== 'undefined' && typeof(restaurant_lng) !== 'undefined') {
		var latlng = new google.maps.LatLng(restaurant_lat, restaurant_lng);
		var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("restaurant_leftmap"), myOptions);
	
		var icon = new google.maps.MarkerImage(
		  '/wp-content/themes/skarppaamad/images/google_maps_icon.png',
			new google.maps.Size(32,34),
			new google.maps.Point(0, 0),
			new google.maps.Point(2, 32),
			new google.maps.Point(32,34)
		);
	
		var marker = new google.maps.Marker({icon: icon, position: latlng, map: map});
	}
}

var google_map_loaded = false;
function load_google_maps() {
  if(google_map_loaded==true) {
	return;
  }
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=init_google_maps";
  document.body.appendChild(script);
  google_map_loaded = true;
}

function init_blogtoppen() {
	bt_track_blog("spEB5kEUy-1l6w6qKlHbxkd5DQ,,");
  
}

function load_blogtoppen() {
  	var blogtoppen_script = document.createElement("script");
  	blogtoppen_script.type = "text/javascript";
  	blogtoppen_script.src = "http://www.blogtoppen.dk/media/js/bt_tracker.js";
  	blogtoppen_script.onload = init_blogtoppen;
// IE 6 & 7
	blogtoppen_script.onreadystatechange = function() {
		if (this.readyState == 'complete') {
			init_blogtoppen();
		}
	}
  	document.body.appendChild(blogtoppen_script);
}

jQuery(document).ready(
	function(){
		
		jQuery.fn.exists = function(){return (jQuery(this).length > 0);}

		/* HTML5 placeholder fix */
		jQuery('[placeholder]').each(function () {
			var input = jQuery(this);
			if (input.val() == '') {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).focus(function() {
			var input = jQuery(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = jQuery(this);
			if (input.val() == '') {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).blur().parents('form').submit(function() {
			jQuery(this).find('[placeholder]').each(function() {
				var input = jQuery(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			});
		});
		
		/* Searchbar hover/active effect */
		jQuery('input.searchfield').bind('mouseenter focus', function(){
			jQuery(this).addClass('searchfield_active');
		})
		.bind('mouseleave blur', function(){
			jQuery(this).removeClass('searchfield_active');
		});
		
		function filter_searchfield_to_url(text) {
			return text.replace(/\//g, "").replace(/  /g, " ").replace(/ /g, "+");
		}
	
		function search_submit() {
			hvor = jQuery('form#findrestaurant input#searchfield_hvor').val();
			if(hvor=='') {
				hvor = 'alle-steder';
			}
			hvad = jQuery('form#findrestaurant input#searchfield_hvad').val();
			if(hvad=='') {
				hvad = 'alle';
			}
			window.location = '/findrestaurant/'+filter_searchfield_to_url(hvor)+'/'+filter_searchfield_to_url(hvad)+'/';
		}
	
		jQuery('form#findrestaurant').submit( function() {
			search_submit();
			return false;
		});
		
		jQuery('a.search_tag').click( function() {
			jQuery('form#findrestaurant input#searchfield_hvad').val(jQuery(this).html());
			search_submit();
			// jQuery('form#findrestaurant').submit();
			
		});
		
		/* HTML5 geolocating part */
		if(usergeo_address!="") {
			jQuery('a#find_me_by_geo').hide();
			jQuery('a#use_my_address').show();
			jQuery('a#use_my_address').click( function() {
				jQuery('form#findrestaurant input#searchfield_hvor').val(usergeo_address);
				jQuery('a#use_my_address').fadeOut(200);
				jQuery('form#findrestaurant').submit();
				
			});	
			
		}
		
		if(navigator.geolocation) {
			jQuery('a#find_me_by_geo').click( function() {
				 navigator.geolocation.getCurrentPosition(function(position){
				    var lat = position.coords.latitude;
				    var lon = position.coords.longitude;
					jQuery.post(ajax_url, { action: "set_usergeo", lat: lat, lng: lon },  function(data) {
						if(data['usergeo_address']) {
							var usergeo_address = data['usergeo_address'];
							jQuery('form#findrestaurant input#searchfield_hvor').val(usergeo_address);
							jQuery('a#find_me_by_geo').fadeOut(200);
							jQuery('form#findrestaurant').submit();

						}
					}, "json");
				},function(error){
				    //use error.code to determine what went wrong
				});
			})
		} else {
			jQuery('a#find_me_by_geo').hide();
		}


		/* Restaurantview */
		if(typeof(restaurant_lat) !== 'undefined' && typeof(restaurant_lng) !== 'undefined') {
			load_google_maps();
		}
		
		load_blogtoppen();
		
	});
