
var map;
var upvicon;
var userMarker;

var items = [];

var ES_south_west='35.35,-9.3';
var ES_north_east='43.80,4.55';

var ES_lat = 40.4167785645;
var ES_lon = -3.70368003845; 
var ES_zoom = 7;


var llz={
'barcelona':[41.38792,2.16992,14],
'bilbao':[43.25769,-2.94041,14],
'granada':[37.18764,-3.59217,14],
'islas baleares':[39.54508,2.89271,10],
'islas canarias':[28.39177,-15.64662,11],
'leon':[42.58829,-5.58135,14],
'madrid':[40.47623,-3.67594,14],
'malaga':[36.76018,-4.41038,14],
'salamanca':[40.95543,-5.68175,14],
'santander':[43.46096,-3.80793,14],
'sevilla':[37.38332,-5.99650,14],
'zaragoza':[41.65629,-0.87661,14]
}


function changevision() {
   $('waiting').show();

	if (items.length == 0) {loadDataPre(0);} else {items = [];}
   //map.clearOverlays();
   centermap(map);
	return false;
}

function showNoResults() {
   $('waiting').hide();
   $('totalinfo').show();   
   $('totalinfo').update('No hay resultados!');
}

function showResults(total,showing) {
   $('totalinfo').hide();
   /*
   if (total>100000) {total='more than 100000'}
   $('totalnumber').update(total);
   $('showingnumber').update(showing);
   */
}


function GetWindowWidthHeight () {
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number')
	{
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return {'width' : myWidth, 'height' : myHeight};
}

function sizeMap (s) {
  var heh = 87;
  var foh = 24;
  var wh = 200;

  var w = GetWindowWidthHeight().width;
  var h = GetWindowWidthHeight().height;

  center_w = w/2;
  
  m = $('map');
  he = $('header');
  fo = $('credit');
  wa = $('waiting');

  if (he) { he.style.top = 0; he.style.left = 0;  he.style.height=heh;}
  if (fo) { fo.style.position='absolute';fo.style.top=h-foh;fo.style.left = 0;  fo.style.height=foh;}
  if (m) { m.style.position='absolute';m.style.top = heh;m.style.bottom = foh; m.style.left = 0;  m.style.width=w; m.style.height=h-foh-heh; }
  if (wa) { wa.style.top = (h-wh)/2;wa.style.left = (w-wh)/2;wa.style.width=wh; wa.style.height=wh;}
}


function loadDataPre(delay) {
    place_name=document.forms.filterform.p_where.value;   
    price_max=document.forms.filterform.p_max_price.value;   
    price_min=document.forms.filterform.p_min_price.value;
    bedroom_min=document.forms.filterform.p_bedroom_min.value;
    property_type=document.forms.filterform.p_type.value;

    loadData(place_name,property_type,price_max,price_min,bedroom_min,delay);    
}


function showItem() {
	var t;

   map.closeInfoWindow();
	map.clearOverlays();

	if (typeof items != 'object') return;
	if (items.length == 0) {
		loadDataPre(0);
		return;
	}

	if ( t = items.pop() ) {
		markerOptions = { icon:upvicon };		
		var marker = new GMarker(new GLatLng(t[0],t[1]),markerOptions);
		map.addOverlay(marker); marker.openInfoWindowHtml(t[2]);
		$('waiting').hide();
	}

}

function showItems() {
   /*
	var t;
	
	if (typeof items != 'object') 
		return;

	if (items.length == 0) {
		loadDataPre(0);
		return;
	} 

	if ( t = items.pop() ) {
		markerOptions = { icon:upvicon };		
		var marker = new GMarker(new GLatLng(t[0],t[1]),markerOptions);
		map.addOverlay(marker); marker.openInfoWindowHtml(t[2]);
		$('waiting').hide();
	} 

	setTimeout( function () {
		map.closeInfoWindow();
		map.removeOverlay(marker);
		showItems();
	}, 7000);  
   */
}

function addItem(lat, lon, info) {
	items.push(arguments);
}


function loadData(place_name,property_type,price_max,price_min,bedroom_min,delay) {

  parameters = {};

  if (!place_name) {parameters['place_name']=place_name}
  if (!property_type) {parameters['property_type']=property_type}
  if (!price_max) {parameters['price_max']=price_max}  
  if (!price_min) {parameters['price_min']=price_min}  
  if (!bedroom_min) {parameters['bedroom_min']=bedroom_min}

  if (!place_name) {
    setTimeout("new Ajax.Request('/getitems', { parameters:{'south_west':ES_south_west,'north_east':ES_north_east,'price_max':price_max,'price_min':price_min,'property_type':property_type,'bedroom_min':bedroom_min}, method:'get', onSuccess : function(transport) { items=[];eval(transport.responseText); }})", delay);  
  }
  else {
    setTimeout("new Ajax.Request('/getitems', { parameters:{'place_name':place_name,'price_max':price_max,'price_min':price_min,'property_type':property_type,'bedroom_min':bedroom_min}, method:'get', onSuccess : function(transport) { items=[];eval(transport.responseText); }})", delay);  
  }

}

function centermap(m) {
  place_name=document.forms.filterform.p_where.value;
  if (place_name) {
    /*m.setCenter(new GLatLng(London_lat,London_lon),London_zoom)*/;
    m.setCenter(new GLatLng(llz[place_name][0],llz[place_name][1]),llz[place_name][2]);
  }
  else {m.setCenter(new GLatLng(ES_lat,ES_lon),ES_zoom);}
  
}


function initicons() {
  upvicon = new GIcon(G_DEFAULT_ICON);
  upvicon.image = "/img/pin.png";
  upvicon.shadow = "/img/pin2.png";
  upvicon.iconSize = new GSize(35,35);
  upvicon.shadowSize = new GSize(52,35);
}

function initmap(m) {
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  initicons();
  centermap(m);
}

function timeevent (sec) { 
	setTimeout( function () {
     showItem();
     timeevent(sec);
	}, sec); 

}


window.onresize = function() { sizeMap() };

window.onload = addCodeToFunction(window.onload,function() {

  if (GBrowserIsCompatible()) {    
    sizeMap('normal');
    map = new GMap2(document.getElementById("map"));
    initmap(map);

  } else {
    alert("Your browser does not support JavaScript!")
  }

  timeevent(7000);
  loadDataPre(0);
});



/* ------------- from ym4r-gm.js ------------------ */
function addCodeToFunction(func,code){
 if(func == undefined)  return code;
 else{
  return function(){
	func();
	code();
  }
 }
}

