var Fse; if (!Fse) Fse = {};

if( !Fse.Portal ) Fse.Portal = {};

Fse.Portal.portletsInitialized = false;

Fse.Portal.reloadPortlet = function( sPortletName ) {
	var sViewName = sPortletName + "-portlet-view";
	var oPortletView = document.getElementById( sViewName );
	if( oPortletView ) {
		var sLoadURL = oPortletView.getAttribute( "loadURL" );
		if( sLoadURL ) {
			Fse.Portal.loadPortlet( sViewName, sLoadURL );
		}
	}
};

Fse.Portal.loadPortlet = function( sPortletId, sURL ) {

	Fse.Ajax.updateContent( sURL, sPortletId );

	/*
   var oSpryOptions = {};
   oSpryOptions.headers = {};
   oSpryOptions.headers.fseAjax = "true";

    var fnOnPortletResponse = function( req ) {
        Spry.Utils.setInnerHTML( sPortletId, req.xhRequest.responseText );
//        var oPortlet = document.getElementById( sPortletId );
//        var oCurvyCorners = new curvyCorners( Fse.Portal.portletCornerSettings, oPortlet );
//        oCurvyCorners.applyCornersToAll();
    };
    Spry.Utils.loadURL( "GET", sURL, true, fnOnPortletResponse, oSpryOptions );
    */
};

Fse.Portal.portletViewEffects = [];

Fse.Portal.portletObserver = {};
Fse.Portal.portletObserver.onPostEffect = function( oEffect ){
    var sViewControlIconID = oEffect.viewControlIconID;
    var oIcon = document.getElementById( sViewControlIconID );
	if (oEffect.direction == oEffect.fseClosing ) {
		if( oIcon ) {
		    oIcon.src = oIcon.src.replace( "contract", "expand" );
		} else {
		    alert( "No Icon ");
		}
		var oExpires = new Date( (new Date()).getTime() + 604800000 ); // 1 week from now
	    Fse.Portal.setCookie( oEffect.cookieName, "closed", oExpires );
	} else {
		if( oIcon ) {
		    oIcon.src = oIcon.src.replace( "expand", "contract" );
		} else {
		    alert( "No Icon ");
		}
		if( oEffect.initialStateAdjusted ) {
			/* force the size back to auto */
	    	oEffect.element.style.height = "auto";
	    	oEffect.element.style.width = "auto";
		}
		var oExpires = new Date( (new Date()).getTime() + 604800000 ); // 1 week from now
	    Fse.Portal.setCookie( oEffect.cookieName, "open", oExpires );
	}
};

Fse.Portal.togglePortletView = function( sPortletViewId, bInitialStateAdjust ) {
    var oEffect = Fse.Portal.portletViewEffects[ sPortletViewId ];
    if( ! oEffect ) {
        if( document.getElementById( sPortletViewId )) {
        	
        	var oPortletView = document.getElementById( sPortletViewId );
        	var sInitialState = oPortletView.getAttribute( "fse:initialState" );
        	if( sInitialState == "open" ) {
	            oEffect = new Spry.Effect.Blind( sPortletViewId, {duration: 150, from:  '100%', to:  '0%', toggle: true});
				oEffect.fseClosing = Spry.forwards;
        	} else {
				oEffect = new Spry.Effect.Blind( sPortletViewId, {duration: 150, from:  '0%', to:  '100%', toggle: true});        	
				oEffect.fseClosing = Spry.backwards;
			}
			
            oEffect.addObserver( Fse.Portal.portletObserver );
            oEffect.viewControlIconID = sPortletViewId + "-control-icon";
            oEffect.cookieName = sPortletViewId + "-state"; 
            oEffect.portletViewId = sPortletViewId;
           	Fse.Portal.portletViewEffects[ sPortletViewId ] = oEffect;
        } else { 
        	alert( "can't find " + sPortletViewId );
        }
    }
    if( oEffect ) {
       oEffect.start();    
    }
};

Fse.Portal.initializePortlet = function( sPortletName ) {
	var sPortletViewId = sPortletName + "-portlet-view";
	var sCookieName = sPortletViewId + "-state"; 
	var oPortletView = document.getElementById( sPortletViewId );
	var sInitialState = oPortletView.getAttribute( "fse:initialState" );
	var oExpires = new Date( (new Date()).getTime() + 604800000 ); // 1 week from now
    Fse.Portal.setCookie( sCookieName, sInitialState, oExpires );

	var oEffect = Fse.Portal.portletViewEffects[ sPortletViewId ];	
    if( oEffect ) {
    	Fse.Portal.portletViewEffects[ sPortletViewId ] = null;
    }
};

Fse.Portal.portletCornerSettings = {
    tl: { radius: 20 },
    tr: { radius: 20 },
    bl: { radius: 20 },
    br: { radius: 20 },
    antiAlias: true,
    autoPad: true,
    validTags: ["div"]
};

if( !Fse.Portal.NavigationManager ) Fse.Portal.NavigationManager = {};



