$(document).ready(function() {

	Debug.Init();

	$('#debug').fadeTo(0,.75).hide();

	$("#recherche").change(function() {
		Forms.Save('recherche');
	});
	
	$('a.fancybox').fancybox({
		'zoomSpeedIn': 300, 
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.8
	});

	Create();

});

function Create() {
	$('#maplist>p>a').each(function(index) {
		var id=$(this).attr('id');
		var dotx = $('#'+id+'').attr('dotx');
		var doty = $('#'+id+'').attr('doty');
		$('#'+id+'').mouseover(function(){$('#dot_'+id+'').removeClass('map_dot_off').addClass('map_dot_on');});
		$('#'+id+'').mouseout(function(){$('#dot_'+id+'').removeClass('map_dot_on').addClass('map_dot_off');});
		MapAlsace.AddDot(id,dotx,doty);
	})
}

var Recherche = {
	Submit : function() {
		if ( $('#type_hotel').val()!='' ) {
			$('#recherche').submit();
		}
	}
}

var Forms = {
	Load : function(id) {
		//
		var formsId = '#' + id;
		$.post(
			'/ajax/form/load',
			{
				'formsId':formsId,
			},
			function(data) {
				if (data.code == 0 && data.build == 1) {
					$.each(data.data, function(i, items){
					Debug.Write('i:'+i+' /item:'+items);
				})
			}
		}, 'json');
		//
	},
	Save : function(id) {
		var formsId = '#' + id;
		$.post(
			'/ajax/form/save',
			{
				'formsId':formsId,
				'data': $(formsId).serialize()
			},
			function(data){
				//
			},
			'json'
		)
	}
}

var Debug = {
	build : 1,
	id : '#debug',
	timestamp : true,
	ts : new Date(),
	increment : 0,
	active : true,
	Init : function() {
		$('body').prepend('<div id="' + Debug.id.substr(1) + '"></div>');
		$(Debug.id).css({
			'position':'fixed',
			'width':'600px',
			'height':'200px',
			'background':'#000',
			'color':'#0F0',
			'font-family':'"Courier New", Courier, monospace',
			'font-size':'8pt',
			'border':'#F00 dashed 1px',
			'margin':'10px',
			'bottom':'0px',
			'right':'0px',
			'z-index':'9999',
			'display':'none',
			'line-height':'10px',
			'overflow':'auto'});
		if (Debug.active) $(Debug.id).show().fadeTo(0,0.5).dblclick(function() {Debug.Clear();}).click(function() { /**/ });
		Debug.Write('debug mode (build ' + Debug.build + ') init, ok.');
	},
	Write : function(value) {
		if (!Debug.active) return false;
		Debug.increment++;
		if (Debug.timestamp) ts = Debug.ts.toGMTString() + ': ';
		$(Debug.id).prepend('<p id="value' + Debug.increment + '">[' + Debug.increment + ']' + ts + value + '</p>');
		$('#value' + Debug.increment + '').css({color:'#0F0'});
		$('#value' + (Debug.increment-1) + '').css({color:'#090'});
	},
	Clear : function() {
		$(Debug.id).html('');
	}
}


/* GOOGLE MAPS */
//  google.load("maps", "2.x");
   
  // Call this function when the page has been loaded
  

var map;
var geocoder;

function initialize() {
	map = new GMap2(document.getElementById("zone_google_maps"));
    map.setCenter(new GLatLng(34, 0), 13);
    geocoder = new GClientGeocoder();
	showLocation($('#adresse').html());
}

function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("Sorry, we were unable to geocode that address");
	}else{
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
		marker = new GMarker(point);
        map.addOverlay(marker);
		marker.openInfoWindowHtml($('#pagetitle').html());
        /*marker.openInfoWindowHtml(place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);*/
	}
}
	
function showLocation(value) {
	var address = value;
	geocoder.getLocations(address, addAddressToMap);
}
	
//google.setOnLoadCallback(initialize);
  
   function findLocation(address) {
      document.forms[0].q.value = address;
      showLocation();
    }
	
	
var MapAlsace = {
	id : "#map",
	AddDot : function(id,x,y) {
		$('<p class="map_dot_off" id="dot_'+id+'"></p>').appendTo(MapAlsace.id);
		$('#dot_'+id+'').css({'position':'absolute','top':x+'px','left':y+'px'});
		$('#dot_'+id+'').mouseover(function(){$('#'+id+'').css({'color':'#fca41e'});$(this).removeClass('map_dot_off').addClass('map_dot_on');}).click(function(){
			document.location.href = $('#'+id+'').attr('href');
		});
		$('#dot_'+id+'').mouseout(function(){$('#'+id+'').css({'color':''});$(this).removeClass('map_dot_on').addClass('map_dot_off');});

		//$('#'+id+'').mouseover(function(){$(this).removeClass('map_dot_off').addClass('map_dot_on');});
		//$('#'+id+'').mouseout(function(){$('#dot_'+id+'').removeClass('map_dot_on').addClass('map_dot_off');});

	}	
}
