$(function () {
	// placeholder fallback
	$("input[placeholder], textarea[placeholder]").placeholder();

	// jQuery UI datepicker
	$(".date").datepicker({
		dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true,
		showOn: "button",
		showAnim: "fadeIn",
		buttonImage: "../../styles/images/icons/date.png",
		buttonImageOnly: true
	});

	// building comparing
	$("input[id^=compare-]").live("click", function () {
		// document.location = $(this).data("link");
		var label = $("label[for=" + $(this).attr("id") + "]");
		var text = label.text();

		if ($(this).is(":checked")) {
			label.html("<a href='/compare/'>" + text + "</a>");
		} else {
			label.text(text);
		}

		$.post($(this).data("link"));
	});

	// flash messages
    $("div.flashes div.flash").live("flashMessage", function () {
		$(this).delay(5000).fadeOut("slow", function () {
			$(this).next("div.flash").trigger("flashMessage");
		    $(this).remove();
		});
    });
	window.setInterval(function () {
		$("div.flashes div.flash").first().trigger("flashMessage");
	}, 1000);


	// paginator
	$("div.paginator form input").change(function () {
		$(this).parents("form").submit();
	});


	// top menu switcher
	var closeFormOnError = false;
	var closeFormOnErrorInterval = window.setInterval(function () {
		if (!closeFormOnError) {
			$(".topmenu .box .error").parents(".box").fadeIn("normal");
		}
	}, 100);
	$(".topmenu .switch").live("click", function (e) {
		e.preventDefault();
		$(this).siblings("div.box").fadeIn("normal");
		closeFormOnError = false;
	});
	$(".topmenu .box .close").live("click", function (e) {
		$(this).parents("div.box").fadeOut("fast");
		closeFormOnError = true;
	});


	// bubble boxes
	$(".bubble-box").hide(0);
	$(".bubble-box .close").attr("title", "Zavrieť").live("click", function () {
		$(this).parent(".bubble-box").hide(0);
	});
	$("*[data-bubblebox]").css({
		position : "relative",
		cursor : "pointer"
	}).live("click", function (e) {
		$(".bubble-box").not(this).hide(0);
		var offset = $(this).offset();
		var box = $("#" + $(this).data("bubblebox") + "[class=bubble-box]");
		box.css({
			top : offset.top - box.height(),
			left : offset.left + ($(this).width()/2) - (box.width()/2)
		}).toggle();
	});


	// tabs
	$(".tabs").tabs({
		select : function (event, ui) {
			window.location.hash = ui.tab.hash;
		}
	});
	window.setInterval(function () {
		$(".tabs").tabs("select", window.location.hash);
	}, 1);

	// form tooltips
	$("form span.help").each(function () {
		var div = $("<div>", {
			id : "tooltip",
			css : {
				position : "absolute",
				display : "none",
				opacity : 0.9
			}
		});
		$("body").append(div);

		$(this).hover(function (e) {
			var t = $(this).data("help");
			if (t.length > 0) {
				div.text(t).show(0);
			} else {
				$(this).css({ cursor : "default" });
			}
		}, function (e) {
			div.empty().hide(0);
		});
		$(this).mousemove(function (e) {
			div.css({
				top : e.pageY - div.height() - 10,
				left : e.pageX - div.width() - 15
			});
		});
	});
});

jQuery.extend({
	googleMap : function (mapId, detectPosition, defaultLatitude, defaulLongitude) {
		if (!defaultLatitude) {
			defaultLatitude = 49.0;
		}
		if (!defaulLongitude) {
			defaulLongitude = 15.5;
		}

		var map = new google.maps.Map(document.getElementById(mapId), {
			// essentials
			zoom      : 7,
			center    : new google.maps.LatLng(defaultLatitude, defaulLongitude),
			mapTypeId : google.maps.MapTypeId.ROADMAP,

			// controls
			scrollwheel : false,

			// user interface
			scaleControl : true,
			scaleControlOptions : {
				position : google.maps.ControlPosition.RIGHT_BOTTOM
			},
			mapTypeControl : true,
			mapTypeControlOptions : {
				style : google.maps.MapTypeControlStyle.DROPDOWN_MENU,
				position : google.maps.ControlPosition.TOP_RIGHT
			},
			navigationControl : true,
			navigationControlOptions : {
				style : google.maps.NavigationControlStyle.SMALL,
				position : google.maps.ControlPosition.TOP_LEFT
			}
		});

		if (detectPosition) {
			if (navigator.geolocation) {
				navigator.geolocation.getCurrentPosition(function (position) {
					map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
					map.setZoom(14);
				});

			} else if (google.gears) {
				var geo = google.gears.factory.create("beta.geolocation");
				geo.getCurrentPosition(function(position) {
					map.setCenter(new google.maps.LatLng(position.latitude, position.longitude));
					map.setZoom(14);
				});
			}
		}

		return map;
	}
});

