
function SourceViewManager()
{
}

SourceViewManager.prototype.loadHandler = function(plugIn, userCtx, argLst) {
    this.plugIn = plugIn;
    this.plugIn.content.onResize = Utils.createDelegate(this, this.resizeHandler);
    this.showLineNumbers = true;

    this.wLineNums = "list-style-type: decimal; margin-left: 60px; padding-left: 10px; padding-right: 30px;";
    this.woLineNums = "list-style-type: none; margin-left: 30px; padding-left: 10px; padding-right: 30px;";

    this.titleText = this.plugIn.content.findName("TitleText");
    this.srcViewDiv = window.document.getElementById('htmlSourceView');
    this.respZip = null;

    this.setSourceProgress(0);
    var downLoader = this.plugIn.CreateObject("downloader");
    var projectFile = "Files/" + this.projectID + ".htm.zip";
    downLoader.AddEventListener("DownloadProgressChanged", Utils.createDelegate(this, this.progressHandler));
    downLoader.AddEventListener("DownloadFailed", Utils.createDelegate(this, this.downloadFailedHandler));
    downLoader.AddEventListener("Completed", Utils.createDelegate(this, this.completedHandler));
    downLoader.Open("GET", projectFile);
    downLoader.Send();
};

SourceViewManager.prototype.downloadFailedHandler = function(sender, args) {
    this.setSourceTitle("Loading of " + this.projectID + " Source Failed");
};

SourceViewManager.prototype.progressHandler = function(sender, args) {
    this.setSourceProgress(Math.floor(sender.DownloadProgress * 100));
};

SourceViewManager.prototype.completedHandler = function(sender, args) {
    this.respZip = sender;
    this.showProjectList();

    this.cabinetButton = new SourceViewButton(this.plugIn, "CabinetBtn", Utils.createDelegate(this, this.showProjectList));
    this.lineNumButton = new SourceViewButton(this.plugIn, "LineNumBtn", Utils.createDelegate(this, this.toggleLineNumbers));
    this.printButton = new SourceViewButton(this.plugIn, "PrinterBtn", Utils.createDelegate(this, this.printHandler));
};

SourceViewManager.prototype.showProjectList = function() {
    this.setSourceTitle(this.projectID);
    this.setSourceFile(this.projectID + ".htm");
    this.setCSSFile();
};

SourceViewManager.prototype.setSourceProgress = function(percentComplete) {
    this.setSourceTitle("Loading " + this.projectID + " source (" + percentComplete + "%) ...");
};

SourceViewManager.prototype.setSourceTitle = function(sourceTitle) {
    this.titleText.Text = sourceTitle;
};

SourceViewManager.prototype.setSourceFile = function(fileName) {
    var treeDoc = this.respZip.GetResponseText(fileName);
    this.srcViewDiv.innerHTML = treeDoc;
    this.scrollToTopOfSource();
};

SourceViewManager.prototype.setCSSFile = function(fileName) {
    var cssDoc = "";
    if (fileName != null)
        cssDoc = this.respZip.GetResponseText(fileName);
    var styleSheet = document.styleSheets[2];
    styleSheet.cssText = cssDoc;
};

SourceViewManager.prototype.scrollToTopOfSource = function() {
    this.srcViewDiv.scrollLeft = 0;
    this.srcViewDiv.scrollTop = 0;
};

SourceViewManager.prototype.toggleLineNumbers = function() {
    this.showLineNumbers = !this.showLineNumbers;
    var styleSheet = document.styleSheets[1];
    styleSheet.removeRule(styleSheet.rules.length - 1);
    styleSheet.addRule("li.li1", this.showLineNumbers ? this.wLineNums : this.woLineNums);
    styleSheet = document.styleSheets[0];
    styleSheet.removeRule(styleSheet.rules.length - 1);
    styleSheet.addRule("li.li1", this.showLineNumbers ? this.wLineNums : this.woLineNums);
};

SourceViewManager.prototype.printHandler = function() {
    window.print();
};

SourceViewManager.prototype.resizeHandler = function(sender, args) {
    this.updateLayout();
};

