Request.tweetmeme = new Class({
    // return json data on re-tweet counts
    Extends: Request.JSONP,
    options: {
        url: "http://api.tweetmeme.com/url_info.jsonc?url={location}"
    },
    initialize: function(location, options) {
        this.parent(options);
        this.options.url = this.options.url.substitute({location: location});
    },
    success: function(data, script) {
        this.parent(data, script);
    }
});

Request.twitterAPI = new Class({
    // return json data with latest tweets form a particular user
    // based upon appden's class http://appden.com/javascript/get-tweets-with-mootools/
    Extends: Request.JSONP,
    options: {
        target: $empty(),
        tweetBits: {
            textClass: "tweetBody",
            dateClass: "tweetDate"
        },
        maxTweets: 6,
        autoLink: true,
        url: "http://twitter.com/statuses/user_timeline/{user}.json"
    },
    initialize: function(user, options) {
        this.parent(options);
        this.options.url = this.options.url.substitute({user: user});
        this.element = document.id(this.options.target);
        if (!this.element)
            return;

        this.element.set("html", "Loading tweets...");
    },
    success: function(data, script) {
        this.element.empty();
        this.parent(data, script);
    },
    showPost: function(post) {
        var creationDate = Date.parse(post.created_at).format("%x %X");
        new Element("div", {
            "class": this.options.tweetBits.textClass,
            "html": this.options.autoLink ? this.linkify(post.text) : post.text
        }).adopt(new Element("div", {
            "class": this.options.tweetBits.dateClass,
            "html": creationDate // + " via " + post.source.replace("\\",'')
        })).inject(this.element);
    },
    addPosts: function(response) {
        if (!response.length || !this.element)
            return;

        response.each(function(post, i) {
            if (i < this.options.maxTweets)
                this.showPost(post);
        }.bind(this));
    },
    linkify: function(text){
        // modified from TwitterGitter by David Walsh (davidwalsh.name)
        // courtesy of Jeremy Parrish (rrish.org)
        return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
            .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
            .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
    }

});

new Asset.css("/js/internal.css");

window.addEvent("domready", function() {
    new Request.twitterAPI("OutdoorTalkUK", {
        target: "myTweets",
        onComplete: function(data) {
            this.addPosts(data);
        }
    }).send();

    var labels = document.getElements("a.ygtvlabel"), navtabs = $("navtabs");
    if (labels.length) {
        labels.each(function(el, i) {
            // hacks into yahoo tree thingie.
            var parent = el.getParent("table");
            if (!parent.hasClass("ygtvdepth0"))
                return;

            var href= el.get("href");
            var li = new Element("li").adopt(new Element("a", {
                "class": "navtab",
                "accesskey": 2+i,
                "href": href,
                "title": el.get("title"),
                "html": el.get("html")
            })).inject(navtabs);

            if (window.location.href == href) {
                navtabs.getElements("li").removeClass("selected");
                li.addClass("selected");
            }

            // subcats
            if (parent.hasClass("active")) {
                var children = parent.getNext().getElements("a.ygtvlabel");

                if (children.length) {
                    var submenu = navtabs.getElement("ul.floatcontainer");
                    submenu.empty();
                    children.each(function(c) {
                        new Element("li", {
                            "class": "subcat"
                        }).adopt(new Element("a", {
                            "class": "",
                            "href": c.get("href"),
                            "title": c.get("title"),
                            "html": c.get("html")
                        })).inject(submenu);

                    });

                }

            }
        });
        document.getElements("div.cms_widget_categorynav").setStyle("display", "none");
    }
});

var C = {
    // console wrapper
    debug: true, // global debug on|off
    quietDismiss: !false, // may want to just drop, or alert instead
    log: function() {
        if (!C.debug) return false;


        if (typeof console == 'object' && typeof console.log != "undefined")
            console.log.apply(this, arguments);
        else
            if (!C.quietDismiss) {
                var result = "";
                for (var i = 0, l = arguments.length; i < l; i++)
                    result += arguments[i] + " ("+typeof arguments[i]+") ";

                alert(result);
            }
    }
}; // end console wrapper.

// google analytics via mootools / dom.
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
    onload: function() {
        var pageTracker = _gat._getTracker("UA-17204901-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    }
});
