(function($){
    // requires aria.js
    var aria = "aria" in $.browser && $.browser.aria;
    
    function calculateShadow(helpNode) {
    	$(".shadow", helpNode)
	  		.css("width", helpNode[0].offsetWidth+"px")
	  		.css("height", helpNode[0].offsetHeight+"px");
    }
        
    $(document).ready(function(){

        if($.browser.msie && $.browser.version <= "7")
        $("#doc").append($(document.createElement("iframe"))
            .attr("id", "shim")
            .attr("frameborder", 0)
            .css("position", "absolute")
            .css("z-index", "99")
            .hide());

        var labels = $(".has-tooltip").each(function(){
            var label = $(this),
            parents = label.parent();

            $("span", this).each(function() {
                var extra = $(this);
                
                $("input", parents).each(function(){
                    var e = extra.clone();
                    
                    if(aria) {
                        e.attr("aria-hidden", "true");
                    }

                    $(this).parent()
                        .append(document.createTextNode(" "))
                        .append(e);
                });
                extra.addClass("hidden");
            });
            
            var helps = label.nextAll(".help");
             
            if(helps.length) {
                var help = $(helps[0]),
				icon = $(document.createElement("span")),
				parents = $(this).parent();

                var input = $("input", parents);

                help.html("<div class=\"shadow\"></div><div class=\"bd\">"+help.html()+"</div>");
				
				//fix for selects overlapping tooltip in ie6 (bgiframe jquery plugin)
				help.bgiframe();
				$(".shadow", help).bgiframe();

                if(aria) {
                    input.attr("aria-describedby", help.attr("id"));
                }
                
                icon.addClass("information-icon").text("(help)")
                    .bind("mouseover", function() {
						
						//close all selects to avoid ugly overlapping of dropdown list over tooltip
						$("select").blur();
						
                        if(!label.hasClass("open")) {
                            label.triggerHandler("show-help");
                        }
                    })
                    .bind("mouseout", function() {
                        if(!label.hasClass("open")) {
                            label.triggerHandler("hide-help");
                        }
                    });

                input.bind("focus", function() {
                    var parents = $(this).parent();

                    if(label.hasClass("open")) {
                        label.removeClass("open")
                            .triggerHandler("hide-help");
                    } else {
                        $("label", parents.next("li")).each(function() {
                            $(this).removeClass("open").triggerHandler("hide-help");
                        });
                    }
                });

                // let Firefox breath.
                window.setTimeout(function() {
                    if($("strong", label).length)
                        $("strong", label).before(icon);
                    else
                        label.append(document.createTextNode(" ")).append(icon);
                    help.hide();
                    if(!$.browser.msie) {
                        label.after(help);
                    } else {
                        label.append(help);
                    }
                    var helpClone = help.clone();
                    $("body").append(helpClone);
                    label[0].__help = helpClone; //label needs a reference to "its" help. save on dom node
                }, 100);
            }
        });
        
        labels.bind("show-help", function() {        	
            var helpicon = $(".information-icon", this)[0],
                offset = $(helpicon).offset(),
                help = this.__help.show(),
                top = (offset.top - $(window).scrollTop()) > help[0].offsetHeight ?
                    -help[0].offsetHeight+offset.top : // top
                    helpicon.offsetHeight+offset.top, // below
                left = helpicon.offsetHeight+offset.left;
                
            help
                .hide()
                .attr("tabindex", "0")
                .css("top", top+"px")
                .css("left", left+"px")
                .css("z-index", 100)
                .show();
                
			calculateShadow(help);
            
            $("#shim")
                .css("top", top+"px")
                .css("left", left+"px")
                .css("width", help[0].offsetWidth+"px")
                .css("height", help[0].offsetHeight+"px")
                .show();

            if(aria)
                help.removeAttr("aria-hidden");
                
        });
        
        labels.bind("hide-help", function() {
        		if(this.__help) {
		            var help = this.__help.hide()
		                .attr("tabindex", "-1")
		                .css("top", "")
		                .css("left", "")
		                .css("z-index", "");
		
		            $("#shim").hide();
		            
		            if(aria)
		                help.attr("aria-hidden", "true");
        		}
        });
    });
})(jQuery);


