jQuery(function($){


 $('.product-left img').hover(
    function(e){
      if ($(this).attr('src').indexOf('prodotto') == -1) {
        $(this).clone().attr('id', 'imgHover')
          .css({ top: (e.pageY + -100)+'px', left: (e.pageX + 10)+'px'})
          .appendTo('body');
      }
    },
    function(e){
      $('#imgHover').remove();
    }
 );

  //
  // Eprice
  //
  (function(){
    var __searchInProgress = false;

    $('a.startEprice').click(function(e) {
      var self = this;
      var novelli_price = Number($(this).attr('price'));

      e.preventDefault();

      if (__searchInProgress) {
        alert('Un\'altra ricerca è attualmente in corso...');
        return false;
      }

      __searchInProgress = true;
      $('<div id="epriceLoader" class="eprice">Attendere prego...'+
        '<img src="/themes/radionovelli2/immagini/ajax-loader.gif" /></div>')
      .appendTo(this);
      $.get(
        'http://www.radionovelli.it/_ajaxproxy.php?op=epriceSearch&q='+ $(this).attr('title'), {},
        function(output) {
          var _timer = setInterval(
            function () {
              var pos = output.indexOf('<li>END');
              if (output && pos != -1) {
                var id_ricerca = Number(/END\:\ ([0-9]+)/.exec(output.substr(pos))[1]);
                clearInterval(_timer);
                __searchInProgress = false;
                $('#epriceLoader').remove();
                if (isNaN(id_ricerca)) return;
                $.get(
                  'http://www.radionovelli.it/_ajaxproxy.php?op=epriceGetPrice&id_ricerca='+ id_ricerca, {},
                  function(data) {
                    if (data.prices[0] > 0) {
                      var eprice = data.prices[0] - 1.00;
                      if (eprice < (novelli_price - (novelli_price * 15 / 100))) {
                        eprice = novelli_price;
                      }
                      eprice = (Math.round(Math.abs(eprice) * 100)/100).toFixed(2).toString();
                      $(self).after('<div class="epriceResult">prezzo €'+ eprice +'</div>');
                      $(self).remove();
                    }
                    else {
                      // window.open('http://eprice.radionovelli.it?posted=true&search='+ $(self).attr('title'));
                      window.open('http://www.radionovelli.it/eprice');
                    }
                  },
                  'json'
                );
                return;
              }
            }
          , 500);
        },
        'text'
      );

      return false;
    });

  })(); // fine Eprice

});
