﻿var ActiveTabID = 'TrackListTab';
var ActiveTableID = 'TrackList';
IndexRowID = "SongNumber";
var ActiveTab;
var ActiveTable;
var CurrentPageType = "";
var CurrentPageSubType = "";
var winloc = window.location;
var uploadImageUrl = winloc.protocol + "//" + winloc.host + "/UploadImage.aspx";
var uploadTrackUrl = winloc.protocol + "//" + winloc.host + "/UploadTrackProcessor.aspx";

function ShowTab(TabID, sender) {
    var TabToDisplay = document.getElementById(TabID);
    if (TabToDisplay != null) {
        if (ActiveTab != null) {
            ActiveTable.style.display = 'none';
            ActiveTab.className = 'NavTab';
        }
        ActiveTab = sender;
        ActiveTable = TabToDisplay;
        ActiveTable.style.display = '';
        sender.className = 'ActiveNavTab';
    }
}
var CurrentPage = window.location.pathname.toLowerCase();
var flashDiv;
function loadTrackGrid() {
    var viewType = '';
    var LastRecordID = FirstRecordID + defaultPageSize;
    switch (CurrentPage) {
        case "/5ktemplate.aspx":
            HelloMusic.DL.GetScreenerSearchResults(strSearch, sortColumn, sortDir, FirstRecordID, LastRecordID, viewType, GetDataGrid_SucceededCallback);
            break;
        case "/5kreviewer.aspx":
            HelloMusic.DL.GetReviewerSearchResults(strSearch, sortColumn, sortDir, FirstRecordID, LastRecordID, viewType, GetDataGrid_SucceededCallback);
            break;
        case "/5kmoderator.aspx":
            HelloMusic.DL.GetModeratorSearchResults(strSearch, sortColumn, sortDir, FirstRecordID, LastRecordID, viewType, GetDataGrid_SucceededCallback);
            break;
        default:
            HelloMusic.DL.GetScreenerSearchResults(strSearch, sortColumn, sortDir, FirstRecordID, LastRecordID, viewType, GetDataGrid_SucceededCallback);
            break;
    }
//    flashDiv = document.getElementById("FlashDiv");
//    var AutoPlay = true;
//    window["CDBBPlayer"] = new Object();
//    var flashObj = new SWFObject("http://wilshiremedia.com/jpf/JPF_Player.swf", "CDBBPlayer", "320", "46", "8", "#ffffff");
//    flashObj.write(flashDiv);
//    if (CurrentPage == "/5kreviewer.aspx")
//        ShowRating('TrackRating');
    //SWFFormFix("CDBBPlayer");
}
function GetDataGrid_SucceededCallback(result) {
    if (result != null && result != "[]" && result != "") {
        if (InnerGrid == null) { InnerGrid = document.getElementById(InnerGridID); }
        var datasetIndex = result.indexOf('<NewDataSet>');
        var DataSet = result.substring(datasetIndex);
        var RawHeaderInfo = result.substring(0, datasetIndex);
        HeaderInfo = Sys.Serialization.JavaScriptSerializer.deserialize(RawHeaderInfo);
        if (!hasGeneratedHeader) {

            RenderDataTableHeader(HeaderInfo);
            hasGeneratedHeader = true;
        }
        if (window.ActiveXObject) {
            DataTable = new ActiveXObject("Microsoft.XMLDOM");
            DataTable.async = "false";
            DataTable.loadXML(DataSet);
        }
        else if (document.implementation.createDocument) {
            parser = new DOMParser();
            DataTable = parser.parseFromString(DataSet, "text/xml");
        }

        if (DataTable.documentElement != null) {
            DataGrid = document.getElementById(DataGridID);
            PageData = new Queue();

            var dt = DataTable.documentElement.childNodes
            var iRow = 0
            //firefox does not recognize the first node and needs to start at the second node.
            if (dt[iRow].childNodes.length == 0) {
                iRow++;
            }

            if (TotalRec == 0) {
                TotalRec = dt.length - 2;
            }
            if (iVisibleRows == 0)
                iVisibleRows = Math.round((InnerGrid.clientHeight == 0 ? parseInt(InnerGrid.style.height) : InnerGrid.clientHeight) / RowHeight);
            DataBind();
        }
        else {
            ClearDataTable();
        }
        if (TotalRec > iVisibleRows) {
            SetScrollSize(DataGrid);
            AttachScrollBarListener(true);
        }
    }
    else {
        ClearDataTable();
    }
}

