/* First thing is to check for old IE and warn users. */

var UA = navigator.userAgent;
var isIE = (UA.indexOf('MSIE') > -1);
var isIE5 = (UA.indexOf('MSIE 5') > -1);
var isIE6 = (UA.indexOf('MSIE 6') > -1);
var isIE7 = (UA.indexOf('MSIE 7') > -1);

if (isIE5 || isIE6){
    alert('Internet Explorer cannot display this help file properly, because it does not support modern Web standards properly. You might prefer to open the Help file in a modern Web browser such as Firefox or Opera.');
}

var FlashColor = '#ffffff';
var FlashTotal = 12;
var Divs = null;
var Articles = new Array();
var Subheadings = new Array();
var Popup = null;
var PopupContent = null;
var AreaClass = 'ClickAreaDiv';
var ClickPicClass = 'ClickableScreenshot';
var CalloutPrefix = 'Call_';
var CalloutClass = 'ScreenshotCallouts';
var PopupObj = null;

/* Object to handle displaying fragments temporarily
   (for the popup functionality) */
function DisplayHost(ContainerEl, ContentEl){
    this.ContainerEl = ContainerEl;
    this.ContentEl = ContentEl;
    this.CurrContent = null;
//Bloody old IE is useless for CSS; have to position the popup manually
    if (isIE6){
        this.ContainerEl.style.position = 'absolute';
        this.ContainerEl.style.left='55%';
    }
}

function DisplayHostUnloadElement(){
//Return any previous element to where it came from
    if ((this.PreviousSource != null)&&(this.CurrContent != null)){
        this.PreviousSource.parentNode.replaceChild(this.ContentEl.removeChild(this.CurrContent), this.PreviousSource);
        this.PreviousSource = null;
        this.CurrContent = null;
    }
}
DisplayHost.prototype.UnloadElement=DisplayHostUnloadElement;

function DisplayHostLoadElement(NewEl){
    if (NewEl == null){return;}
    this.UnloadElement();
    if (NewEl != null){
//Create a placeholder element if there isn't one already
        if (this.PreviousSource == null){
            this.PreviousSource = document.createElement('div');
        }
//Lift the new element from its parent, replacing it with the placeholder...
        this.CurrContent = NewEl.parentNode.replaceChild(this.PreviousSource, NewEl);
//...and append it to our display element.
        this.ContentEl.appendChild(this.CurrContent);
    }
}
DisplayHost.prototype.LoadElement=DisplayHostLoadElement;

function DisplayHostShow(NewEl){
    this.UnloadElement();
    this.LoadElement(NewEl);
    this.ContainerEl.style.display = 'block';
    if (isIE6){
        this.ContainerEl.style.top = (document.body.scrollTop) + 50 + 'px';
    }
}
DisplayHost.prototype.Show=DisplayHostShow;

function DisplayHostHide(){
    this.UnloadElement();
    this.ContainerEl.style.display = 'none';
}
DisplayHost.prototype.Hide=DisplayHostHide;

/* Article object */
function Article(El){
    this.El = El;
    this.id = El.getAttribute('id');
    var NList = El.getElementsByTagName('img');
    this.ClickPic = null;
    for (var i=0; i<NList.length; i++){
        if (HasClass(NList[i], ClickPicClass)){
            this.ClickPic = new ClickPic(NList[i].getAttribute('id'));
        }
    }
}

function ArticleShow(){
    this.El.style.display = 'block';
    if (this.ClickPic != null){
        this.ClickPic.PositionAreas();
    }
    ShowSubheadings(false);
}
Article.prototype.Show=ArticleShow;

function ArticleHide(){
    this.El.style.display = 'none';
}
Article.prototype.Hide=ArticleHide;

function StartUp(){
    Popup = document.getElementById('Popup');
    PopupContent = document.getElementById('PopupContent');
    PopupObj = new DisplayHost(Popup, PopupContent);
    var i;
    Divs = document.getElementsByTagName('div');
    for (i=0; i<Divs.length; i++){
        if (HasClass(Divs[i], 'Article')){
            Articles.push(new Article(Divs[i]));
        }
    }
    
    var Heads = document.getElementsByTagName('h2');
    for (i=0; i<Heads.length; i++){
        if (HasClass(Heads[i], 'PartTitle')){
            Subheadings.push(Heads[i]);
        }
    }
    
    InitClickPics();

//Parse the url to figure out what should be shown
    var id = location.hash;
    if (id.length > 1){
        if (id.charAt(0) == '#'){
            id = id.substring(1, id.length);
        }
        var TargEl = document.getElementById(id);
        if (TargEl != null){
            if (HasClass(TargEl,'Article')){
                ShowArticle(id);
            }
            else{
                JumpTo(id);
            }
        }
    }
    else{
        id = 'TOC';
        document.getElementById('TOC').style.display = 'block';
    }
}

