//make sure namespaces are defined
if(typeof TGN=="undefined")
    var TGN={};

if(typeof TGN.Util=="undefined")
    TGN.Util={};


TGN.Util.HoverTip = function(){
    this.init();
};


TGN.Util.HoverTip.tipHideTimer=null;
TGN.Util.HoverTip.tipShowTimer=null;
TGN.Util.HoverTip.tipHideTimout=0;

//if it hasn't already been loaded load the the js file for getting and setting cookies
if(typeof setCookie=="undefined"){
    document.write("<script type='text/javascript' src='http://c.mfcreative.com/js/utility.js'> <\/script>");
}

TGN.Util.HoverTip.tip = null;


TGN.Util.HoverTip.prototype.init = function()
{
        this.panel =    new YAHOO.widget.Panel("div_hoverTip", {
					    fixedcenter: false,
						underlay: false,
						close: true,
						modal: false,
						visible: false,
						zIndex:1100,
						draggable: false
						//effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .5}
					});

	    this.panel.setHeader(""); 
	    this.panel.setBody("");
	    this.panel.setFooter("");
	    this.panel.cfg.getProperty("strings").close = " ";
	    //Set the z-index at some big numbet to make sure it is always on top
	    this.panel.cfg.setProperty('zIndex', 1100);
	    this.panel.render(document.body);
	    document.getElementById("div_hoverTip").className="hovertip";
};
TGN.Util.HoverTip.CurrTip = null;
TGN.Util.HoverTip.prototype.show = function()
{
	//this was added as yui 2.7.0 panel steals focus.  We want to push focus back
	//to the origin of the message, but this can result in another onfocus event firing.
		if(TGN.Util.HoverTip.CurrTip != null && TGN.Util.HoverTip.CurrTip == this.elementId && this.panel.cfg.getProperty("visible"))
		{
			return;
		}
		TGN.Util.HoverTip.CurrTip = this.elementId;

        this.panel.setHeader("<h3>" + this.title + "</h3>"); 
        var tipHtml = "<div id=\"knub\"></div><div class=\"tl\"></div><div class=\"tr\"></div><div class=\"bl\"></div><div class=\"br\"></div><div id=\"tp_content\" class=\"content\">" + this.body + "</div>";
        this.panel.setBody(tipHtml);
        this.panel.cfg.setProperty('context',[this.elementId, this.tAlign, this.eAlign]);
        
        //perform any overrides here
        if(this.width > 0){
	        document.getElementById("div_hoverTip").style.width = this.width + "px";
	        document.getElementById("tp_content").style.width = this.width - 26 + "px";
	        document.getElementById("tp_content").style.marginLeft = "-" + (this.width - 14) + "px";
	        document.getElementById("tp_content").style.paddingLeft = "6px";
	        document.getElementById("tp_content").style.paddingRight = "6px";
	        document.getElementById("tp_bd").style.marginLeft = this.width - 14 + "px";
	    }
	    
	    var knubId = "knub";
	    //if the tip is to be aligned to the right side of the element then switch the knub
	    if(this.tAlign[1] == 'r'){
	        var knub = document.getElementById(knubId);
	        knub.style.background = "url(i/tip_knub_r.png) no-repeat 0 0;";
	        //knubId = "knub_r";
	        var pos = YAHOO.util.Dom.getXY(this.elementId);
	        document.getElementById("div_hoverTip_c").style.left = (pos[0] - this.width - 30) + "px"; 
	    }
	    //make sure style on div is set to display, it is set to none by default to fix a problem in IE
	    document.getElementById("div_hoverTip").style.display = "block";

        // place the box at 80% of center and the nub also at 80% of center
        document.getElementById(knubId).style.top = (0.8*((document.getElementById("div_hoverTip").scrollHeight/2)-(23))) + "px";
        document.getElementById("div_hoverTip").style.top = (0.8*(0-((document.getElementById("div_hoverTip").scrollHeight/2)-(document.getElementById(this.elementId).scrollHeight/2))))+"px";
	    
		this.panel.show();
		var elSource = document.getElementById(this.elementId);
	    if( TGN.Util.HoverTip.tipHideTimout >= 0 && elSource.tagName == "INPUT" ) {
			//yui 2.7.0 panel steals focus.  We want to give focus back where it
			//came from, like a text box after its shown itself and stolen the focus for itself.	
			//needs time to be sure the panel has been rendered and shown
			function getDisFunc(elId) { 
				return (function() {
					TGN.Util.HoverTip.close(); 
					//document.getElementById("ModuleTemplate1_gsln").value += "2"; 
					YAHOO.util.Event.removeListener(elId, "blur"); 
				});
			}

			function delayHover(zPanel, myEl) {
				return (function() {
						document.getElementById(myEl).focus();
	    				YAHOO.util.Event.addListener(myEl, "blur", getDisFunc(myEl));
				});
			}
			setTimeout(delayHover(this.panel, this.elementId),100);
		}
};
TGN.Util.HoverTip.close = function(){
        if(null != TGN.Util.HoverTip.tip && null != TGN.Util.HoverTip.tip.panel && TGN.Util.HoverTip.tip.panel.cfg.getProperty("visible"))
            TGN.Util.HoverTip.tip.panel.hide();
};

