var PopupManager = new function() {
    this.newWindow = function (strURL,strWinName) {
        var strOptions = "";
        if ((strWinName == "Help") || (strWinName == "Contact") || (strWinName == "Terms") || (strWinName == "Privacy")) {
            strOptions = "scrollbars,resizable,height=600,width=560"; 
        } else { 
           strOptions = "scrollbars,resizable,height="+(screen.availHeight - 60)+",width=700,top=30,left="+ ((screen.availWidth-700)/2); 
        }
        
        window.open(strURL, strWinName, strOptions).focus();
    }
}

function popUp(strURL,strWinName) {
    PopupManager.newWindow(strURL,strWinName);
}

function changeSavedImageLink() {
    document.getElementById('popup_add_link').style.display = 'none';
    document.getElementById('popup_added_text').style.display = 'block';
}

function addImageFromPopUp(pii,mungedPii, figId, locator, height, title, caption) {
    removeOnClickFromSaveButton();
    if(opener){
        opener.addImage(pii, mungedPii, figId, locator, height, title, caption);
        changeSavedImageLink();
    } else{
        alert('Please close this window and revisit the site to add this image.');
    }
}



function displayImg (imageLetter) {
    var contentDiv = document.getElementById('content');
    var child = contentDiv.firstChild;
    while (child != null) {
        var childId = child.getAttribute('id');
        if (childId != null && childId.indexOf('popup_image') != -1) {
            child.style.display = 'none';
            var popupImageLetter = childId.substring(childId.length - 1, childId.length);
            document.getElementById('btn' + popupImageLetter).style.backgroundColor = '#ddd';
        }
        
        child = child.nextSibling;
    }   
    
    document.getElementById("popup_image" + imageLetter).style.display = "block";
    highlightButton(imageLetter);
}

function displayImgForNewEntitledPage (imageLetter) {

 	var figUrl = location.href;
    var figPos = figUrl.indexOf('figureIdLetter');
    if(figPos != -1)
    {
    var subFig = figUrl.substring(figPos);
    var newUrl = figUrl.replace(subFig, 'figureIdLetter='+imageLetter);
    }
    else
    {
    var newUrl = figUrl + '&figureIdLetter=' + imageLetter;   
    }	
    location.replace(newUrl);
}

function highlightButton(buttonLetter) {
    var buttonInput = document.getElementById("btn" + buttonLetter);
    if (buttonInput != null) {
        buttonInput.style.backgroundColor = 'orange';
    }
}

function createNewVisitorImageURL(figId, locator){
    var figUrl = location.href;
    var figPos = figUrl.indexOf('fig');
    var locatorPos = figUrl.indexOf('locator') - 1;
    var subFig = figUrl.substring(figPos, locatorPos);
    var newUrl = figUrl.replace(subFig, figId);
    if (newUrl.indexOf('locator') != -1)
    {
    locatorPos = newUrl.indexOf('locator');
    var piiPos = newUrl.indexOf('pii') - 1;
    var subLocator = newUrl.substring(locatorPos, piiPos);
    newUrl = newUrl.replace(subLocator, 'locator='+locator);
    }
    location.replace(newUrl);
}

function newVisitorImage(newVisitorImageURL){
    var newVisitorImageUrl = newVisitorImageURL;
    location.assign(newVisitorImageUrl);
}