var cookieManager = new Cookie;
cookieManager.path = "/design";

function hasReader(bool) {
	if (bool && bool != "false") {
		window._hasReader = true;
		var query = (new String(location.search)).replace(/^\?/, "").split("&");
		var mode = "flash";
		for (var i = 0, n = query.length; i < n; i++) {
			if (query == "static") {
				mode = "static";
				break;
			}
		}
		if (mode != "static") {
			if (cookieManager.get("static") != "true") {
				cookieManager.set("static", "true");
				location.reload();
			}
		}
	}
}

function switchTo(mode) {
	var attrName = (!window.opera && document.all) ? "className" : "class";
	if (mode) {
		if (document.body.setAttribute) {
			document.body.setAttribute(attrName, mode);
		} else {
			document.body[attrName] = mode;
		}
		cookieManager.set("static", mode == "static" ? "true" : "false");
	} else {
		if (document.body.removeAttribute) {
			document.body.removeAttribute(attrName, mode);
		} else {
			document.body[attrName] = "";
		}
		cookieManager.set("static", "false");
	}
}

(function(){
	var required  = "9.0.45";
	var minWidth  = 980;
	var minHeight = 600;
	var flashId   = "externalFlashContent";

	var w        = window;
	var d        = document;
	var de       = d.documentElement;
	var ua       = navigator.userAgent;
	var env      = {};
	env.isIE     = (d.all && !w.opera && ua.indexOf("MSIE ") != -1);
	env.isSafari = (ua.indexOf("AppleWebKit") != -1);
	env.isGecko  = (ua.indexOf("Gecko/") != -1);
	env.revision = (env.isIE) ? parseFloat(ua.match(/MSIE ([\d\.]+)/)[1]) : 0;
	env.isWin    = (ua.indexOf("Win") != -1);
	env.isMac    = (ua.indexOf("Mac") != -1);
	env.isSafeOS = (function(){
		if (env.isWin) {
			var reg = ua.match(/Windows NT ([0-9]+\.[0-9]+)/);
			return ((reg && parseFloat(reg[1]) < 6 && parseFloat(reg[1]) >= 5) || ua.match(/Windows 98/) || ua.match(/Windows 2000/));
		} else if (env.isMac) {
			return (ua.match(/OS X/) && !env.isIE);
		}
		return false;
	})();

	function addClass(node, cName) {
		var attrName = (d.all) ? "className" : "class";
		var current = node.getAttribute(attrName);
		var classes = current ? current.split(" ") : [];
		for (var i = 0, n = classes.length; i < n; i++) {
			if (classes[i] == cName) return;
		}
		classes.push(cName);
		node.setAttribute(attrName, classes.join(" "));
	}

	function addEvent(node, type, listener) {
		if (node.addEventListener) {
			if (type == "mousewheel" && env.isGecko) {
				type = "DOMMouseScroll";
			}
			function clear() {
				node.removeEventListener(type, listener, false);
				w.removeEventListener("unload", arguments.callee, false);
			}
			node.addEventListener(type, listener, false);
			w.addEventListener("unload", clear, false);
		} else if (node.attachEvent) {
			function clear() {
				node.detachEvent("on" + type, listener);
				w.detachEvent("onunload", arguments.callee);
			}
			node.attachEvent("on" + type, listener);
			w.attachEvent("onunload", clear);
		} else {
			var func = (node["on" + type]) ? node["on" + type] : null;
			if (func) {
				node["on" + type] = function(e){
					func(e);
					listener(e);
				};
			} else {
				node["on" + type] = listener;
			}

			var unload = (w["onunload"]) ? w["onunload"] : null;
			if (unload) {
				w["onunload"] = function(e) {
					unload(e);
					node["on" + type] = null;
					w["onunload"]     = null;
				}
			} else {
				w["onunload"] = function() {
					node["on" + type] = null;
					w["onunload"]     = null;
				}
			}
		}
	}

	function resizeHandler(){
		var style = arguments.callee.container.style;
		if (minWidth)  style.width   = (de.offsetWidth  < minWidth)  ? minWidth  + "px" : "100%";
		if (minHeight) style.height  = (de.offsetHeight < minHeight) ? minHeight + "px" : "100%";
	}
	function mousewheelHandler(e){
		var delta = (e.wheelDelta) ? e.wheelDelta : (e.detail) ? e.detail * -60 : 0;
		try {
			arguments.callee.xInterface.jsEvent({type : "onmousewheel", detail : delta});
		} catch(err) {}
	}
	function loadHandler(){
		var object    = d.getElementById(flashId);
		if (env.isIE && env.revision < 7) {
			resizeHandler.container = object.parentNode;
			addEvent(w, "resize", resizeHandler);
			resizeHandler();
		}
		mousewheelHandler.xInterface =  (env.isIE) ? d[flashId] : object;
		addEvent(object, "mousewheel", mousewheelHandler);
	}

	function embed() {
		if (document.getElementById(flashId)) return;
		var flashVars = {};
		var hash = location.hash;
		if (hash) {
			hash = hash.replace(/^#/, "");
			if (hash.match(/^\/[^\/]+/)) {
				flashVars.page = hash.match(/^\/([^\/]+)/)[1];
			}
		}
		swfobject.embedSWF(
			// File
				"swf/main.swf",
			// Target
				"flash-content",
			// Width
				"100%",
			// Height
				"100%",
			// Version
				required,
			// Express Install
				"swf/expressInstall.swf",
			// Flashvars
				flashVars,
			// Param
				{
					scale              : "noscale",
					allowscriptaccess  : "always",
					menu               : "false"
				},
			// Attributes
				{
					id   : flashId,
					name : flashId
				},
			// onComplete
				loadHandler
		);
		var object = document.getElementById(flashId);
		if (object.focus) object.focus();
	}



	if (!swfobject.hasFlashPlayerVersion("6.0.65") || (!swfobject.hasFlashPlayerVersion(required) && !env.isSafeOS)) return;

	document.write('<link rel="stylesheet" type="text/css" href="css/movie.css" media="all">');

	addEvent(window, "load", function(){
		if (swfobject.hasFlashPlayerVersion(required)) {
			var switcher = d.createElement("a");
			switcher.innerHTML = "Flash Version";
			addClass(switcher, "switcher");
			switcher.setAttribute("id", "toFlash");
			switcher.setAttribute("href", "#" + flashId);
			addEvent(switcher, "click", function(e){
				embed();
				if (!e) {
					e = window.event;
				}
				if (e.preventDefault) {
					e.preventDefault();
				} else {
					e.returnValue = false;
				}
				switchTo(switcher.getAttribute("href").split("#")[1]);
				return false;
			});
			(d.getElementById("footer")).appendChild(switcher);
		}

		var query = (new String(location.search)).replace(/^\?/, "").split("&");
		for (var i = 0, n = query.length; i < n; i++) {
			if (query == "static") {
				switchTo("static");
				return;
			}
		}

		if (cookieManager.get("static") == "true") {
			switchTo("static");
			return;
		}

		embed();
	});
})();
