// Brain Atlas Image Viewer Application -- windows.js
// Version: @BuildVersion@
// Date: @DATE@ -- @TIME@
// Copyright (c) 2004 Vulcan, Inc. All Rights Reserved.

// sub-windows need global variable for addressing from parent
var newWindow=null;
var helpWindow=null;
var faqWindow=null;
var supportWindow=null;
var subscribeWindow=null;
var feedbackWindow=null;
var tourWindow=null;
var examplesWindow=null;
var genesWindow=null;
var infoWindow=null;
var pundCWindow=null;
var legendWindow=null;
var RADocWindow=null;
var tutorialWindow=null;
var dlExplorerWindow=null;
var whatsNewWindow=null;

// giving parent window a name so we can reference it if needed
window.name = "main";
var msgPopup = "Your POP-UP BLOCKER has prevented us from launching a new window.\nPlease disable the POP-UP BLOCKER and try again.";

function trimWS(str) {
	// leading spaces
//  while (str.substring(0,1) == ' ') {
//		str = str.substring(1, str.length);
//	}
    // trailing spaces
	while (str.substring(str.length-1,str.length) == ' ') {
		str = str.substring(0, str.length-1);
	}
	return str;
}

function randNum(range) {
	// range is variation (+/-)
	x = Math.floor(Math.random()*range);
	y = Math.round(Math.random());
	if (y==0) { x = -x;}
	return x;
}

