/* serverview.js
 *
 * Codero.com custom accordion controls
 *
 * Written by Jeff Parker - jeffreyp(at)codero.com
 *
 * "parent" rows are clickable
 * "child" rows hold the data we want to show/hide
 *
 * $Id: serverview.js 1910 2009-09-29 19:12:40Z codero $
 */

$(document).ready(function()
{

    var theAccordion = {
        a : null,
        //isfirst : this.nextRowisFirst(),
        myclass : "tbl-row-first",
        plus : '/images/design/icon-table-open.gif',
        minus : '/images/design/icon-table-close-grey.gif',
        setplus : function(){
            this.a.find('img').attr('src', this.plus).attr('alt', 'Close');
        },
        setminus : function(){
            this.a.find('img').attr('src', this.minus).attr('alt', 'Expand');
        },
        add : function(){
            this.a.next().next().addClass(this.myclass);
        },
        remove : function(){
            this.a.next().next().removeClass(this.myclass);
        },
        nextRowisFirst : function(){
            var k = this.a.next().next().hasClass(this.myclass);
            return k;
        }
    }
    $(function() {
            $('tr.parent')
                    .css("cursor","pointer")
                    .click(function(){
                            //$(this).siblings('.child-'+this.id).toggle();
                            //
                            //toggle() broken for IE - hackish workaround for now
                            if($(this).siblings('.child-'+this.id).css('display') != 'none')
                            {
                                $(this).siblings('.child-'+this.id).hide();
                            }
                            else
                            {
                                $(this).siblings('.child-'+this.id).show();
                            }
                            //wheee! initialize object with jquery obj
                            theAccordion.a = $(this);
                            //did it just expand or collapse? check sibling row state to check.
                            if($(this).next().css('display') != 'none')
                            {
                                //it just expanded
                                if(!theAccordion.nextRowisFirst()) theAccordion.add();
                                //change image
                                theAccordion.setminus();
                            }
                            else
                            {
                                //it just collapsed
                                (theAccordion.nextRowisFirst()) ? theAccordion.remove() : theAccordion.add();
                                //change image
                                theAccordion.setplus();
                            }
                            //prevent jump
                            return false;
                    });
            //prevent jump
            return false;
    });

});
