
$(document).ready(function() {
    $("ul#topnav li").each(function(a, b){
            $(this).find('span').css({display: 'none'});
    });
				
    $("ul#topnav li").click(function() { //Hover over event on list item
				$('ul#topnav li').each(function(){
						$(this).css({backgroundColor: 'transparent'});
				});
				$(this).css({backgroundColor: '#CCD8D8'});
					
        var blockFlag = false;
        var mItem = $(this);
        $("ul#topnav li").each(function(a, b){
            if ($(this).find('span').css('display') == 'block')
            {
                $(this).find('span').css({display: 'none'});
                mItem.find('span').css({display: 'block'});
                blockFlag = true;
            };
        });
        if (blockFlag == false)
        {
            $(this).find("span").stop(true,true).slideDown(500); //Show the subnav
        };
    });
		
    $("ul#topnav").mouseleave(function() { //on hover out...
            $(this).find("span").stop(true,true).slideUp(500, function(){navBar();}); 
    });
		
		$("ul#topnav li").hover(
				function(){
					blockFlag = false;
					$('ul#topnav li').each(function(){
							var liSpanDisplay = $(this).find('span').css('display');
							if(liSpanDisplay == 'block')
							{
								blockFlag = true;
							}
					});
					if(!blockFlag)
					{
						$('ul#topnav li').each(function(){
							$(this).css({backgroundColor: 'transparent'});
						});
						$(this).css({backgroundColor: '#CCD8D8'});
					}
				},
				function(){
					blockFlag = false;
					$('ul#topnav li').each(function(){
							$(this).css({backgroundColor: 'transparent'});
							var liSpanDisplay = $(this).find('span').css('display');
							if(liSpanDisplay == 'block')
							{
								blockFlag = true;
								$(this).css({backgroundColor: '#CCD8D8'});
							}
					});
					if(!blockFlag)
					{
						navBar();
					}
				}
		);		
		
	function navBar()
	{
				$('ul#topnav li').each(function(){
						$(this).css({backgroundColor: 'transparent'});
				});
		$('ul#topnav li').each(function(){
			var pathname = window.location.pathname;
			var liAText = $(this).children().html();
			if(pathname.search('/about/') > -1 && liAText == 'ABOUT')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
			if(pathname.search('/resent-work/') > -1 && liAText == 'RECENT WORK')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
			if(pathname.search('/multimedia-prints/') > -1 && liAText == 'MULTIMEDIA PRINTS')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
			if(pathname.search('/residencies/') > -1 && liAText == 'RESIDENCIES')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
			if(pathname.search('/archive/') > -1 && liAText == 'ARCHIVE')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
			if(pathname.search('home.html') > -1 && liAText == 'HOME')
			{
				$(this).css({backgroundColor: '#CCD8D8'});
			}
		
		});
	};

	navBar();

});
