// uniknijmy konfliktow nazw przez wykorzystanie anonimowych funkcji
(function($){
    $.fn.extend({
        // w miejsce 'pluginname' wpisujemy nazwe wtyczki
        subMenu: function(options) {
            var defaults = {
                getAjaxCommentsUrl          : '',
                event                       : 'hover',
                activeElementId             : null,
                classOnActive               : 'active',
                exprTrig                    : 'ul',
                initedStock                 : [],
                templates   :{
                    ERROR_1 : 'Zaloguj się'
                }
            }
            options =  $.extend(defaults, options); // nadpiszą się tu

            // dla każdego węzła spełaniającego warunki
            return this.each(function() {
                var $this = $(this);
                $this.initEvent($this,options);
                $('body').click(function(){
                    $this.hideSubMenu(options.activeElementId,$this,options);
                    options.activeElementId = null;
                });


            });
        }
    });
    $.fn.initEvent = function($this,o){
        if(o.event == 'click'){
            $.fn.initEventClick($this,o);
        }
        if(o.event == 'hover'){
            $.fn.initEventHover($this,o);
        }
    }
    $.fn.initEventHover = function($this,o){
        if($this.attr('id') == ''){
            $this.attr('id',$this.attr('class')+'Id_'+Math.round(1000*Math.random()));
        }
        $this.parent().hover(function(){
            $this.eventElement($this,o);
            },function(){
            $this.eventElement($this,o);
        });
        o.initedStock[o.initedStock.length]=$this.attr('id');
    }
    $.fn.initEventClick = function($this,o){
        if($this.attr('id') == ''){
            $this.attr('id',$this.attr('class')+'Id_'+Math.round(1000*Math.random()));
        }
        $this.click(function(){
            $this.eventElement($this,o);
            return false;
        })
        o.initedStock[o.initedStock.length]=$this.attr('id');
    }
    $.fn.eventElement = function($this,o){
        if(o.activeElementId == null){
            if($this.showSubMenu($this.attr('id'),$this,o)){
                o.activeElementId = $this.attr('id');
                return true;
            }else{
                $this.error(o,'ERROR_1');
                return false;
            }
        }
        if($this.attr('id') == o.activeElementId && o.activeElementId != null){
            if($this.hideSubMenu($this.attr('id'),$this,o)){
                o.activeElementId = null;
                return true;
            }else{
                $this.error(o,'ERROR_2');
                return false;
            }
        }
        if($this.attr('id') != o.activeElementId){
            if(!$this.hideSubMenu(o.activeElementId,$this,o)){
                $this.error(o,'ERROR_3');
                return false;
            };
            if($this.showSubMenu($this.attr('id'),$this,o)){
                o.activeElementId = $this.attr('id');
                return true;
            }else{
                $this.error(o,'ERROR_4');
                return false;
            };
        }
        return false;
    }
    $.fn.showSubMenu = function(id,$this,o){
        trigg = $this.getTriggerById(id,o);

        if(trigg.html()){
            trigg.fadeIn('300');
            $('#'+id).addClass(o.classOnActive);
            trigg = null;
            return true;
        }else{
            return false;
        }
    }
    $.fn.hideSubMenu = function(id,$this,o){
        trigg = $this.getTriggerById(id,o);
        if(trigg.html()){
            trigg.hide();
            $('#'+id).removeClass(o.classOnActive);
            trigg = null;
            return true;
        }else{
            return false;
        }
    }

    $.fn.getTriggerById = function(id,o){

        return $('#'+id).parent().find(o.exprTrig);
    }
    $.fn.error = function(o,errorId){
//        alert(o.templates[errorId]);
    }
    /**
     * testujemy dane wejsciowe
     */
    $.fn.testOptions = function(options){

    }
})(jQuery); // przekaz do funkcji referencje na framework
