
/*************************************************************************
 * Moves a copy of the "Rate This Article" form from the location the Velocity
 * template inserts it to
 * TODO: Please put this in a more sensible location later
 */
function copyForm() {

        var form = document.getElementsByName('RateNRecommendForm')[ 0 ];
        var form_block = document.getElementById('hidden_rate_it');
        var formTarget = document.getElementById('ratingFormDiv');

        if (form) {
            form.id = 'newForm';
            form.className = form.className + '_visible';
            if (formTarget) {
                formTarget.appendChild(form_block);
            }
        }

}


/*************************************************************************
 * Inserts a link to the top of the page at the end of the article's main text
 */
function insertTopLink() {
        var article = document.getElementById( 'body' );

        if ( article != null ) {

                var aTag = document.createElement( 'a' );
                aTag.href = 'javascript:scroll(0,0);'
                aTag.className = 'topLink';
                aTag.appendChild( document.createTextNode( 'Back to Top' ) );

                for ( i = 0; i < article.childNodes.length; ++i ) {
                        if ( article.childNodes[ i ].tagName && 
                             article.childNodes[ i ].tagName.toString().toLowerCase() == 'div' ) {
                                article.childNodes[ i ].appendChild( aTag );
                                // end the loop
                                i = article.childNodes.length;
                        }
                }//for
        }//if article
}


/*************************************************************************
 * Function to be called as an onload event.  This just calls the other
 * helper functions for the page form one location, and does no real work 
 * itself.
 */
function initArticlePage() {
    rateItStars();
    copyForm();
    relocateDiv();
    fixRelatedArticleLinks();
}


/*************************************************************************
 * Points the article links to the location inside support portal.
 * Should be called after the article loads.
 * An assumption is being made that the related articles will be contained
 * in the last UL tag on the page.
 */
function fixRelatedArticleLinks() {

  var relocated = document.getElementById( 'relocateRA' );
  if(relocated) {
        var aTags = relocated.getElementsByTagName( 'a' );

        var baseHref = window.location.toString();

        var bestBet = false;
        if ( baseHref.indexOf( 'bbid' ) > -1 ) {
            bestBet = true;
        }
        for ( var i = 0; i < aTags.length; ++i ) {
            /*
            var id = aTags[ i ].href.toString().replace( /.*articleId(=|%3D)(\d+)/, "$2" );
            var text = aTags[ i ].innerHTML;
            aTags[ i ].href = baseHref.replace( /(articleId(%3D)?)\d+/g, "$1" + id );
            aTags[ i ].innerHTML = text; // needed because IE6 is funky
            if ( bestBet ) {
                aTags[ i ].href += '&bestBetTitle=' + escape( aTags[ i ].innerHTML.replace( /\s+/g, ' ' ) );
            }           
            */
                        
            if ( aTags[i].href != '' ) {
                var r=aTags[ i ].href.replace(/mysupport/i,"microsites");
                aTags[i].href=r;
            }              
        }
  }
}


/************************************************************************
 * Function taken from production
 */
function hideShowAll(){
  var imgs = document.getElementById( "body" ).getElementsByTagName("img");
  var changeTo = "none";
  if(imgs[0].style.display == "none"){
    changeTo = "block";
  }
  for(var x=0;x<imgs.length;x++){
    imgs[x].style.display = changeTo;
  }
}


/************************************************************************
 * Changes all relative links to point at "http://help.aol.com" + the link
 */
function updateRelativeArticleLinks() {
    var aTags = document.getElementById( "body" ).getElementsByTagName("a");

    for( var i = 0; i < aTags.length; i++ ) {
        
        if ( aTags[i].href != '' &&
		aTags[ i ].href.match( /(kjump.adp\?.*)/ )
        ) {
            aTags[ i ].href = "http://help.channels.aol.com/" + aTags[ i ].href.match( /(kjump.adp\?.*)/ )[0];
        }
              
    }
}


/************************************************************************
 * Initializes the stars in the rate it form.
 */
function rateItStars() {

    var star_div = document.getElementById('rate_it_stars'),
        stars = [],
        rating = 0,

        starHover = function() {
            if (rating === 0) {
                for (var i = 0; stars[i] != this; i++) {
                    stars[i].className = 'hover';
                }
                stars[i].className = 'hover';
            }
        },

        starUnHover = function() {
            if (rating === 0) {
                for (var i = 0; i < 5; i++) {
                    stars[i].className = '';
                }
            }
        },

        starClick = function() {
            if (rating === 0) {
                for (var i = 0; i < 5; i++) {
                    stars[i].className = 'rate';
                    if (stars[i] === this) {
                        rating = i + 1;
                        break;
                    }
                }
                setRatingFromDropdown(document.getElementById('newForm'),rating);
            }
        };
    //
    if (star_div) { 
        for (var x = star_div.firstChild; x != null; x = x.nextSibling) {
            if (x.nodeType == 1) {
                x.onmouseover = starHover;
                x.onmouseout = starUnHover;
                x.onclick = starClick;
                stars.push(x);
            }
        }
    }

}

