/*
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 * This collection of scripts is built on top of the very light weight jQuery library.
 * Credit for jquery menu goes to Myles Angell with a bit of shim correction from Adam Cooper
 */

jQuery(document).ready(function(){
	jQuery("div.firstwave li").hoverClass("sfHover");
	jQuery(".tools").prepend("<div></div><span>Tools</span>");
	jQuery(".toolsadmin").prepend("<div></div><span>Project Admin</span>");
	jQuery(".subdiv").prepend("<div></div><span>&nbsp;</span>");
	jQuery("label").click(function(){jQuery(this).find("input[@type='checkbox']").check();});
});
jQuery.fn.hoverClass = function(c) {
        return this.each(function(){
	        jQuery(this).hover(
                	function() { jQuery(this).addClass(c); },
                	function() { jQuery(this).removeClass(c); }
        	);
        });
};

jQuery.fn.check = function() {
	return this.each(function() {
		this.checked = true;
	});
};
// function to break up project nav submenu items which have too many characters
cleanProjNavItems = function() {
	// for each of the "firstwave ul li a" elements ...
	jQuery(".firstwave ul li a").each(function(){
		// wordWrap the contents and re-assign
		jQuery(this).html(wordWrap(jQuery(this).text(), 20, " ", true));
	});
};
// wordWrap function adapted from http://www.bigbold.com/snippets/posts/show/869
wordWrap = function(bigString, m, b, c){
    var i, j, s, r = bigString.split(" ");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
    return r.join(" ");
};
jQuery(document).ready(cleanProjNavItems);
// function for re-sizing each of the firstwave ul\'s in case they go beyond the visible browser window
resizeNavDropdowns = function(){
        // for every "firstwave ul" element ...
        jQuery("#frame div.firstwave ul").each(function(){
                // if the element\'s y position + height is more than the browser window height
                if(jQuery(this).height() + 255 > TB_getPageSize()[1]){
                        // reset the height of this element to be the browser window height, minus all the SF.net top banner overhead
                        jQuery(this).height((TB_getPageSize()[1] - 340) + "px");
                        // set overflow:auto to the element to enable the scroll bar
                        jQuery(this).addClass("flow");
                        jQuery(this).find("sup").css("font-size","70%");
                }
         });
};

jQuery(document).ready(resizeNavDropdowns);
