﻿/// <reference path="../jquery-1.5.js" />

var CurrentIndexCategories = new Array();
var CurrentIndexCategoryIndex = 0;
$(document).ready(function () {
    IndexesMarqueeListeners = new Array();
    var _xhr = $.post(
        Action("Indexes", "GetGoodNames"), 
        function (res) 
        {
            if (!res.Error) {
                for (var i = 0; i < res.GoodNames.length; i++) {
                    CurrentIndexCategories.push(res.GoodNames[i]);
                }
                //ShowIndexesTabs();
                ShowIndexesMarquee();
            }
        }
    );

    XHRS.push(_xhr);
});

function ShowIndexesMarquee() {
    var xhr = $.ajax({
        url: Action("Indexes", "_Marquee"),
        data: { GoodId: CurrentIndexCategories[CurrentIndexCategoryIndex].Id },
        success:
            function (res) {
                var desc = CurrentIndexCategories[CurrentIndexCategoryIndex].ShortName ? " <span style='font-weight:normal;font-size:11px'>(" + CurrentIndexCategories[CurrentIndexCategoryIndex].ShortName + ")</span>" : "";
                $("#marquee-goodname").html(CurrentIndexCategories[CurrentIndexCategoryIndex].Name);

                $("#marquee-container").html(res);

                $('#marquee-container marquee').marquee('pointer').mouseover(function () {
                    $(this).trigger('stop');
                }).mouseout(function () {
                    $(this).trigger('start');
                }).mousemove(function (event) {
                    if ($(this).data('drag') == true) {
                        this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
                    }
                }).mousedown(function (event) {
                    $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
                }).mouseup(function () {
                    $(this).data('drag', false);
                }).bind("end", function () {
                    ShowIndexesMarquee();
                });

                CurrentIndexCategoryIndex++;
                if (CurrentIndexCategoryIndex == CurrentIndexCategories.length) CurrentIndexCategoryIndex = 0;
            },
        error: ShowError
    });
}

//function ShowIndexesMarquee() {
//    var xhr = $.post(Action("Indexes", "GetCurrentIndexes"),
//    { GoodId: CurrentIndexCategories[CurrentIndexCategoryIndex].GoodId },
//    function (res) {
//        if (!res.Error) {
//            // Firing MarkqueeEnd Event
//            //            for (var i = 0; i < IndexesMarqueeListeners.length; i++) {
//            //                IndexesMarqueeListeners[i].OnMarqueeEnd && IndexesMarqueeListeners[i].OnMarqueeEnd(res);
//            //            }
//            CurrentIndexCategoryIndex++;
//            if (CurrentIndexCategoryIndex == CurrentIndexCategories.length) CurrentIndexCategoryIndex = 0;
//            var desc = res.GoodName == "W3" ? "(пшен. 3 кл.)" : res.GoodName == "W4" ? "(пшен. 4 кл.)" : res.GoodName == "B2" ? "(ячм. фураж.)" : res.GoodName == "SF" ? "(подсолн.)" : "";
//            $("#marquee-goodname").html(res.GoodName + " " + desc);
//            $("#marquee-container").html('<marquee scrollamount="2" direction="left" loop="1" behavior="scroll"></marquee>');
//            s = "<table><tr>";
//            for (var i = 0; i < res.Data.length; i++) {//
//                s += "<td class='index-cell'><span class='basis-name'>" + res.Data[i].Basis + " </span><span class='price'> " + res.Data[i].CurPrice + " </span>";
//                s += parseFloat(res.Data[i].Change) == 0 ? "" : parseFloat(res.Data[i].Change) > 0
//                    ? "<span style='color:green;font-size:11px;font-weight:bold'>" + res.Data[i].Change + "</span>" :
//                    "<span style='color:red;font-size:11px;font-weight:bold'>" + res.Data[i].Change + "</span>";
//                s += "</td>";
//            }
//            s += "</tr></table>";
//            $("#marquee-container marquee").append(s);
//            $('#marquee-container marquee').marquee('pointer').mouseover(function () {
//                $(this).trigger('stop');
//            }).mouseout(function () {
//                $(this).trigger('start');
//            }).mousemove(function (event) {
//                if ($(this).data('drag') == true) {
//                    this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
//                }
//            }).mousedown(function (event) {
//                $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
//            }).mouseup(function () {
//                $(this).data('drag', false);
//            }).bind("end", function () {
//                ShowIndexesMarquee();
//            });

//        }
//    });
//    //setTimeout(x.abort(), 3000);
//}