//
// Okay, obviously this should be a single function, but I'm brute force man today.
//
function openHelpWindow(theURL,winName,features) {
    if (!helpWindow || helpWindow.closed) {
        helpWindow = window.open(theURL,winName,features);
        try
        {
            var wname = helpWindow.name;
            helpWindow.focus();
        }
        catch(e)
        {
            alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        helpWindow.focus( );
    }
}

function openFaqWindow(theURL,winName,features) {
    if (!faqWindow || faqWindow.closed) {
        faqWindow = window.open(theURL,winName,features);
        try
	{
	    var wname = faqWindow.name;
	    faqWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        faqWindow.focus( );
    }
}

function openWhatsNewWindow(theURL,winName,features) {
    if (!whatsNewWindow || whatsNewWindow.closed) {
        whatsNewWindow = window.open(theURL,winName,features);
        try
	{
	    var wname = whatsNewWindow.name;
	    whatsNewWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        whatsNewWindow.focus( );
    }
}

function openSupportWindow(theURL,winName,features) {
    if (!supportWindow || supportWindow.closed) {
        supportWindow = window.open(theURL,winName,features);
        try {
	        var wname = supportWindow.name;
	        supportWindow.focus();
	    }
	    catch(e) {
	        alert(msgPopup);
        }
    }
    else {
        // window is already open, so bring it to the front
        supportWindow.focus( );
    }
}


function openSubscribeWindow(theURL,winName,features) {
    if (!subscribeWindow || subscribeWindow.closed) {
        subscribeWindow = window.open(theURL,winName,features);
        try {
	        var wname = subscribeWindow.name;
	        subscribeWindow.focus();
	    }
	    catch(e) {
	        alert(msgPopup);
        }
    }
    else {
        // window is already open, so bring it to the front
        subscribeWindow.focus( );
    }
}

function openDlExplorerWindow(theURL,winName,features) {
    if (!dlExplorerWindow || dlExplorerWindow.closed) {
        dlExplorerWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = dlExplorerWindow.name;
	    dlExplorerWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        dlExplorerWindow.focus( );
    }
}


function openFeedbackWindow(theURL,winName,features) {
    if (!feedbackWindow || feedbackWindow.closed) {
        feedbackWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = feedbackWindow.name;
	    feedbackWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        feedbackWindow.focus( );
    }
}

function openTourWindow(theURL,winName,features) {
    if (!tourWindow || tourWindow.closed) {
        tourWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = tourWindow.name;
	    tourWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        tourWindow.focus( );
    }
}

function openTutorialWindow(theURL,winName,features) {
    if (!tutorialWindow || tutorialWindow.closed) {
        tutorialWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = tutorialWindow.name;
	    tutorialWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        tutorialWindow.focus( );
    }
}

function openExamplesWindow(theURL,winName,features) {
    if (!examplesWindow || examplesWindow.closed) {
        examplesWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = examplesWindow.name;
	    examplesWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        examplesWindow.focus( );
    }
}

function openGenesWindow(theURL,winName,features) {
    if (!genesWindow || genesWindow.closed) {
        genesWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = genesWindow.name;
	    genesWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        genesWindow.focus( );
    }
}

function openInfoWindow(theURL,winName,features) {
    if (!infoWindow || infoWindow.closed) {
        infoWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = infoWindow.name;
	    infoWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        infoWindow.focus( );
    }
}

function openPundCWindow(theURL,winName,features) {
    if (!pundCWindow || pundCWindow.closed) {
        pundCWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = pundCWindow.name;
	    pundCWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        pundCWindow.focus( );
    }
}

function openLegendWindow(theURL,winName,features) {
    if (!legendWindow || legendWindow.closed) {
        legendWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = legendWindow.name;
	    legendWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        legendWindow.focus( );
    }
}

function openRADocWindow(theURL,winName,features) {
    if (!RADocWindow || RADocWindow.closed) {
        RADocWindow = window.open(theURL,winName,features);
	try
	{
	    var wname = RADocWindow.name;
	    RADocWindow.focus();
	}
	catch(e)
	{
	    alert(msgPopup);
        }
    } else {
        // window is already open, so bring it to the front
        RADocWindow.focus( );
    }
}

function openSingleViewWindow(winURL) {
    //opens the single_view.jsp page (feeding in the argument imageID)
    var t = new Date();
    var winName = "W" + t.getHours() + "" + t.getMinutes() + "" + t.getMilliseconds(); //"unique" names allow multiple instances
    var w=800;
    var h=600;
    var posLeft=(screen.width)?(screen.width-w)/2:100;
    var posTop=(screen.height)?(screen.height-h)/2:100;
    var settings='width='+w+',height='+h+',top='+posTop+',left='+posLeft+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
    var wwin = window.open(winURL,winName,settings);
    try
    {
        var wname = wwin.name;
        wwin.focus();
    }
    catch(e)
    {
        alert(msgPopup);
    }
}

function newPopWindow(winURL,winName) {
	// usage:  <a href="url.blah.com" onclick="newWindow(this.href,'nameblah');return false" onfocus="this.blur()">YourLinkText</a>
	// need to trim spaces:
	winName = trimWS(winName);
        w=390;
        h=300;
	posLeft=(screen.width)?(screen.width-w)/2:100;
	posTop=(screen.height)?(screen.height-h)/2:100;
	settings='width='+w+',height='+h+',top='+posTop+',left='+posLeft+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	newWindow=window.open(winURL,winName,settings);
    try
    {
        var wname = newWindow.name;
        newWindow.focus();
    }
    catch(e)
    {
        alert(msgPopup);
    }
}

function newExtWindow(winURL,winName) {
    // opens external window
	// usage:  <a href="url.blah.com" onclick="newWindow(this.href,'nameblah');return false" onfocus="this.blur()">YourLinkText</a>
    // TODO: REFACTOR ALL OPENERS TO USE SINGLE FUNCTION
    //
	// need to trim spaces:
	//
	winName = trimWS(winName);

	//
	// dimentions
	//
	winWidth=800;
	winHeight=600;
	scrWidth=screen.width;
	scrHeight=screen.height;

	//
	// random offsets
	//
	offLeft = randNum(75);
	offTop = randNum(50);

	//
	// new window positions
	//
	posLeft=(scrWidth)?((scrWidth-winWidth)/2)+offLeft:100;
	posTop=(scrHeight)?((scrHeight-winHeight)/2)+offTop:100;

	//
	// open the window
	//
	settings='width='+winWidth+',height='+winHeight+',top='+posTop+',left='+posLeft+',scrollbars=yes,location=yes,directories=yes,status=yes,menubar=yes,toolbar=yes,resizable=yes';
	newWindow=window.open(winURL,winName,settings);
    try
    {
        //var wname = newWindow.name;
        newWindow.focus();
    }
    catch(e)
    {
        alert(msgPopup);
    }
}