Fse.Portal.calculateAbsolutePosition = function( oElement ) {

  var oPoint = {};
  oPoint.x = oElement.offsetLeft;    
  oPoint.y = oElement.offsetTop;
  
  var oParent = oElement.offsetParent;
  while( oParent ) {
      oPoint.x += oParent.offsetLeft;
      oPoint.y += oParent.offsetTop;
      oParent = oParent.offsetParent;
  }

  return oPoint;
     
};

Fse.Portal.NavigationManager.openSubMenu = function() {
    Spry.Utils.addClassName( this, "over" );
};

Fse.Portal.NavigationManager.closeSubMenu = function() {
  Spry.Utils.removeClassName( this, "over" );
};

Fse.Portal.NavigationManager.initialize = function() {
    var sId = "main-menu";
    var oNavigation = document.getElementById( sId );
    if( ! oNavigation ) {
        return;
    }
    for (var i=0; i < oNavigation.childNodes.length; i++){
    	var oNode = oNavigation.childNodes[i];
    	if (oNode.nodeName=="LI") {
    	    /* Note: these handlers must be added in this manner, 
    	     * using Fse.EventManager.addEventHandler doesn't work */
    	    oNode.onmouseover = Fse.Portal.NavigationManager.openSubMenu;
    	    oNode.onmouseout = Fse.Portal.NavigationManager.closeSubMenu;
    	    
    	    
    	    /* find subnav if any */
    	    var oSubNav = null;
    	    for( var si=0; si < oNode.childNodes.length && ! oSubNav; si++ ) {
    	       if( oNode.childNodes[si].nodeName == "UL" ) {
    	           oSubNav = oNode.childNodes[si];
    	       }
    	    }
    	    /* add class patch logic to subNav items */
    	    if( oSubNav ) {
    	        for( var si=0; si < oSubNav.childNodes.length; si++ ) {
    	            if( oSubNav.childNodes[si].nodeName == "LI") {
    	                oSubNav.childNodes[si].onmouseover = function() { Spry.Utils.addClassName( this, "over" ); };
    	                oSubNav.childNodes[si].onmouseout = function() { Spry.Utils.removeClassName( this, "over" ); };
    	            }
    	        }
    	    }
     	}
    }
};

Fse.Portal.popupWindow = function( sName, sURL, iWidth, iHeight ) {
   
   var sSizeString = "";
   var sPositionString = ""
   if( iWidth ) {
       sSizeString = sSizeString + ",width=" + iWidth;
       if( screen.width ) {
           sPositionString = sPositionString + ",left=" + ( screen.width - iWidth ) / 2;
       }
   }
   
   if( iHeight ) {
       sSizeString = sSizeString + ",height=" + iHeight;
       if( screen.height ) {
          sPostionString = sPositionString + ",top=" + (screen.height - iHeight) / 2;
       }
   }

   var sOptions = "resizable=yes,scrollable=yes,scrollbars=yes,location=no" + sSizeString + sPositionString;
   
   var sSafeName = sName.replace( "\s", "_" );
   var oWindow = window.open( sURL, sSafeName, sOptions );
   if( oWindow ) {
       oWindow.focus();
   }
};

	// Correctly handle PNG transparency in Win IE 5.5 or higher.
	// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

Fse.Portal.correctPNG = function() 
	   {
	   for(var i=0; i<document.images.length; i++)
	      {
		  var img = document.images[i]
		  var imgName = img.src.toUpperCase()
		  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		     {
			 var imgID = (img.id) ? "id='" + img.id + "' " : ""
			 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			 var imgStyle = "display:inline-block;" + img.style.cssText 
			 if (img.align == "left") imgStyle = "float:left;" + imgStyle
			 if (img.align == "right") imgStyle = "float:right;" + imgStyle
			 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
			 var strNewHTML = "<span " + imgID + imgClass + imgTitle
			 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			 img.outerHTML = strNewHTML
			 i = i-1
		     }
	      }
	   };
	   

Fse.Portal.popProfile = function( iClientId, iToken ) {
	window.open("https://www.appslibrary.com/fspro/2.0/profile.cfm?id=" + iClientId + "&tk=" + iToken, "profile", "left=25,top=25,width=550,height=430,scrollbars=no,resizable=yes,menubar=no,status=no,toolbar=no");
};

Fse.Portal.getCookie = function( sName, sDefault ) {
	var sRE = sName + "=([^;]*);?";
	var oRE = new RegExp( sRE );
	
	if( oRE.test( document.cookie ) ){
		return decodeURIComponent( RegExp["$1"] );
	} else {
		return sDefault;
	}
};

