$(document).ready(function()
{
  var actuals = '';
  var codeNums = new Array();
  var selected = new Array();
  var activeLinks = new Array();
  $('.dato').each(function()
  {
    id = parseInt($(this).attr('id').replace('dato_', ''));
    codeNums[id] = 0;
    selected[id] = new Array();
    activeLinks[id] = false;
  });

  $('.code_field').each(function()
  {
    id = parseInt($(this).attr('id')
                         .replace('codice_finitura_', '')
                         .replace('codice_colore_', '')
                         .replace('codice_ottica_', '')
                 );
    codeNums[id]++;
  });
  function getLinksId(elementId)
  {
    return parseInt($(elementId).attr('id')
             .replace('segna_appunti_', '')
             .replace('richiedi_info_', '')
             .replace('scheda_tecnica_', ''));
  }
  function isClickable(elementId)
  {
    return activeLinks[getLinksId(elementId)];
  }
  function checkComplete(id)
  {
    var done = 0;
    if (selected[id]['finitura'] == 1)
    { done++; }
    if (selected[id]['colore'] == 1)
    { done++; }
    if (selected[id]['ottica'] == 1)
    { done++; }
    return (codeNums[id] == done);
  }
  function getCode(id)
  {
    var code = '';
    $('.code_field_' + getLinksId('#' + id)).each(function()
    {
      if (!$(this).hasClass('isFinitura'))
      { code += '@'; }
      code += $(this).text();
    });
    return code;
  }

  function isInFavorites(code)
  {
    var pass = false;
    notesString = $('#notes_saved').text();
    notes = notesString.split('.');
    for (var i = 0; i < notes.length; i++)
    {
      if (code == notes[i])
      { pass = true; }
    }
    notes = actuals.split('.');
    for (var i = 0; i < notes.length; i++)
    {
      if (code == notes[i])
      { pass = true; }
    }
    return pass;
  }

  var prev = '';
  var defaultImg = '';
  var clicked = false;

  function doClick(element, name)
  {
    // Ricava il selettore dell'elemento
    id = parseInt($(element).parent().attr('id').replace('cambia_' + name + '_dato_', ''));
    sel = '#codice_' + name + '_' + id;

    $(sel).text($(element).children('span').text().replace(name + '_' + id + '_', ''))
          .css({textDecoration: 'none', backgroundColor: '#F5DEC5'})
          .animate({backgroundColor: '#FFF'}, 'slow');
    selected[id][name] = 1;
    activeLinks[id] = checkComplete(id);

    $(element).parent().children('.float_item').css('border', '1px solid transparent').removeClass('item_selected');
    $(element).css('border', '1px solid #C7C7C7').addClass('item_selected');
    if (isInFavorites(getCode('segna_appunti_' + id)))
    {
      $('#segna_appunti_' + id).children('img').attr('src', $('#segna_appunti_' + id).children('img').attr('src').replace('appunti.gif', 'ico_aggiunto.gif'));
    }
    else
    {
      $('#segna_appunti_' + id).children('img').attr('src', $('#segna_appunti_' + id).children('img').attr('src').replace('ico_aggiunto.gif', 'appunti.gif'));
    }
    clicked = true;
  }

  function doHoverOn(element, name)
  {
    id = $(element).parent().attr('id').replace('cambia_' + name + '_dato_', '');
    sel = '#codice_' + name + '_' + id;
    prev = $(sel).text();
    $(sel).text($(element).attr('id').replace(name + '_' + id + '_', ''))
          .css({textDecoration: 'none', fontWeight: 'bold'});
    if (!$(element).hasClass('item_selected'))
    { $(element).css('border-color', '#999'); }
    defaultImg = $('#segna_appunti_' + id).children('img').attr('src');
    if (isInFavorites(getCode('segna_appunti_' + id)))
    {
      $('#segna_appunti_' + id).children('img').attr('src', $('#segna_appunti_' + id).children('img').attr('src').replace('appunti.gif', 'ico_aggiunto.gif'));
    }
    else
    {
      $('#segna_appunti_' + id).children('img').attr('src', $('#segna_appunti_' + id).children('img').attr('src').replace('ico_aggiunto.gif', 'appunti.gif'));
    }
  }

  function doHoverOff(element, name)
  {
    id = $(element).parent().attr('id').replace('cambia_' + name + '_dato_', '');
    sel = '#codice_' + name + '_' + id;
    if (!$(element).hasClass('item_selected'))
    {
      $(sel).text(prev).css('text-decoration', 'none');
    }
    $(sel).css({fontWeight: 'normal'});
    if (!$(element).hasClass('item_selected'))
    { $(element).css('border-color', 'transparent'); }
    if (!clicked)
    {
      $('#segna_appunti_' + id).children('img').attr('src', defaultImg);
      defaultImg = '';
    }
    clicked = false;
  }

  $('.seleziona_finiture')
    .click(function()
    { doClick('#' + $(this).attr('id'), 'finitura'); })
    .hover(function()
    { doHoverOn('#' + $(this).attr('id'), 'finitura'); },
    function()
    { doHoverOff('#' + $(this).attr('id'), 'finitura'); });
  $('.finiture .the_one_and_only').each(function()
    { doClick('#' + $(this).attr('id'), 'finitura'); });

  $('.seleziona_colori')
    .click(function()
    { doClick('#' + $(this).attr('id'), 'colore'); })
    .hover(function()
    { doHoverOn('#' + $(this).attr('id'), 'colore'); },
    function()
    { doHoverOff('#' + $(this).attr('id'), 'colore'); });

  $('.seleziona_ottiche')
    .click(function()
    { doClick('#' + $(this).attr('id'), 'ottica'); })
    .hover(function()
    { doHoverOn('#' + $(this).attr('id'), 'ottica'); },
    function()
    { doHoverOff('#' + $(this).attr('id'), 'ottica'); });
  $('.ottiche .the_one_and_only').each(function()
    { doClick('#' + $(this).attr('id'), 'ottica'); });

  $('.segna_appunti').click(function()
  {
    if (!isClickable('#' + $(this).attr('id')))
    {
      alert($('#super_hidden_div').text());
      return false;
    }
    if (!isInFavorites(getCode($(this).attr('id'))))
    {
      myUrl = $('#add_notes_super_hidden_div').text() + '/main/addNote/code/' + getCode($(this).attr('id')) + '/idDato/' + $(this).attr('id').replace('segna_appunti_', '');
      $.post(myUrl, function(data)
      {
        $('#appunti a').text(data);
      });
      $(this).children('span').css('color', '#F5DEC5').animate({color: '#000'}, 'slow');
      $(this).children('img').attr('src', $(this).children('img').attr('src').replace('appunti.gif', 'ico_aggiunto.gif'));
      actuals += '.' + getCode($(this).attr('id'));
    }
    else
    {
      alert($('#already_inserted').text());
    }
  });

  $('.richiedi_info').click(function()
  {
    if (!isClickable('#' + $(this).attr('id')))
    {
      alert($('#super_hidden_div').text());
      return false;
    }
    myUrl = 'http://www.linealight.com/area_info.php?lang='
            + $('#lang_box').text() + '&traddel_cod='
            + getCode($(this).attr('id')).replace('@', '-').replace('@', '-');
    window.open(myUrl);
  
  });

  $('.scheda_tecnica').click(function()
  {
    if (!isClickable('#' + $(this).attr('id')))
    {
      alert($('#super_hidden_div').text());
      return false;
    }
    code = getCode($(this).attr('id'));
    code = code.replace('@', '_').replace('@', '_');
    id = $(this).attr('id').replace('scheda_tecnica_', '');
    myUrl = '/traddel/web/pdf/' + id + '/' + code + '.pdf';
    if ($.browser.msie)
    {
      window.location.href = myUrl;
    }
    else
    {
      window.open(myUrl);
    }
  });

});
