/*-------------------------------------------------------------------------------
    Author : Stephen Blum
    Purpose: Patch for catalog-blank-page issue.  Solves quickly in a user
             transparent fassion the blank page a user always runs into.
             This method was selected as E-Market is soon to be sunsetted.
-------------------------------------------------------------------------------*/

(function() {

window.catini = {
    /*-------------------------------------------------------------------------------
        Temp Catalog Fix
        ----------------
        Fix for the blank page issue (transparent to the users).
    -------------------------------------------------------------------------------*/
    domain        : 'readysetgo',
    session       : document.getElementById("bmsid").value,
    readyCatalog  : false,
    anchors       : [],

    tmpCatalogFix : function() {
        // Do nothing if not logged in.
        if (!catini.session) return false;

        // Init Links
        var nav = catini.getElementsByClassName("items")[0],
        anchors = nav.getElementsByTagName("a"),
        counter = 0;

        catini.anchors.each = anchors.each = function(fn) {
            for (var pos = 0, len = this.length; pos < len;) fn(this[pos++]);
        };

        anchors.each(function(anchor) {
            if (/catalog/.test(anchor.href)) {
                catini.anchors.push(anchor);
            }
        });
        catini.anchors.each(function(anchor) {
            anchor.id = 'cl' + counter;
            anchor.setAttribute( 'id', 'cl' + counter );
            anchor.style.color      = '#774499';
            anchor.href = [
                "javascript:catini.catalogGo( '",
                anchor.href.replace(
                    /^.+(?:\.com|www)\/([\w\/]*)\?sid=.+$/,
                    "$1" ),
                "' );"
            ].join('');
        });

        // Init Catalog
        return catini.request ({
            url: 'http://' +
                 catini.domain +
                 '.catalog.upsellusa.com/?sid=' +
                 catini.session,
            noret: true,
            onSuccess: function() {
                return catini.request ({
                    url: 'http://'+catini.domain+'.catalog.upsellusa.com/category/76',
                    noret: true,
                    onSuccess: function() {
                        catini.anchors.each(function(anchor) {
                            anchor.style.color = '#222';
                        });
                        return catini.readyCatalog = true;
                    }
                });
            }
        });
    },

    catalogGo : function(loc) {
        var goloc = 'http://' +
                    catini.domain +
                    '.catalog.upsellusa.com/' +
                    loc +
                    '?sid=' +
                     catini.session;

        catini.anchors[0].innerHTML =
            'Loading Catalog...' +
            '<img src="graphics/loading.gif" border="0"/>';

        if (catini.readyCatalog) {
            location = goloc;
        }
        else {
            setTimeout( function(){ catini.catalogGo(loc); }, 20 );
        }
    },

    /*-------------------------------------------------------------------------------
        Request
        -------
        We are using super secret ninjutsu ninja skill to create a breach in JS
        security. Actually we are just simply making a old school ajax request
        manager. This is the method that came before any ajax standards were
        implemented. Well... there are a few other methods, but this one works
        for what we need.
    -------------------------------------------------------------------------------*/
    reqCount : 0,
    request : function(args)
    {
        var bmiframe   = document.createElement("iframe");
        var bmiframeId = "bmiframe_" + catini.reqCount.toString();

        args.onSuccess = args.onSuccess || function(){};

        catini.requestCount++;

        bmiframe.setAttribute("id", bmiframeId);
        bmiframe.setAttribute("src", args.url);
        bmiframe.style.display = "none";

        bmiframe.onreadystatechange = function() {
            if ( bmiframe.readyState == "loading" ||
                 bmiframe.readyState == "complete" ) {
                return args.onSuccess('');
            }
        };

        bmiframe.onload = function() {
            return args.onSuccess('');
        };

        return document.
               getElementById("bmiframe_loader").
               appendChild(bmiframe);
    },

    getElementsByClassName : function( className, node ) {
        var nodes = [];

        catini.stepNode(
            document.getElementsByTagName("body")[0],
            function(node) {
                if (node.className === className) nodes.push(node);
            }
        );

        return nodes;
    },

    stepNode : function( node, fun ) {
        fun(node);
        node = node.firstChild;
        while (node) {
            catini.stepNode( node, fun );
            node = node.nextSibling;
        }
    }
};

})();
