/*
 *  © 2000-2007 deviantART, Inc. All rights reserved.
 */
///independant



AutoExec = [];

AutoExec.time = function ()
{
    if (document.getElementById('deviantART-loves-you')) {
        clearInterval(AutoExec.time.timer);
        AutoExec.loaded();
    }
}



Events = {
    hook: function(node, ev, f)
    {
        ev = 'on' + ev;
        if (typeof node[ev] != 'function'){ // first time?
            // TODO investigate the speed benefit of having onclick.calls vs., for example, onclickcalls
            var handler = new Function('e', 'return Events._handler(this, window.event || e, arguments.callee.calls);');
            handler.calls = [];
            node[ev] = handler;
        }
        node[ev].calls.push(f);
        return true;
    }
    ,unhook: function(node, ev, f)
    {
        var a, i;
        a = node['on' + ev].calls;
        for (i = 0; a[i]; i++) {
            if (a[i] == f){
                a.splice(i, 1);
                return true;
            }
        }
        return false;
    }
    ,_handler: function(node, e, calls)
    {
        var i, conclusion;
        conclusion = true; // hehe
        for (i = 0; calls[i]; i++){
            if (calls[i].call(node, e, e.target || e.srcElement) ==  false) {
                if (e.cancelable) {
                    e.preventDefault();
                }
                conclusion = false;
            }
        }
        return conclusion;
    }
    ,stop: function()
    {
        if (window.event) {
            window.event.cancelBubble = true;
        }
        return false;
    }
}
GUID = {
     latest: 10000
    ,get: function ()
    {
        return GUID.latest++;
    }
}
Browser = {};
Browser.isKHTML = navigator.userAgent.indexOf("KHTML") >= 0;
Browser.isGecko = (!Browser.isKHTML) && navigator.product == "Gecko";
Browser.isIE = (
       (!Browser.isGecko)
    && navigator.cpuClass != undefined
    && navigator.appName == "Microsoft Internet Explorer"
);
Browser.isIE55 = Browser.isIE && (document.onmousewheel == undefined);
Browser.isOpera = (
       (!(Browser.isIE || Browser.isGecko || Browser.isKHTML))
    && document.attachEvent != undefined
);
Browser.isMac = navigator.appVersion.indexOf("Mac") >= 0;
Browser.isWin = navigator.appVersion.indexOf("Windows") >= 0;





