﻿/// --------------------------------------------------
/// mainScreen object
/// --------------------------------------------------
var mainScreen =
{
    mainModalExtender: null,           // modalExtender object on main page
    mainModalTitleSpan: null,          // title span object
    mainModalAbstractSummary: null,     // abstractSummary paragraph object
    mainModalDownloadButton: null,     // button that triggers the download
    mainModalAcceptanceCheckbox: null, // checkbox that has to be checked prior to triggering download
    mainModalErrorMessageSpan: null    // span that displays error message
}

mainScreen.Init = function() {
    /// <summary>
    /// Initializes mainScreen variables
    /// </summary>
    this.mainModalExtender = $find('mbMain');
    this.mainModalDownloadButton = $get("downloadBtn");
    this.mainModalTitleSpan = $get("spanTitle");
    this.mainModalAbstractSummaryParagraph = $get("paragraphAbstractSummary");
    this.mainModalAcceptanceCheckbox = $get("acceptanceCheckBox");
    this.mainModalErrorMessageSpan = $get("errorMessage");
};
mainScreen.ShowModal = function(_title, _url) {
    /// <summary>
    /// Shows modal dialog with title
    /// </summary>
    /// <param name="_title">Title of modal popup</param>
    /// <param name="_url">Url to the content to be downloaded</param>
    this.mainModalTitleSpan.innerHTML = _title;
    this.mainModalTitleSpan.textContent = _title;
    this.mainModalDownloadButton.onclick = function() { mainScreen.Download(_url); };
    this.mainModalExtender.show();
};

mainScreen.ShowModal = function(_title, _abstractSummary, _url) {
    /// <summary>
    /// Shows modal dialog with title
    /// </summary>
    /// <param name="_title">Title of modal popup</param>
    /// <param name="_url">Url to the content to be downloaded</param>
    this.mainModalTitleSpan.innerHTML = _title;
    this.mainModalTitleSpan.textContent = _title;

    if (_abstractSummary != '') {
        this.mainModalAbstractSummaryParagraph.innerHTML = 'Summary: ' + _abstractSummary;
        this.mainModalAbstractSummaryParagraph.textContent = 'Summary: ' + _abstractSummary;
    }
    this.mainModalDownloadButton.onclick = function() { mainScreen.Download(_url); };
    this.mainModalExtender.show();
};

mainScreen.Download = function(_url) {
    if (this.mainModalAcceptanceCheckbox.checked) {
        this.mainModalErrorMessageSpan.style.display = "none";
        mainScreen.CancelModal();
        window.open(_url);
    }
    else {
        this.mainModalErrorMessageSpan.style.display = "";
    }
};

mainScreen.CancelModal = function() {
    /// <summary>
    /// Hides modal dialog 
    /// </summary>
    this.mainModalExtender.hide();
};


/// --------------------------------------------------
/// Page events processing
/// --------------------------------------------------

Sys.Application.add_load(
    applicationLoadHandler
    );
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
    endRequestHandler
    );
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(
    beginRequestHandler
    );

function applicationLoadHandler() {
    /// <summary>
    /// Raised after all scripts have been loaded and 
    /// the objects in the application have been created 
    /// and initialized.
    /// </summary>
    mainScreen.Init()
}

function endRequestHandler() {
    /// <summary>
    /// Raised before processing of an asynchronous 
    /// postback starts and the postback request is 
    /// sent to the server.
    /// </summary>

    // TODO: Add your custom processing for event
}

function beginRequestHandler() {
    /// <summary>
    /// Raised after an asynchronous postback is 
    /// finished and control has been returned 
    /// to the browser.
    /// </summary>

    // TODO: Add your custom processing for event
}

function deactivateCapsuleClub() {
    return confirm('Are you sure that you want to proceed with deactivating this Capsule Club?');
}

function reactivateCapsuleClub() {
    return confirm('Are you sure that you want to proceed with reactivating this Capsule Club?');
   }


   function onSilverlightError(sender, args) {
   	var appSource = "";
   	if (sender != null && sender != 0) {
   		appSource = sender.getHost().Source;
   	}

   	var errorType = args.ErrorType;
   	var iErrorCode = args.ErrorCode;

   	if (errorType == "ImageError" || errorType == "MediaError") {
   		return;
   	}

   	var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

   	errMsg += "Code: " + iErrorCode + "    \n";
   	errMsg += "Category: " + errorType + "       \n";
   	errMsg += "Message: " + args.ErrorMessage + "     \n";

   	if (errorType == "ParserError") {
   		errMsg += "File: " + args.xamlFile + "     \n";
   		errMsg += "Line: " + args.lineNumber + "     \n";
   		errMsg += "Position: " + args.charPosition + "     \n";
   	}
   	else if (errorType == "RuntimeError") {
   		if (args.lineNumber != 0) {
   			errMsg += "Line: " + args.lineNumber + "     \n";
   			errMsg += "Position: " + args.charPosition + "     \n";
   		}
   		errMsg += "MethodName: " + args.methodName + "     \n";
   	}

   	throw new Error(errMsg);
   }