Fse.Portal.setCookie = function( sName, sValue, oExpires, sPath, sDomain, bSecure ) {

	// save these to prevent overrun
	/*
	var sCFID = Fse.Portal.getCookie( "CFID" );
	var sCFTOKEN = Fse.Portal.getCookie( "CFTOKEN" );
	var sJSESSIONID = Fse.Portal.getCookie( "JSESSIONID" );
	*/

	var sCookie = sName + "=" + encodeURIComponent( sValue );
	if( oExpires ) {
		sCookie += "; expires=" + oExpires.toGMTString();
	}
	if( sPath ) {
		sCookie += "; path=" + sPath;
	}
	if( sDomain ) {
		sCookie += "; domain=" + sDomain;
	}
	if( bSecure ) {
		sCookie += "; secure";
	}
	document.cookie = sCookie;
	
	/*
	if( sCFID && Fse.Portal.getCookie( "CFID" ) != sCFID ) {
		document.cookie = "CFID=" + encodeURIComponent(sCFID);
	}
	if( sCFTOKEN && Fse.Portal.getCookie( "CFTOKEN" ) != sCFTOKEN ) {
		document.cookie = "CFTOKEN=" + encodeURIComponent(sCFTOKEN);
	}
	if( sJSESSIONID && Fse.Portal.getCookie( "JSESSIONID" ) != sJSESSIONID ) {
		document.cookie = "JSESSIONID=" + encodeURIComponent(sJSESSIONID);
	}*/
	
};


if( window.attachEvent ) {
 window.attachEvent( "onload", Fse.Portal.correctPNG );
}

Fse.Portal._helpContext = "";
Fse.Portal._helpContentURL = "/help.cfm";

Fse.Portal.setHelpContentURL = function( sHelpContentURL ) {
	Fse.Portal._helpContentURL = sHelpContentURL;
};

Fse.Portal.setHelpContext = function( sHelpContext ) {
	Fse.Portal._helpContext = sHelpContext;
};

Fse.Portal.showQuickHelp = function ( sIndexToken ) {
	var sURL = Fse.Portal._helpContentURL + "?context=" + encodeURIComponent( Fse.Portal._helpContext );
	sURL = sURL + "&index=" + encodeURIComponent( sIndexToken );
	if( window.title ) {
		sURL = sURL + "&title=" + encodeURIComponent( window.title.innerHTML );
	}
	sURL = sURL + "&location=" + encodeURIComponent( location.href );
	Fse.Portal.popupWindow( "Online_Help", sURL, 650, 500 );
};

Fse.Portal.punchoutInitialized = function( oFrame ) {

	var sHref;
	var sTarget;
	var sTitle;
	
	var oDocument = oFrame.contentDocument;
	if( ! oDocument ) {
		try {
			oDocument = oFrame.contentWindow.document;
		} catch ( x ) {
			return;
		}
	}

	if( oDocument ) {

		try {
			YAHOO.util.Dom.setStyle( oFrame, "height", oDocument.body.offsetHeight * 1.25 + "px" );
			oFrame.setAttribute( "scrolling", "auto" );
		} catch ( xx ) {
			return;
		}

	}

	
};



Fse.Portal.addWebLogComment = function( sWebLogId ) {
	
	var oWebLog = document.getElementById( sWebLogId );
	var sHandlerURL = oWebLog.getAttribute( "fse:handlerURL" );
	
	var mySuccess = function() {
		var oWebLog = document.getElementById( sWebLogId );
		var sHandlerURL = oWebLog.getAttribute( "fse:handlerURL" );
		var sHTMLSocketId = oWebLog.getAttribute( "fse:htmlSocketId" );
	
		Fse.Ajax.updateContent( sHandlerURL + "&view=weblogEntries&maxPosts=0", sHTMLSocketId );
	};
	
	var oOptions = {
		src : sHandlerURL + "&view=postWeblogCommentDialog",
		onSuccess : mySuccess
	};
	
	Fse.DialogManager.show( "postWebLogCommentDialog", oOptions );
};

Fse.Portal.reviseWebLogComment = function( sWebLogId, nPostId, nPostTk ) {
	
	var oWebLog = document.getElementById( sWebLogId );
	var sHandlerURL = oWebLog.getAttribute( "fse:handlerURL" );
	
	var mySuccess = function() {
		var oWebLog = document.getElementById( sWebLogId );
		var sHandlerURL = oWebLog.getAttribute( "fse:handlerURL" );
		var sHTMLSocketId = oWebLog.getAttribute( "fse:htmlSocketId" );
	
		Fse.Ajax.updateContent( sHandlerURL + "&view=weblogEntries&maxPosts=0", sHTMLSocketId );
	};
	
	var oOptions = {
		src : sHandlerURL + "&view=postWeblogCommentDialog&postId=" + nPostId + "&postTk=" + nPostTk,
		title : "Revise Note",
		onSuccess : mySuccess
	};
	
	Fse.DialogManager.show( "postWebLogCommentDialog", oOptions );
};


Fse.Portal.showAllWebLogPosts = function( sWebLogId ) {

	var oWebLog = document.getElementById( sWebLogId );
	var sHandlerURL = oWebLog.getAttribute( "fse:handlerURL" );
	var sSocketId = oWebLog.getAttribute( "fse:htmlSocketId" )

	Fse.Ajax.updateContent( sHandlerURL + "&view=weblogEntries&maxPosts=0", sSocketId );
};
