﻿var _divPreview;
var _dataMartId;
var _dataFilter;

/*
 * Displays a preview of the nominated data set
 */
function Preview(dataMartId, dataFilter) {
    // Ignore the request if a preview is loading
    if (xmlHttp) {
        alert("Please wait until the preview has loaded.");
        return;
    }

    BuildPreviewContainer();
    
    // Store the data mart id and data filter for later
    _dataMartId = dataMartId;
    _dataFilter = dataFilter;

    var url = "Handlers/DataHandler.ashx?Service=preview&DataMartId=" + dataMartId + "&DataFilter=" + dataFilter;
    
    SendXmlHttpRequest(url, OnPreviewLoaded, OnError);
}

/*
 * Build and position the container
 */
function BuildPreviewContainer() {
    // Hide existing preview
    if (_divPreview) {
        HidePreview();
    }
    
    // Remove previous preview
    var oldPopup = document.getElementById("divPreview")
    if (oldPopup) {
        oldPopup.parentNode.removeChild(oldPopup);
    }

    var html = GetProgressImage("Loading preview...", null, "text-align:center;margin:2em;");
    _divPreview = BuildInlinePopup("divPreview", html, null);
    _divPreview.className = "DataPreview";
    
    // Center the box. Default width/height is 640x480 (configured in stylesheet).
    var viewport = CalculateViewport();
    _divPreview.style["left"] = parseInt((viewport[0] / 2) - 320) + "px";
    _divPreview.style["top"] = parseInt((viewport[1] / 2) - 240 + GetScrollTop()) + "px"
    
    ShowInlinePopupByRef(_divPreview, true);
}

function OnPreviewLoaded(responseText) {
    var q;
    QueryString("q") ? q = "q=" + QueryString("q") + "&" : q = "";
    var hrefData = "Data.aspx?" + q + "d=" + _dataMartId + "&f=" + _dataFilter;
    var hrefBrowse = "Explorer.aspx?d=" + _dataMartId + "&f=" + _dataFilter;

    var html = "<a href=\"javascript:;\" onclick=\"HidePreview()\" title=\"Close\"><img src=\"_Images/Close.gif\" alt=\"Close\" class=\"X\" /></a>";
    
    html += "<div class=\"Meta\">";
    html += "<h2>" + GetResponse("SeriesName") + "</h2>";
    html += "Source:&nbsp;" + GetResponse("DataSetName") + "&nbsp;|&nbsp;" + GetResponse("DataSetOwner");
    html += "</div>";
    html += "<div class=\"Records\">Displaying first " + GetResponse("PageSize") + " of " + GetResponse("RecordCount") + " records.</div>";
    
    html += "<div class=\"DataContainer1\">" + responseText + "</div>";
    
    // Styles for IconBar are in Global.css
    html += "<div class=\"IconBar Links\">";
    html += "<span><a href=\"" + hrefData + "\"><img src=\"_Images/IconTable.png\" alt=\"View data\" /></a>&nbsp;";
    html += "<a href=\"" + hrefData + "\">View data</a></span>";
    //html += "<span><a href=\"" + hrefBrowse + "\"><img src=\"_Images/IconBrowse.png\" alt=\"Explore\" /></a>&nbsp;";
    //html += "<a href=\"" + hrefBrowse + "\">Explore</a></span>";
    //html += "<span><a href=\"javascript:;\" onclick=\"HidePreview();\">Hide preview</a></span>";
    html += "</div>";

    _divPreview.innerHTML = html;
    DiscardXmlHttpRequest();
}

function HidePreview() {
    HideInlinePopup("divPreview", true, true);
    _divPreview = null;
}

function OnError(errorText) {
    _divPreview.innerHTML = BuildMessageBox(errorText, "error");
}
