var map;
var upvicon;
var userMarker;

var items = [];

var UK_south_west='49.55372551347579,-8.06396484375';
var UK_north_east='61.05828537037916,5.2734375';

var UK_lat = 51.5099983215;
var UK_lon = -0.134690001607; 
var UK_zoom = 7;


var llz={'london':[51.5099983215,-0.134690001607,11],
'manchester':[53.554179,-2.068176,11],
'liverpool':[53.575385,-2.60376,11],
'birmingham':[52.653895,-1.525726,11],
'glasgow':[56.008363,-3.922119,11],
'edinburgh':[55.152197,-1.29364,11],
'belfast':[54.667875,-5.751343,11],
'newcastle':[55.152197,-1.29364,11],
'sheffield':[53.580277,-1.013489,11],
'cardiff':[51.587736,-2.975922,11]
}


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 results!');
}

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':UK_south_west,'north_east':UK_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(UK_lat,UK_lon),UK_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();
	}
    }
}

