# encoding: utf-8

module OptMenu
    module OptMenuActiveSupport
        def render_opt_menu
            url = get_opt_menu_path(session[:last_opt_menu] || "")
            
            content_tag(:div, :class=>"opt_menu") do
                javascript_tag do
                  opt_menu_js(url)
                end
            end
        end
        
        def opt_menu_item(title, url, options = {})
            options.merge!({:class => options[:node] ? "menu_node" : "menu_item"})
            url = get_opt_menu_path(url) if options.delete(:node)
                                  
            link_to title, url, options
        end     
        
        protected 
        
        def opt_menu_js(url)
          js = ""
          js << "jQuery(document).ready(function(){"
          js << "    $.ajax({" 
          js << "        type: \"GET\","
          js << "        dataType: \"html\","            
          js << "        url: \"#{url}\","
          js << "        async: false,"
          js << "        success: function(data){"
          js << "            $(\".opt_menu\").html(data);"
          js << "        },"
          js << "        error: function(data){"  
          js << "            alert(data);"
          js << "        }"
          js << "      })"
          js << "})"
          js.html_safe
        end
        
    end
end