var nebulamarket_client2 = new nebula_market_client();

function AdminbuscarProducto(){
    var url = '/Admin/Productos';
    var search = jQuery('input#search').val();
    search = urlencode(search);
    var search_filter = jQuery('#search_filter').val();

    var searchUrl = url + '?search=' + search;

    if(search_filter.length > 3){
        searchUrl += '&search_filter=' + search_filter;
    }

    orion.xhr.load(searchUrl,'content');
}

function AdminborrarProducto(prid){
    if (confirm('confirmar borrado')){
        nebulamarket_client2.products_del(prid,function(data){
            //$('tr#tr' + prid).remove();
            orion.xhr.refresh();
            alert(data);
        });
    }
}

function searchUsers(){
        $('input#buscar').click(function(){
        var search = $('input#search').val();
        var params = '{"search" : "' +search + '"}';

        if(search.length > 0){
            orion.xhr.load('/Admin/Usuarios_Admin?queryobj='+urlencode(params), 'content');
        }
    });

    $('input#search').keypress(function (e) {
       if (e.which == 13){
           $('input#buscar').click();
       }

     });
}

var elpique = {
    add: function(){
        orion_box.xhr('/Admin/elpique/add', 500, 500);
    },
    del: function(id){
        if (confirm('confirmar borrado')){
            jQuery.post('/Admin/elpique/del', {id:id}, function(){
               orion.xhr.refresh();
            }, 'json');
        }
    },
    edit: function(id){
        orion_box.xhr('/Admin/elpique/edit?id='+id, 500, 500);
    }
}

var App = {};
App.Admin = {};

App.Admin.Content = {

    metadataadd : function(refId, name, value,labelId){
        $.post('/Admin/Content/metadataadd', {
            'refId': refId,
            'name': name,
            'value' : value,
            'labelId' : labelId
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                orion.xhr.refresh();
            }

        }, 'json');
    },

    metadataremove : function(id,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/Content/metadataremove', {
                'id': id
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },

    metalabeladd : function(label,refId,type){
        $.post('/Admin/Content/metalabeladd', {
            'label': label,
            'refId': refId,
            'type' : type
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                orion.xhr.refresh();
            }

        }, 'json');
    },

    metalabelremove : function(labelId,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/Content/metalabelremove', {
                'id': labelId
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },

    presvisible : function(presId,visible){

        if(visible){
            visible = 1;
        }else{
            visible = 0;
        }

        $.post('/Admin/Content/presvisible', {
            'presId': presId,
            'visible': visible
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                //orion.xhr.refresh();
            }

        }, 'json');
    }

}

App.Admin.newsletter = {

    add : function(title, desc,content){
        $.post('/Admin/newsletter/add', {
            'title': title,
            'desc' : desc,
            'content' : content
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                orion.xhr.refresh();
            }

        }, 'json');
    },

    change : function(newsId,title, desc){
        $.post('/Admin/newsletter/change', {
            'newsId' : newsId,
            'title': title,
            'desc' : desc
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                //orion.xhr.refresh();
            }

        }, 'json');
    },

    remove : function(id,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/newsletter/remove', {
                'id': id
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },
    
    removeEmail : function(id,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/newsletter/removeEmail', {
                'id': id
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },

    manage: function(id){
        var url = '/Admin/newsletter/manage?id=' + id;
        orion.xhr.load(url, 'content');
    },

    mails: function(newsId){
        var UserCamp = new OrionModalBox('UserCamp', {title:'Agregar Emails al Newsletter',width:500,height:600});
        var url = '/Admin/newsletter/mails?id=' + newsId;
        UserCamp.load(url);
    },

    cabecera: function(newsId){
        var UserCamp2 = new OrionModalBox('UserCamp2', {title:'Imagen de la cabecera',width:738,height:353});
        var url = '/Admin/newsletter/cabecera?id=' + newsId;
        UserCamp2.load(url);
    },

    cabeceraselector: function(newsId,imgId){
        $.post('/Admin/newsletter/cabeceraselector', {
            'newsId': newsId,
            'imgId' : imgId
        }, function(data){
            if(orion.forms.jsonResponse(data, 0) == true){
                var box = new OrionModalBox();
                box.close('UserCamp2');
            }

        }, 'json');
    },

    reset: function(newsId,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/newsletter/reset', {
                'id': newsId
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },

    sent: function(newsId,confirm_msg){
        if(confirm(confirm_msg)){
            $.post('/Admin/newsletter/sent', {
                'id': newsId
            }, function(data){
                if(orion.forms.jsonResponse(data, 0) == true){
                    orion.xhr.refresh();
                }
            }, 'json');
        }
    },

    noticias: function(newsId){
        
    }

}

$(document).ready(function() {
    // buscador de usuarios
    searchUsers();

});



