﻿function SuckerFishNav(listID, isTopLevelImages, showHoverOnSelected)
{    
    //Vars (None)
    this._listID = listID;
    this._showHoverOnSelected = showHoverOnSelected;
    
	//Prototype this class
	if (typeof(_SuckerFishNav_prototype_called) == 'undefined')
	{
		_SuckerFishNav_prototype_called = true;					

        //IE Specific mouseover classes
		SuckerFishNav.prototype.IEOnMouseOver = function(e, target) {target.addClassName('sfhover');};
		SuckerFishNav.prototype.IEOnMouseOut = function(e, target) {target.removeClassName('sfhover');};
		SuckerFishNav.prototype.ImageOver = imageOver;
		SuckerFishNav.prototype.ImageOut = imageOut;
	}
	
	//Get the main list
	var listElem = $(this._listID);
	if (listElem) {
	    //Only do this if the top level nav items are images
	    if (isTopLevelImages == true) { 
	        //Everyone needs the imageover and imageout stuff
	        var childLI = listElem.childElements();
	        var length = childLI.length;
    	    
	        if (length > 0) {
	            for (i=0; i< length; i++) {
	                if (!childLI[i].hasClassName('separator')) {
                        childLI[i].observe('mouseover', this.ImageOver.bindAsEventListener(this, childLI[i]));
                        childLI[i].observe('mouseout', this.ImageOut.bindAsEventListener(this, childLI[i]));	                
	                }
	            }
	        }
	    }
	
	    //IF IE, All li's need to change style from show to hide,
	    //and all ULs get iframes
	    //This should be IE 5.5-7. Now it is only 5.5-6. -BryanP 09/27/07
	    if (!window.opera && window.createPopup && !(document.documentElement && typeof document.documentElement.style.maxHeight!="undefined")) {	    

            //IE 7 supports the hover pseudo class, so no IE mouseover	    
	        if (!(document.documentElement && typeof document.documentElement.style.maxHeight!="undefined")) {
	            //Get all LI elements
	            var liElems = listElem.getElementsBySelector('li');
	            var length = liElems.length;
    	        
	            //Loop through LIs and attach IE mouseovers
	            if (length > 0) {
	                for (i=0; i< length; i++) {
	                    if (!liElems[i].hasClassName('separator')) {
	                        liElems[i].observe('mouseenter', this.IEOnMouseOver.bindAsEventListener(this, liElems[i]));
	                        liElems[i].observe('mouseleave', this.IEOnMouseOut.bindAsEventListener(this, liElems[i]), useCapture = true);
	                    }
	                }
	            }
	        }
	        
	        //While IE 7 does not have issues with <select> tags ignoring z-index, new 
	        //bugs have been reported for ActiveX/Object tags, so we need to keep this.
	        //Actually, this is causing more issues than it is worth, so IE7 will not get
	        //these.  --Bryanp 09/27/07
	        //Get all sub ULs so we can slip an ifram in behind the 
	        //dropdowns to fix a bug in IE	        
	        var ulElems = listElem.getElementsBySelector('ul');
	        var length = ulElems.length;
	        
	        //Loop through ULs and build IFrames
	        if (length > 0) {
	            for (i=0; i< length; i++) {
                    var iframeBlocker = Element.extend(HtmlBuilder.iframe({frameborder: '0'}));
                    iframeBlocker.className = "iframeBlocker";
                    iframeBlocker.style.border = 'none';
                    iframeBlocker.style.zIndex = '-1';
                    iframeBlocker.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
                    
                    //ulElems[i].insertBefore(iframeBlocker, ulElems[i].childNodes[0]);                                
                    ulElems[i].appendChild(iframeBlocker);                                
                    Position.absolutize(iframeBlocker);
                    Position.clone(ulElems[i], iframeBlocker);
	            }
	        }
    	}
	}
    
    //Handles the mouse over event	
	function imageOver(e, target) 
	{
	    //If we are not supposed to change the rollover image for the "on" tab
	    //Then skip the image swap.
	    if (this._showHoverOnSelected || !target.hasClassName('selected')) {
            //We assume that there should not be any images in the dropdown navs
            var image = target.getElementsBySelector('img');
            
	        if (image.length == 1) {    	        
                //The id is actually the beginning of the name of the image.
                image[0].src = image[0].src.substring(0,image[0].src.lastIndexOf('/')) + '/' + image[0].id + '-over.gif';
	        }
	    }
    }
    
    //Handles the mouse over event
    function imageOut(e, target)
    {        
        //If we are not supposed to change the rollover image for the "on" tab
        //Then there is no reason to replace the image back to its original state.
        if (this._showHoverOnSelected || !target.hasClassName('selected')) {
  	        //We assume that there should not be any images in the dropdown navs
	        var image = target.getElementsBySelector('img');
  	        if (image.length == 1) {
	            //The id is actually the beginning of the name of the image.
	            var imageName = image[0].id;
	            if (target.hasClassName('selected'))
	                imageName += '-on.gif';
	            else
	                imageName += '.gif';
	            image[0].src = image[0].src.substring(0,image[0].src.lastIndexOf('/')) + '/' + imageName;
	        }
	    }
    }    
}

