var template = {
	landscape: true,
	small: false,
	callbacks: new Array(),
	ios: false,
	iosPre5: false,
	fadeInTimeout: null,

	addInit: function (func) {
		if (navigator.userAgent.match(/iphone|ipad|ipod/i) !== null) {
			$(window).load(func);
		} else {
			$(document).ready(func)
		}
	},

	init: function () {
		template.ios = navigator.userAgent.match(/iphone|ipad|ipod/i) !== null
		template.iosPre5 = template.ios && navigator.userAgent.match(/[1-4]_[0-9]/) !== null;

		$(window).resize(template.resize);
		template.resize();

		if (window.addEventListener) {
			window.addEventListener('DOMMouseScroll', template.wheel, false);
		}
		window.onmousewheel = document.onmousewheel = template.wheel

		if (template.ios) {
			var fixed = $("#header,#footer");
			fixed.css("position", "absolute");
			$("#content")[0].addEventListener("touchstart", function () { fixed.css({ opacity: 0 }); });
			$("#content")[0].addEventListener("touchmove", function () { fixed.css({ opacity: 0 }); });
			window.onscroll = function () { template.moveFixed() };
			window.addEventListener("orientationchange", template.resize);
			if (typeof gallery == "undefined") {
				$(window).load(template.hideURLbar);
				window.addEventListener("orientationchange", template.hideURLbar);
			}
			template.moveFixed(0, 0);
		}
	},

	hideURLbar: function () {
		setTimeout(function () {
			window.scrollTo(0, 1);
		}, 0);
	},

	resize: function () {
		var win = $(window);
		//traceVar({ w: win.width(), h: win.height() });
		if (win.width() / 1.05 > win.height()) {
			template.setLandscape();
		} else {
			template.setPortrait();
		}

		if (win.width() < 400 || win.height() < 400) {
			template.setSmall();
		} else {
			template.setLarge();
		}
		if (template.ios) template.moveFixed();
	},

	setLandscape: function () {
		if (!template.landscape) {
			template.landscape = true;
			$("#page").removeClass("portrait").addClass("landscape");
			setTimeout(template.switched, 0);
		}
	},

	setPortrait: function () {
		if (template.landscape) {
			template.landscape = false;
			$("#page").removeClass("landscape").addClass("portrait");
			setTimeout(template.switched, 0);
		}
	},

	setSmall: function () {
		if (!template.small) {
			template.small = true;
			$("#page").addClass("small");
		}
	},

	setLarge: function () {
		if (template.small) {
			template.small = false;
			$("#page").removeClass("small");
		}
	},

	switched: function (callback) {
		if (typeof(callback) != "function") {
			for (i in template.callbacks) {
				template.callbacks[i].call(this);
			}
		} else {
			template.callbacks.push(callback);
		}
	},

	wheel: function (e) {
		if (!template.landscape) return;

		var delta = 0;
		if (!e) e = window.event /* For IE. */
		if (e.wheelDelta) { /* IE/Opera. */
			delta = e.wheelDelta;
		} else if (e.detail) { /** Mozilla case. */
			delta = -e.detail * 60;
		}

		if (delta) {
			var left = $(window).scrollLeft()
			$(window).scrollLeft(delta > left ? 0 : left - delta);
		}
	},

	moveFixed: function (x, y) {
		if (typeof slideshow == "undefined" || !slideshow.running) {
			$("#header,#footer").hide();
			clearTimeout(template.fadeInTimeout);
			template.fadeInTimeout = setTimeout(function () {
				$("#header").fadeTo(500, 1);
				if (!template.small) $("#footer").fadeTo(500, 1);
			}, 500);
		}

		if (x == null) x = window.pageXOffset;
		if (y == null) y = window.pageYOffset;
		//traceVar({ x: x, y: y });
		if (template.landscape) {
			$("#header").css({
				top: "0px",
				left: x
			});
			$("#footer").css({
				top: "auto",
				bottom: "20px",
				left: x
			});
		} else {
			$("#header").css({
				top: y,
				left: "0px"
			});
			$("#footer").css({
				top: window.innerHeight + y - $("#footer").outerHeight(),
				bottom: "auto",
				left: "0px"
			});
		}
	}
}

function spacer(item) {
	var frame = $(item);
	$(window).resize(resize);
	template.switched(resize);
	resize();

	function resize() {
		if (template.landscape) {
			frame.css({ "height": "auto", "width": $(window).width() / 4 })
		} else {
			frame.css({ "width": "auto", "height": $(window).height() / 4 })
		}
	}
}
