//global var
var active_subul_node	= null;
var active_subdiv_node	= null;
var active_anchor_node	= null;

function initSlider()
{
    //assign node to variable
    var menu_parent     = $('menu');
    var ul_node         = menu_parent.getElement('ul');
	
    //if ul node exist find all li nodes, return a array
    if(ul_node)
    {
        var li_nodes        = ul_node.getChildren('li');
        var total_li_nodes  = li_nodes.length;
        
        //if total li nodes greater then zero, loop through the li nodes and find al ul subnodes, returns a array
        if(total_li_nodes>0)
        {
            li_nodes.each(function(li_node, index)
            {
                var anchor_node                 = li_node.getElement('a');
                var div_subnode                 = li_node.getElement('div');
                var ul_subnode                  = li_node.getElement('ul');
                var now_active_anchor_node      = li_node.getElement('.active');

                //if a ul subnode exist set style and effect 
                if(div_subnode && ul_subnode)
                {
                    anchor_node.addEvent('click',function()
                    {
                        
                        if (active_subul_node != ul_subnode)
                        {
                            //get ul subnode style
                            var get_ul_style            = ul_subnode.getStyle('display');
                            var ul_get_position         = ul_subnode.getStyle('top');
                            var ul_position_array       = ul_get_position.split('px');
                            var ul_position             = ul_position_array[0];
                            
                            // slider / effect settings
                            var slider_duration         = 400;
                            var listener_node_top_start = -32;
                            var listener_node_top_end   = 0;
                            
                            var curr_menu_parent = $('menu');
                            
                            if (curr_menu_parent)
                            {
                                var curr_ul_node = curr_menu_parent.getElement('ul');
                                
                                if (curr_ul_node)
                                {
                                    var curr_li_nodes        = curr_ul_node.getChildren('li');
                                    var curr_total_li_nodes  = curr_li_nodes.length;
                                    
                                    curr_li_nodes.each(function(curr_li_node, index)
                                    {
                                        var curr_anchor_node = curr_li_node.getElement('a');
                                        curr_anchor_node.set('class', '');
                                    });
                                }
                            }
                            
                            // active anchor
                            if (active_anchor_node) active_anchor_node.set('class', '');
                            this.set('class', 'active');
                            
                            // show filter
                            $('page_filter').setStyle('display','block');
                            
                            if (active_subul_node)
                            {
                                var slide_up_effect = new Fx.Morph(active_subul_node, {duration: slider_duration, transition: Fx.Transitions.Quad.easeOut});
                                slide_up_effect.start({'top': [listener_node_top_end, listener_node_top_start]}).chain(function()
                                {
                                    active_subdiv_node.setStyle('display','none');
                                    div_subnode.setStyle('display','block');
                                    
                                    var slide_down_effect = new Fx.Morph(ul_subnode, {duration: slider_duration, transition: Fx.Transitions.Quad.easeOut});
                                    slide_down_effect.start({'top': [listener_node_top_start, listener_node_top_end]}).chain(function()
                                    {
                                    	// hide filter
			                            $('page_filter').setStyle('display','none');
			                            
                                        // set active sub ul
                                        active_subul_node	= ul_subnode;
                                        active_subdiv_node	= div_subnode;
                                        active_anchor_node	= anchor_node;
                                    });
                                });
                            }
                            
                            // first time
                            else
                            {
                                div_subnode.setStyle('display','block');
                                
                                var slide_down_effect = new Fx.Morph(ul_subnode, {duration: slider_duration, transition: Fx.Transitions.Quad.easeOut});
                                slide_down_effect.start({'top': [listener_node_top_start, listener_node_top_end]}).chain(function()
                                {
                                	// hide filter
		                            $('page_filter').setStyle('display','none');
		                            
                                    // set active sub ul
                                    active_subul_node	= ul_subnode;
                                    active_subdiv_node	= div_subnode;
                                    active_anchor_node	= anchor_node;
                                });
                            }
                            
                            //get all anchor nodes then show portfoliopage
                            if(this.get('text') == 'portfolio')
                            {
                                var portfolio_anchor_nodes  = ul_subnode.getElements('a');
                                
                                portfolio_anchor_nodes.each(function(portfolio_anchor_node)
                                {
                                    portfolio_anchor_node.addEvent('click',function()
                                    {
										var category_name = this.get('title');
										lg_portfolio.showPopup(''+category_name+'');
										return false;
                                    });    
                                });
                            }
                            
                        }
                        return false;
                    });
                }
            });
        }
    }
}