String.prototype.stub = function () {
	return this.unaccent().toLowerCase().gsub(/['’]/, "").gsub(/&/, "and").gsub(/\W+/, "-");
}
String.prototype.unaccent = function () {
	var new_str = this;
	new_str = new_str.gsub(/[ÀÁÂÃÄÅÆǍǞǠǢǺǼȀȂȦAĀĂĄ]/, 'A');
	new_str = new_str.gsub(/[àáâãäåæǎǟǡǣǻǽȁȃȧaāăą]/, 'a');
	new_str = new_str.gsub(/[ÇĆĈĊČƇ]/, 'C');
	new_str = new_str.gsub(/[çćĉċčƈ]/, 'c');
	new_str = new_str.gsub(/[ÈÉÊËĒĔĖĘĚȄȆȨ]/, 'E');
	new_str = new_str.gsub(/[èéêëēĕėęěȅȇȩ]/, 'e');
	new_str = new_str.gsub(/[ĜĞĠĢƓǤǦ]/, 'G');
	new_str = new_str.gsub(/[ĝğġģǥǧ]/, 'g');
	new_str = new_str.gsub(/[ĤĦȞ]/, 'H');
	new_str = new_str.gsub(/[ĥħȟ]/, 'h');
	new_str = new_str.gsub(/[ÌÍÎÏĨĪĬĮİĲƗǏȈȊ]/, 'I');
	new_str = new_str.gsub(/[ìíîïĩīĭįıĳǐȉȋ]/, 'i');
	new_str = new_str.gsub(/[Ĵ]/, 'J');
	new_str = new_str.gsub(/[ĵ]/, 'j');
	new_str = new_str.gsub(/[ĶƘǨ]/, 'K');
	new_str = new_str.gsub(/[ķĸƙǩ]/, 'k');
	new_str = new_str.gsub(/[ĹĻĽĿŁ]/, 'L');
	new_str = new_str.gsub(/[ĺļľŀł]/, 'l');
	new_str = new_str.gsub(/[ÑŃŅŇŊǸ]/, 'N');
	new_str = new_str.gsub(/[ñńņňŉŋǹȠ]/, 'n');
	new_str = new_str.gsub(/[ÒÓÔÕÖØŌŎŐŒǑǪǬǾȌȎȪȬȮȰ]/, 'O');
	new_str = new_str.gsub(/[òóôõöøōŏőœƍǒǫǭǿȍȏȫȭȯȱ]/, 'o');
	new_str = new_str.gsub(/[ŔŖŘȐȒ]/, 'R');
	new_str = new_str.gsub(/[ŕŗřȑȓ]/, 'r');
	new_str = new_str.gsub(/[ßŚŜŞŠȘ]/, 'S');
	new_str = new_str.gsub(/[śŝşšș]/, 's');
	new_str = new_str.gsub(/[ÙÚÛÜŨŪŬŮŰŲƯƱƲǓǕǗǙǛȔȖ]/, 'U');
	new_str = new_str.gsub(/[ùúûüũūŭůűųưǔǖǘǚǜȕȗ]/, 'u');
	new_str = new_str.gsub(/[ÝŶŸȲ]/, 'Y');
	new_str = new_str.gsub(/[ýÿŷƳƴȳ]/, 'y');
	new_str = new_str.gsub(/[ŹŻŽƵȤ]/, 'Z');
	new_str = new_str.gsub(/[źżžƶȥ]/, 'z');
	return new_str;
}


var RichardAnsett = {};

RichardAnsett.Application = Class.create({
	
	initialize: function () {
		this._base_title = document.title.toString();
		this._photo_arranger = new RichardAnsett.PhotoArranger("page");
		RichardAnsett.set_page_visibility("visible");
		SWFAddress.onChange = this._on_change.bind(this);
	},
	
	_on_change: function () {
		this._photo_arranger.goto("#" + SWFAddress.getValue().split("/").last());
	},
	
	setTitle: function (title) {
		if (this._base_title.include("—"))
			SWFAddress.setTitle(this._base_title + " — " + title);
	}
	
})
RichardAnsett.init = function () {
	RichardAnsett.app = new RichardAnsett.Application();
}

RichardAnsett.set_page_visibility = function (value) {
	if (document.styleSheets[0]["addRule"]) {
		document.styleSheets[0].addRule(
			"#page",
			"visibility: " + value + " !important"
		);
	} else if (document.styleSheets[0]["insertRule"]) {
		document.styleSheets[0].insertRule(
			"#page { visibility: " + value + " !important }",
			document.styleSheets[0].cssRules.length
		);
	}
}

RichardAnsett.PhotoArranger = Class.create({
	
	initialize: function (root) {
		this._root = $(root);
		if (!this._root) return;
		
		this._images = this._root.select("img");
		if (this._images.length < 1) return;
		
		this._root.setOpacity(0);
		
		this._create_carousel();
		
		this._jump_preventer = new Element("div", {
			"style": "position: fixed; top: 0; width: 0; height: 0; overflow: hidden;"
		});
		$(document.body).insert({ top: this._jump_preventer });
		
		var width = 0;
		var height = 0;
		var image_height = 0;
		var image_width = 0;
		
		this._carts = this._images.map(function (image) {
			
			var image_id = image.identify().stub();
			image.writeAttribute("id", image_id + "_image");
			
			var cart = new Element("div", { "class": "cart", "id": image_id + "_cart" });
			var image_div = new Element("div", { "class": "image" });
			cart.insert(image_div);
			var caption = new Element("div", { "class": "caption" });
			cart.insert(caption);
			
			var cursor = image;
			if (cursor.up().nodeName.toLowerCase() == "p") cursor = cursor.up();
			
			var para = cursor.next();
			var first_para = true;
			
			image_div.insert(image.remove());
			
			while (para && para.nodeName.toLowerCase() == "p" && !(para.down("img"))) {
				var next = cursor.next();
				caption.insert(para.remove());
				if (first_para == true) {
					first_para = false;
					para.addClassName("first");
				}
				para = next;
			}
			
			var cart_button = new Element("a", {
				"class": "cart_button",
				"href": "#" + image_id,
				"title": image.readAttribute("title")
			});
			cart_button.update(image.readAttribute("title"));
			this._button_carts.insert(cart_button);
			
			this._carousel.insert(cart);
			
			width = Math.max(width, cart.getWidth());
			height = Math.max(height, cart.getHeight());
			image_width = Math.max(image_width, image.getWidth());
			image_height = Math.max(image_height, image.getHeight());
			
			this._jump_preventer.insert(new Element("a", { "id": image_id }));
			
			return cart;
			
		}.bind(this));
		
		this._carousel_container.setStyle({ width: width + "px" });
		this._carousel_mask.setStyle({ height: height + "px" });
		this._controls.setStyle({ width: image_width + "px" });
		
		this._carts.each(function (cart, n) {
			cart.setStyle({ width: width + "px", left: (width * n) + "px" });
			cart.down().setStyle({ height: image_height + "px" });
		}.bind(this));
		
		this._current = this._carts.first();
		this._current_button = this._button_carts.childElements().first();
		this._current_button.addClassName("active");
		
		this._cleanup(this._root);
		
		if (this._images.length >= 2) {
			this._controls.setStyle({ top: image_height + "px" });
			this._controls.observe("click", this._on_click.bind(this));
		} else {
			this._controls.hide();
			this._carousel_container.addClassName("solo");
		}
	},
	
	_cleanup: function (element) {
		if (element.hasClassName("carousel_container")) return;
		
		element.childElements().each(this._cleanup.bind(this));
		if (element.empty()) element.remove();
	},
	
	_create_carousel: function () {
		this._carousel_container = new Element("div", { "class": "carousel_container" });
		this._carousel_mask = new Element("div", { "class": "carousel_mask" });
		this._carousel = new Element("div", { "class": "carousel" });
		this._controls = new Element("div", { "class": "controls" });
		this._button_previous = new Element("a", { "href": "#previous", "class": "slide_button slide_previous" });
		this._button_previous.update("Previous");
		this._button_next = new Element("a", { "href": "#next", "class": "slide_button slide_next" });
		this._button_next.update("Next");
		this._button_carts = new Element("div", { "class": "carts" });
		
		this._carousel_container.insert(this._carousel_mask);
		this._carousel_mask.insert(this._carousel);
		this._carousel_container.insert(this._controls);
		this._controls.insert(this._button_previous);
		this._controls.insert(this._button_next);
		this._controls.insert(this._button_carts);
		
		this._root.insert({ top: this._carousel_container });
	},
	
	_on_click: function (event) {
		var e = event.element();
		
		if (e.nodeName.toLowerCase() != "a") return;
		
		event.stop();
		
		var href = e.readAttribute("href");
		
		switch (href) {
		case "#previous":
			href = this._button_carts.childElements()[Math.max(0, this._carts.indexOf(this._current) - 1)].readAttribute("href");
			break;
		case "#next":
			href = this._button_carts.childElements()[Math.min(this._carts.length - 1, this._carts.indexOf(this._current) + 1)].readAttribute("href");
			break;
		}
		
		href = href.gsub("#", "");
		
		SWFAddress.setValue(href);
	},
	
	goto: function (href) {
		if (!this._carts) return;
		
		switch (href) {
		case "#previous":
			this._current = this._carts[Math.max(0, this._carts.indexOf(this._current) - 1)];
			break;
		case "#next":
			this._current = this._carts[Math.min(this._carts.length - 1, this._carts.indexOf(this._current) + 1)];
			break;
		default:
			this._current = $(href.split("#").last() + "_cart");
			if (!this._current) this._current = this._carts.first();
		}
		
		RichardAnsett.app.setTitle(this._current.down("img").readAttribute("title"));
		
		this._controls.setStyle({ width: this._current.down("img").getWidth() + "px" });
		this._carousel.setStyle({ left: (-this._current.positionedOffset().left) + "px" });
		this._current_button.removeClassName("active");
		this._current_button = this._button_carts.childElements()[this._carts.indexOf(this._current)];
		this._current_button.addClassName("active");
		
		this._root.setOpacity(1);
	}
	
})

document.observe("page:ready", RichardAnsett.init);

