/****************************************************************************
 * Frost Jedi Graemlin Creator JavaScript Additions v1.2.0 (July 14, 2005)
 * © MMIII - MMV by TerraFrost. All rights reserved.
 *
 * http://www.frostjedi.com/terra/scripts/graemlin/
 *
 * Comments, criticisms, suggestions, and additions welcome! Post them 
 * here:
 *
 * http://www.frostjedi.com/phpbb/viewforum.php?f=6
 *
 * Released under the Creative Commons Attribution 2.0 license:
 * http://creativecommons.org/licenses/by/2.0/
 ****************************************************************************/

var textSource;
var baseRange;
var tempText = '';

// ie. constants
var TEXTAREA = 0;
var URL = 1;

function init() {
   // The purpose of the following two lines are described at this URL:
   // http://www.frostjedi.com/terra/scripts/demo/caretBug.html
   document.forms[0].text.focus();
   if (document.selection) baseRange = document.selection.createRange();
}

function textarea_handler() {
   if (textSource == null) {
      document.forms[0].text.select();
      initFocus = false;
   } else if (textSource == URL) {
      document.forms[0].text.value = tempText;
      tempText = document.forms[0].url.value;
      document.forms[0].url.value = '';
      textSource = TEXTAREA;
   }
}

function url_handler()
{
   if (textSource == TEXTAREA || textSource == null) {
      var txtarea = document.forms[0].text;
      document.forms[0].url.value = tempText;
      tempText = txtarea.value;
      txtarea.value = '';
      textSource = URL;
   }
}

/* insertText returns false to cancel the current click action. More information can be found here:
   http://groups.google.com/group/comp.lang.javascript/msg/b9ae49d3b99e2e4b?hl=en& */
function insertText(text) {
   var txtarea = document.forms[0].text;
   if (txtarea.createTextRange && txtarea.caretPos) {
      if (baseRange.boundingHeight != txtarea.caretPos.boundingHeight) {
         document.forms[0].text.focus();
         saveCaret();
      }
      txtarea.caretPos.text = text;
   } else if (txtarea.selectionEnd && (txtarea.selectionStart | txtarea.selectionStart == 0)) 
      mozInsert(txtarea, text, "");
   else
      txtarea.value += text;
   return false;
}

function saveCaret() {
   if (document.selection) document.forms[0].text.caretPos = document.selection.createRange();
}

// The following function is from the "BBCodes & smilies use enhancement" phpBB mod (c) -=ET=-:
// http://www.phpbb.com/phpBB/viewtopic.php?t=175999
function mozInsert(txtarea, openTag, closeTag) {
   if (txtarea.selectionEnd > txtarea.value.length) { txtarea.selectionEnd = txtarea.value.length; } 

   var startPos = txtarea.selectionStart; 
   var endPos = txtarea.selectionEnd+openTag.length; 

   txtarea.value=txtarea.value.slice(0,startPos)+openTag+txtarea.value.slice(startPos); 
   txtarea.value=txtarea.value.slice(0,endPos)+closeTag+txtarea.value.slice(endPos); 

   txtarea.selectionStart = startPos+openTag.length; 
   txtarea.selectionEnd = endPos; 
   txtarea.focus(); 
}

