var Category = (function(){   
   return {
        getIDsFromHash :(function(){
            var arr = window.location.hash.split('-');
            if( arr.length == 2 ) {
                arr[0]  = arr[0].substr(1,arr[0].length-1);
                return arr;
            }
            else {
                return false;
            }
        }),
        setHash : (function(){
            window.location.hash = '#'  + $("#parent_category_id  option:selected").val() + '-' + $("#category_id  option:selected").val();
        }),
        demand : ( function( categoryName ) {
            jQuery.post (
                '/category/demand/',
                { 'category_name' : categoryName },
                function(data){ 
                    var json = eval('(' + data+')');
                    if(json.result == 'ok') {
                        $('#demand_message').html('申請を受付ました。');
                    }
                    else {
                        $('#demand_message').html('正しく入力してください。');
                    }
                } 
            );
        }),
        loadParentCategories : ( function( selected_parent_category_id , selected_category_id ) {
            jQuery.getJSON(
                '/ajax/category/',
                function(json){ 
                    $("#parent_category_id").empty();
                    var i = 0;
                    var select_box = document.getElementById('parent_category_id');
                    jQuery.each( json, function(){ 
                        select_box.options[i++] = new Option( this.category_name  , this.category_id );
                    } );
                    if( selected_parent_category_id ) {
                        $("#parent_category_id").val( selected_parent_category_id );
                    }
                    Category.loadChildCategories( selected_category_id );
                } 
            );
        }),
        loadChildCategories : ( function( selected_category_id ) {
            var parent_category_id = $("#parent_category_id option:selected").val();
            jQuery.getJSON(
                '/ajax/category/' + parent_category_id + '/' ,
                function(json){ 
                    $("#category_id").empty();
                    var i = 0;
                    var select_box = document.getElementById('category_id');
                    jQuery.each( json, function(){ 
                        select_box.options[i++] = new Option( this.category_name  , this.category_id );
                    } );
                    if( selected_category_id ) {
                        $("#category_id").val( selected_category_id );
                        window.location.hash = '#' + $("#parent_category_id option:selected").val() + '-' + $("#category_id option:selected").val();
                    }
                    else {
                        window.location.hash = '#' + $("#parent_category_id option:first").val() + '-' + $("#category_id option:first").val();
                    }
                } 
            );
        })
    }
})();