function ShowHideEmbeddedTOC(Id){
    var El = document.getElementById(Id);
    if (El != null){
        if (El.style.display != 'block'){
            El.style.display = 'block';
        }
       else{
           El.style.display = 'none';
       }
    }
}

function OpenEmbeddedTOCs(){
    var Els = document.getElementsByTagName('ol');
    for (var i=0; i<Els.length; i++){
        if (HasClass(Els[i], 'EmbeddedTOC')){
            Els[i].style.display = 'block';
        }
    }
}

function ShowSubheadings(Show){
    if (Show == true){
        for (var i=0; i<Subheadings.length; i++){
            Subheadings[i].style.display = 'block';
        }
    }
    else{
        for (var i=0; i<Subheadings.length; i++){
            Subheadings[i].style.display = 'none';
        }    
    }
}

var ArticleTrail = new Array();

function ShowArticle(ArticleId){
    for (var i=0; i<Articles.length; i++){
        if (Articles[i].id == ArticleId){
            Articles[i].Show();
            PopupObj.Hide();
            ArticleTrail.push(ArticleId);
        }
        else{
            Articles[i].Hide();
        }
    }
}

function GoBack(){
    if (ArticleTrail.length > 1){
//Remove this one
        ArticleTrail.pop();
//Show the previous one
        ShowArticle(ArticleTrail.pop());
    }
 //Otherwise show the TOC
    else{
        ShowArticle('TOC');
    }
}

function ShowNextArticle(){
   var Found = false;
   PopupObj.Hide();
   for (var i=Articles.length-1; i>=1; i--){
        if (Articles[i-1].El.style.display == 'block'){
            Articles[i-1].Hide();
            Articles[i].Show();
            ArticleTrail.push(Articles[i].id);
            Found = true;
        }
        else{
            Articles[i].Hide();
        }
    }
    if (Found == false){
        Articles[0].Show();
    }
}

function ShowPreviousArticle(){
   var Found = false;
   PopupObj.Hide();
   for (var i=0; i<Articles.length-1; i++){
        if (Articles[i+1].El.style.display == 'block'){
            Articles[i].Show();
            Articles[i+1].Hide();
            ArticleTrail.push(Articles[i].id);
            Found = true;
        }
        else{
            Articles[i].Hide();
        }
    }
    if (Found == false){
        Articles[Articles.length-1].Show();
    }
}

function ShowAll(){
    ShowSubheadings(true);
    OpenEmbeddedTOCs();
    for (var i=0; i<Articles.length; i++){
        Articles[i].Show();
    }
    InitClickPics();
}

function GetContainingArticle(ItemId){
    var Result = '';
    for (var i=0; i<Articles.length; i++){
        var Els = Articles[i].El.getElementsByTagName('*');
        for (var j=0; j<Els.length; j++){
            if (Els[j].getAttribute('id') == ItemId){
                Result = Articles[i].id;
            }
        }
    }
    return Result;
}

function GetScreenshotCallout(ItemId){
    var Result = '';
    var N = document.getElementById(ItemId);
    if (N != null){
        var NPar = N.parentNode;
        while ((NPar != document.documentElement)&&(HasClass(NPar, CalloutClass) == false)){
            N = N.parentNode;
            NPar = N.parentNode;
        }
    }
    if (HasClass(NPar, CalloutClass)){
        Result = N.getAttribute('id');
    }
    return Result;
}

function JumpTo(ItemId){
    ShowArticle(GetContainingArticle(ItemId));
    document.location.hash = ItemId;
//If the item you're jumping to is a hidden popup, then show it.
    var CalloutId = GetScreenshotCallout(ItemId);
    if (CalloutId.length > 0){
        PopupObj.Show(document.getElementById(CalloutId));
    }
    FlashItem(ItemId);
}

