$(function() {
    $('a.nyroModalLink').click(function(e) {
        e.preventDefault();
        parent.$.nyroModalManual({
            url: $(this).attr('href')
        });
    });
  
    $(function(){
        if( $('body#results').length ){
            $.ajax({
                url: "/ajax/filters.php",
                data: 'path=' + encodeURIComponent(document.location.pathname),
                type:'post',
                success:function(_data){
                    $('div#filters img.loader').remove();
                    $('div#filters').append(_data);
                    $('#search_results #filters h1').click(function () {
                        if($(this).next('ul').css('display') == 'block') {
                            $(this).css('background-position', 'bottom');
                            $(this).next('ul').slideUp(800, 'easeInOutCirc');
                        }else{
                            $(this).css('background-position', 'top');
                            $(this).next('ul').slideDown(800, 'easeInOutCirc');    
                        }
                    });                    
                }
            });           
        }
    });
});  
  
function voting_init(_el)
{
   _el = $(_el); 
   
   if(_el.length)
   {
        load_new_item();

        $('#bestProducts .jaime').click(function () {
            loader_add(true);
            return false;
        });
        $('#bestProducts .jaime_pas').click(function () {
            loader_add(false);
            return false;
        });   
   }
}

var last_item_loaded = null
function callback_load_new_item(data)
{
    if(data.res!=1){
        $('#bestProducts .loader,#bestProducts .item').remove();
        return;
    }
    last_item_loaded = data;
    $('#bestProducts img.product').attr('src', data.img);
    $('#bestProducts img.product').attr('title', data.img);
    $('#bestProducts a.link').attr('href', data.link);
    $('#bestProducts h1 a.link').text(data.title);
    $('#bestProducts a.jaime').attr('href', data.link);
    
    _text = $('a.jaime img').attr('title');
    $('a.jaime').html( '<img src="/img/review_like.png" alt="+" title="' + _text.replace('"', '\\"') + '" /> ' + _text.replace('%NUM%', data.positif) );
    
    _text = $('a.jaime_pas img').attr('title');
    $('a.jaime_pas').html( '<img src="/img/review_nolike.png" alt="+" title="' + _text.replace('"', '\\"') + '" /> ' + _text.replace('%NUM%', data.negatif) );
    loader_remove();
}

function load_new_item()
{                                 
    $.getJSON("/ajax/request-voting.php", {}, callback_load_new_item);
}

function loader_add(_b_type) 
{   
    $('#bestProducts .loader').fadeIn(250);
    $('#bestProducts .item').fadeOut(250, function() {
        voting(_b_type);
    });    
}
    
function loader_remove() 
{        
    $('#bestProducts .loader').fadeOut(250);
    $('#bestProducts .item').fadeIn(250);        
}

function voting(_type)
{
    send_voting(last_item_loaded.p, _type);
    load_new_item();
}

function p_voting(_p, _type, _el)
{
    send_voting(_p, _type);
    $(_el).parent().find('a').each(function(){
        $(this)[0].onclick = function(event){event.preventDefault();event.stopPropagation();return false;};
    });
    
    
    

    _text = $(_el).text();
    _r = /\((\d+)\)/;
    _t_r = _r.exec(_text);
    _r2 = '';
    if( _t_r.length == 2 )
    {
        _r2 = parseInt(_t_r[1])+1;
    }
        
    
    if( $(_el).hasClass('jaime') ){
        $(_el).html('<img alt="+" src="/img/review_like.png"> ('+_r2+')');
    }
    else{
        $(_el).html('<img alt="-" src="/img/review_nolike.png"> ('+_r2+')');
    }
    
}

function send_voting(p, r)
{
    $.ajax({
      url: "/ajax/voting.php",
      data: 'p='+p+'&v='+(r?1:0),
      type:'post'
    });
}

function signal_product(_p, _el)
{
    $.ajax({
      url: "/ajax/flag.php",
      data: 'p='+_p,
      type:'post',
      success:function(){
          $(_el).text( $(_el).attr('rel') );
      }
    });    
    
}
