$(document).ready(function() {
    if ($("#directory-columns").length > 0) {
        //$.getJSON(feedBaseURL + "?members=False&callback=?",
        //function(k) {
            var k = directoryFeed;
            var l = ["first", "second", "third"];
            var m = ["News & Politics", "Music", "Writers"];
            var n = ["Sports", "Environment", "Lifestyle"];
            var o = ["Business", "Technology", "Entertainment", "Travel", "Shopping"];
            var p = 0;
            $.each(k.categories,
            function(i, b) {
                wraperColumn = l[p];
                p++;
                if (((i + 1) % 3) == 0) {
                    p = 0
                }
                if ($.inArray(b.name, m) > -1) {
                    wraperColumn = "first"
                }
                if ($.inArray(b.name, n) > -1) {
                    wraperColumn = "second"
                }
                if ($.inArray(b.name, o) > -1) {
                    wraperColumn = "third"
                }
                $("<div>").attr({
                    "id": b.id,
                    "class": "column"
                }).appendTo("#directory-columns ." + wraperColumn);
                $("<h1>").text(b.name).appendTo("#" + b.id);
                $("<ul>").attr({
                    "class": "subcolumn",
                    "id": "subcolumns-" + b.id
                }).appendTo("#" + b.id);
                $.each(b.groups,
                function(i, a) {
                    $("<li>").attr({
                        "class": "sub",
                        "id": a.id
                    }).appendTo("#subcolumns-" + b.id);
                    if (((i + 1) % 2) == 0) {
                        $("#subcolumns-" + b.id + " li#" + a.id).addClass("alt")
                    }
                    $("#subcolumns-" + b.id + " > li#" + a.id).html("<p class=\"head\"><span class=\"title ellipsis\" title=\"" + a.name + "\">" + a.name + "</span><span class=\"count\">" + a.members_count + "</span></p>");
                    $("<ul>").attr({
                        "class": "members",
                        "id": "members-" + a.id
                    }).appendTo("#subcolumns-" + b.id + " li#" + a.id)
                })
            });
            firstColOrder = m;
            $.each($("#directory-columns .first div.column"),
            function(i, a) {
                firstColOrder[$.inArray($(a).children("h1").text(), m)] = a;
                $(a).remove()
            });
            $.each(firstColOrder,
            function(i, a) {
                $(a).appendTo("#directory-columns .first")
            });
            secondColOrder = n;
            $.each($("#directory-columns .second div.column"),
            function(i, a) {
                secondColOrder[$.inArray($(a).children("h1").text(), n)] = a;
                $(a).remove()
            });
            $.each(secondColOrder,
            function(i, a) {
                $(a).appendTo("#directory-columns .second")
            });
            thirdColOrder = o;
            $.each($("#directory-columns .third div.column"),
            function(i, a) {
                thirdColOrder[$.inArray($(a).children("h1").text(), o)] = a;
                $(a).remove()
            });
            $.each(thirdColOrder,
            function(i, a) {
                $(a).appendTo("#directory-columns .third")
            });
            if ($("#directory-columns").length > 0) {

                $(".subcolumn li.sub p.head").click(function() {
                    $("p.activeSWF").removeClass("activeSWF");
                    if (!$(this).parent().hasClass("open")) {
                        $(".subcolumn li.open").removeClass("open");

                    }
                    var g = $(this).parents("div.column").attr("id");
                    var h = $(this).parents("li.sub").attr("id");
                    if ($("#subcolumns-" + g + " li#" + h + " #members-" + h + " li").length == 0) {
                        var j = feedBaseURL + "/" + g + "/" + h;
                        $.getJSON(j + "?callback=?",
                        function(b) {
                            members_count = b.members.length;
                            $.each(b.members,
                            function(i, a) {
								var desc;
								if (a.text == null) {
									desc = "";
								} else {
									desc = a.text;
								};
                                member = $("<li>").html("<li><img src='" +  a.image + "' alt='' class='avatar' /><p><span><a href=\"http://twitter.com/" + a.screen_name + "\">@ " + a.screen_name + "</a></span><span class=\"count\">" + a.followers_count + " followers</span></p><p class=\"description\">" + desc + "</p></li>").appendTo("#subcolumns-" + g + " #members-" + h);
                                if (i > 6) {
                                    $(member).addClass("show-more")
                                }
                            });
                            $(this).blur();
                            if (members_count > 7) {
                                var c = " show-more"
                            } else {
                                var c = ""
                            }
                            action = $("<li>").attr({
                                "class": "actions"
                            }).html("<p class='activeSWF'><span class=\"add\" id=\"add-" + h + "\"></span><span class=\"more" + c + "\"><a href=\"#more\">More</a></span></p>").appendTo("#members-" + h);
                            $(action).find("span.more a").toggle(function(e) {
                                e.preventDefault();
                                $(this).parents("ul.members").find("li.show-more").show();
                                $(this).text("Less")
                            },
                            function(e) {
                                e.preventDefault();
                                $(this).parents("ul.members").find("li.show-more").hide();
                                $(this).text("More")
                            });
                            if ($("#add-" + h).length > 0) {
                                var d = new SWFObject("/assets/installbadge/badge_directory_0109.swf", "swf", "215", "150", "9.0.115", "#000000");
                                d.useExpressInstall('/assets/installbadge/expressinstall.swf');
                                d.addVariable("airversion", "1.5.2");
                                d.addVariable("directoryident", "GroupImport");
                                d.addVariable("appname", "TweetDeck");
                                if(navigator.platform.indexOf('Linux') == "-1") {
                        			d.addParam("wmode", "transparent");
                        		}
                        		d.addVariable("appurl", "http://downloads.tweetdeck.com/TweetDeck_0_32.1.air");
                        		d.addVariable("appversion", "0.32.1");

                                var f = "";
                                $.each(j,
                                function(i, a) {
                                    dec = j.charCodeAt(i);
                                    hex = dec.toString(16);
                                    f += hex
                                });
                                d.addVariable("directoryurl", f);
                                d.write("add-" + h)
                            }
                        })
                    }
                    $(this).parent().toggleClass("open");

                })
            }
    }
});

function installAIR()
{
    $("p.activeSWF").css("height","150px");
}
