﻿var __isDirty = false;

function writeContactEmail()
{
  var user = 'results';
  var domain = 'wmep.org';
  var text = 'Email Us';
  document.write('<a href=\"mailto:' + user + '@' + domain + '\">' + text + '</a>');
}

function initializeContentPager(pageContainerId)
{
  var pageContainer = document.getElementById(pageContainerId);
  var first = true;
  var pageCount = 0;
  
  for (var i = 0; i < pageContainer.childNodes.length; i++)
  {
    if (pageContainer.childNodes[i].className == 'ContentPage')
    {
      if (!first)
        pageContainer.childNodes[i].style.display = 'none';
      else
        first = false;
      pageCount++;
    }
  }
  
  var prevLink = document.getElementById(pageContainerId + '_PrevPageLink');
  var nextLink = document.getElementById(pageContainerId + '_NextPageLink');
  prevLink.style.display = 'none';
  if (pageCount == 1)
    nextLink.style.display = 'none';
  nextLink.pageCount = pageCount; // Save page count with link
}

function nextContentPage(pageContainerId, nextLink)
{
  var pageContainer = document.getElementById(pageContainerId);
  var curPageFound = false;
  var newPageIndex = 0;
  
  for (var i = 0; i < pageContainer.childNodes.length; i++)
  {
    if (pageContainer.childNodes[i].className == 'ContentPage')
    {
      if (curPageFound)
      {
          pageContainer.childNodes[i].style.display = 'block';
          break;
      }
      else
      {
        if (pageContainer.childNodes[i].style.display != 'none')
        {
          pageContainer.childNodes[i].style.display = 'none';
          curPageFound = true;
        }
      }
      newPageIndex++;
    }
  }
  
  var nextLink = document.getElementById(pageContainerId + '_NextPageLink');
  if (newPageIndex == nextLink.pageCount - 1)
  {
    nextLink.style.display = 'none';
  }
  else
  {
    nextLink.style.display = '';
  }
  var prevLink = document.getElementById(pageContainerId + '_PrevPageLink');
  prevLink.style.display = '';
}

function prevContentPage(pageContainerId, prevLink)
{
  var pageContainer = document.getElementById(pageContainerId);
  var prevPage = null;
  var newPageIndex = 0;

  for (var i = 0; i < pageContainer.childNodes.length; i++)
  {
    if (pageContainer.childNodes[i].className == 'ContentPage')
    {
      if (pageContainer.childNodes[i].style.display != 'none')
      {
        pageContainer.childNodes[i].style.display = 'none';
        prevPage.style.display = 'block';
        break;
      }
      else
      {
        prevPage = pageContainer.childNodes[i];
      }
      newPageIndex++;
    }
  }
  newPageIndex--;
  
  var nextLink = document.getElementById(pageContainerId + '_NextPageLink');
  nextLink.style.display = '';
  var prevLink = document.getElementById(pageContainerId + '_PrevPageLink');
  if (newPageIndex == 0)
  {
    prevLink.style.display = 'none';
  }
  else
  {
    prevLink.style.display = '';
  }
}

function openPrintContentWindow(id)
{
  var w = window.open('/Print.aspx?id=' + id, 'PrintContentWindow', 'width=600,height=400,scrollbars=1,menubar=0,toolbar=0,resizable=1');
  w.focus();
}

function clickButton(e, buttonid)
{
  var evt = e ? e : window.event;
  var bt = document.getElementById(buttonid);
  if (bt)
  {
    if (evt.keyCode == 13)
    {
      bt.click();
      return false;
    }
  }
}

function showFlash(swfFile, outputDivID, width, height)
{
  var swf = new SWFObject(swfFile, "Video", width, height, "8", "#ffffff");//"#336699");
  swf.addParam("wmode","Transparent");
  if (!swf.write(outputDivID)) {
    var outputDiv = document.getElementById(outputDivID);
    if (outputDiv) {
      outputDiv.innerHTML = "<p align='center'>We are sorry. Your browser does not have Flash Player available for this site.</p><p align='center'><a href='/FlashHelp.aspx'>Help</a></p>";
    }
  }
}

