$(document).ready(function(){
    applyIcons();
    $.tabs("links");
    suggestForm();
});


// Better than my tabs (edited slightly from the version below):
// http://www.stilbuero.de/2006/05/13/accessible-unobtrusive-javascript-tabs-with-jquery/
$.tabs = function(containerId, start) {
    var ON_CLASS = 'on';
    var id = '#' + containerId;
    var i = (typeof start == "number") ? start - 1 : 0;

    // create divs and tab titles:
    var sections = [ 'New','Cool','Hot','Featured','Search' ];
    for (var si=0,sj=sections.length; si<sj; si++) {
        var s = sections[si].toLowerCase();
        var li = $.LI({Class:s},$.A({href:'#links_'+s},sections[si]));
        $($(id+" ul").get(0)).append(li);
        
        var div = $.DIV({ id:'links_'+s },' ');
        $(id).append(div);
    }

    $(id + '>div:lt(' + i + ')').add(id + '>div:gt(' + i + ')').hide();
    $(id + '>ul>li:nth-child(' + i + ')>a').addClass(ON_CLASS);
    $(id + '>ul>li>a').click(function() {
        if (!$(this).is('.' + ON_CLASS)) {
            var re = /([_\-\w]+$)/i;
            var target = $('#' + re.exec(this.href)[1]);
            if (target.size() > 0) {
                $(id + '>div:visible').hide();
                if ($("*",target).size()==0) {
                    target.html("Loading...");
                    target.load("/ajax/links.ajax.php?section="+target.get(0).id,
                        function(){
                            applyIcons(target);
                        }
                    ).show();
                } else {
                    target.show();
                }
                $(id + '>ul>li>a').removeClass(ON_CLASS);
                $(this).addClass(ON_CLASS);
            } else {
                alert('There is no such container.');
            }
        }
        return false;
    });
};


// Apply Linkjam icons to links:

function applyIcons(scope) {
    $(".qhoolink_hot a",scope).each(function(){  applyIcon(this,'hot'); });
    $(".qhoolink_cool a",scope).each(function(){ applyIcon(this,'cool'); });
    $(".qhoolink_new a",scope).each(function(){  applyIcon(this,'new'); });
    $(".qhoolink_feat a",scope).each(function(){ applyIcon(this,'featured'); });
    $(".qhoolink_ecom a",scope).each(function(){ applyIcon(this,'shopping'); });
}

function applyIcon(e,i) {
    var icon = document.createElement('img');
    icon.src = '/images/lj/'+i+'.gif';
    $(e).after(icon);
//    e.appendChild(icon);
}

function suggestForm() {

    $("div.suggest").each(function(){
    
        // create container form form
        //var div = $.DIV({Class:'suggest_form'},'');
        //$(div).hide();
        //$(this).append(div);
	
	var box = $(this);
 
        var form = $("form",box);
	//form.hide();

        var link = $(this);
        $("a",box).click(function(){
            
		//form.toggle();
		return false;

		return;

            formbox.load("/ajax/jamlink.ajax.php",
                function(){
                    
                    $("form",this).submit(function(){

                        var f = this; // the <form> element
            
                        var url = f.attributes['action'] && // IE requires this crap
                        f.attributes['action'].nodeValue || f.getAttribute('action');
                        
                        $.ajax({
                            type: 'POST',
                            url: url,
                            data: $.param($(f).formdata()),
                            dataType: "json",
                            error: function() {
                                alert("Error!");
                            },
                            success: function(xml,status) {
                                if (!xml.responseText) return false;
                                eval("var json = "+xml.responseText);
                                switch (json.severity) {
                                    case 'notice':
                                        formbox.load("/ajax/jamlink.ajax.php",function(){ $(this).prepend(json.markup); });
                                        //$(f).before(json.markup).slideUp().remove();
                                        f.reset(); // clear the form
                                        //$(f).remove();
                                        break;
                                    case 'error':
                                        $(f).prepend(json.markup);
                                        break;
                                    default: /* error! */ break;
                                }
                            },
                            complete: function(xml,status) {
                                // we might want to do something here. maybe.
                            }
                        });
                        return false;
                    });
                }
            ).show();
            return false;
        });
    });

}

// Make home page sections (boxes) user-controllable... sort of:
/*
var editSections = {

    boxes: '',

    init: function() {
        var scope = this;

        boxes = $("div#main div.editable");
        if (!$(boxes).get(0)) return false;
        
        boxes.each(function(i){
            
            var box = this;
            
            // add a wrapper element around box content (but not header)
            var boxContent = $(box).html();
            $("*",box).remove();
            var html = DomBuilder.apply();
            var boxWrapper = html.DIV({'class':'wrapper'});
            $(box).append(boxWrapper);
            $(".wrapper",box).append(boxContent);
            $(boxWrapper).before($("h2",boxWrapper).get(0));
            
            // create menu buttons and add them to the DOM
            var html = DomBuilder.apply();
            var editMenu = html.DIV(
                html.A({href: '#'}, "x"),
                html.A({href: '#'}, "-")
            );
            var title = $("h2",this).get(0);
            $(title).append(editMenu);

            // apply 'onclick' actions to each menu button
            var editButtons = $("div a",title).each(function(i){
                this.onclick = function() {
                    switch ($(this).html()) {
                        case 'x': scope.close(box); break;
                        case '-':
                        case '+': scope.minimize(this,box); break;
                    } return false;
                }
            });            
        });
        
    },
    
    minimize: function(link,box) {
        switch ($(link).html()) {
            case '+':
                $($(".wrapper",box).get(0)).slideDown("normal");
                $(link).html("-");
                break;
            case '-':
                $($(".wrapper",box).get(0)).slideUp("normal");
                $(link).html("+");
                break;
        }
    },
    
    close: function(box) {
        $(box).slideUp("fast");
    }

}
*/
