/*
 * Explicitly using the jQuery namespace here instead of the $ shortcut to avoid 
 * conflicts with the use of mootools on the patient and practitioner payment pages.
 * 
 * see http://docs.jquery.com/Using_jQuery_with_Other_Libraries
 */

$.fn.x = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var x = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     x += o.offsetLeft;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = x;
             } else {
                 result = Math.min(result, x);
             }
         } else {
             o.style.left = n + 'px';
         }
     });
     return result;
};

$.fn.y = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var y = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     y += o.offsetTop;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = y;
             } else {
                 result = Math.min(result, y);
             }
         } else {
             o.style.top = n + 'px';
         }
     });
     return result;
};

jQuery(document).ready(function(){
	jQuery(".flyout_panel").hide();
	
	jQuery(".flyout_panel").bind("mouseleave", function() {
		hideNavigation();
	});
	
	jQuery('#nav_module a img').mouseout(function() {
		jQuery('.flyout_panel').hide();
	});
	
	/* gdx additions start [jea 19.nov.10] */
//	setActiveItems();
	$setActiveItems();
//	toggleLayer("submenu_" + getLayerName());
	$showLayer("submenu_" + getLayerName());
	/* gdx additions end [jea 19.nov.10] */
});

function hideNavigation()
{
	jQuery(".flyout_panel").hide();
}

function showNavigation(navpane_id, parent)
{
	hideNavigation();
	pane = "#flyout" + navpane_id;
	posY = (jQuery(parent).y());
	posX = (jQuery(parent).x());
	// jQuery(pane).corner("round 10px");
	jQuery(pane).show();
	jQuery(pane).css("z-index", "10000");
	jQuery(pane).css("top", posY+"px");
	jQuery(pane).css("left", (posX+210)+"px");
}

/* gdx additions start [jea 19.nov.10] */

/**
 * Search through the array of nav_module anchors. Apply the class .current
 * to any anchors with the same pathname as the document pathname.
 */
function setActiveItems()
{
	var lNavModule = document.getElementById("nav_module");
	
	if (lNavModule)
	{
		var lAnchorArray = lNavModule.getElementsByTagName("a");
		var lPathname    = document.location.pathname;

		for (var i = 0; i < lAnchorArray.length; i++)
		{
			if (lAnchorArray[i].pathname == lPathname)
			{
				lAnchorArray[i].id = "active_menu";
				
				// This works to disable the flyout for the active 
				// page but feels a little clunky [jea 18.nov.10] 
				// Causes the flyout to be temporarily positioned
				// incorrectly [jea 18.nov.10]
				/*
				var parentNode = lAnchorArray[i].parentNode;
				
				while (parentNode != null && parentNode.className != "flyout_panel")
				{
					parentNode = parentNode.parentNode;
				}
				if (parentNode != null)
				{
					parentNode.id = "active_menu";
				}
				*/
			}
		}
	}
}

/**
 * jQuery version of setActiveItems
 * 
 * Finds all the anchors that are descendants of nav_modules that
 * have a pathname equal to the pathname of the current document and 
 * applies the class .current
 */
function $setActiveItems()
{
   var lPath = location.pathname;

   if (lPath)
   {
	   jQuery('#nav_module a[href = "' + lPath + '"]').attr('id', 'active_menu');
   }
}


/* The following from http://snook.ca/archives/javascript/clear_links_to_1 */
/*
function clearCurrentLink()
{
    var a = document.getElementsByTagName("A");
    
    for (var i = 0; i < a.length; i++)
    {
        if (a[i].href == window.location.href.split("#")[0])
        {
            removeNode(a[i]);
        }
    }
}

function removeNode(n)
{
    if (n.hasChildNodes())
    {
        for (var i = 0; i < n.childNodes.length; i++)
        {
            n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
        }
    }
    n.parentNode.removeChild(n);
}
*/


/**
 * Returns the name of the layer from current document pathname
 */
function getLayerName()
{
	var arr = document.location.pathname.split('/');
	
	return arr[1].toLowerCase();		
}


/**
 * Toggles the display property of the element with the given id
 * @param whichLayer
 */
function toggleLayer(whichLayer)
{
	var lStyle;
	
    if (document.getElementById)
    {
        // this is the way the standards work
    	lStyle = document.getElementById(whichLayer).style;
    }
    else if (document.all)
    {
        // this is the way old msie versions work
    	lStyle = document.all[whichLayer].style;
    }
    else if (document.layers)
    {
        // this is the way nn4 works
    	lStyle = document.layers[whichLayer].style;
    }
     
    if (lStyle)
    {
    	lStyle.display = lStyle.display ? "" : "block";
    }
}

/**
 * Sets the display property of the element with the given id
 * @param pLayer
 */
function $showLayer(pLayer)
{
	jQuery('#' + pLayer).show();
}

/* gdx additions end [jea 19.nov.10] */


