﻿String.prototype.htmlEncode = function() {
  return this
		.replace(/\</gi, '&lt;')
		.replace(/\>/gi, '&gt;')
		.replace(/\&/gi, '&amp;')
		.replace(/\&nbsp;/gi, '&#160;'); //&nbsp; replacement for xhtml compatibility.
};

$(document).ready(function() {
  $('.search input[type="text"]').focus(function() {
    $(this).addClass('focus');
    $(this).val('');
  });
  $('.search input[type="text"]').blur(function() {
    $(this).removeClass('focus');
    if (!$(this).val().length)
      $(this).val('Search the web');
  });

  $('.videoimg').click(function() {
    $('#greyout').show();
    $('#videodialog').show();
    $('.close').show();
    $('#silverlightControlHost').show();

    var windowWidth = document.documentElement.clientWidth,
      popupWidth = $("#videodialog").width(),
      scrolledY = document.documentElement.scrollTop,
      left = windowWidth / 2 - popupWidth / 2;

    $("#videodialog").css({ "top": scrolledY, "left": left, 'margin-top': "112px" });
  });

  $('.close').click(function() {
    $('#greyout').hide();
    $('#videodialog').hide();
    $('#silverlightControlHost').empty();
  });

  function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
      appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    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);
  }

  function highlightDownloadArea(fOn) {
    document.getElementById("overlay").className = (fOn) ? "fadeSome" : "fadeLots";
  }

  function CloseWindow() {
    window.close();
  }
});