if(typeof Prototype != 'undefined') {

	Object.extend(Function.prototype, {
		onLoad: function() {
			Event.observe(window, "load", this);
		}
	});

	window.loading = true;
	(function() { window.loading = false }).onLoad();

	Object.extend(Element, {
		withinViewport: function(el) {
			var elOffset = el.cumulativeOffset();
			var vpOffset = document.viewport.getScrollOffsets();
			var elDim = el.getDimensions();
			var vpDim = document.viewport.getDimensions();
			if (elOffset[1] + elDim.height < vpOffset[1] || elOffset[1] > vpOffset[1] + vpDim.height ||
				elOffset[0] + elDim.width < vpOffset[0]  || elOffset[0] > vpOffset[0] + vpDim.width) {
					return false;
				}
			return true;
		}
	});
}
if((typeof tinyMCE!='undefined')) {
	Event.observe(window, "load", function() {
			if ($$("textarea.auto-tinymce")) {
				tinyMCE.init({
						mode:"specific_textareas",
						editor_selector:"auto-tinymce",
						theme:"advanced",
						theme_advanced_buttons1:"bold,italic,underline,separator,strikethrough,undo,redo,bullist,numlist,link,unlink,formatselect",
						theme_advanced_buttons2:"",
						theme_advanced_buttons3:"",
						theme_advanced_path_location:"none",
						theme_advanced_blockformats:"p,h1,h2,preformatted",
						valid_elements:"strong,b,em,i,u,ul,ol,li,a,p,h1,h2,pre,br",
						paste_create_paragraphs : true,
						paste_auto_cleanup_on_paste : true,
						paste_convert_middot_lists : true,
						paste_unindented_list_class : "unindentedList",
						paste_convert_headers_to_strong : false,
						imagemanager_document_base_url: "/",
						imagemanager_relative_urls: false,
						imagemanager_remove_script_host: true,
						content_css:"/property_editor_styles.css"
				});
		}
	});
}

var businesshighlight='';
pagehasmap=true;
var curpoint, curmarker;
var infoTabs = new Array();
var curtab;
var markerList=new Array();
var propertyList={};
var geodataCategories=new Array();
var geodataMarkers=new Array();

if (!salePrices)
	var salePrices = [75000, 100000, 125000, 150000, 175000, 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000, 400000, 425000, 450000, 475000, 500000, 750000, 1000000];
if (!letPrices)
	var letPrices = [350, 450, 500, 550, 600, 650, 700, 800, 1000, 1500, 2000];


function popUp(URL,width,height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left = 100,top = 100');");
}

function searchForAndPopupMarker(xs) {
	// Could have property ID in xs[0]:
	pid = xs[0];
	if (propertyList[pid]) {
		GEvent.trigger(propertyList[pid], "click");
		return;
	}
	for(var j=0; j<xs.length; j++) {
		//alert(xs[j]);
		for(var i=0; i<markerList.length; i++){
			if(xs[j]==markerList[i][0]) {
				//alert('match!');
				GEvent.trigger(markerList[i][1],'click');
				return;
			}
		}
	}
	alert('Property not visible on map, wait for map to load or try zooming out.');
}
function createLinkMarker(point,description,icon,linkurl) {
        var marker = new GMarker(point, {title:description, icon:icon});
        map.addOverlay(marker);
        GEvent.addListener(marker, "click", function() {
                //alert(linkurl);
                document.location.href=linkurl;
        });
}
function createZoomMarker(point,description,icon,zoomlevel) {
        var marker = new GMarker(point, {title:description, icon:icon});
        map.addOverlay(marker);
        GEvent.addListener(marker, "click", function() {
                //alert(marker.getPoint().toString()+marker.description+marker.foo); 
                map.setCenter(point,zoomlevel);
        });
}

