addLoadListener(initWYSIWYG);

function initWYSIWYG()
{
  if (typeof(document.designMode) == "string" && (document.all || document.designMode == "off"))
  {
    var textareas = getElementsByAttribute("class", "wysiwyg");

    for (var i = 0; i < textareas.length; i++)
    {
      convertWYSIWYG(textareas[i]);
    }
  }

  return true;
}

function convertWYSIWYG(textarea)
{
  var textareaID = textarea.getAttribute("id");
  var textareaName = textarea.getAttribute("name");
  var textareaValue = textarea.value;

  var input = document.createElement("input");
  input.setAttribute("type", "hidden");
  input.setAttribute("id", textareaID);
  input.setAttribute("name", textareaName);
  input.value = textareaValue;

  var iframe = document.createElement("iframe");
  iframe.className = "wysiwygIframe";
  iframe.setAttribute("id", "iframeemailbody");
  iframe.setAttribute("frameborder", "0");

  textarea.parentNode.replaceChild(iframe, textarea);

  if (typeof iframe.document != "undefined" && typeof iframe.contentDocument == "undefined" && typeof iframe.contentWindow == "undefined")
  {
    iframe.parentNode.replaceChild(textarea, iframe);

    return false;
  }

  iframe.parentNode.insertBefore(input, iframe);

  iframe.contentWindow.document.open();
  iframe.contentWindow.document.write('<html><head><style type="text/css">@import "/themes/default/styles-wysiwyg-iframe.css";</style></head><body><div>' + input.value + '</div></body></html>');
  iframe.contentWindow.document.close();
  iframe.contentWindow.document.designMode = "on";

  var form  = iframe.parentNode;

  while (form != null && form.nodeName.toLowerCase() != "form")
  {
    form = form.parentNode;
  }

  if (form != null)
  {
    attachEventListener(form, "submit", function(){input.value = iframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML;}, false);
  }

  var toolbar = document.createElement("div");
  toolbar.className = "wysiwygToolbar";
  iframe.parentNode.insertBefore(toolbar, iframe);

  var buttonBold = document.createElement("a");
  buttonBold.className = "wysiwygButtonBold";
  buttonBold.setAttribute("href", "#");
  buttonBold.appendChild(document.createTextNode("Bold"));
  buttonBold.command = "bold";
  buttonBold.iframe = iframe;
  buttonBold.onmousedown = mousedownToolbar;
  buttonBold.onclick = executeWYSIWYG;
  toolbar.appendChild(buttonBold);

  var buttonItalic = document.createElement("a");
  buttonItalic.className = "wysiwygButtonItalic";
  buttonItalic.setAttribute("href", "#");
  buttonItalic.appendChild(document.createTextNode("Italic"));
  buttonItalic.command = "italic";
  buttonItalic.iframe = iframe;
  buttonItalic.onmousedown = mousedownToolbar;
  buttonItalic.onclick = executeWYSIWYG;
  toolbar.appendChild(buttonItalic);

  var buttonLink = document.createElement("a");
  buttonLink.className = "wysiwygButtonLink";
  buttonLink.setAttribute("href", "#");
  buttonLink.appendChild(document.createTextNode("Link"));
  buttonLink.command = "createLink";
  buttonLink.iframe = iframe;
  buttonLink.onmousedown = mousedownToolbar;
  buttonLink.onclick = executeWYSIWYG;
  toolbar.appendChild(buttonLink);

  var buttonImage = document.createElement("a");
  buttonImage.className = "wysiwygButtonImage";
  buttonImage.setAttribute("href", "#");
  buttonImage.appendChild(document.createTextNode("Image"));
  buttonImage.command = "insertimage";
  buttonImage.iframe = iframe;
  buttonImage.onmousedown = mousedownToolbar;
  buttonImage.onclick = executeWYSIWYG;
  toolbar.appendChild(buttonImage);

  var buttonSmile = document.createElement("a");
  buttonSmile.className = "wysiwygButtonSmile";
  buttonSmile.setAttribute("href", "#");
  buttonSmile.appendChild(document.createTextNode("Image"));
  buttonSmile.command = "insertimage";
  buttonSmile.iframe = iframe;
  buttonSmile.onmousedown = mousedownToolbar;
  buttonSmile.onclick = executeSmile;
  toolbar.appendChild(buttonSmile);

  var buttonFrown = document.createElement("a");
  buttonFrown.className = "wysiwygButtonFrown";
  buttonFrown.setAttribute("href", "#");
  buttonFrown.appendChild(document.createTextNode("Image"));
  buttonFrown.command = "insertimage";
  buttonFrown.iframe = iframe;
  buttonFrown.onmousedown = mousedownToolbar;
  buttonFrown.onclick = executeFrown;
  toolbar.appendChild(buttonFrown);

  var buttonLaugh = document.createElement("a");
  buttonLaugh.className = "wysiwygButtonLaugh";
  buttonLaugh.setAttribute("href", "#");
  buttonLaugh.appendChild(document.createTextNode("Image"));
  buttonLaugh.command = "insertimage";
  buttonLaugh.iframe = iframe;
  buttonLaugh.onmousedown = mousedownToolbar;
  buttonLaugh.onclick = executeLaugh;
  toolbar.appendChild(buttonLaugh);

  var buttonSlant = document.createElement("a");
  buttonSlant.className = "wysiwygButtonSlant";
  buttonSlant.setAttribute("href", "#");
  buttonSlant.appendChild(document.createTextNode("Image"));
  buttonSlant.command = "insertimage";
  buttonSlant.iframe = iframe;
  buttonSlant.onmousedown = mousedownToolbar;
  buttonSlant.onclick = executeSlant;
  toolbar.appendChild(buttonSlant);

  var buttonWink = document.createElement("a");
  buttonWink.className = "wysiwygButtonWink";
  buttonWink.setAttribute("href", "#");
  buttonWink.appendChild(document.createTextNode("Image"));
  buttonWink.command = "insertimage";
  buttonWink.iframe = iframe;
  buttonWink.onmousedown = mousedownToolbar;
  buttonWink.onclick = executeWink;
  toolbar.appendChild(buttonWink);

  var buttonYuck = document.createElement("a");
  buttonYuck.className = "wysiwygButtonYuck";
  buttonYuck.setAttribute("href", "#");
  buttonYuck.appendChild(document.createTextNode("Image"));
  buttonYuck.command = "insertimage";
  buttonYuck.iframe = iframe;
  buttonYuck.onmousedown = mousedownToolbar;
  buttonYuck.onclick = executeYuck;
  toolbar.appendChild(buttonYuck);

  return true;
}

