﻿$(document).ready(function () {
    var chartUrlFormats = new Array();
        chartUrlFormats["Minute"] = ["http://image2.sinajs.cn/newchart/min/n/{stockId}.gif?{time}"];
        chartUrlFormats["Daily"] = ["http://image2.sinajs.cn/newchart/daily/n/{stockId}.gif?{time}"];
        chartUrlFormats["Weekly"] = ["http://image2.sinajs.cn/newchart/weekly/n/{stockId}.gif?{time}"];
        chartUrlFormats["Monthly"] = ["http://image2.sinajs.cn/newchart/monthly/n/{stockId}.gif?{time}"];

    var popupQuoteDiv = $("#popup_quote")[0];
    var popupQuoteChart = $("#popup-quote-chart");
    var startTimeoutHidden = false;

    var fade = window.navigator.userAgent.toString().toLowerCase().indexOf("chrome") != -1 ||
               window.navigator.userAgent.toString().toLowerCase().indexOf("firefox") != -1;

    var changeChart = function (oButton) {
        $(".selected", ".popup-quote-bar").removeClass("selected");
        $(oButton).addClass("selected");
        var chartType = $(oButton).attr("ChartType");
        var chartUrlFormat = chartUrlFormats[chartType];
        var stockId = $("#popup_quote").attr('stockId');

        $(".popup-quote-content").empty();
        $(".popup-quote-content").addClass("loading");
        var imgCount = chartUrlFormat.length;
        for (var i = 0; i < chartUrlFormat.length; i++) {
            var imageUrl = chartUrlFormat[i].replace("{stockId}", stockId).replace("{time}", (new Date()).getTime());
            $(".popup-quote-content").append(loadImage(imageUrl, imgCount));
        }
    };
    var loadImage = function (imageUrl, imgCount) {
        var imgContainer = $("<div></div>");
        imgContainer.addClass("loading");
        var img = new Image();
        $(img).load(function () {
            imgContainer.append(this);
            if (--imgCount == 0)
                $(".popup-quote-content").removeClass("loading");
        }).attr('src', imageUrl);
        return imgContainer;
    };
    var refreshStockQuote = function () {
        var stockId = $("#popup_quote").attr('sinaStockId');
        try {
            GetStockQuotes(stockId, getStockQuoteComplete);
        }
        catch (error) { }
    };

    var getStockQuoteComplete = function (quotes) {
        var quote = quotes[0];
        $(".stock-name", ".popup-quote-info").text(quote.StockName);
        if (quote.IsSuspended) {
            $(".stock-price", ".popup-quote-info").text("停牌");
        }
        else {
            var priceFixedLength = quote.StockId == "sz399001" ? 0 : 2;
            var percentage = (Math.round((quote.Price - quote.Close) * 10000 / quote.Close) / 100).toFixed(2);
            if (quote.Price > quote.Close) {
                $(".stock-price", ".popup-quote-info").removeClass("price-green").addClass("price-red").text(quote.Price.toFixed(priceFixedLength));
                $(".stock-change", ".popup-quote-info").removeClass("price-green").addClass("price-red").text('+' + (quote.Price - quote.Close).toFixed(2));
                $(".stock-percentage", ".popup-quote-info").removeClass("price-green").addClass("price-red").text('+' + percentage + '%');
                $(".price_up_down_image", ".popup-quote-info").attr("src", "/Images/price_up_12_18_arrow.gif").css("visibility", "visible").removeClass("down").addClass("up");
            }
            else if (quote.Price < quote.Close) {
                $(".stock-price", ".popup-quote-info").removeClass("price-red").addClass("price-green").text(quote.Price.toFixed(priceFixedLength));
                $(".stock-change", ".popup-quote-info").removeClass("price-red").addClass("price-green").text((quote.Price - quote.Close).toFixed(2));
                $(".stock-percentage", ".popup-quote-info").removeClass("price-red").addClass("price-green").text(percentage + '%');
                $(".price_up_down_image", ".popup-quote-info").attr("src", "/Images/price_down_12_18_arrow.gif").css("visibility", "visible").removeClass("up").addClass("down");
            }
            else {
                $(".stock-price", ".popup-quote-info").removeClass("price-red").removeClass("price-green").text(quote.Price.toFixed(priceFixedLength));
                $(".stock-change", ".popup-quote-info").removeClass("price-red").removeClass("price-green").text((quote.Price - quote.Close).toFixed(2));
                $(".stock-percentage", ".popup-quote-info").removeClass("price-red").removeClass("price-green").text(percentage + '%');
                $(".price_up_down_image", ".popup-quote-info").css("visibility", "hidden");
            }
        }
    };
    var clearQuoteInfo = function () {
        $(".stock-name", ".popup-quote-info").text("");
        $(".stock-price", ".popup-quote-info").text("");
        $(".stock-change", ".popup-quote-info").text("");
        $(".stock-percentage", ".popup-quote-info").text("");
        $(".price_up_down_image", ".popup-quote-info").css("visibility", "hidden");
    };
    if (popupQuoteDiv != null) {
        $(".popup-quote-link").each(function () {
            $(this).hover(
                function () {
                    startTimeoutHidden = false;
                    var _this = $(this);
                    $("#popup_quote").attr('stockId', _this.attr("stockId"));
                    setTimeout(function () {
                        if ($("#popup_quote").attr('stockId') != _this.attr("stockId") || startTimeoutHidden)
                            return;
                        startTimeoutHidden = false;
                        var position = _this.offset();
                        popupQuoteDiv.style.left = position.left + _this.width() + "px";
                        var clientHeight = document.documentElement.clientHeight;
                        if (position.top + 360 > clientHeight)
                            popupQuoteDiv.style.top = clientHeight - 360 + "px";
                        else
                            popupQuoteDiv.style.top = position.top + "px";
                        clearQuoteInfo();
                        if (fade)
                            $("#popup_quote").fadeIn();
                        else
                            $("#popup_quote").css("display", "block");
                        $("#popup_quote").addClass("loading");
                            $("#popup_quote").attr('stockId', _this.attr("stockId"));
                        $("#popup_quote").attr('sinaStockId', _this.attr("stockId"));
                        changeChart($(".selected", ".popup-quote-bar"));
                        refreshStockQuote();
                        if (isTrading) {
                            _this.everyTime(5000, refreshStockQuote, 0);
                        }
                    }, 1500);
                },
                function (event) {
                    startTimeoutHidden = true;
                    if (popupQuoteDiv.style.display == "none") return;
                    startTimeoutHidden = true;
                    setTimeout(function () {
                        if (startTimeoutHidden == true) {
                            if (fade)
                                $("#popup_quote").fadeOut();
                            else
                                $("#popup_quote").css("display", "none");
                            $(this).stopTime();
                        }
                    }, 1000);
                }
            );

            $(popupQuoteDiv).hover(
                function () { startTimeoutHidden = false; popupQuoteDiv.style.display = "block"; },
                function (event) {
                    if (popupQuoteDiv.style.display == "none") return;
                    startTimeoutHidden = true;
                    setTimeout(function () {
                        if (startTimeoutHidden == true) {
                            if (fade)
                                $("#popup_quote").fadeOut();
                            else
                                $("#popup_quote").css("display", "none");
                            $(this).stopTime();
                        }
                    }, 1000);
                }
            );
        });

        $(document).click(function (event) {
            if (popupQuoteDiv.style.display == "none") return;
            else if ($(event.target).parents().index(popupQuoteDiv) > 0)
                return;

            popupQuoteDiv.style.display = "none";
            $(document).stopTime();
        });
    }

    $("#show_minute_chart").bind("click", function () {
        changeChart($(this));
    });

    $("#show_daily_chart").bind("click", function () {
        changeChart($(this));
    });

    $("#show_weekly_chart").bind("click", function () {
        changeChart($(this));
    });

    $("#show_monthly_chart").bind("click", function () {
        changeChart($(this));
    });
});