function autoAddMarkers (criteria) {
        var center = map.getCenter();
        var latLngStr = center.toString();
        var bounds = map.getBounds();
        var sw = bounds.getSouthWest();
        var ne = bounds.getNorthEast();
        searchBusinessesAddMarkers(
                ne.lat(), ne.lng(), sw.lat(), sw.lng(), map.getZoom(), map, criteria);
}
function resizeV2(map, points) {
	var bounds = new GBounds(points);

	var sw = new GLatLng(bounds.maxY, bounds.minX);
	var ne = new GLatLng(bounds.minY, bounds.maxX);

	var latLngBounds = new GLatLngBounds(sw, ne);

	var center = latLngBounds.getCenter();
	var zoom = map.getBoundsZoomLevel(latLngBounds);
	map.setCenter(center, zoom);
}

function resizeFromLatLng(sw,ne,zoom) {
	var latLngBounds = new GLatLngBounds(sw, ne);
	var center = latLngBounds.getCenter();
	if (!zoom)
		var zoom = map.getBoundsZoomLevel(latLngBounds);
	map.setCenter(center, zoom);
}

var firstrun=true;
var points = new Array();
var oldzoom, newzoom;

if (typeof(Array.prototype.strInArray) == "undefined") {
	Array.prototype.strInArray = function (value) {
		// Returns true if the passed string is found in the
		// array. Returns false if it is not.
		var i;
		var tl = this.length;
		for (i=0; i < tl; i++) {
			if (this[i]==value) {
				return true;
			}
		}
		return false;
	};
}
if (typeof(Array.prototype.inArray) == "undefined")
	Array.prototype.inArray = Array.prototype.strInArray;

function mapLayerCheckboxes() {
	$$("input.maplayercheckbox").each(function(el) {
			name = el.id;
			switchLayer(name, $F(el));
	});
}
function switchLayer(layer,state) {
	if(geodataMarkers[layer]) {
		for(i=0;i<geodataMarkers[layer].length;i++) {
			if(state) {
				geodataMarkers[layer][i].show();
			} else {
				geodataMarkers[layer][i].hide();
			}
		}
	}
}

var googlemaps_debug = false;

function searchBusinessesAddMarkers(nelat, nelng, swlat, swlng, zoom, map, criteria) {
	GDownloadUrl("/xml/ajax/find_businesses.php?nelat="+nelat+"&nelng="+nelng+"&swlat="+swlat+"&swlng="+swlng+"&zoom="+zoom+"&"+criteria, function(data, responseCode) {
			//GLog.write("/ajax/find_businesses.php?nelat="+nelat+"&nelng="+nelng+"&swlat="+swlat+"&swlng="+swlng+"&zoom="+zoom);
			//console.log(data);
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			var i = 0;
			newzoom = zoom;
			if((!firstrun && (oldzoom != newzoom))) {
				map.clearOverlays();
				points = new Array();
			}
			oldzoom = newzoom;
			//markerList=new Array(); // clear marker list for searching
			for (i = 0; i < markers.length; i++) {
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
				parseFloat(markers[i].getAttribute("lng")));
			if(parseFloat(markers[i].getAttribute("lat"))!=0 && !(points.inArray(point)) ) {
				points.push(point);
				var type = markers[i].getAttribute("type");
				var search = markers[i].getAttribute("search");
				var source = markers[i].getAttribute("source");
				icon = mapicons[type];
				var description = markers[i].getAttribute("description");
				var pids = eval('('+markers[i].getAttribute("pids")+')');
				var id = markers[i].getAttribute("id"); // a unique id for the marker - the first house it contains 
				//				alert(id);
				var houses = markers[i].getElementsByTagName("house");
				infoTabs[id] = new Array();
				var k = 0; var toinc = false;
				infoTabs[id][k]=new Array();
				for(j=0;j<houses.length;j++){
					var html = houses[j].getAttribute("html");
					if(houses.length>2){
						if((j+1)<=(Math.floor((houses.length/3)-1)*3)) {
							html = '<div style="text-align:right; font-size:8pt; width:50px; float:right"><a href="javascript:void(0);" onclick="curtab++; curmarker.openInfoWindowTabsHtml(infoTabs['+id+'][curtab]);">More...</a></div>' + html;
						}
						if(((j+1)%3==0) && ((j+1)!=houses.length)){
							toinc=true;
						}
						if((j+1)>3) {
							html = '<div style="text-align:left; font-size:8pt; width:50px; float:left;"><a href="javascript:void(0);" onclick="curtab--; curmarker.openInfoWindowTabsHtml(infoTabs['+id+'][curtab]);">Previous...</a></div>' + html;
						}
						html = "<div style='width:"+(88*3)+"px; clear:both;'>"+html+'</div>';
					}
					infoTabs[id][k].push(new GInfoWindowTab("Property "+(j+1),html));
					if(toinc) { k++; infoTabs[id][k]=new Array(); } toinc=false;
				}
				var html2 = markers[i].getAttribute("html2");
				var m = createPopupMarker(point,description,icon,html2,id);
				var t = [search,m];
				markerList.push(t);
				for (p=0; p < pids.length; p++)
					propertyList[pids[p]] = m;
				//alert(source);
				if(source=='geodata') {
					m.hide();
					//alert(geodataCategories);
					if(!geodataCategories.strInArray(type)){
						geodataMarkers[type]=new Array();
						geodataCategories.push(type);
					}
					geodataMarkers[type].push(m); // now we can iterate over our geodataMarkers for a given type and hide() or show() them all
				}
			}
		}
		if (googlemaps_debug) {
			map.addOverlay(new GMarker(new GLatLng(googlemaps_debug[0], googlemaps_debug[1])));
		}
		firstrun=false;
		mapLayerCheckboxes();
	});
}

