PopupManager = {
	
	//Variable to keep track of the Operating System 
	OS: null, 
	
	//constants used in this class.
	constants: {
		osWin: "win",
		osMac: "mac",
		internetExplorer: "IE",
		unsupported: "unsupported",
		themeXp: "xp",
		themeMac: "mac_os_x",
		themeDefault: "dialog"
	},

	//Variable to keep the browser size and offset.
	webBrowser: {
		width: 0,
		height: 0,
		rightPadding: 0,
		bottomPadding: 0,
		version: "unknown"
	},
	
	//Variable to keep track of the popup window ID 
	Application:  {
	
	    lastId: 0,
	
	    getNewId: function() {
	    
	    	PopupManager.Application.lastId++;
	        return "window_id_" + PopupManager.Application.lastId;
	    }
	  
	},
	
	/**
	  * Initializer. 
	  */
	init: function() {
		PopupManager.captureSystemInfo();
    },
    
    /**
      * Captures the system information, such as the browser version, Operating system,
      * and the browser width and height.
      */
    captureSystemInfo: function() {
    	//browser version detection
		if (navigator.appVersion.match(/MSIE/) == "MSIE") {
			PopupManager.webBrowser.version = PopupManager.constants.internetExplorer;
		} else {
			PopupManager.webBrowser.version = PopupManager.constants.unsupported;
		}
		
		//browser size detection
		PopupManager.webBrowser.width = document.body.clientWidth;
		PopupManager.webBrowser.height = document.body.clientHeight;
		
		//Operating System Detection
		if (navigator.appVersion.toLowerCase().indexOf("win")!=-1) PopupManager.OS = PopupManager.constants.osWin;
		else if (navigator.appVersion.toLowerCase().indexOf("mac")!=-1) PopupManager.OS = PopupManager.consants.osMac;
		else PopupManager.OS = PopupManager.constants.unsupported;
	},
	
	/**
	 * This function creates a modal DHTML popup window.
	 * @param targetUrl String value of the URL to be loaded to the popup content. 
	 *                  Note that you can insert any external URL. (i.e. http://www.yahoo.com)
	 * @param windowTitle String value displayed in the popup window title bar.
	 * @param windowWidth Integer value in pixel used to set the popup window width.
	 * @param windowHeight Integer value in pixel used to set the popup window height.
	 */
	createModalPopup: function(targetUrl, windowTitle, windowWidth, windowHeight) {   
	    PopupManager.createPopupWindow(targetUrl, windowTitle, windowWidth, windowHeight,PopupManager.getOSTheme(), true);
	},
	
	/**
	 * This function creates a non-modal DHTML popup window.
	 * @param targetUrl String value of the URL to be loaded to the popup content. 
	 *                  Note that you can insert any external URL. (i.e. http://www.yahoo.com)
	 * @param windowTitle String value displayed in the popup window title bar.
	 * @param windowWidth Integer value in pixel used to set the popup window width.
	 * @param windowHeight Integer value in pixel used to set the popup window height.
	 */
	createPopup: function(targetUrl, windowTitle, windowWidth, windowHeight) {     
	    PopupManager.createPopupWindow(targetUrl, windowTitle, windowWidth, windowHeight, PopupManager.getOSTheme(), false);
	},
	
	/**
	 * This function creates a DHTML popup window.
	 * @param targetUrl String value of the URL to be loaded to the popup content. 
	 *                  Note that you can insert any external URL. (i.e. http://www.yahoo.com)
	 * @param windowTitle String value displayed in the popup window title bar.
	 * @param windowWidth Integer value in pixel used to set the popup window width.
	 * @param windowHeight Integer value in pixel used to set the popup window height.
	 * @param isModal Boolean value that indicates modal or non-modal window.
	 */
	createPopupWindow: function(targetUrl, windowTitle, windowWidth, windowHeight, themeName, isModal, closable) {
	    //if Internet Explorer, add 3 more pixel to both width and height.
	    //(CSS layout issue)
	    if (PopupManager.webBrowser.version == PopupManager.constants.internetExplorer) {
	        windowWidth = parseInt(windowWidth) + 3;
	        windowHeight = parseInt(windowHeight) + 3;
	    }
  
	    //Popup window property
	    var popupProperties = { className: themeName, 
	                            title: windowTitle, 
	                            width: windowWidth, 
	                            height: windowHeight, 
	                            zIndex: 100,                 //Initial layer order of the popup window.
	                            resizable: true, 
	                            closable: closable, 
	                            minimizable: false, 
	                            maximizable: false, 
	                            url: targetUrl};             //target URL to populate the content with.

	    PopupManager.createFlexPopupWindow(popupProperties, isModal);
	},
	
	/** 
	 * This function creaes a DHML Popup window based on the property map passed in.
	 * @param popupProperties a map that contains all the properties of a popup window. 
	 *        example properties:
	 *        var popupProperties = { className: 'mac_os_x',   //name of the window theme
	                                  title: 'test title',     //popup window title
	                            	  width: 600,              //popup window width
	                            	  height: 300,             //popup window height
	                            	  zIndex: 100,             //Initial Layer order
	                            	  resizable: true,         //resizable window
	                            	  closable: true,          //closable window
	                            	  minimizable: false,      //minimizable window
	                            	  maximizable: false,      //maximizable window
	                            	  url: 'http://www.google.com'};         //content URL
	 * @param isModal boolean to indicate whether to want a modal window or not
	 */
	createFlexPopupWindow: function(popupProperties, isModal) {
		//Create a new popup window by instantiating a new prototype Window class.
		
		var win = new Window(PopupManager.Application.getNewId(), popupProperties); 
  
	    //Display the popup window at the center.
		win.showCenter(isModal);  
		
	
	},
	
	/**
	 * This function returns theme name based on the user's OS.
	 * @return theme name
	 */
	getOSTheme: function() {
		//change the window theme based on the user's OS.
	    var themeName = null;
	    if (PopupManager.OS == PopupManager.constants.osWin) {
	    	themeName = PopupManager.constants.themeXp;
	    } else if (PopupManager.OS == PopupManager.constants.osMac) {
	    	themeName = PopupManager.constants.themeMac;
	    } else {
	    	themeName = PopupManager.constants.themeDefault;
	    	
	    }
	    
	    return themeName;
	},
	
	/**
	 * This function closes Prototype Window class popup window.
	 */
	closeWindowPopup: function(windowObj) {
	    top.Windows.close(this.getPopupWindowObjID(windowObj));
	},
	
	/**
	 * This function closes Prototype Window class popup window and also reloads parent window.
	 */
	closeWindowPopup_reload: function(windowObj,targetURL) 
	{
	    top.Windows.close(this.getPopupWindowObjID(windowObj));
		location.href = targetURL;

		/*window.navigate( targetURL);
		window.navigate(targetURL);
		window.location = targetURL;
		window.location.href = targetURL;
		window.location.replace = targetURL;*/
		//window.location.reload(false);
	    
			  /*  top.Windows.close(this.getPopupWindowObjID(windowObj));
		parent_div=windowObj.frameElement.parentNode.parentNode.parentNode.parentNode.parentNode;
		parent_div.getElementsByTagName("table")[0].getElementsByTagName("td")[1].getElementsByTagName("div")[0].innerHTML="aaa";
		windowObj.location.href="http://google.com";*/


	}, 
	
	
	/**
	 *  This function returns a Prototype Window class object ID used to create the current popup window.
	 *  @param the current window object.
	 */
	getPopupWindowObjID: function(windowObj) {
	    //Retrieve id attribute of the IFrame that surrounds the windowObj(the Prototype Popup window).
	    var iFrameID = windowObj.frameElement.id;  
	    
	    //Since Prototype Popup uses IFrame with id attribute with (WindowId + "_content"),
	    //get the substring before "_content" string would return the Window ID string.
	    return iFrameID.substring(0, iFrameID.indexOf("_content"));
	},
	
	reloadPopupWindow: function(windowObj,targetUrl, windowTitle, windowWidth, windowHeight)
	{
		parent_div=windowObj.frameElement.parentNode.parentNode.parentNode.parentNode.parentNode;
		parent_div.getElementsByTagName("table")[0].getElementsByTagName("td")[1].getElementsByTagName("div")[0].innerHTML=windowTitle;
		windowObj.location.href=targetUrl;
	}
};