﻿var ID = function(id) {
    return document.getElementById(id);
};

var EOS = {
    ObjectContainerIDs: new Array(),

    AddObjectContainerIDs: function() {
        for (var i = 0; i < arguments.length; i++) {
            this.ObjectContainerIDs.push(arguments[i]);
        }
    },

    UserID: undefined,
    BaseUrl: "http://www.evolveoursite.com",

    Init: function(id, url /* DEPRECATED */, button, vanchor, vadjust, hanchor, hadjust, width) {

        // Bum out for unsupported browsers...
        if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6) {
            // ID('EvolveShowButton').style.position = "absolute";
            return;
        }

        // Bum out if id not set...
        if (id == undefined || id == null || id == '') {
            alert("The script that activates this button has not been installed correctly!");
            return;
        }

        // Set the account id
        if (id != undefined) this.UserID = id;

        // Set the base url - DEPRECATED
        /* if (url != undefined) this.BaseUrl = url; */

        // Insert button and overlay html into page...
        document.write(this.ButtonHtml);
        document.write(this.OverlayHtml);

        if (vanchor != undefined && vanchor == "T") {
            ID('EvolveShowButton').style.top = (vadjust != undefined ? vadjust : 10) + 'px';
        }
        else {
            ID('EvolveShowButton').style.bottom = (vadjust != undefined ? vadjust : 10) + 'px';
        }

        if (hanchor != undefined && hanchor == "L") {
            ID('EvolveShowButton').style.left = (hadjust != undefined ? vadjust : 10) + 'px';
        }
        else {
            ID('EvolveShowButton').style.right = (hadjust != undefined ? vadjust : 10) + 'px';
        }

        // Set up the button...
        if (button == undefined) {
            button = 'H00.gif';
            width = 120;
        }
        ID('EvolveShowButtonImg').src = this.BaseUrl + '/_Assets/Images/Buttons/' + button;
        ID('EvolveShowButtonImg').width = width;
    },

    SetOverlayPosition: function() {
        var height = document.documentElement.scrollHeight;

        ID('EvolveContainer').style.height = height + 'px';
        ID('EvolveContainer').style.top = 0;
        ID('EvolveScreen').style.height = height + 'px';
        ID('EvolveScreen').style.top = 0;

        ID('EvolveIframe').style.height = '100%';
        ID('EvolveIframe').style.width = '100%';

        ID('EvolveScreen').style.display = 'block';
        ID('EvolveContainer').style.display = 'block';

        if (ID('EvolveIframe').removeEventListener) {
            ID('EvolveIframe').removeEventListener('load', EOS.SetOverlayPosition, false);
        } else if (ID('EvolveIframe').detachEvent) {
            ID('EvolveIframe').detachEvent('onload', EOS.SetOverlayPosition);
        }
    },
    
    ShowAdmin: function() {
        // Check to see if it is a request to show a comment...
        var parameters = location.search.replace("?", "").split("&");
        for (var i in parameters) {
            var splits = parameters[i].split("=");

            if (splits && splits.length > 1 && splits[0] == "eos_comment") {
                var commentID = splits[1];
                EOS.Show(EOS.BaseUrl + '/PopUp/Admin.aspx?UserID=' + EOS.UserID + '&CommentID=' + commentID);
            }
        }
    },

    Show: function(url) {
        // HACK - Hide annoying flash objects.
        for (var i in this.ObjectContainerIDs) {
            var obj = ID(this.ObjectContainerIDs[i]);
            if (obj != undefined && obj != null) {
                obj.style.display = 'none';
            }
        }
    
        var height = document.documentElement.scrollHeight;
        var width = document.documentElement.scrollWidth;
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

        if (url == undefined || url == null) {
            url = this.BaseUrl + '/PopUp/Default.aspx?UserID=' + this.UserID + '&HostPageUrl=' + document.location.href + '&HostPageHeight=' + height + '&HostPageWidth=' + width + '&ScrollTop=' + scrollTop;
        }

        if (ID('EvolveIframe').getAttribute('src') == '') {
            ID('EvolveIframe').setAttribute('src', url);
            if (ID('EvolveIframe').addEventListener) {
                ID('EvolveIframe').addEventListener('load', EOS.SetOverlayPosition, false);
            } else if (ID('EvolveIframe').attachEvent) {
                ID('EvolveIframe').attachEvent('onload', EOS.SetOverlayPosition);
            }
        }

        //ID('EvolveOverlay').style.height = height+'px';
        ID('EvolveOverlay').style.display = 'block';

        MessageIntervalID = window.setInterval('EOS.TryGetMessage();', 100);
    },

    MessageIntervalID: -1,
    TryGetMessage: function() {
        if (location.hash == '#eos_close') {
            window.clearInterval(MessageIntervalID);

            location.hash = null;

            ID('EvolveOverlay').style.display = 'none';

            // HACK - Show annoying flash objects.
            for (var i in this.ObjectContainerIDs) {
                var obj = ID(this.ObjectContainerIDs[i]);
                if (obj != undefined && obj != null) {
                    obj.style.display = null;
                }
            }
        }

        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (location.hash == '#eos_scroll' && scrollTop != 0) {
            window.scrollTo(0, 0);
        }
    },
    
    // Html for the Evolve button itself...
    ButtonHtml: '<a id="EvolveShowButton" href="#" onclick="EOS.Show(); return false;" title="Evolve Our Site">' +
                '    <span style="display:none;">Show "Evolve Our Site" Tool</span>' +
                '    <img id="EvolveShowButtonImg" alt="Evolve Our Site" src="" width="120" />' +
                '</a>',

    // Html for the screen overlay (when user clicks the Evolve button)...
    OverlayHtml:
        '<div id="EvolveOverlay" style="display:none">' +
        '    <div id="EvolveScreen" style="display:none"></div>' +
        '    <div id="EvolveContainer" style="display:none">' +
        '        <iframe src="" id="EvolveIframe" name="EvolveIframe" allowTransparency="true" scrolling="no" frameborder="0"></iframe>' +
        '    </div>' +
        '</div>'
}

var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

window.onload = EOS.ShowAdmin;