SourceViewManager.prototype.updateLayout = function() {
    var viewWidth = this.plugIn.content.actualWidth;
    var viewHeight = this.plugIn.content.actualHeight;
    if (viewWidth < 300)
        viewWidth = 300;
    if (viewHeight < 300)
        viewHeight = 300;

    var viewBox = this.plugIn.content.findName("SourceView");
    var titleBox = this.plugIn.content.findName("SourceTitle");
    var buttonsBox = this.plugIn.content.findName("ViewButtons");
    var srcViewDiv = window.document.getElementById('htmlSourceView');
    if ((viewBox != null) && (titleBox != null) && (srcViewDiv != null) && (buttonsBox != null)) {
        var marginTop = 60, marginSides = 30;

        viewBox.width = viewWidth - marginSides;
        viewBox.height = viewHeight - (marginTop / 2 + marginSides / 2);
        viewBox["Canvas.Left"] = marginSides / 2;
        viewBox["Canvas.Top"] = marginTop / 2;
        viewBox.RadiusX = marginSides / 2;
        viewBox.RadiusY = marginSides / 2;

        titleBox.width = viewWidth - (2 * marginSides);
        titleBox["Canvas.Left"] = marginSides;

        srcViewDiv.style.top = marginTop;
        srcViewDiv.style.left = marginSides;
        srcViewDiv.style.width = viewWidth - (2 * marginSides);
        srcViewDiv.style.height = viewHeight - (marginTop + marginSides);

        buttonsBox["Canvas.Left"] = viewWidth - (marginSides + 126);
    }
};

SourceViewManager.prototype.selectItemHandler = function() {
    if ((window.event == null) || (window.event.srcElement == null) || (window.event.srcElement.tagName != "SPAN"))
        return;
    var srcElem = window.event.srcElement;
    if (srcElem.className != "srcLnk")
        return;
    var fileName = srcElem.innerText;
    srcElem = srcElem.parentNode;
    while (true) {
        srcElem = srcElem.parentNode;
        if ((srcElem == null) || (srcElem.tagName != "UL") || (srcElem.className == "srcList"))
            break;
        srcElem = srcElem.parentNode;
        if ((srcElem == null) || (srcElem.tagName != "LI") || (srcElem.className != "srcPath") || (srcElem.firstChild == null))
            break;
        fileName = srcElem.firstChild.innerText + '/' + fileName;
    }
    fileName = this.projectID + "/" + fileName;

    this.setSourceTitle(fileName);
    this.setSourceFile(fileName + ".htm");
    this.setCSSFile(fileName + ".css");
};

function SourceViewButton( plugIn, buttonID, clickFxn )
{
    this.buttonID = buttonID;
    this.clickFxn = clickFxn;
    this.buttonImg = plugIn.content.findName( this.buttonID );
    this.normalImg = plugIn.content.findName( this.buttonID + "N" );
    this.hoverImg = plugIn.content.findName( this.buttonID + "H" );
    this.downImg = plugIn.content.findName( this.buttonID + "D" );

    this.buttonImg.addEventListener( "MouseEnter", Utils.createDelegate( this, this.handleMouseEnter ) );
    this.buttonImg.addEventListener( "MouseLeave", Utils.createDelegate( this, this.handleMouseLeave ) );
    this.buttonImg.addEventListener( "MouseLeftButtonDown", Utils.createDelegate( this, this.handleMouseButtonDown ) );
    this.buttonImg.addEventListener( "MouseLeftButtonUp", Utils.createDelegate( this, this.handleMouseButtonUp ) );
};

SourceViewButton.prototype.handleMouseEnter = function(sender, eventArgs) {
    this.buttonImg.Source = this.hoverImg.Source;
};

SourceViewButton.prototype.handleMouseLeave = function(sender, eventArgs) {
    this.buttonImg.Source = this.normalImg.Source;
};

SourceViewButton.prototype.handleMouseButtonDown = function(sender, eventArgs) {
    this.buttonImg.Source = this.downImg.Source;
};

SourceViewButton.prototype.handleMouseButtonUp = function(sender, eventArgs) {
    this.buttonImg.Source = this.hoverImg.Source;
    if (this.clickFxn != null)
        this.clickFxn(this.buttonID);
};


