

function makeLinksFit(){

    //debugger
    
    //makeRuler();

    var goMeeboDiv = document.getElementById('gomeeboers-top-10-div');
    //console.log(goMeeboDiv);
    //console.log(goMeeboDiv.style.width);

    if(!goMeeboDiv) return;
    
    for(i = 1; i<=10; i++){
        var cur = document.getElementById("gomeeboers-top-10-"+i);
        var numSpan = document.getElementById("gomeeboers-top-10-num-"+i);
        var ellipsized = getEllipsisText(parseInt(goMeeboDiv.style.width) -
                                         numSpan.offsetWidth-50,cur.innerHTML,cur);
        //while (cur.firstChild) cur.removeChild(cur.firstChild);
        //cur.appendChild(document.createTextNode(ellipsized));
        cur.innerHTML = ellipsized;
        //console.log("\t\t"+cur.innerHTML);
        //console.log(i+": num width: "+numSpan.offsetWidth+" ellipses text: "+cur.innerHTML);
        //console.log(numSpan);
    }

    //debugger
}


function makeRuler(){
    var rulerDiv = document.createElement("div");
    rulerDiv.style.display='none';
    rulerDiv.id = 'ruler';
    document.body.appendChild(rulerDiv);
}

function getTextLength(text, el) {

	if (!text || text.length <= 0)
		return 0;

	var rulerSpan = document.getElementById('ruler');
	var fontSize;
	var fontWeight;
	var fontFamily;

   	if (!rulerSpan){
		rulerSpan = document.createElement('span');
        rulerSpan.id = 'ruler';
		rulerSpan.style.left = '400px';
        rulerSpan.style.top = '0px';
        rulerSpan.style.position = 'absolute';
        //this is a good line to comment out if you are testing ellipses
		rulerSpan.style.visibility = 'hidden';
        rulerSpan = document.body.appendChild(rulerSpan);
    }

	if (el){

		var fontSize = getCSSProp(el, 'font-size');
		var fontWeight = getCSSProp(el, 'font-weight');
		var fontFamily = getCSSProp(el, 'font-Family');

        rulerSpan.style.fontSize = fontSize ? fontSize : '11px';
        rulerSpan.style.fontWeight = fontWeight ? fontWeight : 'normal';
        rulerSpan.style.fontFamily = fontFamily ? fontFamily : 'Tahoma, Arial, sans serif';
	}
	                
    rulerSpan.innerHTML = text;
	rulerSpan.className = '';//reset
	//this is another good line to comment in if you are debugging
	//the ellipses
	//$('copyright').innerHTML = fontSize + " " + fontWeight + " " + fontFamily;
    return rulerSpan.offsetWidth;
}

function getEllipsisText(width, msg, el){

    //console.log(width);
    
	if (!msg || !msg.length || !width)
		return msg;

	width = parseInt(width);//in case string is passed in
	var lastIndex = msg.length;

    
	var textLength = getTextLength(msg, el);

    if ((textLength <= width) || (textLength <= 0))
		return msg;

	var ellipsisLength = getTextLength('...', el);//will still be longer
	if (ellipsisLength > width)//even truncated text will be longer than width	   
		 return msg.charAt(0) + '...';//minimum text is four characters long - is this the functionality that we want?

	textLength = getTextLength(msg + "...", el);

	while (textLength >= width) {
	   	msg = msg.substring(0, lastIndex--);
       	textLength = getTextLength(msg + "...");
	}

	return msg + "...";
}

function getCSSProp (element, prop) {
   	//convert a hyphened format (border-width) into borderWidth
	var camelProp = prop.replace(/-\D/gi, function(sMatch) {
            return sMatch.charAt(sMatch.length - 1).toUpperCase();
        });


  	if (element.style && element.style[camelProp]) {

    	// inline style property
    	return element.style[camelProp];
  	} else if (element.currentStyle) {
		//Mitigation of IE Crash. Sometimes, IE will not have currentStyle ready to be accessed
		//and we encounter some sort of resource conflict resulting in an access violation that
		//then brings down the browser hard. This HACK is to force the re-calculation of the
		//currentStyle of the element by changing the display style.
		var oldDisplay = element.style.display;
		element.style.display = "block";
		element.style.display = oldDisplay;

    	return element.currentStyle[camelProp];
  	} else if (document.defaultView && document.defaultView.getComputedStyle) {
    	// external stylesheet for Mozilla and Safari 1.3+
    	prop = prop.replace(/([A-Z])/g,"-$1");
    	prop = prop.toLowerCase();

		var cs = document.defaultView.getComputedStyle(element, "");
		if (cs)
			return cs.getPropertyValue(prop);
		return '';
  	} 

   	// Safari 1.2 & Off-Dom handling
   	return null;
}