function showFlashVideo(flvFile, outputDivID, width, height)
{
  var swf = new SWFObject("/Flash/player.swf", "Video", width, height, "8", "#ffffff");//"#336699");
  swf.addVariable("movie", flvFile);
  swf.addParam("wmode","Transparent");
  if (!swf.write(outputDivID)) {
    var outputDiv = document.getElementById(outputDivID);
    if (outputDiv) {
      outputDiv.innerHTML = "<p align='center'>We are sorry. Your browser does not have Flash Player available for this site.</p><p align='center'><a href='/FlashHelp.aspx'>Help</a></p>";
    }
  }
}

function openDocumentMgrPopup(targetId)
{
  window.open('/Admin/FileManagerPopup.aspx?ContentFileTypeId=1&TargetInputId=' + targetId,
              'FileManagerPopup', 'width=600,height=400,scrollbars=1,menubar=0,toolbar=0,resizable=1');
}

function openFlashMgrPopup(targetId)
{
  window.open('/Admin/FileManagerPopup.aspx?ContentFileTypeId=2&TargetInputId=' + targetId,
              'FileManagerPopup', 'width=600,height=400,scrollbars=1,menubar=0,toolbar=0,resizable=1');
}

function openImageMgrPopup(targetId)
{
  window.open('/Admin/FileManagerPopup.aspx?ContentFileTypeId=3&TargetInputId=' + targetId,
              'FileManagerPopup', 'width=600,height=400,scrollbars=1,menubar=0,toolbar=0,resizable=1');
}

function openVideoMgrPopup(targetId)
{
  window.open('/Admin/FileManagerPopup.aspx?ContentFileTypeId=4&TargetInputId=' + targetId,
              'FileManagerPopup', 'width=600,height=400,scrollbars=1,menubar=0,toolbar=0,resizable=1');
}

// setPopupLink()
// author: pvh 2009-04-26
// desc:
//   This method says that all links with a rel tag set to external will popup the link in a new window.
//   If the browser doesn't support attribute css selectors, the popup will degrade gracefully and navigate
//   to the link in the current window.
function setPopupLinks() {
  $('A[rel="popup"]').click(function() {
    window.open($(this).attr('href'),null,"height=600,width=800,scrollbars=yes,status=yes,location=yes,resizable=yes,menubar=yes");
    return false;
  });
}

// setLinkCanceling
// author: pvh 2009-04-26
// desc:
//   This method disables all links on the page when the form is dirty
function setLinkCanceling() {
  $('DIV.WebPartMenuBar IMG, :submit, DIV.WebPart > DIV > DIV TD A').click(function() {
    __isDirty = false;
  });
}

// setFormDirty
// author: pvh 2009-04-26
// desc:
//   This method sets the form as dirty on load if it detects any divs with a dirty attribute
function setFormDirty() {
  __isDirty = ($('DIV[dirty="true"]').get().length > 0);
}

// attachBrowserEventHandlers
// author: pvh 2009-04-26
// desc:
//   Attach browser level event handlers
function attachBrowserEventHandlers() {
  window.onbeforeunload = function() { if (__isDirty) { return "You have unsaved changes.  Are you sure you want to leave and lose those changes?"; } }
}

// isEditComplete
// author: pvh 2009-04-07
// desc:
//   Returns a true or false to indicate whether there are any webparts being edited on the page.
function isEditComplete() {
  if ($('DIV[editing="true"]').get().length > 0) {
    alert("You have webparts open for editing.  Accept or Cancel these edits before saving.");
    return false
  }
  return true;
}

/* startup js */
$(document).ready(function() {
  setPopupLinks();
  setLinkCanceling();
  setFormDirty();
  attachBrowserEventHandlers();
});