/**
 * APS11 Contact List
 *
 * APS11 Kontaktliste (Seitenliste) umbauen
 *
 * @access public
 * @author mdt
 * @since 2012/01/05
 * @version 0.1a - unstable alpha
 */

( function ( $ ) {

    // the function name of the plugin
    var pluginName = "aps11_contactlist",

    // the used methods
    methods = {

        init:function ( options ) {
            var contactList = $(this);
            var contactTree = $('<ul class="resultlist contacttree"></ul>').insertAfter(contactList);
            var organisations = {};
            var orga, person;

            $('li',contactList).each(function(){
                orga = $('.organisation strong',this).text();
                person = $('.persondata',this);
                if( typeof(organisations[orga]) === 'undefined') {
                    organisations[orga]=new Array();
                }
                organisations[orga].push(person);
            });
            $.each(organisations,function(key) {
                var handle = $('<a class="handle collapsed">'+key+'</a>');
                var node = handle.appendTo(contactTree).wrap('<li class="organisation"></li>');
                var nodeChilds = $('<ul class="nodeChilds invisible"></ul>').insertAfter(node);
                handle.click(function(){
                    $('ul',$(this).parent()).toggleClass('invisible');
                    $(this).toggleClass('expanded').toggleClass('collapsed');
                    return false;
                });
                $.each( this, function() {
                    $('a',this).click(function(){
                        $.fancybox.showLoading();
                        $('<div id="aps11_contactlist_persondetail"></div>').appendTo('body');
                        $('#aps11_contactlist_persondetail').load(
                            this.href+' .teaser_aps11-member',
                            function() {
                                $.fancybox($('#aps11_contactlist_persondetail'),{
                                    padding: 20,
                                    fitToView: false,
                                    width: 500,
                                    height: 230,
                                    autoSize:false
                                });
                            }
                        );
                        return false;
                    });
                    this.appendTo(nodeChilds).wrap('<li class="contact" />');
                })
            })
            contactList.hide();
        }
    };

    /**
     * jQuery plugin registration and method call logic
     *
     * Usually you don't have to change this.
     */
    $.fn[ pluginName ] = function ( method ) {
        // get the target
        var $this = $( this );

        // Method logic
        if ( typeof methods[ method ] === "function" ) {
            return methods[ method ].apply( $this, Array.prototype.slice.call( arguments, 1 ) ) || $this;
        } else if ( typeof method === 'object' || !method ) {
            return methods.init.apply( $this, arguments ) || $this;
        } else {
            $.error( "Method " + method + " does not exist on jQuery." + pluginName );
            // return always $this, becuase of fluent usage
            return $this;
        }
    };
})( jQuery );
