//saves us from crashing JavaScript when calling console.debug/error/warn/log
if (!window.console) {
	window.console = {
		error : function () {},
		debug : function () {},
		log : function () {},
		warn : function () {}	
	};
}


var _PORTAL_BASE_URL;    
var _PORTAL_HOST_URL;

var portal = {};

portal._uniqueId = 0;
portal.uniqueId = function()
{
	return ++portal._uniqueId;
}

/**
 * In some places, we just need to alert the user of an error that is regardless of the mode
 * it occured in. 
 * @param errorMsg
 * @return
 */
function handleError(errorMsg) {
	errorPopup(errorMsg);
}

function handleJSONError(response) {
	if (response.outcome == 'AccessDenied') {
		window.location = _PORTAL_HOST_URL;
		return;
	}
	
	var errors = "";
	for(var x = 0; x < response.messages.length; x++) {
		errors = errors + response.messages[x] + "<br />";
	}
	errorPopup(errors);
}

function checkForJSONError(response) {
	if (response.status == "FAIL") {
		return true;
	}
	else {
		return false;
	}
}

///Pop ups//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
portal._popups = {};

function removePopupKeydown(e, id)
{
	switch (e.which)
	{
	case 27:
		portal.removePopup(id);
		break;
	}
}

/**
 * @param opts {
 * 		classNames: [array] additional css classes
 * 		msg: [string] the message to display
 * } 
 */
portal.createPopup = function(opts) {
	var id = "popup_"+portal.uniqueId();
	portal._popups[id] = {};
	
	var classNames = ['popup'];
	if (opts.classNames)
		classNames = classNames.concat(opts.classNames);
	var div =('<div class="pm_sheet popUpBackground"></div>');
	var popUpMsg =('<div class="'+classNames.join(' ')+'" id="'+id+'"><a href="#close" onclick="portal.removePopup(\''+id+'\');return false"><img border="0" class="errorClose" src="'+ baseURL +'images2/myclose-red.png"/></a>' + opts.msg +'</div>');
	$(".ui-panel1").eq(0).prepend(popUpMsg).prepend(div);
	var keyHandler = function(e) {
		removePopupKeydown(e, id);
	};
	
	portal._popups[id].keyHandler = keyHandler;
	$(document).bind("keydown", keyHandler);
	return id;
}

portal.removePopup = function(id) {
	var popup = $(".popup");
	if (id) {
		if (portal._popups[id]) {
			popup = popup.filter('#'+id);
			$(document).unbind("keydown", portal._popups[id].keyHandler);
		}
	}
	popup.prev('.pm_sheet').remove();
	popup.remove();

	if (id) {
		portal._popups[id] = null;
		delete portal._popups[id];
	}
}

//creates error pop up box, like alert()
function errorPopup(msg)
{
	return portal.createPopup({
		msg: msg,
		classNames: ['errorPopup']
	});
}

// removes error pop up box
function errorPopupRemove()
{
	portal.removePopup(id);
}

//message popup box
function showMsg(msg)
{
	return portal.createPopup({
		msg: msg,
		classNames: ['msgPopup']
	});
}

//removes message popup box
function removeMsg(id)
{
	portal.removePopup(id);
}