var mapsMarkerClick = function(point, marker, id) {
	marker.openInfoWindowTabsHtml(infoTabs[id][0]);
	curpoint=point; curmarker=marker; curtab=0;
};

function createPopupMarker(point,description,icon,html2,id) {
	var marker = new GMarker(point, {title:description, icon:icon});
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() { mapsMarkerClick(point, marker, id) });
	return marker;
}

function updateDistinctSelectTree(from_id, to_id, from_field, to_field) {
	value = $F(from_id);
	to = $(to_id);
	Element.infanticide(to);
	to.appendChild(Builder.node("option", "Loading..."));
	new Ajax.Request("/ajax/distinct_select_tree", {
		method:"post",
		postBody:"from_field="+escape(from_field)+"&from_value="+escape(value)+"&to_field="+escape(to_field),
		onSuccess:function(t) {
			Element.infanticide(to);
			data = eval("("+t.responseText+")");
			data.each(function(v) {
				to.appendChild(Builder.node("option", v));
			});
			to.selectedIndex = 0;
		}
	});
}

if (typeof(Element) == "undefined")
	Element = {}

Element.infanticide = function(node) {
	node = $(node)
		if (node)
			while (node.hasChildNodes())
				node.removeChild(node.firstChild)
}
Element.staticize = function(element) {
	element = $(element);
	if (element.getStyle('position') == 'static') return;

	element.style.position = 'static';
	element.style.top    = "";
	element.style.left   = "";
	element.style.width  = "";
	element.style.height = "";
	return element;
};

// the following is for FSBO stuff
function fetchXmlData(id,source,elem,num) {
    new Ajax.Updater(elem,"/propertyEditor/fetchXml", {
      asynchronous:true, method:"post",
      postBody:"source="+source+"&elem="+elem+"&id="+id,
      onSuccess: function() { count++; if(count==num) Effect.Appear('viewlisting'); },
      onFailure: function() { alert('Error'); }
    });
}
function deleteImage (id) {
  if(window.confirm('Are you sure you want to delete this image?')){
    handler = function () {
      victim = $('image'+id);
      $(victim).style.display='none';
    }
    new Ajax.Request("/propertyEditor/deleteImage", {
      asynchronous:true, method:"post",
      postBody:"id="+id,
      onSuccess:handler,
      onFailure: function() { alert('Error'); }
    });
    
  }
}
function deleteProperty (id) {
  if(window.confirm('Are you sure you want to delete this property? If it is live, you will no longer be billed for this property, it will be completely unlisted from this site and all our partners.')){
    handler = function () {
      window.location.reload();
    }
    new Ajax.Request("/hub/deleteProperty", {
      asynchronous:true, method:"post",
      postBody:"id="+id,
      onSuccess:handler,
      onFailure: function() { alert('Error'); }
    });
  }
}
function deleteFromWatchList (id) {
	handler = function () {
		window.location.reload();
	}
	new Ajax.Request("/hub/deleteFromWatchList", {
		asynchronous:true, method:"post",
		postBody:"id="+id,
		onSuccess:handler,
		onFailure: function() { alert('Error'); }
	});
}
function numsToHtml() {
  html='<h3>Overview</h3><p>&nbsp;</p>';
  for(i=0;i<$('bedrooms').value;i++)
    html += '<h3>Bedroom '+(i+1)+'</h3><p>&nbsp;</p>';
  for(i=0;i<$('receptions').value;i++)
    html += '<h3>Reception Room '+(i+1)+'</h3><p>&nbsp;</p>';
  for(i=0;i<$('kitchens').value;i++)
    html += '<h3>Kitchen '+(i+1)+'</h3><p>&nbsp;</p>';
  for(i=0;i<$('otherrooms').value;i++)
    html += '<h3>Other room '+(i+1)+'</h3><p>&nbsp;</p>';
  html+='<h3>Further information</h3><p>&nbsp;</p>';
  return html;
}

