(function(jQuery) {
    jQuery.fn.checkboxTree = function(settings) {
        settings = jQuery.extend({
            collapsedarrow: "img-arrow-collapsed.gif",
            expandedarrow: "img-arrow-expanded.gif",
            blankarrow: "img-arrow-blank.gif",

            /* Callbacks
            The callbacks should be functions that take one argument. The checkbox tree
            will return the jQuery wrapped LI element of the item that was checked/expanded.
            */
            onExpand: null,
            onCollapse: null,
            onCheck: null,
            onUnCheck: null,
            onHalfCheck: null,
            onLabelHoverOver: null,
            onLabelHoverOut: null,

            /* Valid choices: 'expand', 'check' */
            labelAction: "expand",

            checkchildren: false
        }, settings);

        var $group = this;

        jQuery(":checkbox + label", $group).click(function() {
            jQuery(this).prev().attr({ checked: !jQuery(this).prev().attr("checked") }).end().toggleClass("checked");
        }).each(function() {
            if (jQuery(this).prev()[0].checked)
                jQuery(this).addClass("checked");
        }).hover(
			    function() { jQuery(this).addClass("over"); },
			    function() { jQuery(this).removeClass("over"); }
		    ).prev().hide();

        if (settings.checkchildren == true) {
            jQuery(":checkbox + label", $group).click(function() {
                var $currclick = jQuery(this);
                var $currrow = jQuery(this).parents("li:first");

                $currrow.find(":checkbox + label").prev().attr({ checked: $currclick.prev().attr("checked") ? "checked" : "" })
				.next().addClass($currclick.hasClass("checked") ? "checked" : "").removeClass($currclick.hasClass("checked") ? "" : "checked")
            }
         )
        }

        //Activar todos os layers que possuem filhos
        // ---- Já não está a funcionar, é configurado no xml
        //jQuery("li:has(ul)", $group).each(function() {
        //    var $currentLi = jQuery(this);

        //$currentLi.find("label:first").addClass("checked");
        //});


        jQuery("li", $group).each(function() {
            var $currentLi = jQuery(this);

            $currentLi.find("label:first").click(function() {

                var ss = "";

                if ($currentLi.children("input:checkbox:first").is(":checked")) {
                    if ($currentLi.parent().length > 0 && $currentLi.parent().is("ul") && $currentLi.parent().attr("olLayerGroup")) {
                        var p = $currentLi.parent()[0];

                        jQuery($currentLi.parent().children("li")).each(function() {
                            if (this != $currentLi[0]) {
                                var ss = jQuery(this)

                                ss.children("label:first").removeClass("checked");
                                ss.children("input:checkbox:first").attr("checked", "");
                            }
                        });
                    }
                }

                //                    if (settings.onCheck) settings.onCheck($currentLi);
                //                } else {
                //                    if (settings.onCheck) settings.onUnCheck($currentLi);
                //                }


                if ($currentLi.find("label:first").hasClass("checked")) {
                    //Set parents visible
                    if ($group.find("span:first").attr("activateParents") == "true") {
                        $currentLi.parents("li:has(ul)").find("label:first").each(function() {
                            if (!$j(this).hasClass("checked")) {
                                $j(this).addClass("checked");
                                $j(this).prev().attr("checked", "checked");
                            }
                        });
                    }

                    //Show layers list on checked
                    if ($group.find("span:first").attr("showGroup") == "true" && $currentLi.children().is("ul")) {
                        $currentLi.children().filter("ul").show();
                        $currentLi.children("img").attr("src", settings.expandedarrow);
                    }
                }

                if ($currentLi.children("input:checkbox:first").is(":checked")) {
                    if (settings.onCheck) settings.onCheck($currentLi);
                } else {
                    if (settings.onCheck) settings.onUnCheck($currentLi);
                }                

            });

            if ($currentLi.is(":has(ul)")) {
                //check the parent boxes if a child is selected
                if ((!$currentLi.children("input:checkbox:first").is(":checked")) && (jQuery("ul li input:checked", $currentLi).length > 0)) {
                    $currentLi.find("label:first").trigger("click");
                }

                var $collapseImage;

                if ($currentLi.children("ul").css("display") == "block") {
                    $collapseImage = jQuery('<img src="' + settings.expandedarrow + '" / >');

                    $collapseImage.toggle(function() {
                        $currentLi.children("ul").hide();
                        $currentLi.children("img").attr("src", settings.collapsedarrow);
                    }, function() {
                        $currentLi.children("ul").show();
                        $currentLi.children("img").attr("src", settings.expandedarrow);
                    });
                } else {
                    $collapseImage = jQuery('<img src="' + settings.collapsedarrow + '" / >');

                    $collapseImage.toggle(function() {
                        $currentLi.children("ul").show();
                        $currentLi.children("img").attr("src", settings.expandedarrow);
                    }, function() {
                        $currentLi.children("ul").hide();
                        $currentLi.children("img").attr("src", settings.collapsedarrow);
                    });
                }

                $collapseImage.addClass("cursorhand");
                $currentLi.prepend($collapseImage);
            } else {
                var $collapseImage = jQuery('<img src="' + settings.blankarrow + '" / >');
                $currentLi.prepend($collapseImage);
            }
        });

        return $group;
    };
})(jQuery);

