var SNF = new Object();
var _gaq = _gaq || [];

SNF.isMSIE = /*@cc_on!@*/false;

SNF.fixMSIE = function() {
    /* Any Internet Explorer compatibility fixes can go here. */
    if (SNF.isMSIE) {
        $$("ul#login li").each(function(item) {
            if (!item.hasClass("last")) {
                item.appendText("| ");
            }
        });
    }
};

// The following hack allows IE to render HTML 5 elements.
// Sadly, Firefox 2 will only render HTML 5 elements properly if the 
// HTML 5 is emitted as XHTML.
SNF.html5Fix = function() {
    if (!document.createElementNS) {
        document.createElement('header');
        document.createElement('footer');
        document.createElement('section');
        document.createElement('article');
        document.createElement('aside');
        document.createElement('time');
        document.createElement('nav');
    }
};

SNF.padSectionListLinks = function() {
    var rows = $$('#section-list tr');
    rows.forEach(
        function(row) {
            var anchor = row.getElements('a');
            if (anchor.length > 0 ) {
                var href = anchor[0].getProperty('href');
                row.setStyle('cursor', 'pointer');
                row.addEvent('click', function(event) {
                    var clickEvent = new Event(event);
                    console.log("button=" + event.button);
                    // Middle-click is 1
                    // TODO: middle-clicking does nothing
                    if (event.button != 1) {
                        document.location = href;
                        clickEvent.stop();
                    }
                });
                row.addEvent('mouseover', function(event) {
                    if (href.indexOf('/') == 0) {
                        href = 'http://www.spaceshipnofuture.org' + href;
                    }
                    window.status = href;
                });
                row.addEvent('mouseout', function(event) {
                    window.status = '';
                });
            }
        }
    );
};

SNF.logout = function() {
    var logoutReq = new Ajax(
        'http://www.spaceshipnofuture.org/logout/', 
        {
            method: 'post', 
            data: Object.toQueryString({ "_return": document.location }), 
            timeout: 5000, 
            onTimeout: function() {
                // TODO: do something more meaningful here
                console.log("Logout request timed out!");
            }, 
            onComplete: function(r) {
                // TODO: confirm that logout succeeded
                console.log("Logout request completed!");
                window.location.reload();
            }
        }
    );
    logoutReq.request();
};

SNF.focusField = function(id) {
    $(id).focus();
    $(id).select();
};

SNF.resetSearchForm = function(field) {
    if (field.value == '') {
        field.style.color = '';
        field.form.reset();
    }
};

SNF.clearSearchForm = function(field) {
    field.value = '';
    field.style.color = "#000";
};

SNF.initBBCodeHelp = function() {
    if ($('bbcodebox')) {
        $('bbcodebox').style.display = '';
        var bbCodeBox = new Fx.Slide('bbcodebox', {duration: 250});
        bbCodeBox.hide();
        $('bbcode-help-show').addEvent('click', function(event) {
            var clickEvent = new Event(event);
            bbCodeBox.toggle();
            clickEvent.stop();
        });
        $('bbcode-help-hide').addEvent('click', function(event) {
            var clickEvent = new Event(event);
            bbCodeBox.slideOut();
            clickEvent.stop();
        });
    }
};

SNF.popup = function(url, windowName) {
    if (!windowName) {
        windowName = "bulbasaur";
    }
    var props = "height=600,width=460,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=yes";
    var w = window.open(url, windowName, props);
    w.focus();
    return false;
};

SNF.gaScript = function() {
    if (window.location.hostname == 'www.spaceshipnofuture.org' || 
        window.location.hostname == 'spaceshipnofuture.org') {
        trackerCode = "UA-3678757-4";
    } else if (window.location.hostname == 'labs.spaceshipnofuture.org') {
        trackerCode = "UA-3678757-5";
    } else if (window.location.hostname == 'profile.spaceshipnofuture.org') {
        trackerCode = "UA-3678575-6";
    }
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    var gaJsUrl = gaJsHost + "google-analytics.com/ga.js";
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = gaJsUrl;
    script.setAttribute("async", "true");
    script.onload = function() {
        // var pageTracker = _gat._getTracker(trackerCode);
        // pageTracker._initData();
        // pageTracker._trackPageview();
        _gaq.push(["_setAccount", trackerCode]);
        _gaq.push(["_trackPageView"]);
    };
    head.appendChild(script);
}

// Call this to actually insert the tracker script
SNF.attachGaScript = function() {
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', SNF.gaScript, false);
    } else if (document.attachEvent) {
        document.attachEvent('onload', SNF.gaScript);
    }
}

SNF.init = function() {
    SNF.fixMSIE();
    SNF.padSectionListLinks();
    if ($('logout-link')) {
        $('logout-link').addEvent('click', function(event) {
            var clickEvent = new Event(event);
            SNF.logout();
            clickEvent.stop();
        });
    }
};


