/**
 * ozLancer Pro
 *
 * Copyright (c) 2011 OzLancer Pro (ozlancer.com)
 * All terms, conditions and copyrights as defined
 * in the ozLancer License Agreement
 * http://www.ozlancer.com/
 *
 */
 
$(document).ready(function() { 	
	$("#id_role_name").autocomplete({
		source: function(request, response) {
			$.ajax({
			  url: base_url+"/category/role_json",
			  data: request,
			  dataType: "json",
			  type: "POST",
			  success: function(data){
				  response(data);
			  }
			});
		  },
		minLength: 3,	
		change: function() { $(this).trigger('change'); }
	});

    slim_tabs('.slimtab');

    /* JSON: Populate roles */
    $.getJSON(base_url+'/category/category_view_json',{}, function(json){
        var roles = '';
        if (json)
        {
            for (var i = 0; i < json.length; i++) {
                roles += '<option value="'+json[i].id+'">'+json[i].category+' &gt;</option>';
            }
            $("#id_role_select_parent").html(roles);
        }
    });

    $.getJSON(base_url+'/category/role_view_json',{}, function(json){
        var roles = '';
        if (json)
        {
            for (var i = 0; i < json.length; i++) {
                roles += '<option value="'+json[i].title+'" rel="'+json[i].category+'">'+json[i].title+'</option>';
            }
            $("#id_role_select_alpha, #id_role_select").html(roles);
            $("#id_role_select").empty();
        }
    });

    /* Action: Select role */
    $('#id_role_select_parent').change(function() {
        var role_parent_id = $(this).val();
        $("#id_role_select").empty();
        $('#id_role_select_alpha option').each(function() {
            var role = $(this);
            var cat_ids = role.attr('rel').split('|');

            if (jQuery.inArray(role_parent_id,cat_ids) > -1) $('#id_role_select').append(role.clone());

        });
    });

    $('#id_role_select_alpha, #id_role_select').change(function() {
        $('#id_role_name').val($(this).val()).valid();
    });


}); 
