// ========================================================================
//	TES JavaScripts Library V1.0
//	
//	File windows.js
//	
//	Copyright (c) 2002-2005 John Wiley and Sons, Inc.
// ========================================================================

// Create an array to hold references to the child windows.
// Each member of this array will be a window object created using the createWindow function below.
var m_Windows = new Array();

// Constructor for the window.
// This function should be called only by the createWindow function below.
function newWindow(in_URL, in_windowName)
{
	var features = "";
	if (null != arguments[2])
	{
		features = arguments[2];
	}
	var URL = in_URL;
	var windowName = in_windowName.replace(/-/g, "_");	
	
	var popupWin = window.open(URL, windowName, features);
	if (popupWin == null) {
	  alert("Pop-up Windows are not enabled!\n\n"+
					"Several features in WileyPLUS use pop-up windows to deliver content and messages to the user.\n"+
					"To use WileyPLUS properly, you must set your browser to allow pop-up windows for this site.");
	}
	return popupWin;
	
	//return window.open(URL, windowName, features);
}

// Add a window to the m_Windows collection.
// If a window was already opened and yet not close - focus the window.
function createWindow(in_URL, in_windowName)
{
	var features = "";
	if (null != arguments[2])
	{
		features = arguments[2];
	}

	if (m_Windows[in_windowName])
	{
		if (!m_Windows[in_windowName].closed)
		{
			m_Windows[in_windowName].focus();
		}
		else
		{
			m_Windows[in_windowName] = new newWindow(in_URL, in_windowName, features);
		}
	}
	else
	{
		m_Windows[in_windowName] = new newWindow(in_URL, in_windowName, features);
	}
    try {
        var baseFrame = findBaseFrame(window);
        baseFrame.setSTNPopupOnLoadListener(m_Windows[in_windowName]);
    } catch(e) {}
    return m_Windows[in_windowName];
}

function findBaseFrame(wnd) {
    if (wnd.loadSTNListenerClass)
        return wnd;
    else if (wnd.opener)
        return findBaseFrame(wnd.opener);
    else
        return findBaseFrame(wnd.top);
}

// Close all the opened windows.
// To close an individual window, its close method is called.
// This function should be called during the onUnload event to automatically close all open windows.
function closeWindows()
{   try{
        for (w in m_Windows)
        {
            if (m_Windows[w]!=null)
            {
                if (!m_Windows[w].closed)
                {
                	if (w!='HELP') {
	                    m_Windows[w].close();
	                    m_Windows[w]=null;
                	}
                }
                else
                {
                    m_Windows[w]=null;
                }
            }
            else
            {
                m_Windows[w]=null;
            }
        }
    }
    catch (err){
        //We need this TRY block to resolve problem with additional elements from Prototype JS framework
    }
}

// Close an individual opened window.
function closeWindow(in_windowName)
{
	if (m_Windows[in_windowName]!=null)
	{
		if (!m_Windows[in_windowName].closed)
		{
			m_Windows[in_windowName].close();
			m_Windows[in_windowName]=null;
		}
		else
		{
			m_Windows[in_windowName]=null;
		}
	}
	else
	{
		m_Windows[in_windowName]=null;
	}
}

// Check if the window is opened or not.
function isWindow(in_windowName)
{
	if (m_Windows[in_windowName]!=null)
	{
		if (!m_Windows[in_windowName].closed)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

// Reload the window if it is opened.
function reloadWindow(in_windowName)
{
	if (m_Windows[in_windowName]!=null)
	{
		if (!m_Windows[in_windowName].closed)
		{
			m_Windows[in_windowName].location = m_Windows[in_windowName].location;
		}
	}
}

// Load in opened window new URL.
function replaceWindow(in_URL, in_windowName)
{
	if (m_Windows[in_windowName]!=null)
	{
		if (!m_Windows[in_windowName].closed)
		{
			m_Windows[in_windowName].focus();
			m_Windows[in_windowName].location.replace(in_URL);
		}
	}
}


// Position the window.
function getParamsString(in_width, in_height, in_xOffset, in_yOffset)
{	
	return "width=" + in_width +
				",height=" + in_height +
					",left=" + ((screen.width-16-in_width)/2 + in_xOffset) +
						",top=" + ((screen.height-60-in_height)/2 + in_yOffset);
}

// Position the window to the right-bottom angle of screen
function getParamsString2(in_width, in_height, in_xOffset, in_yOffset)
{	
	return "width=" + in_width +
				",height=" + in_height +
					",left=" + ((screen.width-16-in_width) - in_xOffset) +
						",top=" + ((screen.height-60-in_height) - in_yOffset);
}

// =========================================================================

// ==== Open Wiley Help === 
function wiley_help(){
	if (arguments[0] != '' && arguments[0] != 'ms') {
		var s_href = "http://catalog.wileyplus.com/Section/id-" + arguments[0] + ".html";
	} else {
		var s_href = "http://catalog.wileyplus.com/Section/id-405190.html";
	}
	
	if (arguments[0] == 'ms') {
		var s_href = "http://www.chemaxon.com/marvin/help/sketch/sketch-index.html";
	}

	if (isWindow("HELP"))
	{
		closeWindow("HELP");
	}
	var s_param = "width=1005,height=480,resizable=yes,copyhistory=no,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no";
	createWindow(s_href, "HELP", s_param);
}
// =========================================================================