function DataBind() {

    var dt = DataTable.documentElement.childNodes
    var iRowsTobind = (iVisibleRows < dt.length ? iVisibleRows : dt.length);
    if (iRowsTobind < iVisibleRows)
        iVisibleRows = iRowsTobind;

    for (var iRow = 1; iRow < iRowsTobind; iRow++) {

        //alert('loading..');
        if (dt[iRow].childNodes.length == 0) {
            continue;
        }
        else {
            BufferDT(iRow, dt[iRow], HeaderInfo, false);
        }
    }
    UpdateGridView();
}
function highlightRow(sender) {
    var dt = DataTable.documentElement.childNodes
    var iRowsTobind = (iVisibleRows < dt.length ? iVisibleRows : dt.length);
    if (iRowsTobind < iVisibleRows)
        iVisibleRows = iRowsTobind;
    var playingIncicatorID = "";
    for (var iRow = 0; iRow < iVisibleRows; iRow++) {
        var RowID = "GridRow" + iRow;
        var Row = document.getElementById(RowID);
        if (Row != null)
            Row.className = "gridRow";
            
        playingIncicatorID = "Col0" + iRow;
        var playingIncicator = document.getElementById(playingIncicatorID);
        if (playingIncicator != null)
            playingIncicator.className = "IconCellCol";
    }
    playingIncicatorID = sender.id.replace("SongTitle", "Col0")
    var NowPlayingIncicator = document.getElementById(playingIncicatorID);
    if (NowPlayingIncicator != null)
        NowPlayingIncicator.className = "ActiveIconCellCol";

    var Row;
    if (is.ie)
        Row = sender.parentElement;
    else
        Row = sender.parentNode;
    ActiveRow = Row;
    switch (CurrentPage) {
        case "/5ktemplate.aspx":
            Row.className = "SelectedRow";
            break;
        case "/5kreviewer.aspx":
            Row.className = "SelectedReviewerRow";
            break;
        case "/5kmoderator.aspx":
            Row.className = "SelectedModeratorRow";
            break;
    }
}
var RatingDisplaying;
var ActiveRow;
function ShowRating(id) {
    if (RatingDisplaying != null)
        RatingDisplaying.className = 'hiddenField';
    RatingDisplaying = document.getElementById(id);
    RatingDisplaying.className = (id == 'AlbumRating' ? 'AlbumRatingHolder' : 'TrackRatingHolder');
    var ReleaseButton = document.getElementById("ReleaseButtonText");
    var MiniReleaseInfo = document.getElementById("MiniReleaseInfo");
    var MiniReleaseDescription = document.getElementById("MiniReleaseDescription");

    if (id == "AlbumRating") {
        MiniReleaseDescription.className = "MiniReleaseDescription";
        MiniReleaseInfo.className = "MiniReleaseInfo";
        ReleaseButton.className = "HiddenInfo";
        if (DataTable != null) {
            var dt = DataTable.documentElement.childNodes
            var iRowsTobind = (iVisibleRows < dt.length ? iVisibleRows : dt.length);
            if (iRowsTobind < iVisibleRows)
                iVisibleRows = iRowsTobind;
            var playingIncicatorID = "";
            for (var iRow = 0; iRow < iVisibleRows; iRow++) {
                var RowID = "GridRow" + iRow;
                var Row = document.getElementById(RowID);
                if (Row != null)
                    Row.className = "gridRow";
            }
        }
    }
    else {
        MiniReleaseDescription.className = "MiniReleaseDescriptionButton";
        MiniReleaseInfo.className = "HiddenInfo";
        ReleaseButton.className = "ButtonText";
        if (ActiveRow != null) {
            highlightRow(ActiveRow);
        }
        else {
            for (var iRow = 0; iRow < iVisibleRows; iRow++) {

                playingIncicatorID = "Col0" + iRow;
                var playingIncicator = document.getElementById(playingIncicatorID);
                if (playingIncicator != null) {
                    if (playingIncicator.className == "ActiveIconCellCol") {
                        ActiveRow = document.getElementById(playingIncicator.id.replace("Col0", "SongTitle"));
                        highlightRow(ActiveRow);
                        break;
                    }
                }
            }
        }
    }
}
function setPick(sender) {
    var cbRateExeptional = document.getElementById("RateExeptional");
    if (sender == cbRateExeptional) {
    }
    //var cbTrackPick = ActiveRow.get
}
function ShowTextBox(sender) {
    var companionTextbox = document.getElementById(sender.id + 'txb');
    companionTextbox.className = (sender.checked == true ? "ExplainTXB" : "hiddenField");
}
function checkThis(sender) {
    var cbid = sender.id.replace("lbl", "");
    var cb = document.getElementById(cbid);
    cb.checked = !cb.checked;
    ShowTextBox(cb);
}

function cancelPrompt() {
    if (promptPanel != undefined) {
        promptPanel.className = "promptHidden";
    }
    if (blocker != undefined) {
        blocker.className = "promptHidden";
    }
}
