//LOAD Dependent libraries
document.write("<script type='text/javascript' src='" + mp_YahooLib + "/build/utilities/utilities.js'> <\/script>");
document.write("<script type='text/javascript' src='" + mp_YahooLib + "/build/container/container-min.js'> <\/script>");
document.write("<script type='text/javascript' src='" + mp_cache + "/lib/TGN/Util/AjaxRequest.js'> <\/script>");

//make sure namespaces are defined
if(typeof TGN=="undefined")
    var TGN={};
if(typeof TGN.Shared=="undefined")
    TGN.Shared={};

TGN.Shared.UserLink = function(){
    
    this.isFireFox = false;
    this.panelHeight = 280;
    if(navigator.userAgent.indexOf("Firefox")!=-1)
    {
            this.isFireFox = true; 
            this.panelHeight = 260;
    }            
    this.panelWidth = 320;
		
    this.panel =    new YAHOO.widget.Panel("div_userLink", {
					    fixedcenter: false,
						underlay: "none",
						close: false,
						modal: false,
						width: this.panelWidth + "px",
						//height: this.panelHeight + "px",
						visible: false,
						zIndex:1000001,
						draggable: false,
						effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .5}
					});
	
	//this.panel.setHeader(""); 
	this.panel.setBody(""); 
	//this.panel.setFooter("");
	this.panel.render(document.body);
	document.getElementById(this.panel.id).className += " userLink";
	this.miniProfile = "";
	this.userHandle = "";
	this.fileId = "";
	this.mac = "";
	//Generic context type is the default
	this.contextType = 0;
	this.contextValue = "";
};

TGN.Shared.UserLink.ulink = null;
TGN.Shared.UserLink.timeout = null;
TGN.Shared.UserLink.profiles = null;

TGN.Shared.UserLink.Profile = function(userHandle, miniProfile){
    this.userHandle = userHandle;
    this.miniProfile = miniProfile;
};

TGN.Shared.UserLink.cancel = function(){
    if(TGN.Shared.UserLink.timeout != null)
        clearTimeout(TGN.Shared.UserLink.timeout);
    TGN.Shared.UserLink.timeout = null;
};

TGN.Shared.UserLink.showMiniProfile = function(elementId, userHandle, fileId, mac, contextType, contextValue){
    
    if(TGN.Shared.UserLink.ulink == null)
    {
        TGN.Shared.UserLink.ulink = new TGN.Shared.UserLink();
                
        if(TGN.Shared.UserLink.profiles == null)
            TGN.Shared.UserLink.profiles = new Array();
    }
    
    TGN.Shared.UserLink.ulink.miniProfile = "";
    //Clear the panel and refreshes it on the ui 
    TGN.Shared.UserLink.ulink.panel.setBody(TGN.Shared.UserLink.ulink.miniProfile);
    TGN.Shared.UserLink.ulink.userHandle = userHandle;
    TGN.Shared.UserLink.ulink.fileId = fileId;
    TGN.Shared.UserLink.ulink.mac = mac;
    TGN.Shared.UserLink.ulink.elementId = elementId;
	TGN.Shared.UserLink.ulink.eAlign = "tr";
	TGN.Shared.UserLink.ulink.pAlign = "tl";
	TGN.Shared.UserLink.ulink.contextType = contextType;
	TGN.Shared.UserLink.ulink.contextValue = contextValue;
	TGN.Shared.UserLink.ulink.panel.cfg.setProperty('context',[TGN.Shared.UserLink.ulink.elementId, TGN.Shared.UserLink.ulink.pAlign, TGN.Shared.UserLink.ulink.eAlign]);
      
    YAHOO.util.Event.addListener(document, "click", TGN.Shared.UserLink.close);  
        
    //I set the classname "bd" to "" so that anything css file that is trying to set the "bd" classname won't affect
    //this panel
    TGN.Shared.UserLink.ulink.panel.body.className = "";
    
    TGN.Shared.UserLink.showLoading();
    
    //check to see if the current userHandle is already on the client if not then make an ajax request to get the profile information
    for(i=0; i<TGN.Shared.UserLink.profiles.length; i++){
        if(TGN.Shared.UserLink.profiles[i].userHandle == userHandle){
            TGN.Shared.UserLink.ulink.miniProfile = TGN.Shared.UserLink.profiles[i].miniProfile;
            TGN.Shared.UserLink.buildProfile();
            return;
        }    
    }
    //The Data isn't on the client so go get it
    //Line shouldn't be needed, the server-side code should make sure the userhandle is urlencoded
    userHandle = escape(userHandle);
    var request = new TGN.Util.AjaxRequest("POST", "/community.ashx", "", { success:TGN.Shared.UserLink.success, failure:TGN.Shared.UserLink.failure }, "f=GetMiniProfile&rurl=" + escape(window.location) + "&uh="+userHandle+"&fileId="+TGN.Shared.UserLink.ulink.fileId+"&mac="+TGN.Shared.UserLink.ulink.mac+"&ctxT="+TGN.Shared.UserLink.ulink.contextType+"&ctxV="+escape(TGN.Shared.UserLink.ulink.contextValue));
    request.invoke();  
};