function ShowPopup(Id){
    if (document.getElementById(Id) != null){
//The following line is causing problems; we should avoid innerHTML.
//        PopupContent.innerHTML = document.getElementById(Id).innerHTML;
        PopupObj.Show(document.getElementById(Id));
    }
}

var FlashCount = 0;
var FlashingItem = null;

function FlashItem(ItemId){
    FlashingItem = document.getElementById(ItemId);
    if (FlashingItem != null){
        FlashCount = 0;
        setTimeout('Flash()', 500);
    }
}

function Flash(){
    if (FlashingItem != null){
        FlashCount++;
        if (FlashCount >= FlashTotal){
            FlashCount = 0;
            FlashingItem.style.backgroundColor = 'transparent';
            FlashingItem = null;
            return;
        }
        if (FlashingItem.style.backgroundColor != 'transparent'){
            FlashingItem.style.backgroundColor = 'transparent';
        }
        else{
           FlashingItem.style.backgroundColor = FlashColor; 
        }
        setTimeout('Flash()', 500);
    }
}

/* Code for handling interactive screenshots. */

var ClickPics = new Array();

function InitClickPics(){
    ClickPics.length = 0;
    var NList = document.getElementsByTagName('img');
    for (var i=0; i<NList.length; i++){
        if (HasClass(NList[i], ClickPicClass)){
            ClickPics.push(new ClickPic(NList[i].getAttribute('id')));
        }
    }
}

/* Clickable area. */
function ClickArea(El){
    this.El = El;
    this.id = El.getAttribute('id');
    this.L = parseInt(El.style.left);
    this.T = parseInt(El.style.top);
    this.W = parseInt(El.style.width);
    this.H = parseInt(El.style.height);
    if (isIE){
        this.El.style.fontSize = Math.max(this.W, this.H) + 'px';
    }
}

/* Screenshot object ("Clickable Picture" or ClickPic). */
function ClickPic(Id){
    this.id = Id;
    this.Prefix = this.id.split('_')[0];
    this.Areas = new Array();
    this.L = 0;
    this.T = 0;
//Find the image element
    this.ImgEl = document.getElementById(this.id);
    if (this.ImgEl != null){
//Get pointers to all the associated clickable areas.
        var NList = document.getElementsByTagName('div');
        for (var i=0; i<NList.length; i++){
            if ((HasClass(NList[i], AreaClass))&&(IdHasPrefix(NList[i], this.Prefix))){
                this.Areas.push(new ClickArea(NList[i]));
            }
        }
    }
}

function ClickPicGetCurrLocation(){
    var Pos = FindPos(this.ImgEl);
    this.L = parseInt(Pos[0]);
    this.T = parseInt(Pos[1]);
}
ClickPic.prototype.GetCurrLocation=ClickPicGetCurrLocation;

function ClickPicPositionAreas(){
    this.GetCurrLocation();
    for (var i=0; i<this.Areas.length; i++){
        this.Areas[i].El.style.left = this.Areas[i].L + this.L + 'px';
        this.Areas[i].El.style.top = this.Areas[i].T + this.T + 'px';
        this.Areas[i].El.style.display = 'block';
    }
}
ClickPic.prototype.PositionAreas=ClickPicPositionAreas;

function IdHasPrefix(El, Prefix){
    var TheId = El.getAttribute('id');
    var Pref = Prefix + '_';
    if (TheId.substring(0, Pref.length) == Pref){return true;}
    else{return false;}
}

function HasClass(El, ClassName){
    if (isIE){return (El.className == ClassName);}
    else{return (El.getAttribute('class') == ClassName);}
}

function Highlight(AreaId){
    if (document.getElementById(AreaId) != null){
        document.getElementById(AreaId).style.borderWidth = '1px';
    }
}

function Unhighlight(AreaId){
    if (document.getElementById(AreaId) != null){
        document.getElementById(AreaId).style.borderWidth = '0px';
    }
}

function FindPos(El) {
    var CurrL = 0;
    var CurrT = 0;
    if (El.offsetParent) {
        CurrL = El.offsetLeft;
        CurrT = El.offsetTop;
        while (El = El.offsetParent){
            CurrL += El.offsetLeft;
            CurrT += El.offsetTop;
        }
    }
    return [CurrL,CurrT];
}

