/** 
 * @fileoverview This is a namespaced mini-library for Schematic
 * The meat n' potatoes here is the SWFBridge function.
 * 
 * @author Richard Herrera rherrera@schematic.com
 * @version 1.0
 */

/**
 * Does Schematic have a namespace? Use this in the meantime...
 */
if (typeof Schematic === "undefined") {
	var Schematic = {};
}

/**
 * Load objects when the DOM loads
 * @author Dean Edwards / Matthias Miller / John Resig / Mark Wubben / Paul Sowden
 */
Schematic.dom = {
	stack : [],
	compile : function() {
		if (arguments.callee.done) {
			return;
		}
		arguments.callee.done = true;
		for (var i = 0; Schematic.dom.stack[i] !== undefined; i++) {
		  Schematic.dom.stack[i]();
		}
	},
	load: function(e) {
		this.stack.push(e);
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", Schematic.dom.compile, null);
		}
		/*@cc_on
		document.write("<script id=__ready defer src=//:><\/script>");
		document.all.__ready.onreadystatechange = function() {
			if (this.readyState == "complete") {
				this.removeNode();
				Schematic.dom.compile();
			}
		}@*/
		if (/KHTML|WebKit/i.test(navigator.userAgent)) { 
			var _timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					clearInterval(_timer);
					Schematic.dom.compile();
				}
			}, 10);
		}
		window.onload = Schematic.dom.compile;
	}
};

/**
 * Graceful redirection for Flash/non-Flash users.
 * @author Richard Herrera rherrera@schematic.com
 * @version 1.0
 */
Schematic.SWFBridge = {
    init: function() {
        if (!so) {
            /**
            * If no SWFObject on the page (subpages), create a dummy one.
            * Actually, all we need is the version, so everything else can be 0...
            */
            var so = new SWFObject(0, "main", 0, 0, "9.0.28");
        }
        var versionIsValid = so.installedVer.versionIsValid(so.getAttribute("version")),
		    hash = (/#/g.test(location.href));
        if (versionIsValid) {
            Schematic.SWFBridge.enable();
        } else {
            document.getElementsByTagName("html")[0].className += " expressInstall";
        }
        /**
        * The magic. If either of the following scenarios, trigger the redirect:
        * 1. Flash is installed and is the correct version, but no anchor is found.
        * 2. An anchor is present, but Flash is not installed or not the correct version.
        */
        if ((!hash && versionIsValid) || (hash && !versionIsValid)) {
            //document.getElementsByTagName("html")[0].className += " hideContent";
            var link = Schematic.SWFBridge.redirect(hash);
            location.replace(link);
        }
    },
    redirect: function(hash) {

        var protocol = location.protocol + "//",
		    host = location.host,
		    path = location.href.split(host)[1];

        if (path == "/" || path == "/#") {
            if (hash)
                return (protocol + host + "/Home/");
            else
                return (protocol + host + "/#");
        }

        //hide content prior to redirecting
        document.getElementsByTagName("html")[0].className += " hideContent";

        if (hash) {
            path = path.replace(/#\/?/g, "");
        } else if (!(/\//.test(path.charAt(0)))) {
            path = "/" + path;
        }

        //ensure ending slash so swf page movement works
        var qsPos = path.indexOf("?");
        if (qsPos > 0) {
            if (path.charAt(qsPos - 1) != "/")
                path = path.replace("?", "/?");
        }
        else {
            if (path.charAt(path.length-1) != "/")
                path = path + "/";
        }
        path = path.replace("//", "/");

        return (protocol + host + (hash ? "" : "/#") + path);

    },
    enable: function() {
        document.getElementsByTagName("html")[0].className += " enableFlash";
    }
};
window.onload = Schematic.SWFBridge.init;