 // set it to true to enable debugging messages to popup in alerts
var PW_DEBUG = false;

function PortletWrapper(portletName,webapp) {
	this.webapp = webapp;
	this.portletName = portletName;
	this.goTo = pwGoTo;
	this.goToNoScroll = pwGoToNoScroll;
	this.scrollUp = pwScrollUp;
	this.refresh = pwRefresh;
	this.goToView = pwGoToView;
	this.goToFullPage = pwGoToFullPage;
	this.editDocument = pwEditDocument;
	this.addDocument = pwAddDocument;
	this.deleteDocument = pwDeleteDocument;
	this.appendParams = pwAppendParams;
	this.goToPage = pwGoToPage;
	this.back = pwBack;
	this.submit = pwSubmit;
	this.defaultParameters  = "AF_renderParam_portlet="+this.portletName+"&" + "AF_renderParam_embed=true";	
	this.debug = pwDebug;
}

function pwSubmit(form,action) {
	this.debug('submiting form ',form.name + ' to action [' + action + '] with portlet name [' + this.portletName + ']');
	if(!action)
		action = form.action;
	if (this.portletName)
    	eval(this.portletName+".submitForm(form,action)");
    else
    	form.submit();
}

function pwGoToPage(url,page,params) {
	this.goTo(url,this.appendParams("AF_page="+page,params))
}
function pwDeleteDocument(key,redirect,params) {
	if(confirm("Are you sure you want to delete it?")) {
		var redir = this.appendParams(redirect,params);
		// also append the default params
		if(this.portletName)
			redir = this.appendParams(redir,this.defaultParameters);
		// we need to encode the redir just in case it contains & or any special characters
		var url = this.webapp+"/delete?AF_key="+key+"&AF_redirect="+encodeURIComponent(redir);
		this.goTo(url);
	}
}

function pwAddDocument(formName,winSpecs,params) {
	window.open(this.appendParams(this.webapp+"/form/"+formName,params),"add",winSpecs);
}

function pwEditDocument(key,winSpecs,params) {
	window.open(this.appendParams(this.webapp+"/doc/"+key,params),"edit",winSpecs);
}

function pwGoToView(viewName,params) {
	this.goTo(this.appendParams(this.webapp + '/view/' + viewName + '?maximizePortlet=true', params));
}

function pwGoToNoScroll(url,params) {
	this.debug('going to',url);
	url = this.appendParams(url ,params);
	this.debug('url has become',url);
	this.debug('portlet name is ' , this.portletName);
	 if (this.portletName)
    	eval(this.portletName+".openUrl(url)");
    else
    	window.location = url;		
}

function pwGoTo(url,params) {
	this.debug('going to',url);
	this.scrollUp();
	url = this.appendParams(url ,params);
	this.debug('url has become',url);
	this.debug('portlet name is ' , this.portletName);
	 if (this.portletName)
    	eval(this.portletName+".openUrl(url)");
    else
    	window.location = url;		
}
function pwScrollUp() { 
   	window.scroll(0,0); // horizontal and vertical scroll targets
}
function pwGoToFullPage(key,params) {
	this.goTo(this.appendParams(this.webapp + '/doc/' + key + '?maximizePortlet=true&AF_deliveryChannel=fullpage', params));
}

function pwRefresh() {
	if (this.portletName)
    	eval(this.portletName+ ".refresh()");
    else
    	window.location.reload();		
}

function pwBack() {
	if (this.portletName)
    	eval("historyBack(" + this.portletName + ")");
    else
    	historyBack(null);		
}

function pwAppendParams(url,params) {
	if(params) {
		if(url.indexOf('?') > 0) {
			url += "&" + params;
		} else {
			url += "?" + params;
		}
	}
	return url;
}

function pwDebug(src,msg) {
	if(PW_DEBUG)
		alert('portletWrapper::' + src + " [" + msg + "]");
}
