$(document).ready(
	function() {
		mimlnet();
		init_rotator();
		map_address();
		map_tools();
		load();
	}
);

/**** IMAGE ROTATION ****/
function init_rotator() {
	var w, i, t, c, h, y;

	$('div.rotate').each(
		function() {
			// get the paragraph
			t = $(this);
			c = t.children().children().filter('img');
			y = c.parent();

			w = h = 0;
			for (i = 0; i < c.length; i++) {
				w = Math.max(w, c[i].width);
				h = Math.max(h, c[i].height);
			}

			for (i = 0; i < c.length; i++) {
// Somehow IE7 is even dumber than IE6 and can't handle this.
//				c[i].style.marginTop = (w - c[i].height) / 2;
			}

			if (w) y.css('width', w);
			if (h) y.css('height', h);
			y.addClass('photo-edge');

			c = t.children().children();
			rotate_in(c, 0);
		}
	);
}

function map_address() {
	$('form#map-address').submit(
		function () {
			showAddress(this.address.value);
			return false;
		}
	);
}

function map_tools() {
	var t = $('#map-tools li').children().filter('input').each(
		function () {
			$(this).click(
				function () {
					toggleGroup(this.id.substr(7));
				}
			)
		}
	);

	if ($('#checkall')) {
		$('#fishy input').each(
			function () {
				if (this.id == 'checkall') {
					$(this).click(
						function () {
							toggleMapIcons(this.checked);
						}
					);
				}
				else {
					$(this).click(
						function () {
							toggleGroup(this.id.substr(7));
						}
					);
				}
			}
		)
	}
}

function toggleMapIcons(check) {
	$('#fishy input').each(
		function () {
			if (this.id != 'checkall' && this.checked != check) {
				toggleGroup(this.id.substr(7));
				this.checked = (check) ? 'checked' : false;
			}
		}
	);
}


function rotate_in(img, idx) {
	var i, o;
//	console.log('in:', img, o);

	i = $(img[idx]);
	if (i.css('display') != 'block') {
		i.fadeIn('fast');
	}

	window.setTimeout(
		function () {
			rotate_out(img, idx);
		},
		8000
	);
}

function rotate_out(img, idx) {
	var n;
	n = (idx + 1) % img.length;

	$(img[idx]).fadeOut(
		'fast',
		function () {
			rotate_in(img, n);
		}
	);
}


function mimlnet() {
	$.post(
		'/miml/ad/fetch/6',
		{ page: location.href },
		function(html) {
			$('#mimlnet div').html(html);
		}
	);
}