var rotators = []

function rotateItems(els, num, options) {
	if (typeof(els) != "object")
		els = [els];
	els.each(function(el) {
		r = new ItemRotator(el, num, options || {})
		rotators.push(r);
	});
}

if (typeof(Class) != "undefined") {
	var ItemRotator = Class.create({
		pos: 0,
		initialize: function (el, num) {
			this.options = Object.extend({
				num: num,
				duration: 10,
				fade_duration: 1,
				async: false,
				absolutize: false
			}, arguments[2] || { });
			this.el = $(el);
			if (!this.el)
				return;
			this.items = this.el.select(".rotate_item");
			if (this.items.length <= this.options.num) {
				this.items.each(function(el) {
					Element.show(el);
				});
				return;
			}

			this.items.each(function(el) {
				Element.remove(el);
			});
			for (var n = 0; n < this.options.num; n++) {
				this.el.appendChild(this.items[n]);
				Element.show(this.items[n]);
			}
			if (!this.options.async)
				this.pos = n;
			window.setTimeout(this.performRotate.bind(this), this.options.duration * 1000);
			if (this.options.async)
				this.options.duration /= this.options.num;
		},
		performRotate: function() {
			this.options.async ? this.asyncRotate() : this.syncRotate();
		},
		// {{{
		syncRotate: function() {
			var to_hide = [];
			var to_show = [];
			this.items.each(function(el, n) { // Hide all items
				if (Element.visible(el))
					to_hide.push(n);
			});
			var hide_effects = [];
			var show_effects = [];
			to_hide.each(function(n) {
				var next_el = this.loop(n+this.options.num, this.items.length);
				hide_effects.push(new Effect.Fade(this.items[n], {
					sync: true,
					afterFinish: function() {
						this.items[n].insert({ before: this.items[next_el]});
						Element.remove(this.items[n]);
					}.bind(this)
				}));
				to_show.push(next_el);
			}.bind(this));
			new Effect.Parallel(hide_effects, {
				duration: 1,
				afterFinish: function() {
					to_show.each(function(n) {
						show_effects.push(new Effect.Appear(this.items[n], { sync: true }));
					}.bind(this));
					new Effect.Parallel(show_effects, {
						duration: 1,
						afterFinish: function() {
							window.setTimeout(this.performRotate.bind(this), this.options.duration * 1000);
						}.bind(this)
					});
				}.bind(this)
			});
		},
		// }}}
		asyncRotate: function(inc) {
			if (this.timer) {
				window.clearTimeout(this.timer);
				this.timer = false
			}
			var num = inc ? inc : this.options.num;
			// This should be easy
			if (this.options.absolutize)
				$A(this.el.getElementsByClassName("rotate_item")).reverse().each(function(el) { el.absolutize() });

			cb2 = function() {
				this.timer = window.setTimeout(this.performRotate.bind(this), this.options.duration * 1000);
				this.pos = inc ? next_el : this.loop(this.pos+1, this.items.length); // XXX: May need to set this beforeStart
			}
			cb1 = function() {
				// Append next
				var next_el = this.loop(this.pos + num, this.items.length);
				this.items[this.pos].insert({ before: this.items[next_el]});
				Element.remove(this.items[this.pos]);

				this.items[next_el].setOpacity(0);
				Element.show(this.items[next_el]);

				if (this.options.absolutize)
					$A(this.el.getElementsByClassName("rotate_item")).reverse().each(function(el) { Element.staticize(el); });

				if (this.options.duration)
					new Effect.Appear(this.items[next_el], {
						duration: this.options.fade_duration,
						afterFinish: cb2.bind(this)
					});
				else
					cb2.bind(this)();
			}

			if (this.options.duration)
				new Effect.Fade(this.items[this.pos], {
					duration: this.options.fade_duration,
					afterFinish: cb1.bind(this)
				});
			else
				cb1.bind(this)();
		},
		prev: function() {
			this.asyncRotate(-1);
		},
		next: function() {
			this.asyncRotate();
		},
		loop: function(n, max) {
			if (n >= max)
				n -= max;
			if (n < 0)
				n += max;
			return n;
		}
	});
}

