﻿var ApplicationApi = null;

window.addEvent('domready', function () {   
    ApplicationApi = new Archetype();
});

function onFrameLoaded(deep) {
    
    if (String(window.location.href) != ApplicationApi.baseUrl() + "#/" + deep)
        window.location.href = ApplicationApi.baseUrl() + "#/" + deep;
}

var Archetype = new Class({
    Implements: [Options, Events],

    host: null,
    deepLink: null,
    stage: null,

    options:
        {
            // this value is set to true if the site is being browsed by an ie browser
            // it is set as an option so the implementation can override this if required
            useIFrameHistory: (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) && new Number(RegExp.$1) <= 7,
            historyIFrameName: "historyFrame",
            controlHost: "silverlightControlHost",
            control: "silverlightControl"
        },

    initialize: function (options) {

        this.setOptions(options);
        this.host = $(this.options.controlHost);
    },

    // starts up the system which checks the url
    // periodically for changes to the anchor tag
    // called from Stage::Stage in Stage.xaml.cs
    stageInit: function () {

        this.stage = $(this.options.control).content.stage;


        if (this.options.useIFrameHistory) {
            var url = String(window.location.href);
            var idx = url.indexOf('#');
            var path = '';
            if (idx != -1)
                path = url.substring(idx + 2, url.length);
            var doc = document.getElementById(this.options.historyIFrameName).contentWindow.document;
            doc.open("javascript:'<html></html>'");
            doc.write("<html><head><scri" + "pt type=\"text/javascript\">parent.onFrameLoaded('" + path + "');</scri" + "pt></head><body></body></html>");
            doc.close();
        }

        //this.WriteUrlForSearchEngineStuff();
        this.checkUrl();
        this.checkUrl.periodical(100);
    },

    //    WriteUrlForSearchEngineStuff: function ()
    //    {
    //        var url = String(window.location.href);
    //        alert(url);
    //    },

    checkUrl: function () {
        var url = String(window.location.href);
        var idx = url.indexOf('#');
        var path = '';
        if (idx != -1) {
            path = url.substring(idx + 2, url.length);
        }
        if (path != ApplicationApi.deepLink) {
            ApplicationApi.deepLink = path;
            ApplicationApi.stage.DeepLinkToPath(path);
        }
    },

    baseUrl: function () {
        var url = String(window.location.href);
        var index = url.indexOf("?");
        if (-1 != index) {
            url = url.substring(0, index);
        }

        var idx = url.indexOf('#');
        if (idx == -1) {
            return url;
        }
        else
            return url.substring(0, idx);
    },

    setDeepLink: function (path) {
        if (this.deepLink == path)
            return;
        this.deepLink = path;
        window.location.href = this.baseUrl() + '#/' + path;
        if (this.options.useIFrameHistory) {
            var doc = document.getElementById(this.options.historyIFrameName).contentWindow.document;
            doc.open("javascript:'<html></html>'");
            doc.write("<html><head><scri" + "pt type=\"text/javascript\">parent.onFrameLoaded('" + path + "');</scri" + "pt></head><body></body></html>");
            doc.close();
        }
    }
});
