
/*  scripts */
// Begin scripts


/**
 * Highlight Profile
 * place in variable to avoid clashes
 * check document for span elements
 * insert data source of athlete profiles
 * insert links to athlete profiles
**/
var HiProf = {
    spans: 0, // spans in document
    match: 0, // matching spans
    count: 0, // matching spans processed
    tries: 0, // attempts to load data source
    aths: [], // profile ids
    // if profile spans exist, link to data source
    init: function() {
        // list span elements
        HiProf.spans = document.getElementsByTagName("span");
        // span elements with specific className
        for (var v = 0; v<HiProf.spans.length; v++) {
            if (HiProf.spans[v].className.match(/\bathHighlight\b/)) {
                HiProf.aths+= HiProf.spans[v].id+",";
                HiProf.match++;
            }
        }
        // relevant spans? link to script to change them
        if (HiProf.match > 0) {
            HiProf.buildScriptEl();
            HiProf.checkDataSource();
        } else {
            return;
        }
    },
    // test if data source is ready
    checkDataSource: function() {
        if (typeof profileHighlight != "undefined") {
            HiProf.updateSpans();
        } else {
            if (HiProf.tries <= 10) {
                setTimeout( HiProf.checkDataSource, 1000);
                HiProf.tries++;
                // document.getElementById("DEBUG").innerHTML=HiProf.tries;
            }
        }
    },
    // write script tag
    buildScriptEl: function() {
        // add data source
        var script = document.createElement("script");
        script.src="/js/ai;profiles_highlight_js/script.js";
        document.getElementById ("hiprosDS").appendChild(script);
    },
    // replace span text with anchor element
    updateSpans: function() {
        while(HiProf.count < HiProf.match) {
            for(var x=0; x<profileHighlight.length ; x++) {
                var p = profileHighlight[x][1].replace(/ /g,'_').toLowerCase();
                // loop through available athletes
                if(HiProf.aths.indexOf(p) > -1) {
                   
                    //var span = document.getElementById(p);       
                    //added by Billy Reisinger, Feb 7 2006
                    //If editors put any line feeds in span ids, it chokes
                    //IE and Opera.
                    var pWithLf = "\n" + p + "\n";
                    var span = (document.getElementById(p) ? document.getElementById(p) : document.getElementById(pWithLf));
                   
                    // Build new anchor element
                    var Anchor=document.createElement("a");
                    Anchor.setAttribute("href", "/athletes/"+profileHighlight[x][0]+"/detail.html");
                    Anchor.appendChild(document.createTextNode(profileHighlight[x][1]));
                    span.innerHTML="";
                    span.appendChild(Anchor);
					span.innerHTML += " ";
                }
            }
            HiProf.count++;
        }
    }
};
HiProf.init();


// End scripts