function executeWYSIWYG(event)
{
  var URLlink = 0;
  if (this.command == 'createLink'){
		var URLlink = prompt('URL of Link? (include full address)');
	}
  if (this.command == 'insertimage'){
		var URLlink = prompt('URL of Image?');
	}
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeSmile(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/smile.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeFrown(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/frown.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeLaugh(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/laugh.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeSlant(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/slant.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeWink(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/wink.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}

function executeYuck(event)
{
  var URLlink = "http://www.rollerhome.com/themes/default/images/smiley/yuck.png";
  this.iframe.contentWindow.document.execCommand(this.command, false, URLlink);
  this.iframe.contentWindow.focus();

  return false;
}


function mousedownToolbar()
{
  return false;
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

function getElementsByAttribute(attribute, attributeValue)
{
  var elementArray = new Array();
  var matchedArray = new Array();

  if (document.all)
  {
    elementArray = document.all;
  }
  else
  {
    elementArray = document.getElementsByTagName("*");
  }

  for (var i = 0; i < elementArray.length; i++)
  {
    if (attribute == "class")
    {
      var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

      if (elementArray[i].className.match(pattern))
      {
        matchedArray[matchedArray.length] = elementArray[i];
      }
    }
    else if (attribute == "for")
    {
      if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
      {
        if (elementArray[i].htmlFor == attributeValue)
        {
          matchedArray[matchedArray.length] = elementArray[i];
        }
      }
    }
    else if (elementArray[i].getAttribute(attribute) == attributeValue)
    {
      matchedArray[matchedArray.length] = elementArray[i];
    }
  }

  return matchedArray;
}