function selectInstructionType(el_it, el_minprice, el_maxprice, cb) {
	el_it = $(el_it);
	el_minprice = $(el_minprice);
	el_maxprice = el_maxprice ? $(el_maxprice) : false;

	if (!el_it)
		return;
	var value = $F(el_it);
	switch (value) {
		case "Letting":
			if (el_minprice) populatePrices(el_minprice, letPrices, "min", selectInstructionType.format.let);
			if (el_maxprice) populatePrices(el_maxprice, letPrices, "max", selectInstructionType.format.let);
			break;
		default:
			if (el_minprice) populatePrices(el_minprice, salePrices, "min", selectInstructionType.format.sale);
			if (el_maxprice) populatePrices(el_maxprice, salePrices, "max", selectInstructionType.format.sale);
			break;
	}
	if (typeof(cb) == "function")
		cb(value.toLowerCase());
	return value;
}

selectInstructionType.format = {};
selectInstructionType.format.sale = selectInstructionType.format.let = "&pound;%%price%%";

function selectInstructionTypeRadio(el_it, el_minprice, el_maxprice, cb) {
	el_minprice = $(el_minprice);
	el_maxprice = $(el_maxprice);
	var inputs = $A(document.getElementsByTagName("input"));
	var v = "";
	inputs.each(function(el) {
			if (el.name != el_it) return;
			if (!el.checked)      return;
			var opts = {}
			if (el.getAttribute("rev"))
				opts = eval('('+el.getAttribute("rev")+')');
			var prices = format = null;
			switch (el.value) {
				case "Sale":
					prices = typeof(opts.prices) != "undefined" ? opts.prices : salePrices;
					format = typeof(opts.format) != "undefined" ? opts.format : selectInstructionType.format.sale;
					break;
				case "Letting":
					prices = typeof(opts.prices) != "undefined" ? opts.prices : letPrices;
					format = typeof(opts.format) != "undefined" ? opts.format : selectInstructionType.format.let;
					break;
			}
			if (prices && format) {
				if (el_minprice) populatePrices(el_minprice, prices, "min", format);
				if (el_maxprice) populatePrices(el_maxprice, prices, "max", format);
			}
			v = el.value;
			if (typeof(cb) == "function")
				cb(el.value.toLowerCase());
	});
	return v;
}

function addSelectOption(el, value, text) {
	o = new Option();
	o.text = text;
	o.value = value.toString();
	el.options[el.options.length] = o;
}

