var animDuration = 1000; // millisecond

function subtabscarousel_initCallback(carousel, state) {
  subtabs_carousel = carousel;
  if (state == 'init') {
    jQuery('#avanti').bind('click', function() {
      if (!carousel.animating && !$(this).hasClass('disabled')) {
        reset = '';
        if ($(this).hasClass('fine')) {
          $(this).parents('form').submit();
          return false;
        }
        var selected = getSelected();
        if (getSelectedTab().next().children().hasClass('last'))
          if (canGoOn(1))
            $(this).addClass('fine');
          else
            $(this).addClass('fine_dis');
        carousel.next();
        if (!canGoOn(1))
          $(this).addClass('disabled');
        $('#indietro').removeClass('hidden');
        return false;
      }
    });
    jQuery('#indietro').bind('click', function() {
      if (!carousel.animating && !$(this).hasClass('disabled')) {
        carousel.prev();
        if ($('#avanti').hasClass('fine') || $('#avanti').hasClass('fine_dis'))
          $('#avanti').removeClass('fine', 'fine_dis');
        var selected = getSelected();
        $('#avanti').removeClass('disabled').removeClass('hidden');
        if (selected == 1)
          $(this).addClass('hidden');
        return false;
      }
    });
  }
};
jQuery("#subtabs-carousel").jcarousel({
  scroll: 1,
  initCallback: subtabscarousel_initCallback,
  buttonNextHTML: null,
  buttonPrevHTML: null,
  easing: 'easeInOutExpo',
  animation: animDuration
});

$('input[type="text"]').each(function() {
  $(this).keyup(function() {
    if (canGoOn())
      $('#avanti').removeClass('disabled');
    else
      $('#avanti').addClass('disabled');
  });
  if ($(this).attr('title') == $(this).val()) {
    $(this).focus(function() {
      if ($(this).attr('title') == $(this).val())
        $(this).val('');
    });
  }
});

$('a#questionario').click(function() {
  subtabs_carousel.next();
  setTimeout("enableButtons()", 1000);
});

$('#subtabs-carousel').click(function() {
  if (!subtabs_carousel.animating && canGoOn()) {
    $('#avanti').removeClass('disabled');
    if ($('#avanti').hasClass('fine_dis'))
      $('#avanti').removeClass('fine_dis').addClass('fine');
  } else if (!subtabs_carousel.animating) {
    $('#avanti').addClass('disabled');
  }
});

cWidth = subtabs_carousel.list.parent().width();
function getSelected() {
  return Math.round(Math.abs(parseInt(subtabs_carousel.list.css('left').replace('px', '')))/cWidth);
}

function getSelectedTab(i) {
  if (i == undefined) i = 0;
  return $(subtabs_carousel.list.children('li').get(getSelected()+i));
}

function enableButtons() {
  $('#avanti').fadeIn('normal');
  $('#indietro').fadeIn('normal');
  $('#indietro').removeClass('hidden');
  $('#avanti').removeClass('hidden');
}

function canGoOn(i) {
  if (i == undefined) i = 0;
  var selected = getSelected();
  var tab = getSelectedTab(i);
  var done = true;
  if (tab.find('input[type="text"]').size() > 0) {
    tab.find('input[type="text"]').each(function() {
      if ($(this).prevAll('input').size() == 0)
        if ($(this).attr('id') == 'prov') {
          done = done && ($(this).val().length == 2 && jQuery.inArray($(this).val().toUpperCase(), provincie) >= 0);
          if ($(this).val().length == 2 && jQuery.inArray($(this).val().toUpperCase(), provincie) == -1)
            $(this).addClass('error');
          else 
            $(this).removeClass('error');
        } else {
          done = done && ($(this).val() != '');
        }
    });
  }
  if (tab.find('input[type="radio"]').size() > 0) {
    var done_radio = false;
    var names = new Array();
    tab.find('input[type="radio"]').each(function() {
      if (!(jQuery.inArray($(this).attr('name'), names) >= 0))
        names.push($(this).attr('name'));
    });
    jQuery.each(names, function(i) {
      done_radio = false;
      $('input[name="'+this+'"]').each(function() {
        if ($(this).attr('checked') && $(this).parent().children('input[type="text"]')) {
          var inptext = $(this).parent().children('input[type="text"]');
          if (inptext.attr('id') == 'peso_kg')
            done_radio = done_radio ||  (parseInt(inptext.val()) > 0 || parseInt(inptext.nextAll('input').val()) > 0);
          else 
            done_radio = done_radio ||  (inptext.val() != '');
        } else {
          done_radio = done_radio || $(this).attr('checked');
        }
      });
      done = done && done_radio;
    });
  }
  return done;
}

