var Contest = {
	currentArchivePage: [],
	archive: {
		go: function(type, step) {
			if ('undefined' === typeof(Contest.currentArchivePage[type])) {
				Contest.currentArchivePage[type] = 1 + step;
			} else {
				Contest.currentArchivePage[type] = Contest.currentArchivePage[type] + step;
			}
			if (0 === Contest.currentArchivePage[type]) {
				Contest.currentArchivePage[type] = 1;
			}
			if (0 !== $('#archivepage'+Contest.currentArchivePage[type]).length) {
				Contest.archive.show(Contest.currentArchivePage[type]);
			} else {
				Contest.currentArchivePage[type]--;
			}
		},
		checkCookie: function(type) {
			var cookie = Cookie.read('contestarchivepage'+type);
			if (cookie) {
				Contest.currentArchivePage[type] = parseInt(cookie);
				Contest.archive.show(cookie);
			}
		},
		show: function(id) {
			$('ol[id^="archivepage"]').hide();
			$('#archivepage'+id).show();
		}
	},
	loadScenery: function(sceneryUrl, xmlUrl) {
		var so = new SWFObject(sceneryUrl, 'sceneries', '686', '421', '9', '#fff');
		so.addParam('allowScriptAccess', 'always');
		so.addParam('menu', 'false');
		so.addVariable('xmlUrl', escape(xmlUrl));
		so.write('application');
	},
	loadDressup: function(url) {
		$('#application').html('<img src="'+url+'" alt="" />');
	}
};

(function($) {
$.fn.carousel = function(settings) {
	settings = $.extend({
		buttonClasses: {
			prev: "button arrow left",
			next: "button arrow right"
		},
		items: "li",
		perPage: 4,
		speed: "normal",
		width: 842
	}, settings);

	return this.each(function() {
		var el = $(this);
		var items = el.find(settings.items);
		var itemWidth = items.eq(0).outerWidth(true);
		var pages = Math.ceil(el.innerWidth() / (itemWidth * settings.perPage)) - 1;
		var container = $("<div/>").addClass("carousel-container")
			.css({ position: "relative", overflow: "hidden", height: el.height(), paddingBottom: 38 /* Space for buttons */ });
		var content = $("<div/>").addClass("carousel-content")
			.css({ width: pages * settings.width, position: "absolute", left: 0, top: 0 });
		var nav = $("<div/>").addClass("carousel-nav");
		var buttons = {
			prev: $("<a/>").attr("href", "#prev").addClass("button arrow left").css({ left: 0 }),
			next: $("<a/>").attr("href", "#next").addClass("button arrow right").css({ right: 0 })
		};
		var page = 0;

		// Stuff all the content into the container div, which will move around
		el.children().appendTo(content.appendTo(container.appendTo(el)));

		buttons.prev.add(buttons.next)
			.css({ position: "absolute", bottom: 0, zIndex: 100 })
			.appendTo(nav.appendTo(container))
			.bind("click.carousel", function(ev) {
				ev.preventDefault();
				var el = $(this);
				// Where should it go!?
				page = (function() {
					// Wrap around to last pages
					if (0 === page && el.hasClass("left")) return pages;
					// Go back to start
					else if (pages === page && el.hasClass("right")) return 0;
					else if (el.hasClass("left")) return page - 1;
					else return page + 1;
				})();

				content.animate(
					{ left: - page * itemWidth * settings.perPage },
					{ duration: settings.speed, queue: false }
				);
		});
	});
}

$(function() {
	$("#mainpage .panel").filter(function() { return $("li", this).length > 4; }).carousel();
});
})(jQuery);