function populatePrices(el, prices, type, format) {
	while (el.options.length > 0)
		el.remove(0);
	prices = $A(prices);
	
	if (typeof(prices[0]) == "string") {
		var mintext = html_entity_decode(prices[0]);
		prices.shift();
	} else var mintext = "No Minimum";

	if (typeof(prices[prices.length-1]) == "string") {
		var maxtext = html_entity_decode(prices[prices.length-1]);
		prices.pop();
	} else var maxtext = "No Maximum";

	if (populatePrices.initialOption)
		addSelectOption(el, "", populatePrices.initialOption);

	if (type == "min")
		addSelectOption(el, "", mintext);

	format = html_entity_decode(format);

	prices.each(function(price) {
			var text = format.replace("%%price%%", number_format(price));
			addSelectOption(el, price, text);
	});
	if (type == "max") {
		addSelectOption(el, "", maxtext);
		el.selectedIndex = (populatePrices.selectedIndex && typeof(populatePrices.selectedIndex.max) == "number") ? populatePrices.selectedIndex.max : el.options.length-1;
	}
}

function number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	//
	if (!b) b = 0;
	if (!c) c = ".";
	if (!d) d = ",";

	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a.toString()
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}

function html_entity_decode(str) {
	var ta = document.createElement("textarea");
	ta.innerHTML=str.replace(/</g,"<").replace(/>/g,">");
	toReturn = ta.value;
	ta = null;
	return toReturn
}

function sortableVisuals(div) {
	div = $(div);
	Sortable.create(div, {
		handle: "handle",
		onUpdate: function(el) {
			data = Sortable.serialize(el);
			new Ajax.Request("/xml/fsbo/sf/web/fsbo.php/propertyEditor/updateImagesOrder", {
				postBody: data
			});
		}
	});
}

function populateAddress(el) {
	value = $F(el);
	if (!value)
		return;
	new Ajax.Request("/xml/ajax/get-property-address.php", { // FIXME: Better URL required
			parameters: {
				id: value
			},
			evalJSON: true,
			onSuccess: function(t) {
				data = $H(eval('('+t.responseText+')'));
				data.each(function(v) {
					$("el_"+v[0]).value = v[1];
				});
			}
	});
}

if (typeof(Class) != "undefined") {
	var MultiLineInput = Class.create({
			initialize: function (el) {
				this.container = $(el);
				this.template = this.container.getElementsByTagName("li")[0].cloneNode(true);
				$A(this.container.getElementsByTagName("li")).each(function(line) { this.addHandlers(line) }, this);
				this.num_inputs = this.template.getElementsByTagName("input").length;
				this.blank_at_end = 1;
				if (this.num_inputs = 1)
					this.blank_at_end = 2;
				this.rescan();
			},
			rescan: function() {
				var lines = $A(this.container.getElementsByTagName("li"));
				var nlines = lines.length;
				var remove = [];
				var blank_at_end = 0;
				lines.each(function(line, n) {
					var inputs = $A(line.getElementsByTagName("input"));
					blank = true;
					inputs.each(function(input) {
						if($F(input)!==null) { // cope with things like checkboxes
							if ($F(input).strip()) {
								blank = false;
								throw $break;
							} else {
								return;
							}
						}
					});
					if (blank) {
						blank_at_end++;
						if (n < nlines-this.blank_at_end)
							remove.push(n);
					} else blank_at_end = 0;
				}, this);
				remove.each(function(n) {
						Effect.Fade(lines[n], {
								duration: 0.5,
								afterFinish: function() {
									Element.remove(lines[n]);
								}
						});
				});
				if (blank_at_end < this.blank_at_end) {
					for (i = 0; i < this.blank_at_end-blank_at_end; i++) {
						var line = this.container.appendChild(this.template.cloneNode(true));
						Element.hide(line);
						Effect.Appear(line, {
								duration: 0.5
						});
						$A(line.getElementsByTagName("input")).each(function(input) {
								input.value = "";
						});
						this.addHandlers(line);
					}
				}
			},
			addHandlers: function(line) {
				$A(line.getElementsByTagName("input")).each(function(input) {
					Event.observe(input, "change", this.rescan.bind(this));
				}, this);
				$A(line.getElementsByTagName("span")).each(function(span) {
					if (Element.hasClassName(span, "eval-js"))
						eval(span.innerHTML);
				});
				$A(line.getElementsByTagName("img")).each(function(img) {
					if (!Element.hasClassName(img, "delete"))
						return;
					Event.observe(img, "click", function() {
						$A(line.getElementsByTagName("input")).each(function(input) {
							input.value = "";
						}, this);
						this.rescan();
					}.bind(this));
				}, this);
			}
	});

	Event.observe(window, "load", initMultiline);

	var Scroller = Class.create({
		initialize: function(el, prev, next) {
			this.el = $(el);
			this.prev = $(prev);
			this.next = $(next);
			this.width = 0;
			$A(this.el.down("div").childNodes).each(function(c) {
				c = $(c);
				if (c.getWidth && c.getStyle)
					this.width += c.getWidth() + parseInt(c.getStyle("margin-left")) + parseInt(c.getStyle("margin-right"));
			}.bind(this));
			this.el.down("div").style.width = this.width + "px";

			this.next.observe("mousedown", function() { this.scroll(1); }.bindAsEventListener(this));
			this.next.observe("mouseup", this.stopScroll.bindAsEventListener(this));
			this.prev.observe("mousedown", function() { this.scroll(-1); }.bindAsEventListener(this));
			this.prev.observe("mouseup", this.stopScroll.bindAsEventListener(this));
		},
		scrolling: false,
		scroll: function(inc) {
			this.scrolling = true;
			this._scroll.bind(this).delay(0.01, inc);
		},
		_scroll: function(inc) {
			this.el.scrollLeft += inc;
			if (this.scrolling)
				this._scroll.bind(this).delay(0.01, inc);
		},
		stopScroll: function() {
			this.scrolling = false;
		}
	});
}