function switchItems(i,j) {
  i--;
  j--;
  var temp = $(subtabs_carousel.list.children().get(j)).children().clone(true);
  $(subtabs_carousel.list.children().get(j)).children().replaceWith($(subtabs_carousel.list.children().get(i)).children().clone(true));
  $(subtabs_carousel.list.children().get(i)).children().replaceWith(temp);
}

var reset = '';

$('#bambino_si').mouseup(function() { resetAll() });
$('#bambino_no').mouseup(function() {
  if (!$(this).hasClass('selected')) {
    switchItems(5,11);
    switchItems(6,12);
    switchItems(7,13);
    reset = "switchItems(5,11);switchItems(6,12);switchItems(7,13);";
  }
});

var seggiolino = '';
$('#pos_1, #pos_2, #pos_3').mouseup(function() {
  if (seggiolino === '') {
    seggiolino = false;
    switchItems(6,7);
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = "switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);switchItems(6,7);";
  } else if (seggiolino) {
    resetAll();
    seggiolino = false;
    switchItems(6,7);
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = "switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);switchItems(6,7);";
  }
});

$('#pos_4, #pos_5').mouseup(function() {
  if (seggiolino === '') {
    seggiolino = true;
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = "switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);";
  } else if (!seggiolino) {
    resetAll();
    seggiolino = true;
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = "switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);";
  }
});

var no_seggiolino = '';
$('.si_seggiolino').mouseup(function() {
  if (no_seggiolino === '') {
    no_seggiolino = false;
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = 'switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);';
  } else if (no_seggiolino) {
    resetAll()
    no_seggiolino = false;
    switchItems(7,8);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = 'switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(7,8);';
  }
});
$('.no_seggiolino').mouseup(function() {
  if (no_seggiolino === '') {
    no_seggiolino = true;
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = 'switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);';
  } else if (!no_seggiolino) {
    resetAll();
    no_seggiolino = true;
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    switchItems(8,9);
    switchItems(9,10);
    switchItems(10,11);
    switchItems(11,12);
    switchItems(12,13);
    reset = 'switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);switchItems(12,13);switchItems(11,12);switchItems(10,11);switchItems(9,10);switchItems(8,9);';
  }
});

function resetAll() {
  if (reset != '') {
    console.log(reset);
    eval(reset);
    reset = '';
  }
}

var provincie = ["AG" , "AL" , "AN" , "AQ" , "AR" , "AP" , "AT" , "AV" , "BA" , "BT" , "BL" , "BN" , "BG" , "BI" , "BO" ,
                 "BZ" , "BS" , "BR" , "CA" , "CL" , "CB" , "CI" , "CE" , "CT" , "CZ" , "CH" , "CO" , "CS" , "CR" , "KR" ,
                 "CN" , "EN" , "FM" , "FE" , "FI" , "FG" , "FC" , "FR" , "GE" , "GO" , "GR" , "IM" , "IS" , "LT" , "LE" , 
                 "LC" , "LI" , "LO" , "LU" , "MC" , "MN" , "MS" , "MT" , "VS" , "ME" , "MI" , "MO" , "MB" , "NA" , "NO" ,
                 "NU" , "OG" , "OT" , "OR" , "PD" , "PA" , "PR" , "PV" , "PG" , "PU" , "PE" , "PC" , "PI" , "PT" , "PN" , 
                 "PZ" , "PO" , "RG" , "RA" , "RC" , "RE" , "RI" , "RN" , "RM" , "RO" , "SA" , "SS" , "SV" , "SI" , "SR" , 
                 "SO" , "SP" , "TA" , "TE" , "TR" , "TO" , "TP" , "TN" , "TV" , "TS" , "UD" , "AO" , "VA" , "VE" , "VB" , 
                 "VC" , "VR" , "VV" , "VI" , "VT" ];