function rewriteDOM()
{
   var buttons = document.getElementById('buttons');

   /* modifying the "first" node is necessary due to the following bug:
      https://bugzilla.mozilla.org/show_bug.cgi?id=264236

      setting oldShow to buttons.firstChild isn't an option for reasons that are elaborated upon in the following URLs:
      http://groups.google.com/group/comp.lang.javascript/msg/1fd09a3a4c1ba8b8
      http://www.zvon.org/xxl/DOM2reference/DOM2/Output/data/examples/Node_childNodes.html */

   var oldShow = buttons.getElementsByTagName('input').item(0);
   var newShow = document.createElement('img');
   newShow.src = oldShow.getAttribute('src');
   newShow.alt = oldShow.getAttribute('alt');
   newShow.style.cursor = 'pointer';
   newShow.onclick = Function('document.forms[0].mode.value="convert"; document.forms[0].submit()');
   buttons.replaceChild(newShow, oldShow);

   // make each button fade as appropriate...

   for (i = 0; i < buttons.childNodes.length; i++) {
      switch (buttons.childNodes[i].nodeName.toLowerCase()) {
         case 'a': origButton = buttons.childNodes[i].firstChild;   break;
         case 'img': origButton = buttons.childNodes[i];   break;
         default: origButton = null; // ie. #text
      }
      if (origButton != null) {
         //origButton.style.filter = 'alpha(opacity:50)';
         if (origButton.filters)
            origButton.filters['DXImageTransform.Microsoft.Alpha'].Opacity = 50;
         origButton.style.opacity = 0.5;
         origButton.style.mozOpacity = 0.5;
         origButton.style.khtmlOpacity = 0.5;
         origButton.onmouseover = Function('fade(this,100,40,5)');
         origButton.onmouseout = Function('fade(this,50,40,5)');
      }
   }

   // create a color swatch that has been loosely based off of the color swatch in phpBB2.2's subSilver template.

   var r = 0, g = 0, b = 0;
   var numberList = new Array(6);
   numberList[0] = '00';
   numberList[1] = '40';
   numberList[2] = '80';
   numberList[3] = 'BF';
   numberList[4] = 'FF';

   var baseHeight = 3;
   var baseWidth = 8;
   var marginRight = 5;

   var properties = document.getElementById('properties');
   properties.removeChild(document.getElementById('color'));

   var color = document.createElement('div');
   color.id = 'color';
   color.style.cssFloat = color.style.styleFloat = 'left';

   var text = document.createElement('div');
   text.style.cssFloat = text.style.styleFloat = 'left';
   text.style.marginRight = marginRight+'px';
   text.appendChild(document.createTextNode('Text Color:'));
   color.appendChild(text);

   var currentColor = document.createElement('div');
   currentColor.id = 'currentColor';
   currentColor.style.marginRight = marginRight+'px';
   currentColor.style.width = '15px';
   currentColor.style.height = 5*baseHeight+'px';
   currentColor.style.background = '#fff';
   currentColor.style.cssFloat = currentColor.style.styleFloat = 'left';
   var selectedColor = document.createElement('img');
   selectedColor.src = 'graemlin/spacer.gif';
   selectedColor.width = 5*baseHeight-2;
   selectedColor.height = 5*baseHeight-2;
   selectedColor.id = 'selectedColor';
   selectedColor.style.margin = '1px';
   selectedColor.style.background = '#000';
   currentColor.appendChild(selectedColor);
   color.appendChild(currentColor);

   var hidden = document.createElement('input');
   hidden.name = 'color';
   hidden.type = 'hidden';
   hidden.value = '000000';
   color.appendChild(hidden);

   var swatch = document.createElement('div');
   swatch.style.width = 25*baseWidth+'px';
   swatch.style.height = 5*baseHeight+'px';
   swatch.style.cssFloat = swatch.style.styleFloat = 'left';
   for (r = 0; r < 5; r++)
      for(g = 0; g < 5; g++) {
         for(b = 0; b < 5; b++) {
            tempColor = '' + numberList[r] + '' + numberList[g] + '' + numberList[b];
            tempImage = document.createElement('img');
            tempImage.src = 'graemlin/spacer.gif';
            tempImage.width=baseWidth;
            tempImage.height=baseHeight;
            tempImage.style.background='#' + tempColor;
            tempImage.style.cssFloat=tempImage.style.styleFloat='left';
            tempImage.style.cursor = 'pointer';
            tempImage.onclick = Function('document.getElementById("selectedColor").style.background="#'+tempColor+'"; document.forms[0].color.value = "'+tempColor+'"');
            tempImage.onmouseover = Function('document.getElementById("currentColor").style.background="#'+tempColor+'"');
            if (r != 0 && g == 0 && b == 0)
               tempImage.style.clear = 'left';
            swatch.appendChild(tempImage);
         }
      }
   color.appendChild(swatch);
   properties.appendChild(color);

   var size = document.getElementById('size');
   var marginCorrection = size.clientHeight - color.clientHeight;
   if (marginCorrection > 0)
      color.style.marginTop = marginCorrection+'px';
   else
      size.style.marginTop = abs(marginCorrection)+'px';
}