(function($) {

    $(".trigger").hover(function() { // quick fix to add hover class to the global trigger
        $(this).addClass(o.hoverClass);
    })

    $.fn.accordion = function(options) {
	
	    // var hash = self.document.location.hash.substring(1);
	    // 
	    // if (hash != "") {
	    //     var anchor = "#" + hash;
	    //     $(anchor).addClass("selected").next(".slider").addClass("showSlider").show();
	    // }
	
        // debug(this);
        var opts = $.extend({}, $.fn.accordion.defaults, options);
        this.each(function() {
            $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            if (o.global) {
                $this.global = $this.children(o.global);
                $this.global.addClass('all-closed');
            }
            $this.trigger = $this.children(o.trigger).not(".global");
            $this.slider = $this.children(o.slider);
            $this.trigger.each(function() {
                $(this).hover(function() {
                    $(this).addClass(o.hoverClass);
                }, function() {
                    $(this).removeClass(o.hoverClass);
                }).click(function() {

                    var slider = $(this).next('.slider');

                    if (slider.is(':visible')) {
                        $(this).removeClass(o.selectedClass);
                        o.animate == true ? slider.slideUp(o.animateSpeed, function() {
                            slider.removeClass('showSlider').addClass('hideSlider').removeAttr('style');
                        }) : slider.hide();
                        $("div.on").show(); //
                    } else {
                        $("div.on").hide(); //					
                        $(this).removeClass(o.hoverClass);
                        $(this).parent().children('.trigger').removeClass('selected');
                        $(this).addClass(o.selectedClass);
                        o.animate == true ? slider.slideDown(o.animateSpeed, function() {
                            slider.removeClass('hideSlider').addClass('showSlider').removeAttr('style');
                        }) : slider.show();
                    }
                    if (o.view == 'connected') {
                        if (o.animate) {
                            $this.children('.slider').not($(this).next('.slider')).slideUp(o.animateSpeed, function() {

                            });
                        } else {
                            $this.children('.slider').not($(this).next('.slider')).hide();
                        }
                    }
                    
                    if(o.onClick != null)
                        o.onClick($(this));
                });
            });
            switch (o.start) {
                case 'all-closed': default:
                    $this.children(o.slider).addClass('hideSlider');
                    break;
                case 'all-open':
                    $this.children(o.slider).addClass('showSlider');
                    break;
                case 'open-first':
                    $this.children(o.slider).not(o.slider + ":first").addClass('hideSlider');
                    break;
            }
        });
    };
    function debug($obj) {
        if (window.console && window.console.log) window.console.log('Accordions found: ' + $obj.size());
    };
    $.fn.accordion.defaults = {
        start: 'all-closed',
        trigger: '.trigger',
        slider: '.slider',
        animate: 'true',
        view: 'connected',
        hoverClass: 'hover',
        selectedClass: 'selected',
        animateSpeed: 500,
        global: null,
        globalOpen: 'all-open',
        globalClosed: 'all-closed',
        onClick: function(sender) {
            // self.document.location.hash = sender.attr('id');
        }
    };

})(jQuery);