var DEFAULT_POPUP_NAME = 'popup';
var DEFAULT_POPUP_WIDTH = 760;
var DEFAULT_POPUP_HEIGHT = 800;

var width;
var height;

var Util = {
	/**
	 * Get the width, height of current browser.
	 * @return {object} dimensions = { width, height }
	 */
	getBrowserSizeForReader : function() {
		//showWH();
		// Default settings for dimensions.
		var dimensions = {
			width : 1024,
			height : 768
		}

		// Netscape settings.	
		if (parseInt(navigator.appVersion) > 3) {
			if (navigator.appName == "Netscape") {
				dimensions.width =  window.outerWidth;
				dimensions.height = window.outerHeight;
				//alert('outerWidth' + dimensions.width +" , height" + dimensions.height);
			}
		}

		// IE settings.
		if (navigator.appName.indexOf("Microsoft") != -1) {
			
				cW=document.body.offsetWidth;
				cH=document.body.offsetHeight;
				window.resizeTo(dimensions.width,dimensions.height);
				barsW=Util.getScreenResolution().width-document.body.offsetWidth;
				barsH=Util.getScreenResolution().height-document.body.offsetHeight;
				wW=barsW+cW;
				wH=barsH+cH;
				
			    dimensions.width = wW;
			    dimensions.height = wH;		
			    //alert('IE outerWidth' + dimensions.width +" , height" + dimensions.height);
		}

		
		return dimensions;		
	},

 
 	/**
	 * Get the width, height of current browser.
	 * @return {object} dimensions = { width, height }
	 */
	getBrowserSize : function() {
		// Default settings for dimensions.
		var dimensions = {
			width : 1024,
			height : 768
		}

		// Netscape settings.	
		if (parseInt(navigator.appVersion) > 3) {
			if (navigator.appName == "Netscape") {
				dimensions.width = window.innerWidth;
				dimensions.height = window.innerHeight;
			}
		}

		// IE settings.
		if (navigator.appName.indexOf("Microsoft") != -1) {
			if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				dimensions.width = document.documentElement.clientWidth;
				dimensions.height = document.documentElement.clientHeight;
			}
			
			else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			    dimensions.width = document.body.clientWidth;
			    dimensions.height = document.body.clientHeight;			
			}
		}

		return dimensions;		
	},
	/**
	 * Get screen resolution.
	 * @return {object} dimensions = { width, height }
	 */ 
	 
	getScreenResolution : function() {
		var dimensions = {
			width : 1024,
			height : 768
		}
		
		// Check if the screen property even exists before getting the width and height.
		if (screen && screen.width > 0 && screen.height > 0) {
			dimensions.width = screen.availWidth;
			dimensions.height = screen.availHeight;
		}
		
		return dimensions;
	}
}

function popup(url, width, height, toolbar, resizable, scrollbar, name, failure)
{
    resizable = 'yes';

	var browserSize = {width : 1024, height : 768};
	var screenSize = {width : 1024, height : 768};

	var xPopup=600;
	var yPopup=800;
	var toolbarVar=1;
	var resizableVar=1;
	var scrollbarVar=1;
	var nameVar = DEFAULT_POPUP_NAME;

	// Get screen resolution and browser size.
	try {    
		screenSize = Util.getScreenResolution();
		browserSize = Util.getBrowserSize();
    }
    
    catch (e) {
    	// Util probably doesn't exist.
    }
	
	// If width, height are less than 0, then set the width, height to the browser size.
	if (width)
		xPopup = (width < 0) ? browserSize.width : width;
	else
		xPopup = DEFAULT_POPUP_WIDTH;
	
	if (height)	
		yPopup = (height < 0) ? browserSize.height : height;
	else
		yPopup = DEFAULT_POPUP_HEIGHT;

	//if(width != null && width <= xMax) xPopup = width;
	//if(height != null && height <= yMax) yPopup = height;
	if(toolbar != null) toolbarVar = toolbar;
	if(resizable != null) resizableVar = resizable;
	if(scrollbar != null) scrollbarVar = scrollbar;
	if(name != null) nameVar = name;

	var xOffset = (screenSize.width - xPopup) / 2;
	var yOffset = (screenSize.height - yPopup) / 2;
	
	xOffset = (xOffset >= 0) ? xOffset : 0;
	yOffset = (yOffset >= 0) ? yOffset : 0;

	var popup = null;
	var windowFeatures = 'width=' + xPopup + ',height=' + yPopup + ',toolbar=' + toolbarVar + ',scrollBars=' + scrollbarVar + ',status=no,resizable=' + resizableVar + ', screenX=' + xOffset + ',screenY=' + yOffset + ',top=' + yOffset + ',left=' + xOffset;
	popup = window.open(url, nameVar, windowFeatures);

	if (popup) {
	    if (!popup.opener)
	         popup.opener = self;
	    if (popup.focus) popup.focus();
    }
    
    else {
    	if (failure) {
	    	failure.call(this, url);
    	}
    }        
}

function popupCover(url, width, height, name, failure)
{
	popupWidth = (width) ? width : 760;
	popupHeight = (height) ? height : null;
	popupName = (name) ? name : null;
	failureHandler = (failure) ? failure : null;
		
	popup(url, popupWidth, popupHeight, 0, 0, null, popupName, failureHandler);
}



