/**
 * Created by IntelliJ IDEA.
 * User: romiras
 * Date: 14.10.2010
 * Time: 12:08:14
 * Для работы скрипта требуется функция getUrlParams из arsagera.js
 */

var funds = {    32087:"ОПИФСИ «Арсагера &ndash; фонд смешанных инвестиций»",
                 32088:"ЗПИФН «Арсагера - жилые дома»",
                 32089:"ЗПИФН «Арсагера &ndash; жилищное строительство»",
                 57919:"ОПИФА «Арсагера &ndash; фонд акций»",
                 62311:"ИПИФА «Арсагера &ndash; акции 6.4»"
             };
  /*
    Фильтрует фонды на странице
  */
  function filterFund(fund_id){
    setLink(fund_id);
    if (fund_id == 'all') jQuery('[class*="pif"]').show();
    else showFund(fund_id);
  }
  /*
    Метод изменяет линки на странице с переходами по датам
    чтобы сохранить текущий выбранный фонд
   */
  function setLink(fund_id){
    var curLink;
    var newLink;

    jQuery("#funds_selector option[value='notfund']").remove();

    if (!jQuery("#funds_selector option[value='"+fund_id+"']").length){
           jQuery("#notFundTransactions").show();
           jQuery("#funds_selector").append(jQuery("<option selected value=\""+fund_id+"\">"+funds[fund_id]+"</option>"));

    } else jQuery("#notFundTransactions").hide();

    jQuery.each(jQuery(".year_pay"), function() {
            curLink = jQuery(this).attr("href");
            var pos = curLink.indexOf("&fund_id");

            if (pos == -1) newLink = curLink+"&fund_id="+fund_id;
            else newLink = curLink.substr(0, pos)+"&fund_id="+fund_id;

            jQuery(this).attr("href", newLink);
            jQuery(".ico_prev").attr("href", newLink);
            jQuery(".ico_next").attr("href", newLink);
        });
    }

  function showFund(fund_id){
     jQuery('[class*="pif"]').hide();
     jQuery('.pif-'+fund_id).show();
     if (!jQuery('.pif-'+fund_id).length) jQuery("#notFundTransactions").show();
  }

  function getFundId(){
     // данный метод находится в файле arsagera.js
     var params = getUrlParams();
     return params['fund_id'];
  }

  function setFilter(){
     var fund_id = getFundId();
     if (!fund_id) return;
     jQuery("#funds_selector option[value='"+fund_id+"']").attr("selected", "selected");
     filterFund(fund_id);
  }

  function setSort(){  
    jQuery.ajax({url: "/transactions/setsort", success: function(){           
            var url = location.href;
            var fund_id = jQuery("#funds_selector option:selected").val();
            var pos = url.indexOf("fund_id");
            if (pos != -1) window.location = url.substr(0, pos)+"fund_id="+fund_id;
            else window.location = url+"?fund_id="+fund_id;
    }});
  }
