var banner;
(function() {
	$$("input.bglabel").each(function(el) {
		var focus = function(ev) {
			var el = ev.element();
			el.style.backgroundImage = "";
		}
		var blur = function(ev) {
			var el = ev.element();
			if (el.value == "")
				el.style.backgroundImage = "url(/images/label-" + el.name + ".png)";
		}

		el.observe("focus", focus)
		el.observe("blur", blur)
		blur({element: function() { return el }})
	});

	if (banner == "internal") banner = "movie.swf";
	if (banner == "movie.swf") {
		var width = "500";
		var height = "90";
		var mt = "0";
	} else {
		var width = "468";
		var height = "60";
		var mt = "18px";
	}
	if (banner && banner.split(".")[1] == "swf") $("advert").update('<object style="margin-top: '+mt+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+width+'" height="'+height+'">\
		<param name="movie" value="/images/banners/'+banner+'" />\
		<param name="quality" value="high" />\
		<param name="wmode" value="transparent">\
		<embed style="margin-top: '+mt+'" src="/images/banners/'+banner+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" wmode="transparent"></embed>\
	</object>');

}).onLoad()
			
var companies_search = function() {
	document.location.hash = Form.serialize("company-form");
	companies_hash();
}

var companies_hash = (function($) {
	return function() {
		if ($("#tabs"))
			$("#tabs").show();
		$.post(location.pathname, document.location.hash.substring(1), function(responseText, textStatus, t) {
			$("#company-search-results").html(responseText);
			try {
				var json = t.getResponseHeader('X-Json');
				var data = $.parseJSON(json);
				place_markers_on_map(data);
			} catch (e) {}
		});
	};
})(jQuery);

var auctions_search = function() {
	document.location.hash = Form.serialize("auction-form");
	auctions_hash();
}

var auctions_quicksearch = function() {
	document.location = "/auctions.html#" + Form.serialize("auction-quickform");
	if (document.location.pathname == "/auctions.html") {
		auctions_hash();
		restore_hash();
	}
}

var auctions_hash = function() {
	if ($("tabs"))
		$("tabs").show();
	new Ajax.Updater("auction-search-results", "/auctions.html", {
		postBody: document.location.hash.substring(1),
		onComplete: function(t) {
			data = eval($F("json"));
			place_markers_on_map(data);
		}
	});
}

function restore_hash(cb) {
	new Ajax.Request("/restore.html", {
		postBody: document.location.hash.substring(1),
		evalJS: "force",
		onSuccess: function() {
			if (typeof cb == "function")
				cb();
		}
	});
}

var place_markers_on_map = function(data) {
	map.clearOverlays();
	if (!data)
		return;
	var minlat = data[0].latitude;
	var maxlat = data[0].latitude;
	var minlng = data[0].longitude;
	var maxlng = data[0].longitude;
	data.each(function(info) {
		minlat = Math.min(minlat, info.latitude);
		maxlat = Math.max(maxlat, info.latitude);
		minlng = Math.min(minlng, info.longitude);
		maxlng = Math.max(maxlng, info.longitude);
		var marker = new GMarker(new GLatLng(info.latitude, info.longitude));
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(info.balloon);
		});
		map.addOverlay(marker);
	});
	var bounds = new GLatLngBounds(new GLatLng(minlat, minlng), new GLatLng(maxlat, maxlng));
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}

var showAuction;
var showCompany;

(function($) {
	$.livequery.registerPlugin("html");
	showAuction = function(id) {
		$("<div></div>").load("/view-auction.html?id="+id, function() {
			var title = $(this).find("div.title").text();
			$(this).dialog({
				autoOpen: true,
				bgiframe: true,
				modal: true,
				title: title,
				width: 500,
				position:'center',
				buttons: {
					Close: function() { $(this).dialog("close"); }
				}
			});
		});
	};
	showCompany = function(id, title) {
		$("<div></div>").load("/view-company.html?id="+id, function() {
			$(this).dialog({
				autoOpen: true,
				bgiframe: true,
				modal: true,
				title: title,
				width: 500,
				position:'center',
				buttons: {
					Close: function() { $(this).dialog("close"); }
				}
			});
		});
	};

	// Ratings function

	$("td.rating img.star").livequery(function() {
		var $this = $(this);
		if ($this.parent().hasClass("rated"))
			return;
		var self = this;
		$this.data("defaultSrc", $this.attr("src"));
		var $stars = $this.parent().find("img.star");

		$this.hover(function() {
			$stars.attr("src", "/images/star-grey.png");
			var src = "/images/star-blue.png";
			$stars.each(function() {
				$(this).attr("src", src);
				if (this == self) src = "/images/star-grey.png";
			});
		}, function() {
			$stars.each(function() {
				var $star = $(this);
				$star.attr("src", $star.data("defaultSrc"));
			});
		}).click(function(ev) {
			ev.preventDefault();
			var id = $this.closest("tr").attr("id").split("-")[1];
			$("<div>").load("/rate.html", { company_id: id, rating: $stars.index(this)+1 }, function() {
				var $dialog = $(this);
				$dialog.dialog({
					modal: true,
					title: $("h2:first", this).remove().text(),
					width: 340,
					buttons: {
						Rate: function() {
							$("form", $dialog).submit();
						},
						Close: function() {
							$dialog.dialog("close");
						}
					}
				}).data("onSuccess", function(data) {
					$dialog.html(data.html);
					var buttons = $dialog.dialog("option", "buttons");
					delete buttons['Rate'];
					$dialog.dialog("option", "buttons", buttons);
					companies_hash();
				});
			}).ajaxError(function() {
				$("<p>Sorry, there was an error</p>").dialog({
					modal: true,
					buttons: {
						Close: function() {
							$(this).dialog("close");
						}
					}
				});
			});
			return false;
		});
	});

	$("form.ajax-form").livequery(function() {
		var $this = $(this);
		$this.append('<input type="submit" style="display: none" />');
		$this.submit(function(ev) {
			ev.preventDefault();
			$.post($this.attr("action"), $this.serialize(), function(data) {
				var $dialog = $this.closest(".ui-dialog-content");
				if (data.success) {
					if ($dialog.data("onSuccess"))
						$dialog.data("onSuccess")(data);
					else
						$dialog.dialog("close");
				} else {
					$this.replaceWith(data.html);
				}
			}, "json");
		});
	});

	$("button.view-comments").livequery(function() {
		var $this = $(this);
		$this.button().click(function(ev) {
			ev.preventDefault();
			var id = $this.closest("table").attr("id").split("-")[2];
			$("<div>").load("/view-comments.html", { id: id }, function() {
				var $dialog = $(this);
				$dialog.dialog({
					autoOpen: true,
					title: $dialog.find("h2:first").remove().text(),
					width: 400,
					modal: true,
					buttons: {
						Close: function() {
							$dialog.dialog("close");
						}
					}
				});
			});
		});
	});
})(jQuery);

