﻿$(document).ready(function() {
    //庫存頁面連結
    $("a[href*='BiHistPage.aspx']").colorbox({ width: "90%", height: "90%", iframe: true });
    $("a[href*='SocialHistPage.aspx']").colorbox({ width: "90%", height: "90%", iframe: true });
});

//String For ReplaceAll
String.prototype.ReplaceAll = function(stringToFind, stringToReplace) {

    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);

    }
    return temp;

}

//For_flot_Chart_BEGIN
function SetMarker(id, keyword) {
    $("#" + id).highlight(keyword);
}


//顯示 tooltip
function showTooltip(x, y, contents) {
    $('<div id="tooltip">' + contents + '</div>').css({
        position: 'absolute',
        display: 'none',
        top: y + 15,
        left: x + 15,
        border: '1px solid #fdd',
        padding: '2px',
        'background-color': '#fee',
        opacity: 0.80
    }).appendTo("body").fadeIn(200);
}

// 至少十位數
function L2(n) { return (n != null && n < 10 && n >= 0 ? "0" : "") + n }

function redirectToResult(url, date) {
    if (date) {
        location.href = url + "&date=" + date.getFullYear() + "" + L2((date.getMonth() + 1)) + "" + L2(date.getDate());
    }
}

// helper for returning the weekends in a period
function weekendAreas(axes) {
    var markings = [];
    var d = new Date(axes.xaxis.min);
    // go to the first Saturday
    d.setUTCDate(d.getUTCDate() - ((d.getUTCDay() + 1) % 7))
    d.setUTCSeconds(0);
    d.setUTCMinutes(0);
    d.setUTCHours(0);
    var i = d.getTime();
    do {
        // when we don't set yaxis the rectangle automatically
        // extends to infinity upwards and downwards
        markings.push({ xaxis: { from: i, to: i + 1 * 24 * 60 * 60 * 1000} });
        i += 7 * 24 * 60 * 60 * 1000;
    } while (i < axes.xaxis.max);

    return markings;
}

//
function generateChart(chartID,d,url) {

    var options = {
        points: { show: true },
        lines: { show: true,
            fillColor: "rgba(255,0,0,0.5)"
        },
        xaxis: { mode: "time",
            timeformat: "%m/%d"
        },
        grid: { color: "rgba(255,255,255,0.5)",
            hoverable: true,
            clickable: true
            ,
            markings: weekendAreas
        }
    };

    $.plot($(chartID), [d], options);

    // Hover Event (hoverable must enabled)
    $(chartID).bind("plothover", function(event, pos, item) {
        if (item) {
            $("#tooltip").remove();
            var date = item.datapoint[0],
            count = item.datapoint[1];

            var D = new Date(date);

            showTooltip(item.pageX, item.pageY, (D.getMonth() + 1) + "/" + D.getDate() + "日 共<b>" + count + "</b>則<i>(點我可以看當天新聞)</i>");
        }
        else {
            $("#tooltip").remove();
        }

    });

    // Click Event (Clickable must enable)
    $(chartID).bind("plotclick", function(event, pos, item) {
        if (item) {
            
            redirectToResult(url, new Date(item.datapoint[0]));
        }
    });
}

//For_flot_Chart_END