// This is a biggie, but it's definitely a codesaver in the long run
Tree = {
    tag: function(node)
    {
        return String(node.tagName).toLowerCase();
    }
    ,gets: function(node, selector, max)
    {
        var cls, tag, i, nodes, ret;

        if (typeof arguments[0] == 'string'){
            selector = arguments[0];
            max = arguments[1];
            node = document;
        }

        selector = selector.split('\.');
        tag = selector[0] || '*';
        if (selector[1]){
            cls = ' ' + selector[1] + ' ';
        }

        if (Browser.isIE55 && tag == '*') {
            nodes = node.all;
        }
        else{
            nodes = node.getElementsByTagName(tag);
        }

        // funtime
        i = -1;
        ret = [];
        while (nodes[++i]){
            if (cls){
                if ((' ' + nodes[i].className + ' ').indexOf(cls) < 0) {
                    continue;
                }
            }
            ret.push(nodes[i]);
            if (ret.length == max){
                break;
            }
        }
        return ret;
    }
    ,get: function(node, selector)
    {
        if (arguments.length < 2){
            selector = arguments[0];
            node = document;
        }
        if (selector.charAt(0) == '#') {
            return node.getElementById(selector.substr(1)); // delightfully unslow
        }
        else {
            return Tree.gets(node, selector, 1)[0];
        }
    }
    ,create: function(selector, attr, contents)
    {
        var i, n, j, altDoc;
        if (this.nodeType == 9){
            altDoc = this;
        }
        selector = selector.split('\.');
        n = (altDoc || document).createElement(selector[0]);
        if (selector[1]) {
            n.className = selector[1];
        }
        if (contents){
            if (!(contents instanceof Array)){
                contents = [contents];
            }
            for (i = 0; i != contents.length; i++){
                if (typeof contents[i] == 'string'){
                    n.appendChild((altDoc || document).createTextNode(contents[i]));
                }
                else{
                    n.appendChild(contents[i]);
                }
            }
        }
        for (i in attr) {
            if (i == 'style') {
                for (j in attr.style) {
                    if (n.runtimeStyle) {
                        n.runtimeStyle.setAttribute(j, attr.style[j]);
                    }
                    else {
                        n.style[j] = attr.style[j];
                    }
                }
            }
            else if (Browser.isIE && i.match(/^on/)) {
                n[i] = new Function(attr[i]);
            }
            else {
                n.setAttribute(i, attr[i]);
            }
        }
        return n;
    }
    ,destroy: function(node)
    {
        node.parentNode.removeChild(node);
        delete node; // experimental
    }
    ,ancestor: function(node, selector)
    {
        var tag, cls, top;

        top = (node.ownerDocument || node.document).documentElement;
        selector = selector.split('\.');
        if (selector[1]) {
            cls = ' '+ selector[1] + ' ';
        }
        tag = selector[0];
        do {
            if (tag && Tree.tag(node) != tag){
                continue;
            }
            if (cls && (' ' + node.className + ' ').indexOf(cls) < 0) {
                continue;
            }
            return node;
        } while ((node = node.parentNode) && node != top);
    }
    ,link: function (node)
    {
        while (node.nodeType != 1 && (node = node.parentNode));
        if (Tree.tag(node) == 'a') {
            return node;
        }
    }
    ,_objectStorage: {}
    ,getObject: function(node, label)
    {
        var i;
        if (!label) {
            label = 'default';
        }
        if (!Tree._objectStorage[label]) {
            return null;
        }
        if (node.id || node.uniqueID){
            return Tree._objectStorage[label][node.id || node.uniqueID];
        }
        else{
            for (i = 0; i != Tree._objectStorage[label].length; i++) {
                if (Tree._objectStorage[label][i].node == node) {
                    return Tree._objectStorage[label][i].obj;
                }
            }
        }
    }
    // deliberately unsophisticated. that may change
    ,setObject: function(node, label, obj)
    {   
        var i;
        if (!obj) {
            obj = label;
            label = 'default';
        }
        if (!Tree._objectStorage[label]) {
            Tree._objectStorage[label] = [];
        }
        if (node.id || node.uniqueID){
            Tree._objectStorage[label][node.id || node.uniqueID] = obj;
        }
        else {
            for (i = 0; i != Tree._objectStorage[label].length; i++) {
                if (Tree._objectStorage[label][i].node == node) {
                    Tree._objectStorage[label][i].obj = obj;
                    return;
                }
            }
            Tree._objectStorage[label].push({node: node, obj: obj});
        }
    }
    ,addClass: function(node, cls)
    {
        node.className += ' '+cls;
    }
    ,removeClass: function(node, cls)
    {
        var classes, i, newClasses;
        classes = node.className.split(' ');
        newClasses = [];
        for (i = 0; i != classes.length; i++){
            if (classes[i] != cls){
                newClasses.push(classes[i]);
            }
        }
        node.className = newClasses.join(' ');
    }
    ,hasClass: function(node, cls)
    {
        return (' ' + node.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
    ,setText: function(node, text)
    {
        node.innerHTML = '';
        node.appendChild(node.ownerDocument.createTextNode(text));
    }
    ,setHTML: function(node, html)
    {
        // Everything seems to support innerHTML these days, but
        //  I'm holding this here just in case it needs expansion
        node.innerHTML = html;
    }
    ,getText: function(node)
    {
        return node.textContent || node.innerText;
    }
    ,insertBefore: function(node, reference_node, parent) {
        if (!reference_node) {
            // IE can't handle node.insertBefore(node, null)
            parent.appendChild(node);
        }
        else {
            reference_node.parentNode.insertBefore(node, reference_node);
        }
    }
}

// from home.js

ShopFun = {
     boxes: []
    ,go: function ()
    {
        ShopFun.boxes = Tree.gets(Tree.get('div.four-squares'), 'div.art');
        for (i = 0; box = ShopFun.boxes[i]; i++) {
            ShopFun.boxes[i].appendChild(ShopFun.boxes[i].cloneNode(true));
        }
        ShopFun.scroll(true);
    }
    ,dataReady: function(success, data)
    {
        var i, count, art;
        if (!success) {
            return DRE.notice('Um, Huston...');
        }
        count = 0;
        for (i in data.response.content) {
            if (count++ == 45) {
                break;
            }
            art = data.response.content[i];
            this.appendChild(
                Tree.create(
                    'span'
                    ,{
                         href: art.url
                        ,title: '"' + art.title + '" - by ' + art.author.username
                        //,onmouseover: 'ShopFun.hover("' + art.title + '", "' + art.author.username + '")'
                        //,onmouseout:  'ShopFun.unhover()'
                        ,style: {
                            backgroundImage: 'url(' + art.thumburl + ')'
                        }
                    }
                )
            );
        }
        this.appendChild(this.cloneNode(true));
        ShopFun.scroll(true);
    }
    ,scroll: function(on)
    {
        if (arguments.callee.timer) {
            if (on) {
                return;
            }
            clearInterval(arguments.callee.timer);
            arguments.callee.timer = null;
        }
        if (on) {
            arguments.callee.timer = setInterval(ShopFun.scrollAgain, 100);
        }
    }
    ,scrollAgain: function()
    {
        var new_style, i, box;
        new_style = (parseInt(ShopFun.boxes[0].style.left || 0) - 1) + 'px';
        if (new_style == '-2550px') {
            new_style = 0;
        }
        for (i = 0; box = ShopFun.boxes[i]; i++) {
            box.style.left = new_style;
        }
    }
}


