(function($) {  

    var currentIndex = -1; 
    var currentSubTab = null;
    var initialIndex = -1;
    var initialSubTab = null;
    var sitemapWrapper = null;
    var timer2 = null;
	
	$.oakland = {		
		defaults: { }
	};
	
	$.fn.extend({
		oakland : function(settings) {
			settings = $.extend({}, $.oakland.defaults, settings);		
		    initialIndex = settings.initialIndex;
		    
		    sitemapWrapper = this;
		    
		    init(this);		    
		}
	});	
	
	function onSitemapletChanged(dl, index) {	
	    
	    
//	    if (timer2)
//	        timer2 = clearTimeout();
	        
	    if (index != currentIndex) {
	        showSubTab(dl, index);
	    }
	}
	
	function showSubTab(dl, index) {    
	    
	    if (currentSubTab)
	        currentSubTab.hide();
	        
	    // sitemapWrapper.cycle(index);	   
	        
	    var subTab = $(dl).next("ul"); 
    	        
	    subTab.show();
	    
	    currentSubTab = subTab;
	    
	    //	$('#FooterSitemap').height(subTab.height() + 120);	
	    $('#FooterSitemap').height(311);    
	    
        currentIndex = index;  
	}
	
	function init() {
	
	    
//	    sitemapWrapper.cycle( 
//	    { 
//	        fx : 'cover',        
//	        slideExpr : 'dl.first ~ ul',
//	        timeout : 0,
//	        direction: 'up',
//	        easing : 'easeOutCirc',	        
//	        cssBefore: { opacity: 1 },
//            animOut:   { opacity: 0 }
//	        
//	    });  

//	    
//	    $(sitemapWrapper).css('position', '');
        
        $('#FooterSitemapWrapper > dl:first').addClass('first');  
	    $('#FooterSitemapWrapper > dl:last').addClass('last');  	    
	    
	    var timer; 
	    
        $(sitemapWrapper).children("dl").each(
            function(index) {  
            
                var dl = this;
                var onMouseOver = function(dl) { 
                        
                        clearTimeout(timer);
                      
                        $(sitemapWrapper).find("dl").removeClass("selected");
                        $(sitemapWrapper).find("dd").removeClass("selected");
                        
                        
                        
//                        timer2 = setTimeout(function()
//                        {
//                            onSitemapletChanged(dl, index);                             
//                        }, 600);
                        
                        // 
                        onSitemapletChanged(dl, index); 
                        
                        $(dl).addClass("selected");
                        $(dl).children("dd").addClass("selected"); 
                        
                    };
                    
                var onMouseOut = function(dl) { 
                        // $(dl).removeClass("selected");                       
                        timer = setTimeout(function() 
                        {
                            $(sitemapWrapper).find("dl").removeClass("selected");
                            $(sitemapWrapper).find("dd").removeClass("selected");
                            $(initialSubTab).addClass("selected");            
                            $(initialSubTab).children("dd").addClass("selected");                 
                            onSitemapletChanged(initialSubTab, initialIndex); 
                             
                        }, 3000); 
                    };
                            
                $(this).hover(
                    function() { onMouseOver(dl) }, 
                    function() { onMouseOut(dl) }
                );
                
                $(this).next("ul").hover(
                    function() { onMouseOver(dl) }, 
                    function() { onMouseOut(dl) }
                );            
               
                if (index == initialIndex) {
                    
                    initialSubTab = this                    
                    $(dl).addClass("selected");
                    $(dl).children("dd").addClass("selected"); 
                    onSitemapletChanged(initialSubTab, initialIndex);
                }
            });
	}
	
})(jQuery);