﻿var CurrentCategory = null;
var CurrentSubCategory = null;
var CurrentSorting = null;
var CurrentDirection = null;
var CurrentLanguage = null;
var UpdaterTimer = null;
var NotIndex = false;
var Emittents = new Array();
function InitWallStreet(opt) {
    CurrentCategory = opt.cat;
    CurrentLanguage = opt.lng;
    NotIndex = opt.notIndex;
    //проверяем категорию
    var pos = document.URL.indexOf("#");
    if (pos > -1) {
        CurrentCategory = document.URL.substring(pos + 1, document.URL.length);
    }
    $("ul.categories > li>a[altName='" + CurrentCategory + "']").parent().addClass("active");
    $("ul.categories > li>a").bind("click", function() {
        CurrentCategory = $.trim($(this).attr("altName"));
        CurrentSubCategory = null;
        if (NotIndex == true) {
            document.location = "/#" + CurrentCategory;
        }
        else {
            if (!$(this).parent().hasClass("active")) {
                $("ul.categories > li.active > ul").slideUp();
                $("ul.categories > li.active").removeClass("active");
                $(this).parent().addClass("active")
                $(this).parent().find(" > ul").slideDown();
                $("#ddlEmittent").val('');
                LoadEmittents();
            }

            StartAutoLoad(true);
        }
    });
    if (!NotIndex) {
        LoadEmittents();
        $("ul.categories > li>ul>li>a").bind("click", function() {
            CurrentSubCategory = $.trim($(this).attr("altName"));
            if (!$(this).parent().hasClass("active")) {
                $("ul.categories > li>ul>li.active").removeClass("active");
                $(this).parent().addClass("active")
                StartAutoLoad(true);
                $("#ddlEmittent").val('');
            }
        });
    }
    UpdateTotalBids();
    setInterval("UpdateTotalBids()", 50000);
}
function LoadData(options) {
    if (options) {
        if (options.sort) {
            CurrentSorting = options.sort;
            CurrentDirection = options.dir;
        }
    }
    $("#divDataTable").load("/page_modules/Data.aspx?" + GetFilter() + GetAntiCacheString(),
    function() {
        if ($("#txtWKN").val())
            FindBids($("#txtWKN").get(0));
        if ($("#ddlEmittent").val())
            FindBids($("#ddlEmittent").get(0));

    }
    );
    return false;
}
function GetFilter() {

    var result = "cat=" + CurrentCategory;
    if (CurrentSubCategory)
        result += "&cat2=" + CurrentSubCategory;
    if (CurrentLanguage)
        result += "&lng=" + CurrentLanguage;
    if (CurrentSorting)
        result += "&sort=" + CurrentSorting;
    if (CurrentDirection)
        result += "&dir=" + CurrentDirection;
    return result;
}
function GetAntiCacheString() {
    return "&aic=" + (new Date()).valueOf();
}
function StartAutoLoad(loadData) {
    StopAutoLoad();
    UpdaterTimer = setInterval("LoadData()", 60000);
    if (loadData)
        LoadData();
}
function StopAutoLoad() {
    if (UpdaterTimer)
        clearInterval(UpdaterTimer);
    delete UpdaterTimer;
    UpdaterTimer = null;
}
function FindBids(sender) {
    var text = $.trim(sender.value).toLowerCase();
    var rows = $("#divDataTable table").get(0).rows;
    if (sender.id == "txtWKN") {
        for (var i = 1; i < rows.length; i++) {
            if (!text || rows[i].cells[0].innerHTML.toLowerCase().match(text)) {
                rows[i].style.display = '';
            }
            else
                rows[i].style.display = 'none';
        }
    }
    else {
        var emitIndex = rows[0].cells.length - 1;
        for (var i = 1; i < rows.length; i++) {
            if (!text || rows[i].cells[emitIndex].innerHTML.toLowerCase().match(text)) {
                rows[i].style.display = '';
            }
            else
                rows[i].style.display = 'none';
        }
    }
}

function ShowProvider(sender) {
    var div = $("divProviders");
    if (div.length == 0)
        div = $("form").append('<div id="divProviders" style="display:none"></div>');
    $("#divProviders").load($(sender).attr("href"),
            function() {
                $("#divProviders").dialog(
                    {
                        title:ProvidersH1,
                        //buttons:                      { "Ok": function() {$(this).dialog("close");}},
                        closeText: 'Close',
                        closeOnEscape: true,
                        modal: false
                      , width: 400
                      , height: 300
                    }
                );
            }
     );
    return false;
}

function LoadEmittents() {
    var ddl = $("#ddlEmittent");
    ddl.html('');
    ddl.append("<option></option>");
    for (var i = 0; i < Emittents.length; i++) {
        var el = Emittents[i];
        if (el[1].indexOf(CurrentCategory, 0) > -1) {
            var add = true;
            var opts = ddl.find("option");
            for (var j = 0; j < opts.length; j++) {
                if (opts.eq(j).val() == el[0]) {
                    add = false;
                    break;
                }
            }
            if (add) {
                {
                    var opt = ddl.append("<option></option>").find("option:last");
                    opt.val(el[0]);
                    opt.text(el[0]);
                }
            }
        }
    }
}
function UpdateTotalBids() {
    $("#spanTotalBids").load("/handlers/totalbids.ashx?" + GetAntiCacheString());
}