/*Parameters:
    id : The id of the element of which the tip will be aligned to 
    type : The type of tip to be rendered, can be empty if no special requirements for the tip are needed
    body : The body of the tip
    title : The title of the tip
    eAlign : Element alignment if reference to the tip, possible values, "tl", "tr", "bl", "br" default is "tr"
    tAlign : Tip alignment in reference to the element, possible values, "tl", "tr", "bl", "br" default is "tl"
    width : This will override the width of the tip defined in css, width must be an integer
*/

TGN.Util.HoverTip.showHoverTip=function(id, type, body, title, eAlign, tAlign, width, hideTimeout, showTimeout)
{
    TGN.Util.HoverTip.stopHideHoverTipTimer(); 
    if(showTimeout!=null && typeof showTimeout == "number" && showTimeout > 0)
    {
        clearInterval(TGN.Util.HoverTip.tipShowTimer);
        TGN.Util.HoverTip.tipShowTimer = setInterval(function(){TGN.Util.HoverTip.showHoverTip(id,type,body,title,eAlign,tAlign,width,hideTimeout,0);}, showTimeout);
        return;
    }
    
    if(hideTimeout==null || typeof hideTimeout != "number" || hideTimeout < 1)
        hideTimeout = 0;
        
    if(TGN.Util.HoverTip.tip == null){
        TGN.Util.HoverTip.tip = new TGN.Util.HoverTip();
        if( hideTimeout > 0 )
        {
            YAHOO.util.Event.addListener(document.getElementById("div_hoverTip"), "mouseout", TGN.Util.HoverTip.startHideHoverTipTimer);
		}
		if( showTimeout > 0 ) 
		{
            YAHOO.util.Event.addListener(document.getElementById("div_hoverTip"), "mouseover", TGN.Util.HoverTip.stopHideHoverTipTimer);
        }
    }

    if( hideTimeout > 0 )
    {
        TGN.Util.HoverTip.tipHideTimout = hideTimeout;
    }
    //Set defaults if params are null or empty
    if(eAlign==null || typeof eAlign == "undefined" || eAlign.length < 1)
        eAlign = "tr";
    if(tAlign==null || typeof tAlign == "undefined" || tAlign.length < 1)
        tAlign = "tl";
    if(title==null || typeof title == "undefined" || title.length < 1)
        title = "Get Better Matches";
    if(width==null || typeof width == "undefined" || width < 1)
        width = 200;
        
    TGN.Util.HoverTip.tip.elementId = id;
    TGN.Util.HoverTip.tip.body = body;
    TGN.Util.HoverTip.tip.title = title;
    TGN.Util.HoverTip.tip.eAlign = eAlign;
    TGN.Util.HoverTip.tip.tAlign = tAlign;
    TGN.Util.HoverTip.tip.width = width;

    var cookieVal;
    if(type == "name" || type == "date" || type == "place")
    {
        //For name, date, place tips we want to set a cookie to only show them once
         cookieVal = getDictionaryCookie('hvt',type); 
         if(cookieVal == 1) return;
         //Set cookie to not show next time
         var exp = new Date();  
         exp.setFullYear(exp.getFullYear() + 20 ,0,14);
         setDictionaryCookie('hvt', type, 1, '', exp);   
    }
    else
    {
        cookieVal = getDictionaryCookie('hvt',type); 
        if(cookieVal == 1) return;
    }
    
    TGN.Util.HoverTip.tip.show();
};

TGN.Util.HoverTip.turnHoverTipsOn=function(){
    //This reason why I set the cookie here instead of just deleting the cookie is if we ever want to track
    //if the customers are turning the tips on and off
    var exp = new Date();  
    exp.setFullYear(exp.getFullYear() + 20 ,0,14);
    setDictionaryCookie('hvt', 'name', 0, '', exp);
    setDictionaryCookie('hvt', 'date', 0, '', exp);
    setDictionaryCookie('hvt', 'place', 0, '', exp);
};

TGN.Util.HoverTip.startHideHoverTipTimer=function(hideTimeout)
{
    if(hideTimeout==null || typeof hideTimeout != "number" || hideTimeout.length < 1)
        hideTimeout = TGN.Util.HoverTip.tipHideTimout;

    clearInterval(TGN.Util.HoverTip.tipHideTimer);
    clearInterval(TGN.Util.HoverTip.tipShowTimer);
    if(TGN.Util.HoverTip.tip!=null && typeof TGN.Util.HoverTip.tip != "undefined" )
    {
        TGN.Util.HoverTip.tipHideTimer = setInterval(TGN.Util.HoverTip.tip.close, hideTimeout);
    }
};

TGN.Util.HoverTip.HideKWTips = function()
{
   setDictionaryCookie('hvt', 'kw', 1, '', '');
   location.reload(true);   

};

TGN.Util.HoverTip.stopHideHoverTipTimer=function(){
    clearInterval(TGN.Util.HoverTip.tipHideTimer);
    clearInterval(TGN.Util.HoverTip.tipShowTimer);
};
document.write("<div id=\"div_hoverTip\" class=\"hovertip\" style=\"display:none\"><div id=\"tp_bd\" class=\"bd\"></div></div>");

