(function($)
{
    var $triggeredElements = new Array();

	$.fn.dependent = function(settings)
    {
        var $param = $.extend({}, $.fn.dependent.defaults, settings);
        var $name = $param.name;
        var $prefix = $param.prefix;
        var $current = $('#'+$param.current);

        $current.hide();
        _hideChilds($current.find('ul[id='+$prefix+'0]'));

        $selected = $current.find('ul[id='+$prefix+'0]').find("input[name="+$name+"]:checked");
        $selectedTitle = '';
        $selectedCats = $current.parent().find('div[id='+$prefix+'selected_cats]').find('span');
        $selectedCats.html($selectedTitle);

        if ($selected.size()) {

            $selected.each(function()
            {
                $selectedTitle = '<div><button type="button" id="'+$prefix+'remove_button_'+$(this).attr('id')+'"ref_id="'+$(this).attr('id')+'">X</button>'+_getTitle($(this).parent().parent(), $prefix)+'<br></div>';
                $selectedCats.append($selectedTitle);
                $removeBtn = $current.parent().find('div[id='+$prefix+'selected_cats]').find('span').find('button[id='+$prefix+'remove_button_'+$(this).attr('id') + ']');
                $removeBtn.click(
                    function ()
                    {
                        $unselect = $current.find('ul[id='+$prefix+'0]').find("input[id="+$prefix+$(this).attr('ref_id')+"]:checked");

                        if ($unselect) {
                            $unselect.attr('checked', '');
                            $(this).parent().remove();
                        }
                    }
                );
            });
        }

        var $sBtnFlag;
        $selectBtn = $current.parent().find('div > button[id='+$prefix+'select_button]');
        $selectBtn.toggle(
            function ()
            {
                $current.show();
                $current.find('ul[id='+$prefix+'0]').show();
                $sBtnFlag = 1;
            },
            function ()
            {
                $current.hide();
                $current.find('ul[id='+$prefix+'0]').hide();
                $selected = $current.find('ul[id='+$prefix+'0]').find("input[name='"+$name+"']:checked");
                $selectedTitle = '';
                $selectedCats = $current.parent().find('div[id='+$prefix+'selected_cats]').find('span');
                $selectedCats.html($selectedTitle);

                if ($selected.size()) {

                    $selected.each(function()
                    {
                        $selectedTitle = '<div><button type="button" id="'+$prefix+'remove_button_'+$(this).attr('id')+'" ref_id="'+$(this).attr('id')+'">X</button>'+_getTitle($(this).parent().parent(), $prefix)+'<br></div>';
                        $selectedCats.append($selectedTitle);
                        $removeBtn = $current.parent().find('div[id='+$prefix+'selected_cats]').find('span').find('button[id='+$prefix+'remove_button_'+$(this).attr('id') + ']');
                        $removeBtn.click(
                            function ()
                            {
                                $unselect = $current.find('ul[id='+$prefix+'0]').find("input[id="+$prefix+$(this).attr('ref_id')+"]:checked");

                                if ($unselect) {
                                    $unselect.attr('checked', '');
                                    $(this).parent().remove();
                                }
                            }
                        );
                    });
                }

                $sBtnFlag = 0;
            }
        );

        $showSelectedBtn = $current.parent().find('div > button[id='+$prefix+'show_selected_button]');
        $showSelectedBtn.click(
            function ()
            {
                $selectBtn = $current.parent().find('div > button[id='+$prefix+'select_button]');

                if ($sBtnFlag == 1) {
                    $selectBtn.trigger('click');
                }

                $selectBtn.trigger('click');

                $expanded = $current.find('ul[id='+$prefix+'0]').find("input[name="+$prefix+"expand]:checked");

                if ($expanded.size()) {

                    $expanded.each(function()
                    {
                        $id = $(this).attr('value');
                        $expandButton = $(this).parent().find('a[id='+$prefix+'expand_button_'+$id+']');

                        if ($expandButton.size()) {

                            $expandButton.each(function()
                            {
                                $expandButton.trigger('click');
                            });
                        }
                    });
                }

                $selected = $current.find('ul[id='+$prefix+'0]').find("input[name="+$name+"]:checked");

                if ($selected.size()) {

                    $selected.each(function()
                    {
                        $toExpand = $(this).parent().parent().parent();
                        $parentId = $toExpand.attr('id');

                        if ($parentId != $prefix+'0') {
                            _expand($toExpand, $prefix);
                        }
                    });
                }

                $triggeredElements = new Array();
            }
        );

        _makeChange($current.find('ul[id='+$prefix+'0]'), $prefix);

		return this;
	};

    function _hideChilds($current)
    {
        $current.hide();
        $current.find('ul').each(function()
        {
            $(this).hide();
        });
    }

    function _makeChange($current, $prefix)
    {
        $expandButton = $current.find('a');
        $expandButton.toggle(
            function ()
            {
                $span = $(this).find('span');
                $span.html('<image src="modpub/Category/minus.jpg">');
                $title = $(this).attr('title');
                $show = $current.find('input[id='+$prefix+'expand_'+$title+']');
                $show.attr('checked', 'true');
                $toexpand = $current.find('li[id='+$prefix+'li_'+$title+']').find('ul[id='+$prefix+$title+']');
                $toexpand.slideDown('slow');
            },
            function ()
            {
                $span = $(this).find('span');
                $span.html('<image src="modpub/Category/plus.jpg">');
                $title = $(this).attr('title');
                $show = $current.find('input[id='+$prefix+'expand_'+$title+']');
                $show.attr('checked', '');
                $toexpand = $current.find('li[id='+$prefix+'li_'+$title+']').find('ul[id='+$prefix+$title+']');
                $toexpand.slideUp('slow');
            }
        );
    }

    function _expand($current, $prefix)
    {
        $id = $current.attr('id');

        if ($triggeredElements.indexOf($id) == -1) {
            $triggeredElements.push($id);
            $toExpandLink = $current.parent().find('a[id='+$prefix+'expand_button_'+$id+']');

            if ($toExpandLink.size()) {

                $toExpandLink.each(function()
                {
                    $toExpandLink.trigger('click');
                });
            }
        }

        $toExpandParent = $current.parent().parent();
        $parentId = $toExpandParent.attr('id');

        if ($parentId != $prefix+'0') {
            _expand($toExpandParent, $prefix);
        }
	}

    function _getTitle($current, $prefix)
    {
        var $title = $current.attr('title');
        var $cParent = $current.parent();
        var $parentId = $cParent.attr('id');

        if ($parentId != $prefix+'0') {
            $pTitle = _getTitle($cParent.parent(), $prefix);
            $title = $pTitle + ' >> ' +$title;
        }

        return $title;
	}
})(jQuery);

