function log(e)
{
  //return false;
  if(typeof(console)=='object') console.log(e);
  else alert(e);  
}

var blank = {
  config:
  {
    wrapperId: '#wrapper'
  },
  init: function()
  {
    if(!$(this.config.wrapperId).length) return false;
  }
}


var themeBox = {
  config:
  {
    wrapperId: '#themeBox'
  },
  init: function()
  {
    if(!$(this.config.wrapperId).length) return false;
    $(this.config.wrapperId+' a').hover(function(){ themeBox.actLink($(this)); },function(){ themeBox.deactLink(); });
    $(this.config.wrapperId+' ul').addClass('active');
  },
  actLink: function(el)
  {
    $(this.config.wrapperId+' a').addClass('not');
    $(el).removeClass('not');
  },
  deactLink: function()
  {
    $(this.config.wrapperId+' a').removeClass('not');
  }
}

// pridat stranku do zaloziek
function AddFavorite(linkObj,addUrl3,addTitle3)
{
     if (document.all && !window.opera){
         window.external.AddFavorite(addUrl3,addTitle3);
         return false;
     }
     else if (window.opera && window.print)     {
         linkObj.title = addTitle3;
         return true;
     }
     else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
         window.sidebar.addPanel(addTitle3,addUrl3,'');
         return false;
     }
     window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k Vašim oblíbeným odkazům.');
     return false;
}
/*
<a href="#" onclick="return AddFavorite(this,'http://<?=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].($_GET['lang']?'?lang='.$_GET['lang']:'')?>','FFS Czech - <?=$title?>');">Přidat do oblíbených</a>
*/


var calcRPSN = {

    addPayment: function ()
    {
    	$('input[name^=cost]:last').after('<input name="cost[]" type="text" class="text" />');
    	$('input[name^=days]:last').after('<input name="days[]" type="text" class="text" />');
    	return false;
    },
    
    addAmount: 0,

    getFloat: function(str){
       return  parseFloat(str.replace(/,/,"."))  ;
    },
  
    fvalue: function (amount,refund,fullCount,countPerYear,pivot){
      calcRPSN.addAmount = 0;
  	  $('input[name^=cost]').each(function(key){
    	    cost = $('input[name^=cost]:eq('+key+')').get(0).value;
    	    date = $('input[name^=days]:eq('+key+')').get(0).value;
     		 	calcRPSN.addAmount = calcRPSN.addAmount + cost / Math.pow(1 + pivot , date / 365.25);
  		});
  		if (pivot==0)
      { 
         return(refund*fullCount-amount+calcRPSN.addAmount);
      }
  	  else  
  	  {
         return((refund/Math.pow(1+pivot,1/countPerYear))*(1/Math.pow(1+pivot,fullCount/countPerYear)-1)/(1/Math.pow(1+pivot,1/countPerYear)-1)-amount+calcRPSN.addAmount);
      }
  	},
  
    recount: function (){
        
        $('#calcResult').removeClass('hidden'); 
                     
        amount = $('input[name=amount]').attr('value');    
        refund = $('input[name=refund]').attr('value'); 
        payments = $('input[name=payments]').attr('value'); 
        period = $('select[name=period]').attr('value'); 

  	    differenceStr = 0;
  	    est1 = 0; 
  	    est2 = 10.001; 
  	    fvalue1 = calcRPSN.fvalue(amount,refund,payments,period,est1);
  	    fvalue2 = calcRPSN.fvalue(amount,refund,payments,period,est2);
  	    if (fvalue1*fvalue2<0) 
  	    {            
            estStr = (est1 + est2)/2;
            fvaluestr = this.fvalue(amount,refund,payments,period,estStr);
        
            do {
               if ((fvalue1*fvaluestr)>0) est1 = estStr;
               if ((fvalue2*fvaluestr)>0) est2 = estStr;
               fvalue1 = this.fvalue(amount,refund,payments,period,est1);
               fvalue2 = this.fvalue(amount,refund,payments,period,est2);
               differenceStr = Math.abs(estStr - (est1 + est2)/2);
               estStr = (est1 + est2)/2;
               fvaluestr = this.fvalue(amount,refund,payments,period,estStr);
            } 
            while (differenceStr>0.000001);    
            $('p#calcResult span').text(Math.round(estStr*10000)/100+"%"); 
  	    }
  	    else if (fvalue1 == 0) 
            $('p#calcResult span').text("0%");
	      else 
            $('p#calcResult span').text("Chyba");
       
       return false;
    }


}


$(document).ready(function() {     

  themeBox.init();
  $('#cjtRPSN').hide();
  
})
