var agt             = navigator.userAgent.toLowerCase();
var major           = parseInt(navigator.appVersion);
var minor           = parseFloat(navigator.appVersion);
var nav             = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                      && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                      && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var nav4            = (nav && (major == 4));
var nav6up          = (nav && (major >= 5));              
var ie              = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var ie3             = (ie && (major < 4));
var ie4             = (ie && (major == 4) && (agt.indexOf("msie 4")!=-1) );
var ie4_5           = (ie && (major == 4) && (agt.indexOf("msie 4.5")!=-1) );
var ie4up           = (ie  && (major >= 4));
var ie5             = (ie && (major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var ie5_1           = (ie && (major == 4) && (agt.indexOf("msie 5.1")!=-1));
var ie5_5           = (ie && (major == 4) && (agt.indexOf("msie 5.5")!=-1));
var ie5up           = (ie && !ie3 && !ie4);
var ie5_1up         = (ie && !ie3 && !ie5 && !ie5_5);
var ie5_5up         = (ie && !ie3 && !ie4 && !ie5 && !ie5_1);
var ie6             = (ie && (major == 4) && (agt.indexOf("msie 6.")!=-1) );
var ie6up           = (ie  && !ie3 && !ie4 && !ie5 && !ie5_5);
var safari			= (nav && (agt.indexOf("safari")!=-1))
var mac             = (agt.indexOf("mac")!=-1);
var win             = ((agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1));    
var pageImages      = new Array();
var pageMenus       = new Array();
var brwidth			= 760;

function IF(object)
{
    return (typeof(object)!='undefined'&&object!=null);
}

function getAbsoluteCoords(element) 
{
    // Not to be used in NS. Needed when adding inflow layers
    var a; if(!IF(element)) return { x:-(document.body.clientWidth-brwidth)/2, y:0 }
    else 
    {
        a = getAbsoluteCoords(element.offsetParent);
        return { x: a.x+element.offsetLeft, y: a.y+element.offsetTop };
    }
}

function preload()
{
    for(var i = 0; i < pageImages.length; i++)
        for(var j = 0; j < pageImages[i].source.length; j++)
            {var img = new Image(); img.src = pageImages[i].source[j];}
    
    for(var i = 0; i < pageMenus.length; i++) with(pageMenus[i])
        {setContent(); update(true);} 
}

function PageImage()
{
    // Prepare references
    window.pageImages[(this.index = window.pageImages.length)] = this;
    
    // Properties
    var args            = arguments;        this.id             = "pageImage_"+this.index;
    this.imageObj       = null;             this.linkObj        = null;
    this.attribs        = -1;               this.linkAttribs    = -1;
    this.code           = "";               this.source         = new Array();
    this.created        = false;            this.href           = -1;
    this.events         = "";               this.imageIndex     = -1;
    this.linkIndex      = -1;               this.useEvents      = true;
    this.imageWidth     = -1;               this.imageHeight    = -1;
    
    // Parse Parameters
    this.source[0]      = (args.length > 0) ? args[0] : "images/pixel.gif";
    this.source[1]      = (args.length > 1 && args[1] != -1) ? args[1] : this.source[0];
    this.href           = (args.length > 2 && args[2] != -1) ? args[2] : -1;
    this.attribs        = (args.length > 3 && args[3] != -1) ? args[3] : "";
    this.linkAttribs    = (args.length > 4 && args[4] != -1) ? args[4] : "";
    this.imageWidth     = (args.length > 5 && args[5] != -1) ? args[5] : -1;
    this.imageHeight    = (args.length > 6 && args[6] != -1) ? args[6] : -1;
    
    if(this.source[0] == this.source[1])
        this.useEvents = false;                
    
    // User-defined Methods
    this.onFocus        = function() {/* User defined */};
    this.onBlur         = function() {/* User defined */};
    this.onClick        = function() {/* User defined */};
    this.onBlurImage    = function() {this.showSource(0);}
    this.onFocusImage   = function() {this.showSource(1);}
    
    // Object Methods
    this.precreate = function()
    {
        with(this)
        {
            if(attribs=="" || !IF(attribs))
                attribs = 'border="0" vspace="0" hspace="0"';

            attribs += (imageWidth!=-1) ? (' width="'+imageWidth+'"') : '';
            attribs += (imageHeight!=-1) ? (' height="'+imageHeight+'"') : '';
                
            code     = '';
            events   = '';

            events  += 'onmouseover="window.pageImages['+index+'].onFocusImage(); window.pageImages['+index+'].onFocus();" ';
            events  += 'onmouseout="window.pageImages['+index+'].onBlurImage(); window.pageImages['+index+'].onBlur();" ';
            events  += 'onclick="window.pageImages['+index+'].onClick();" ';

            code    += (href==-1) ? '' : ('<a href="'+href+'"'+((linkAttribs!=-1)?(' '+linkAttribs):' ')+' '+((useEvents)?events:"")+'>');
            code    += '<img id="'+id+'" src="'+source[0]+'" '+attribs+' '+((href==-1&&useEvents)?events:'')+'>';
            code    += (href==-1) ? '' : '</a>';

            if(!useEvents)
                onBlurImage = onFocusImage = function() {};
        }
        return this;            
    }
    this.create = function()
    {
        with(this)
        {
            precreate(); document.write(code);
            imageObj    = document.images[(imageIndex = document.images.length-1)];
            linkObj     = (href==-1) ? null : document.links[(linkIndex = document.links.length-1)];
            created     = true;
        }
        return this;
    }
    this.showSource = function(imageId)
    {
        var src = this.source[imageId];
        var pre = "";
        
        if(!this.created || typeof(src)=='undefined' || src == null || src == "")
            return;
            
        if(this.imageObj.src.indexOf(location.host)!=-1)
            pre += location.protocol + "//" + location.host;
            
        pre += ((location.port!="") ? ":"+location.port : "") + "/";
        
        if(this.imageObj.src != (pre + src))
            this.imageObj.src = src;
    }
    this.showSource.lastSrc = "";            
    this.reset = function()
        {if(!this.created) return -1; if(this.imageObj.src != this.source[0]) this.imageObj.src = this.source[0];}
    
    this.top = function()
        {if(!this.created) return -1; if(nav4) return this.imageObj.y; else return getAbsoluteCoords(this.imageObj).y;}

    this.left = function()
        {if(!this.created) return -1; if(nav4) return this.imageObj.x; else return getAbsoluteCoords(this.imageObj).x;}
        
    this.width = function()
        {if(!this.created) return -1; if(nav4) return this.imageObj.width; else { return this.imageObj.offsetWidth;}}

    this.height = function()                
        {if(!this.created) return -1; if(nav4) return this.imageObj.height; else { return this.imageObj.offsetHeight;}}
}
PageImage.showSource = function(imageId)
{
    for(var i = 0; i < pageImages.length; i++)
        pageImages[i].showSource[imageId];
}
PageImage.reset = function()
{
    for(var i = 0; i < pageImages.length; i++)
        pageImages[i].reset();
}

function PageMenu()
{
    // Register Object
    window.pageMenus[(this.index = window.pageMenus.length)] = this;

    // Properties
    this.layerObj       = null;         this.id             = null;
    this.left           = 0;            this.top            = 0;
    this.width          = 0;            this.height         = 0;
    this.code           = "";           this.events         = "";
    this.content        = "";           this.created        = false;
    this.showAlone      = true;         this.showing        = false;
    this.style          = "";           this.attribs        = "";
    this.doc            = null; // Netscape 4 Only!
    
    // Browser-dependant Properties and Methods
    this.VISIBLE        = (nav4) ? 'show' : 'visible';
    this.INVISIBLE      = (nav4) ? 'hide' : 'hidden';
    this.css            = function() {return (nav4) ? this.layerObj : this.layerObj.style;}
    
    // Parse Arguments
    var args            = arguments;
    this.id             = (args.length > 0 && args[0]!=null) ? args[0] : "pageMenu_"+this.index;
    this.left           = (args.length > 1) ? args[1] : 0;
    this.top            = (args.length > 2) ? args[2] : 0;
    this.width          = (args.length > 3) ? args[3] : 0;
    this.height         = (args.length > 4) ? args[4] : 0;
    this.content        = (args.length > 5) ? args[5] : "";
    this.attribs        = (args.length > 6) ? args[6] : "";
    this.style          = (args.length > 7) ? args[7] : "";
    
    // User-defined Methods
    this.beforeUpdate   = function() {/* User defined */}
    this.afterUpdate    = function() {/* User defined */}
    this.mouseOver      = function() {/* User defined */}
    this.mouseOut       = function() {/* User defined */}
    this.mouseMove      = function() {/* User defined */}
    
    // Methods
    this.precreate = function()
    {
        with(this)
        {
            events   = '';
            events  += 'onmouseover="PageMenu.show('+index+').mouseOver();" ';
            events  += 'onmouseout="PageMenu.hide('+index+').mouseOut();" ';
            events  += 'onmousemove="PageMenu.get('+index+').mouseMove();" ';
            
            code     = '';
            code    += (nav4) 
                        ? '<layer id="'+id+'" visibility="'+INVISIBLE+'" left="'+left+'" top="'+top+'" pagex="'+left+'" pagey="'+top+'" width="'+width+'" height="'+height+'" clip="0,0,'+(width)+','+(height)+'" '+attribs+' '+events+((style!="")?(' style="'+style+'"'):'')+' z-index="1">'
                        : '<span id="'+id+'" '+attribs+' style="position:absolute;visibility:'+INVISIBLE+';left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px; clip:rect(0px, '+width+'px, '+height+'px, 0px); z-index:1;'+style+'" '+events+'>';
            code    += content;
            code    += (nav4)
                        ? '</layer>'
                        : '</span>';
        }
        return this;
    }
    this.create = function()
    {
        if(this.created) return this;
        with(this)
        {                    
            precreate(); document.write(code);
            layerObj    = (nav4) ? document.layers[this.id] : ((IF(document.all)) ? document.all[this.id] : document.getElementById(this.id));
            doc         = (nav4) ? layerObj.document : null;
            created     = true;
            update();
        }
        return this;
    }
    this.setContent = function()
    {
        var htmlContent = (arguments.length > 0) ? arguments[0] : this.content;
        this.content    = htmlContent;
        
        if(!this.created) return this;
        if(nav4) {this.doc.open(); this.doc.write(htmlContent); this.doc.close();}
            else {this.layerObj.innerHTML = htmlContent;}
                
        return this;
    }
    this.update = function()
    {
        if(!this.created) return this;

        var needsUpdate = (arguments.length > 0) ? arguments[0] : false;
        
        if(parseInt(this.css().left) != this.left)      needsUpdate = true;
        if(parseInt(this.css().top) != this.top)        needsUpdate = true;
        if(parseInt(this.css().width) != this.width)    needsUpdate = true;
        if(parseInt(this.css().height) != this.height)  needsUpdate = true;
        
        if(!needsUpdate) return;
        
        this.beforeUpdate();
//        this.css().left      = this.left + ((!nav4) ? "px" : 0);
        this.css().left      = this.left+(document.body.clientWidth-brwidth)/2+"px";
        this.css().top       = this.top + ((!nav4) ? "px" : 0);
        this.css().width     = this.width + ((!nav4) ? "px" : 0);
        this.css().height    = this.height + ((!nav4) ? "px" : 0);
        this.afterUpdate();

        return this;
    }
    this.syncToImage = function(pageImage)
    {
        if(IF(pageImage))
        {
            this.sync           = pageImage;
            this.modx           = (arguments.length > 1) ? arguments[1] : 0;
            this.mody           = (arguments.length > 2) ? arguments[2] : 0;
            this.beforeUpdate   = function() 
            {
                if(IF(this.sync)) 
                {
                    this.top     = (this.sync.top() + this.sync.height());
                    this.left    = this.sync.left();
                }
                if(mac && ie5up)
                {
                    this.top    -= 36+"px";
                }
                else if(mac && ie4_5)
                {
                    this.top     = 52+"px";
                }
                else if(mac && ie4)
                {
                    this.top     = 52+"px";
                }
				// sets this.
				    if(safari)
                {
                    this.top    -= 8+"px";
                    this.left    -= 8+"px";
                }
                
                this.top += this.mody; this.left += this.modx;
            }
        }
        return this;
    }
    
    // Simple Methods
    this.getContent = function()        {return this.content;}
    this.show = function()              {if(!this.created) return; if(!this.showing) {this.css().visibility = this.VISIBLE; this.showing = true;}}
    this.hide = function()              {if(!this.created) return; if(this.showing) {this.css().visibility = this.INVISIBLE; this.showing = false;}}
    this.moveTo = function(x,y)         {this.left = x; this.top = y; this.update();}
    this.moveBy = function(x,y)         {this.left += x; this.top += y; this.update();}             
}
// Static Methods
PageMenu.count = function()
{
    return window.pageMenus.length;
}
PageMenu.get = function(index)
{
    return window.pageMenus[index];
}        
PageMenu.showing = function()
{
    var count = 0;
    for(var i = 0; i < PageMenu.count(); i++)
        if(PageMenu.get(i).showing) count++;
        
    return count;
}        
PageMenu.show = function(index)
{
    var pm = PageMenu.get(index);
    if(IF(pm))
    {
        if(!pm.created) return "Not Yet Created";
        if(pm.showAlone && PageMenu.showing() > 1 && !pm.showing) PageMenu.hideAll();                
        if(!pm.showing)
        {
            pm.update();
            pm.show();
        }
        return pm;
    }
    return null;
}
PageMenu.showAll = function(force)
{
    for(var i = 0; i < PageMenu.count(); i++)
        PageMenu.show(i);
}
PageMenu.hide = function(index)
{
    var pm = PageMenu.get(index);
    if(IF(pm))
    {                
        if(!pm.created) return "Not Yet Created";
        pm.hide();
        return pm;
    }
    return null;
}
PageMenu.hideAll = function()
{
    for(var i = 0; i < PageMenu.count(); i++)
        PageMenu.hide(i);
}
PageMenu.forMenuItems = function()
{
    var count   = arguments[1];
    var height  = (21*(count+((count>0)?1:0)));
    var width   = 162;
    var layer   = new PageMenu(null,0,0,width,height).create();
    var content = "";
    var fmiImg  = arguments[0];
    
    with(layer)
    {
        syncToImage(fmiImg);
        sync.child          = layer;
        sync.timer          = 0;                
        
        with(sync)
        {
            onFocusImage    = onBlurImage   = function() {}
            onFocus         = function()
            {
                // Clear Timer Just In Case
                clearTimeout(this.timer); this.timer = 0;
                
                // Reset Everything
                PageMenu.hideAll(); PageImage.reset();
                
                // Show Desired Menu and Data
                this.showSource(1);
                this.child.update();
                this.child.show();
            }
            onBlur          = function()
                {this.timer = setTimeout('pageImages['+this.index+'].reset();pageMenus['+this.child.index+'].hide();',40);};
        }
        // Strange Note: It appears the if the mouse moves in a layer that is displayed on top of
        // of an element with a background image repeating calls to mouseOut and mouseOver will occur.
        // The timeouts prevent this from happening.
        mouseOver           = function() {clearTimeout(this.sync.timer);this.sync.timer = 0;}
        mouseOut            = function() {this.sync.timer = setTimeout('PageImage.reset();PageMenu.hideAll()',40);}
        mouseMove           = function() {this.show();}
    }
			if(count>0)
			{
				if(mac && ie) content+= '<span style="position:absolute; top:0; left:10px;" id="inner_'+layer.id+'">';
				
				content += '<table cellspacing="0" cellpadding="0" border="0" class="topnav" bgcolor="#333333">';
				content += ' <tr>';
				content += '  <td>';
				for(var i = 2; i < arguments.length; i+=2)
				{
					content += '<a href="'+arguments[i]+'"';
						if(arguments[i].substring(0,4) == "http" && arguments[i].substring(0,13) != "http://online" && arguments[i].substring(0,25) != "http://www.academyart.edu" && arguments[i].substring(0,25) != "http://arc.academyart.edu" && arguments[i].substring(0,25) != "http://faculty.academyart")
						{
							content += ' target=_blank';
						}
					content += '>';
					content += arguments[(1+i)];
				}
		
				content += '  </td>';
				content += ' </tr>';               
				content += '</table>';
				if(mac && ie) content+= '</span>';
			}
			layer.content   = content;
			layer.height    = height;
			layer.width     = width;
			
			return layer;                      
		}