
var SU = SU || {};

SU.arfolyam = SU.arfolyam || Class.create({
    
    initialize: function () {
        // UNIX timestamp formaban
        this.nDatumig = 0;
        this.nDatumtol = 0;
        this.nErtekZaro = 0;
        this.nErtekNyito = 0;
    },

    initChart: function () {
        this.nDatumtol = $("datumtol").value;
        this.nDatumig = $("datumig")[$("datumig").length-1].value;
        var oChartSWF = 
            (navigator.appName.indexOf("Microsoft") != -1) ? window["linechart"] : document["linechart"];
        this.nErtekNyito = this.formatNumber(oChartSWF.getChartValue(0, this.nDatumtol));
        this.nErtekZaro = this.formatNumber(oChartSWF.getChartValue(0, this.nDatumig));    
    },
         
    updateChartClick: function () {
        this.nDatumig = $F("datumig");
        this.nDatumtol = $F("datumtol");
        var oChartSWF = 
            (navigator.appName.indexOf("Microsoft") != -1) ? window["linechart"] : document["linechart"];
        oChartSWF.zoomToDate(this.nDatumtol, this.nDatumig);
    },
    
    printValues: function () {
        $("cim_datumtol").update(this.convertTimestamp(this.nDatumtol));
        $("cim_datumig").update(this.convertTimestamp(this.nDatumig));
        $("ertek_nyito").update(this.nErtekNyito);
        $("ertek_zaro").update(this.nErtekZaro);
        $("hozam_eves").update(this.formatNumber(this.calculateAnnualYield()));
        $("hozam_idoszak").update(this.formatNumber(this.calculatePeriodicReturn()));
    },
    
    updateChartZoom: function () {
        var oChartSWF = 
            (navigator.appName.indexOf("Microsoft") != -1) ? window["linechart"] : document["linechart"];
        this.nDatumtol = oChartSWF.getStartDate();
        this.nDatumig =  oChartSWF.getEndDate(); 
        this.nErtekNyito = this.formatNumber(oChartSWF.getChartValue(0, this.nDatumtol));
        this.nErtekZaro = this.formatNumber(oChartSWF.getChartValue(0, this.nDatumig));
    },
    
    printValuesZoom: function () {
        $("ertek_nyito").update(this.nErtekNyito);
        $("ertek_zaro").update(this.nErtekZaro);
        $("cim_datumtol").update(this.convertTimestamp(this.nDatumtol));
        $("cim_datumig").update(this.convertTimestamp(this.nDatumig));
        $("hozam_eves").update(this.formatNumber(this.calculateAnnualYield()));
        $("hozam_idoszak").update(this.formatNumber(this.calculatePeriodicReturn()));
        var that = this;
        [{date: this.nDatumtol, object: $('datumtol')}, 
            {date: this.nDatumig, object: $('datumig')}].each(function(aItem) {
            for (var i=0; i<aItem.object.length; i++)
                aItem.object[i].selected = (aItem.object[i].value == aItem.date) ? true : false;
        });
    },

    clearValues: function () {
        $("ertek_nyito").update("-");
        $("ertek_zaro").update("-");
        $("cim_datumtol").update("-");
        $("cim_datumig").update("-");
        $("hozam_eves").update("-");
        $("hozam_idoszak").update("-");
    },

    calculatePeriodicReturn: function () {
        return this.roundNumber((this.nErtekZaro/ this.nErtekNyito- 1)*100, 7);
    },
      
    calculateAnnualYield: function () {
        var nDaysBetween = Math.round((parseInt(this.nDatumig, 10) - parseInt(this.nDatumtol, 10))/3600/24);
        return  this.roundNumber((Math.pow(parseFloat(this.nErtekZaro)/ parseFloat(this.nErtekNyito),365/ nDaysBetween)-1)*100,7);    },
    
    convertTimestamp: function (nTimestamp) {
        var oDate = this.convertTimestampCore(nTimestamp);
        return oDate["year"]+ "."+ oDate["month"]+ "."+ oDate["day"];
    },
    
    convertTimestampCore: function (nTimestamp) {
        var oDate = new Date(nTimestamp*1000);    
        var sMonth =  (oDate.getMonth()+1);
        var sDay = oDate.getDate();
        var sYear = oDate.getFullYear();
        sMonth = (sMonth<10) ? "0"+ sMonth : sMonth;
        sDay = (sDay<10) ? "0"+ sDay : sDay;
        return {year: sYear, month: sMonth, day: sDay};
    },

    roundNumber: function (nNumber, nDecimals) {
    	return Math.round(nNumber* Math.pow(10, nDecimals))/ Math.pow(10, nDecimals);
    },
    
    formatNumber: function (nNumber) {
        return nNumber.toFixed(4);
    }
});

/*var aLoad = function (sSelect, sFund) 
{
    var sFile;
    if (sSelect == "j") 
        sFile = "jelentesek.php";
    if (sSelect == "i") 
        sFile = "ismertetok.php";
    window.open(sFile+"?sEx="+sFund, "_new", "resizable=0,scrollbars=1,toolbar=0,location=0,menubar=0,status=0,width=555,height=500");
};


var aLoadPdf = function (sFile) 
{
    window.open(sFile, "_new", "resizable=1,scrollbars=1,toolbar=0,location=0,menubar=0,status=0,width=830,height=600");
};


var aLoadSzotar = function () 
{
    window.open("szotar.php", "_new", "resizable=1,scrollbars=1,toolbar=0,location=0,menubar=0,status=0,width=600,height=600");
};
*/