TGN.Shared.UserLink.showLoading = function(){
    TGN.Shared.UserLink.ulink.panel.setBody('<div id=\"mp_knub\"></div><div id=\"mp_knub_r\"></div><div class=\"un_container\"><div id=\"mp_body\" class=\"un_content clearfix\" style=\"background:#ffffff;\"><a href=\"javascript:TGN.Shared.UserLink.captureClick();\" id=\"un_close\"><img id=\"close_button\" src=\"' + mp_cache + '/lib/TGN/Shared/Assets/images/close.gif\"></a><div class=\"un_data\" style=\"margin-left:120px;margin-right:50px\"><br><img src=\"' + mp_cache + '/lib/TGN/Shared/Assets/images/loading.gif\"/><br><br></div></div></div>');
    TGN.Shared.UserLink.SetPosition();
    TGN.Shared.UserLink.ulink.panel.show();      
};

TGN.Shared.UserLink.success = function(o){
    TGN.Shared.UserLink.ulink.miniProfile = o.responseText;
    //add the new data to the array
    var p = new TGN.Shared.UserLink.Profile(TGN.Shared.UserLink.ulink.userHandle, TGN.Shared.UserLink.ulink.miniProfile); 
    TGN.Shared.UserLink.profiles[TGN.Shared.UserLink.profiles.length] = p;
    TGN.Shared.UserLink.buildProfile();
};

TGN.Shared.UserLink.failure = function(o){
};

TGN.Shared.UserLink.buildProfile = function(){
    //clear the default width so that it can be dynamic now base on the content of the body
    TGN.Shared.UserLink.ulink.panel.cfg.setProperty('width', '');
    TGN.Shared.UserLink.ulink.panel.setBody(TGN.Shared.UserLink.ulink.miniProfile);
    TGN.Shared.UserLink.SetPosition();
    TGN.Shared.UserLink.ulink.panel.show();    
    //Track that the mini-profile was clicked on
    var omniT = s_gi(s_account);
	s_pageName = "MiniProfile";		
	/*jsl:ignore*/
	void(omniT.t());
	/*jsl:end*/
};

TGN.Shared.UserLink.SetPosition = function(){
    //make sure the profile will align correctly to the link depending on where the link is on the page
    
    //Get needed elements
    var link = document.getElementById(TGN.Shared.UserLink.ulink.elementId);
    var panel = document.getElementById(TGN.Shared.UserLink.ulink.panel.id);
    var knub = document.getElementById("mp_knub");
    var knub_r = document.getElementById("mp_knub_r");
    
    //Constants
    var panelOffset = 54;
    var userLinkHeight = 16;
    var panelHeight = panel.clientHeight;
    var panelWidth = panel.clientWidth;
    //since the panel is aligned to the top of the element when you have the knub at the bottom you must
    //offset the panel pluse the height of the element
    
    var bottomPanelSpace = panelHeight - (panelOffset + userLinkHeight);
        
    //Reset any values
    knub.style.display = "none";
    knub_r.style.display = "none";
    panel.style.left = "17px";
    
    //===== ADJUST MINIPROFILE LEFT OR RIGHT =====//
    var windowWidth;
    if(TGN.Shared.UserLink.ulink.isFireFox)
        windowWidth = window.innerWidth;
    else
    {
        if(document.documentElement.clientWidth > 0)
            windowWidth = document.documentElement.clientWidth;
        else
            windowWidth = document.body.clientWidth;
    }
    
    var linkX = TGN.Shared.UserLink.GetPos(link, "x");
    var pageX = TGN.Shared.UserLink.GetScrollX();
    var leftSpace = linkX - pageX;//probably need to add on a littl more here for the length of the link 
    var rightSpace = windowWidth - leftSpace;
    if(panelWidth > rightSpace && leftSpace > panelWidth)
    {
        knub = document.getElementById("mp_knub_r");
        //20 constant is for the thumbnail and 17 is for the width of the knub
        var leftOffset = panelWidth + link.offsetWidth + 20 + 17;
        panel.style.left = "-" + leftOffset + "px";
        //I'm not sure why this works but this line needs to be here in order for the box to 
        //look good when it's on the left hand side of the userlink
        document.getElementById("div_userLink_c").style.width="100%";
    }
    
    //===== ADJUST MINIPROFILE TOP OR BOTTOM =====//
    var linkY = TGN.Shared.UserLink.GetPos(link, "y");
    var pageY = TGN.Shared.UserLink.GetScrollY();
    //The space between the link and the top of the viewable page
    var topSpace =  linkY - pageY;
    var windowHeight;
    if(TGN.Shared.UserLink.ulink.isFireFox)
    {
        windowHeight = window.innerHeight;
        //if scroll bars are added to the bottom of the window Firefox does not account for that in it's innerHeight
        //property so subtract some constant to make up for that
        if(window.scrollMaxX > 0)
            windowHeight -= 15;
    }
    else
    {
        if(document.documentElement.clientHeight > 0)
            windowHeight = document.documentElement.clientHeight;
        else
            windowHeight = document.body.clientHeight;
    }
    //The space between the bottom of the link and the bottom of the viewable page
    var bottomSpace = windowHeight - (topSpace + userLinkHeight);
        
    //adjust the panel offset and the knub if the topSpace won't fit the standard height
    if(topSpace < panelOffset)
    {
        if(topSpace > 0)
            panelOffset = topSpace - 2;
        panel.style.top = "-" + panelOffset + "px";
        knub.style.top = panelOffset * .80 + "px";
    }
    //only move the panel up if it won't fit on the bottom and it won't get cut off on the top, never cut off the top
    else if(bottomSpace < bottomPanelSpace && topSpace > bottomPanelSpace)
    {
        //the 2 constant is added to add some buffer at the bottom of the browser window
        bottomPanelSpace = bottomPanelSpace + 2;
        //if the bottom is going to get cut off and there is still room at the top bump it up a little
        if(bottomSpace < panelOffset)
        {
            bottomPanelSpace = bottomPanelSpace + (panelOffset - bottomSpace)
        }        
        
        //make sure if we have added to the bottomPanelSpace there is still room at the top
        if(topSpace > bottomPanelSpace)
        {
            panel.style.top = "-" + bottomPanelSpace + "px";
            knub.style.top = bottomPanelSpace * .96 + "px";
        }
    }
    else
    {
        panel.style.top = "-" + panelOffset + "px";
        knub.style.top = panelOffset * .85 + "px";
    }
     
    knub.style.display = "block";
};

