﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

$.chart = {
    reload: function() {
        $('div.chart img').attr('src', '/indices/chart/?' + new Date().getTime());
    }
};

$(function() {

    // Tabs
    // ----------------------------------------------------------------

    $('ul.head a').click(function(e) {
        e.preventDefault();
        $(this).parent('li').addClass('select').siblings('li').removeClass('select');

        if ($(this).attr('class') == 'rts') {
            $.cookie('Market.Tab', 'RTS', { expires: 365 });
            $(this).parent('li').parent('ul').siblings('ul.rts').show();
            $(this).parent('li').parent('ul').siblings('ul.micex').hide();
            $(this).parent('li').parent('ul').siblings('ul.funds').hide();
            $('div.interval li').show();
        } else if ($(this).attr('class') == 'micex') {
            $.cookie('Market.Tab', 'MICEX', { expires: 365 });
            $(this).parent('li').parent('ul').siblings('ul.rts').hide();
            $(this).parent('li').parent('ul').siblings('ul.micex').show();
            $(this).parent('li').parent('ul').siblings('ul.funds').hide();
            $('div.interval li').show();
        } else {
            $.cookie('Market.Tab', 'FUNDS', { expires: 365 });
            $(this).parent('li').parent('ul').siblings('ul.rts').hide();
            $(this).parent('li').parent('ul').siblings('ul.micex').hide();
            $(this).parent('li').parent('ul').siblings('ul.funds').show();
            $('div.interval a.i-w').parent('li').hide();
            $('div.interval a.i-m').parent('li').hide();
            $('div.interval a.i-q').parent('li').hide();
            var i = $.cookie('Market.Interval');
            if (i == 'w' || i == 'm' || i == 'q') {
                $.cookie('Market.Interval', '1', { expiration: 365 });
                $('div.interval a.i-1').addClass('selected');
            }
        }
    });

    // Checkboxes
    // ----------------------------------------------------------------

    $("div.indicesContent div.options input[type='checkbox']").click(function(e) {

        var symbols = $.cookie('Market.Symbols');
		
        if ($(this).attr('id').substring(0, 5) == 'cbRTS') {
            $("input[id^='cbMICEX']").filter("input[type='checkbox']").attr('checked', '');
            $("ul.funds input").filter("input[type='checkbox']").attr('checked', '');
        } else if ($(this).attr('id').substring(0, 7) == 'cbMICEX') {
            $("input[id^='cbRTS']").filter("input[type='checkbox']").attr('checked', '');
            $(".funds input[id^='cb']").filter("input[type='checkbox']").attr('checked', '');
        } else {
            $("input[id^='cbRTS']").filter("input[type='checkbox']").attr('checked', '');
            $("input[id^='cbMICEX']").filter("input[type='checkbox']").attr('checked', '');
        }

        if (symbols == null) {
            $('div.indicesContent div.options input').each(function() {
                if ($(this).attr('checked')) {
                    if (symbols == null) {
                        symbols = $(this).attr('id').substring(2);
                    } else {
                        symbols += ',' + $(this).attr('id').substring(2);
                    }
                }
            });
        } else {
            var arr = symbols.split(',');
            var newArr = new Array();
            var num = 0;
            if ($(this).attr('checked')) {
                newArr[++num] = $(this).attr('id').substring(2);
            }
            for (var i = 0; i < arr.length; i++) {
                if ($('#cb' + arr[i]).attr('checked')) {
                    newArr[++num] = arr[i];
                }
            }
            symbols = newArr.join(',');
        }
        $.cookie('Market.Symbols', symbols, { expiration: 365 });
        $.chart.reload();
    });

    // Interval
    // ----------------------------------------------------------------

    $('div.interval a').each(function() {
        var i = $(this).attr('class');
        i = i.substring(2).replace(' selected', '');
        $(this).data('interval', i);
    }).click(function(e) {
        e.preventDefault();
        $.cookie('Market.Interval', $(this).data('interval'), { expiration: 365 });
        $('div.interval a').removeClass('selected');
        $(this).addClass('selected');
        $.cookie('Market.Period', null);
        $('#startDate').val('');
        $('#endDate').val('');
        $.chart.reload();
    });

    // Period (dates)
    // ----------------------------------------------------------------

    $.datepicker.setDefaults($.extend({ showMonthAfterYear: false, changeMonth: true, changeYear: true }, $.datepicker.regional['ru']));

    $('#startDate').datepicker({ onSelect: function(dateText, inst) {
        var endDate = $('#endDate').val();
        if (endDate != '') {
            $.cookie('Market.Interval', null);
            $.cookie('Market.Period', dateText + '-' + endDate, { expiration: 365 });
            $('div.interval a').removeClass('selected');
            $.chart.reload();
        }
    }
    });
    $('#startDateIcon').click(function(e) {
        $('#startDate').datepicker('show');
    });

    $('#endDate').datepicker({ onSelect: function(dateText, inst) {
        var startDate = $('#startDate').val();
        if (startDate != '') {
            $.cookie('Market.Interval', null);
            $.cookie('Market.Period', startDate + '-' + dateText, { expires: 365 });
            $('div.interval a').removeClass('selected');
            $.chart.reload();
        }
    }
    });
    $('#endDateIcon').click(function(e) {
        $('#endDate').datepicker('show');
    });

    // Price Type
    // ----------------------------------------------------------------

    $('.priceType input').click(function(e) {
        $.cookie('Market.PriceType', $(this).attr('id') == 'rbPrice' ? 'Price' : 'Value');
        $.chart.reload();
    });
});