function initMultiline() {
	MultiLineInput.inputs = [];
	$$("ul.multiline-input").each(function(el) {
		MultiLineInput.inputs.push(new MultiLineInput(el));
	});
}

function showCurrency(currency) {
	var els = $$("div.currency");
	els.each(function(el) {
			if (el.hasClassName(currency))
				el.show();
			else
				el.hide();
	});
	new Ajax.Request("/setcurrency/"+currency);
}

function getCurrentImage() {
	var n = 0;
	$$("img.imagechangermain").each(function(img) {
		if (!Element.visible(img))
			return;
		n = img.id.split("imagechanger")[1];
		throw $break;
	});
	return Number(n);
}

function getTotalImages() {
	var images = $$("img.imagechangermain");
	var last = images[images.length-1];
	return Number(last.id.split("imagechanger")[1])+1;
}

function prevImage() {
	var cur = getCurrentImage();
	if (cur == 0)
		return;
	hideall();
	Element.show("imagechanger"+(cur-1));
}
function nextImage() {
	var cur = getCurrentImage();
	var total = getTotalImages();
	if (cur == total-1)
		return;
	hideall();
	Element.show("imagechanger"+(cur+1));
}


function bookmark(){
	var title = document.title;
	var url = document.location;
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		window.external.AddFavorite(url,title);
	} else if (navigator.appName == "Netscape") {
		window.sidebar.addPanel(title,url,"");
	} else {
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
	}
}

function addToShortlist(id) {
	new Ajax.Request("/ajax/add_to_shortlist", {
		parameters: {
			id: id
		},
		onSuccess: updateShortlistCallback
	});
	$$(".shortlist-property-"+id).invoke("addClassName", "remove-from-shortlist");
}

function deleteFromShortlist(id) {
	new Ajax.Request("/ajax/delete_from_shortlist", {
		parameters: {
			id: id
		},
		onSuccess: updateShortlistCallback
	});
	$$(".shortlist-property-"+id).invoke("removeClassName", "remove-from-shortlist");
}

function toggleInShortlist(id) {
	var el = $$(".shortlist-property-"+id)[0];
	if (el.hasClassName("remove-from-shortlist")) {
		deleteFromShortlist(id);
		el.removeClassName("remove-from-shortlist");
		el.addClassName("add-to-shortlist");
	} else {
		addToShortlist(id);
		el.removeClassName("add-to-shortlist");
		el.addClassName("remove-from-shortlist");
	}
}

function updateShortlistCallback(t) {
	data = eval('('+t.responseText+')');
	$("shortlist").update(data);
}

