/*
 * clase cliente para obtener datos de Nebula
 * presentaciones, spots, ... etc
 */
function nebula_content_client(){

    /**
     * carga una serie de spots rotativos y aleatorios en un div contenedor
     * @param divspot id del div para mostrar la rotacion
     * @param category ID de la categoria de anuncios a mostrar
     * @param interval tiempo en seg para rotar los anuncios
     * @param caract caracteres a mostrar
     */
    this.spot = function(divspot,category,interval,caract){
        $('div#'+divspot).load('/NebulaContent/api/presentation_spot?category_id=' +category+ '&cut=' + caract);
        setInterval(function(){
            $('div#'+divspot).load('/NebulaContent/api/presentation_spot?category_id=' +category+ '&cut=' + caract);
        },interval * 1000);
    }

    /**
     * seteo el ancho de forma dinamica de los contenidos
     * en las presentaciones
     * requiere: dimensions de jquery
     */
    this.helper_set_pres_size = function(){
        var content_width = $('div#content').width();
        var slide_gallery_table_width = $('#slide_gallery_table').width();
        var resto = (content_width - slide_gallery_table_width);

        // llevamos el ancho del texto al limite con las imagenes
        $('div#slide_content').width(resto - 35);
        
    }

    /**
     * elimino una presentacion
     *
     * @param presid
     * @param confirm_msg
     * @param callback funcion que se ejecuta al borrar la presentacion
     */
    this.pres_delete_confirm = function(presid,confirm_msg,callback){
        var url = '/NebulaContent/api/presentation_del';
        if(confirm(confirm_msg)){
            $.post(url, {
                pres_id: presid
            }, function(data){
                if(typeof callback != 'undefined'){
                    callback.call(data);
                }
            });
        }
    }
    
    /**
     * elimino una presentacion
     *
     * @param presid
     * @param callback funcion que se ejecuta al borrar la presentacion
     */
    this.pres_delete = function(presid,callback){
        var url = '/NebulaContent/api/presentation_del';
        $.post(url, {
            pres_id: presid
        }, function(data){
            if(typeof callback != 'undefined'){
                callback.call(data);
            }
        });
    }

    /**
     * elimino una categoria de presentaciones
     *
     * @param catId
     * @param confirm_msg
     * @param callback funcion que se ejecuta al borrar la categoria
     */
    this.cat_delete_confirm = function(catId,confirm_msg,callback){
        var url = '/NebulaContent/presentations/catdelete';
        if(typeof confirm_msg == 'undefined' || confirm_msg.length == 0 || confirm_msg == null){
            confirm_msg = 'confirm delete';
        }

        if(confirm(confirm_msg)){
            $.post(url, {
                catId: catId
            }, function(data){
                if(orion.forms.jsonResponse(data, 0)){
                    if(typeof callback == 'function'){
                        callback.call(data,data);
                    }
                }
            },'json');
        }
    }
    
    /**
     * elimino una categoria de presentaciones
     *
     * @param catId
     * @param callback funcion que se ejecuta al borrar la categoria
     */
    this.cat_delete = function(catId,callback){
        var url = '/NebulaContent/presentations/catdelete';
        $.post(url, {
            catId: catId
        }, function(data){
            if(orion.forms.jsonResponse(data, 0)){
                if(typeof callback == 'function'){
                    callback.call(data,data);
                }
            }
        },'json');
    }

    /**
     * cambio la descripcion de un recurso imagen
     */
    this.pres_img_change_desc = function(presid,resid,newdesc,callback){
        var url = '/NebulaContent/api/pres_img_change_desc';
        $.post(url, {
            'pres_id':presid,
            'res_id':resid,
            'desc':newdesc
        }, function(data){
            orion.forms.jsonResponse(data, 0);
            if(typeof callback != 'undefined')
                callback.call();
        }, 'json');
    }

    /**
     * borrado de recursos de una presentacion
     */
    this.presentation_delete_resources = function(presid,resid,confirm_msg,callback){
        var url = '/NebulaContent/api/presentation_delete_resources';
        if(typeof confirm_msg == 'undefined'){
            confirm_msg = 'Confirm Action';
        }
        if(confirm(confirm_msg)){
            $.post(url, {
                res_id: resid,
                pres_id: presid
            }, function(data){
                if(orion.forms.jsonResponse(data, 0)){
                    if(typeof callback != 'undefined'){
                        callback.call(data);
                    }
                }
            },'json');
        }
    }

}

var ncc = new nebula_content_client();
