var prod_url,
    insurance_type,
    chartSeriesDataTable = [];

var insurance = {
  select: function(){
    $('#insurance_type').delegate('li', 'click', function(){
      $('#insurance_type').find('.selected').removeClass('selected');
      $(this).addClass('selected');
      $('#insurance_type').find('.itemhead').html($(this).html() + '<span>&nbsp;</span>');
      $('#ins_type_hidden').val($(this)[0].id);
      var itembody = $('#insurance_type').find('.itembody')[0];
      items.closeItem(itembody);
      insurance.updateAjax($(this)[0].id, prod_url);
    });
  },
  updateAjax: function(id, prod_url){
    prod_url = prod_url.replace('/0', '/'+id.replace('prod_', ''));
    $.ajax({
        dataType: 'json',
        url:      prod_url,
        context:  document.body,
        success:  insurance.updateSuccess
    })
  },
  updateSuccess: function(data){
    if (data.type == 0){
      alert(data.message);
    }
    else if (data.type == 1){
      $('#funds_of_insurance').html(data.template);
    }
  },
  sendAjaxLinechart: function(){
    ajaxloading.show('#fund_data');
    
    $('#form_fund_information').ajaxForm({
        dataType : 'json',
        success  : insurance.sendSuccessGetLinechart
    });
  },
  sendAjaxFundData: function(get_data_url){
    ajaxloading.show('#fund_data_items');

    $('#form_fund_information').ajaxSubmit({
        url      : get_data_url,
        dataType : 'json',
        success  : insurance.sendSuccessGetData
    });
  },
  sendSuccessGetLinechart: function(data){
    if (data.type == 0){
      $('#fund_data').html('<p id="send_message" class="message_error">'+data.message+'</p>');
    }
    else if (data.type == 1){
      $('#fund_data').html(data.template);

      insurance.sendAjaxFundData(data.data_url);
    }
  },
  sendSuccessGetData: function(data){
    if (data.type == 0){
      $('#fund_data_items').html('<p id="send_message" class="message_error">'+data.message+'</p>');
    }
    else if (data.type == 1){
      $('#fund_data_items').html(data.template);
      var checked_fund = $('#funds_of_insurance_inner').find('b.checked').length;
      for (var f=0; f<checked_fund; f++){
        $('#table_' + chartSeriesDataTable[f].id).dataTable({
          'bPaginate'     : false,
          'bLengthChange' : false,
          'bFilter'       : false,
          'bSort'         : false,
          'bInfo'         : false,
          'bAutoWidth'    : false,
          'bDestroy'      : true,
          'aaData'        : chartSeriesDataTable[f].data,
          'aoColumns'     : [
                      { 'sTitle' : unit_date  },
                      { 'sTitle' : unit_price },
                      { 'sTitle' : unit_nav   }
          ]
        });

        $('#table_' + chartSeriesDataTable[f].id + ' th:first').addClass('first');
        $('#table_' + chartSeriesDataTable[f].id + ' tr').each(function(){
          $(this).find('td:first').addClass('first');
        });
      }

      var p = 0;
      $('#funds_of_insurance_inner').find('p').each(function(){
        if ($(this).find('input').attr('checked')){
          $('#fund_data_items .' + $(this).children('input')[0].id + ' b').addClass($(this)[0].className);
        }
        p++;
      });
    }
  }
}

var funds = {
  hover: function(){
    $('#funds_of_insurance').delegate('p', 'mouseover', function(){
      $(this).addClass('hover');
    });
    $('#funds_of_insurance').delegate('p', 'mouseout', function(){
      $(this).removeClass('hover');
    });
  },
  labelClick: function(){
    $('#funds_of_insurance').delegate('label', 'click', function(){
      return false;
    });
  },
  selectAll: function(){
    $('#fmChbx-select_all').bind('click', function(){
      var select_all = this;
      if($(select_all).hasClass('on')){
        $('#funds_of_insurance p').each(function(){
          $(this).find('b').removeClass('checked');
          $(this).find('input').attr('checked', '');
        });
        $(select_all).removeClass('on');
      }
      else{
        $('#funds_of_insurance p').each(function(){
          $(this).find('b').addClass('checked');
          $(this).find('input').attr('checked', 'checked');
        });
        $(select_all).addClass('on');
      }
    });
  }
}


var ajaxloading = {
  show: function(el) {
    $(el).html('<img class="ajaxloading" src="/images/ajax-loader.gif" alt="Loading..." />');
  },
  close: function(el) {
    $(el).html('');
  }
}


var items = {
  anim: function(el, el_height){
    $(el).animate({
      height: el_height
    }, 500, 'linear');
  },
  elHeight: function(el){
    el_height_org = $(el).height();
    $(el).css('height', 'auto');
    el_height = $(el).height();
    $(el).css('height', el_height_org);
    return el_height;
  },
  animItem: function(){
    $('.box_items').delegate('.itemhead', 'click', function(){
      var item = $(this).parents('.item');
      var itembody = $(item).children('.itembody');
      if($(item).hasClass('closed')){
        items.openItem(itembody);
      }
      else{
        items.closeItem(itembody);
      }
    });
  },
  openItem: function(el){
    var item = $(el).parents('.item');
    $(item).removeClass('closed');
    items.anim(el, parseInt(items.elHeight(el)));
    if ($('#insurance_type').length && $(el).parent().parent()[0].id == 'insurance_type'){
      $('#insurance_type h3').html(insurance_type);
    }
  },
  closeItem: function(el){
    items.anim(el, 0);
    var item = $(el).parents('.item');
    $(item).addClass('closed');
    if ($('#insurance_type').length && $('ul.insurances li.selected').length && $(el).parent().parent()[0].id == 'insurance_type'){
      $('#insurance_type h3').html($('ul.insurances li.selected').text() + '<span>&nbsp;</span>');
    }
  },
  fastCloseItem: function(el){
    $(el).css('height', '0');
    var item = $(el).parents('.item');
    $(item).addClass('closed');
  }
}


var header = {
  sites: function(){
    $('.cig_sites_wrap').hover(
      function(){
        $('.cig_sites_body').css('height','121px');
        $('.cig_sites_body ul')
          .css('display','block')
          .animate({
            top: 0
          }, 100);
      },
      function(){
        $('.cig_sites_body ul').animate({
          top: -101
        }, 100, function(){
          $('.cig_sites_body').css('height','0px');
          $('.cig_sites_body ul').css('display','none');
        });
      }
    );
  }
}


$(document).ready(function(){
  header.sites();
  $('#fundinfo_submit').click(function(){ insurance.sendAjaxLinechart() });

  if($('form.jniceit').length) $('form.jniceit').NiceIt();
  if($('.box_items').length) items.animItem();

  insurance.select();

  funds.hover();
  funds.labelClick();
  funds.selectAll();

  if ($('#insurance_type')) insurance_type = $('#insurance_type h3').html();

  $('#form_fund_information p.block span.fmInput input').each(function(){
    $(this).keypress(function(){
      var options = $('#period select option');
      var last_option = options[options.length-1];
      $(last_option).attr('selected', 'selected');
      $('#interv strong span cite').html($(last_option).html());
    });
  });

  if ($('.insurances li').length == 1){
    var li = $('#insurance_type li');
    li.addClass('selected');
    $('#insurance_type').find('.itemhead').html(li.html() + '<span>&nbsp;</span>');
    $('#ins_type_hidden').val(li[0].id);
    var itembody = $('#insurance_type').find('.itembody')[0];
    $(itembody).css('height', '0');
    $('#insurance_type .item').addClass('closed');
    insurance.updateAjax(li[0].id, prod_url);
  }
});