TGN.Shared.UserLink.captureClick = function(o){
    //This is basically a function that does nothing. I just needed a dead end for when the close button is clicked,
    //the close button will pick up the event and close the panel
};

TGN.Shared.UserLink.close = function(o){
   
    var browser = navigator.userAgent;
    var panelXY = YAHOO.util.Dom.getXY(TGN.Shared.UserLink.ulink.panel.id);
    var minX = panelXY[0];
    var maxX = panelXY[0] + TGN.Shared.UserLink.ulink.panelWidth;
    var minY = panelXY[1];
    var maxY = panelXY[1] + document.getElementById(TGN.Shared.UserLink.ulink.panel.id).clientHeight;
    
    var elX;
    var elY;
    //Use page property for firefox
    if(TGN.Shared.UserLink.ulink.isFireFox)
    {
        elX = o.pageX;
        elY = o.pageY;
    }
    else
    {
        elX = o.clientX;
        elY = o.clientY;
    }
    
    var userLinkClicked = false;
    var target;
    if(TGN.Shared.UserLink.ulink.isFireFox)
        target = o.target;
    else
        target = o.srcElement;
    if(target.id.indexOf("uli_") != -1 || target.id.indexOf("ula_") != -1)
        userLinkClicked = true; 
    if(!userLinkClicked){
        if( (elY >= maxY || elY <= minY) || (elX >= maxX || elX <= minX) || target.id == "close_button" || target.className == "contactLink"){
            TGN.Shared.UserLink.ulink.panel.hide(); 
            YAHOO.util.Event.removeListener(document, "click", TGN.Shared.UserLink.close);
        }
    }
};

TGN.Shared.UserLink.GetPos = function(obj,dir)
{
    if(dir=="x")
        pos = obj.offsetLeft;
    else
        pos = obj.offsetTop;
    temp = obj.offsetParent;
    while(temp != null)
    {
        pos += (dir=="x") ? temp.offsetLeft : temp.offsetTop;
        temp = temp.offsetParent;
    }
    return pos;
};

TGN.Shared.UserLink.GetScrollX = function()
{
    if(window.pageXOffset != null) 
        return window.pageXOffset;
    else if (document.documentElement.scrollLeft != null)
        return document.documentElement.scrollLeft;
    else
        return document.body.scrollLeft;
};

TGN.Shared.UserLink.GetScrollY = function()
{
    if(window.pageYOffset != null) 
        return window.pageYOffset;
    else if (document.documentElement.scrollTop != null)
        return document.documentElement.scrollTop;
    else
        return document.body.scrollTop;
};

//Tracking function will track how many times the mini-profile is shown
TGN.Shared.UserLink.TrackHover = function(hoverType){
		var omniT=s_gi(s_account);
		//add evars here..
		if(hoverType == "NoSub") {
			s_pageName = "Search:IndivRecHover:NoSub";		
		}
		else if(hoverType == "Hover"){
			s_pageName = "Search:IndivRecHover:Sub";		
		}
		/*jsl:ignore*/
		void(omniT.t());
		/*jsl:end*/
};