var asEnableRadius = function (radius) {
	if (!radius) return;
	//radius.enable();
	radius.options[0].value = "";
	radius.options[0].text = radius.originalText;
};
var asDisableRadius = function (radius) {
	if (!radius) return;
	//radius.disable();
	radius.options[0].value = "";
	radius.options[0].text = "this area only";
	radius.options.selectedIndex = 0;
};


function applyAjaxSearch() {
	$$("input.ajax-complete").each(function(el, n) {
		if (el.ajaxSearchApplied)
			return;
		el.ajaxSearchApplied = true;
		el.originalName = el.getAttribute("name");
		var radius = $$("select.ajax-radius")[n];
		radius.originalText = radius.options[0].text;
		div = Builder.node("div", {"class":"ajax-search-results", "style":""});
		if (el.id)
			div.id = "as-"+el.id;
		//el.insert({after:div});
		document.body.appendChild(div);
		var parameters = "field="+escape(el.getAttribute("rel"))+"&show_type="+Number(Boolean(el.hasClassName("ac-show-type")));
		if (el.getAttribute("rev"))
			parameters += "&parameters="+el.getAttribute("rev");
		new Ajax.Autocompleter(el, div, "/ajax/search-autocomplete", {
			paramName: "value",
			parameters: parameters,
			callback: function(inp, str) {
				el.name = el.originalName;
				if (el.hasClassName("ac-set-name")) asEnableRadius(radius);
				return str;
			},
			updateElement: function(li) {
				el.value = li.down("span.data").firstChild.nodeValue;
				if (el.hasClassName("ac-set-name")) {
					el.name = li.getAttribute("rel");
					asDisableRadius(radius);
				} else {
					//asEnableRadius(radius);
				}
			},
			select: "result",
			minChars: 1
		});
		el.observe("change", function() {
		});
	});

	$$("input.ajax-complete-detailed").each(function(el, n) {
		if (el.ajaxSearchApplied)
			return;
		el.ajaxSearchApplied = true;
		div = Builder.node("div", {"class":"ajax-search-results", "style":"z-index: 100"});
		$(document.body).insert({top: div});
		//el.insert({after:div});
		//document.body.appendChild(div);
		new Ajax.Autocompleter(el, div, "/ajax/search-autocomplete-detailed", {
			paramName: "value",
			updateElement: function(li) {
				el.value = li.down("span.data").firstChild.nodeValue;
				var radius = $$("select.ajax-radius")[n];
				var type = li.getAttribute("rel");
				el.name = type == "postcode" ? "part_postcode" : "radius_location";
				ajaxSearchOnchange(el, radius);
			},
			select: "result",
			minChars: 1
		});
	});
	if (window.loading)
		applyAjaxSearch.delay(0.1);
}
if(typeof Prototype != 'undefined')
	applyAjaxSearch();

ajaxSearchOnchange = function (el, radius) {
	switch (el.name) {
		case "part_postcode":
			radius.options[0].text = "this postcode";
			radius.options[0].value = "-";
			radius.selectedIndex = 0;
			break;
		case "radius_location":
			radius.options[0].text = "select distance:";
			radius.options[0].value = "";
			break;
	}
}

if (typeof (ListSearch) == "undefined") var ListSearch = function(form_id) {
	var el = document.getElementById(form_id);
	el.setAttribute("action", "/search/");
}
if (typeof (MapSearch) == "undefined") var MapSearch = function(form_id) {
	var el = document.getElementById(form_id);
	el.setAttribute("action", "/search2/");
}

var touchHandler = function(event) {
    var touches = event.changedTouches,
        first = touches[0],
        type = "";
    
    switch(event.type)
    {
        case "touchstart": type = "mousedown"; break;
        case "touchmove":  type="mousemove"; break;        
        case "touchend":   type="mouseup"; break;
        default: return;
    }
        
    //initMouseEvent(type, canBubble, cancelable, view, clickCount,
    //           screenX, screenY, clientX, clientY, ctrlKey,
    //           altKey, shiftKey, metaKey, button, relatedTarget);
    
    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1,
                              first.screenX, first.screenY,
                              first.clientX, first.clientY, false,
                              false, false, false, 0/*left*/, null);
                                                                            
    first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}
