
var TextMasterUI = (function (){
    var config = {
           daBox           : ""
          ,stringFromBox   : { value: "" }
          ,stringToBox     : { value: "" }
          ,charFromBox     : { value: "" }
          ,charToBox       : { value: "" }
          ,stringCheck     : { checked: "1" }
          ,charCheck       : { checked: "1" }
          ,charMax         : { value: "160" }
          ,charMaxDefault  : 160
          ,charTarget      : 102
    };
    
    var init = function init(){
        

          TextMaster.config["warn-function"] = showWarning;
          
          TextMasterUI.config.daBox         = document.boxform.dabox;
          TextMasterUI.config.stringFromBox = document.stringform.stringfrombox;
          TextMasterUI.config.stringToBox   = document.stringform.stringtobox;
          TextMasterUI.config.charFromBox   = document.charform.charfrombox;
          TextMasterUI.config.charToBox     = document.charform.chartobox;
          TextMasterUI.config.stringCheck   = document.stringform.stringcheck;
          TextMasterUI.config.charCheck     = document.charform.charcheck;
          TextMasterUI.config.charMaxDefault= 160;
          TextMasterUI.config.charMax       = function(){
               var option = document.maxcontrolform;
               if(option.sorange.checked){
                    return parseInt(option.sorange.value, 10);
               } else if(option.sother.checked){
                    return parseInt(option.sother.value, 10);
               }
               
               return TextMasterUI.config.charMaxDefault;
          };
          
          $("button").button();
          
          $("#tabs").tabs();
          $("#menu").tabs();
          
          $("#stringdialog").dialog({
               resizable: false,
               autoOpen: false,
               position: ["right","top"],
               buttons: {
                    "Ok": function() {
                    $(this).dialog("close");
                    }
               }
          });
          
          $("#chardialog").dialog({
               resizable: false,
               autoOpen: false,
               position: ["right","bottom"],
               buttons: {
                    "Ok": function() {
                    checkCharFormInput();
                    $(this).dialog("close");
               }
               }
          });
        
          $("#dabox").keypress(setCounter);
          setInterval(setCounter, 500);
          
          $("#sub-button").click(function(){
              var startRange, endRange, text;
              [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
              text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.customSubs(text.substring(startRange, endRange)) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.customSubs(text);
               };
               setTextFromBox(text);
          });
          
          $("#strip-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.stripText(text.substring(startRange, endRange)) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.stripText(text);
               };
               setTextFromBox(text);
          });
          
          $("#moreify-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.moreifyText(text.substring(startRange, endRange), TextMasterUI.config.charMax, showWarning) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.moreifyText(text, TextMasterUI.config.charMax, showWarning);
               };
               setTextFromBox(text);
          });
          
          $("#minify-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.minifyText(text.substring(startRange, endRange)) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.minifyText(text);
               };
               setTextFromBox(text);
          });
          
          $("#ukify-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.minifyText(text.substring(startRange, endRange)) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.ukifyText(text);
               };
               setTextFromBox(text);
          });
          
          $("#euro-button").click(function(){
              var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               var text  = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +"\u20AC" +
                         text.substring(endRange);
               } else {
               text = text.substring(0, startRange) + "\u20AC" + text.substring(startRange);
               };
               setTextFromBox(text);
               setCaretPos(config.daBox, ++startRange);
          });
          
          $("#pound-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               var text  = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +"\u00A3" +
                         text.substring(endRange);
               } else {
               text = text.substring(0, startRange) + "\u00A3" + text.substring(startRange);
               };
               setTextFromBox(text);
               setCaretPos(config.daBox, ++startRange);
          });
          
          $("#lc-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         text.substring(startRange, endRange).toLowerCase() +
                         text.substring(endRange);
               } else {
                    text = text.toLowerCase();
               };
               setTextFromBox(text);
          });
          
          $("#uc-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         text.substring(startRange, endRange).toUpperCase() +
                         text.substring(endRange);
               } else {
                    text = text.toUpperCase();
               };
               setTextFromBox(text);
          });
          
          $("#nws-button").click(function(){
               var startRange, endRange, text;
               [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
               text = getTextFromBox();
               if(startRange !== endRange){
                    text = text.substring(0, (startRange)) +
                         TextMaster.normalizeWS(text.substring(startRange, endRange)) +
                         text.substring(endRange);
               } else {
                    text = TextMaster.normalizeWS(text);
               };
               setTextFromBox(text);
          });
          
          $("#clear-button").click(function(){
               setTextFromBox("");
               if(config.daBox.focus){
                    config.daBox.focus();
               };
          });
          
          $("#replacestring-button").click(function(){
               $("#stringdialog").dialog('open');
          });
          
          $("#replacechars-button").click(function(){
               $("#chardialog").dialog('open');
          });
          
          
          $("#tabs").height( $("#menu").height() );
          
          var pos, height;
          var boxWidth, boxHeight, elWidth, elHeight, conWidth, conHeight, ref;
          
          boxWidth = $("#dabox").width();
          conWidth = $("#tabholder").width();

          elWidth = $("#topmenubar").width();
          ref = (((conWidth - boxWidth)/2) + boxWidth) - elWidth + 5;
          $("#topmenubar").css( {
            left: (ref) + "px"
          });
          
          elWidth = $("#maxcontrols").width();
          ref -= (elWidth + 5);
          $("#maxcontrols").css( {
            left: (ref) + "px"
          });
          
          elWidth = $("#selrange").width();
          ref -= (elWidth + 5);
          $("#selrange").css( {
            left: (ref) + "px"
          });
            
          elWidth = $("#remainingcounter").width();
          ref -= (elWidth + 5);
          $("#remainingcounter").css( {
            left: (ref) + "px"
          });
          
          $("#remainingcounter2").css( {
            left: (ref) + "px"
          });
          
          elWidth = $("#counter").width();
          ref -= (elWidth + 5);
          $("#counter").css( {
            left: (ref) + "px"
          });
            
          $("#daboxholder").css( {
               left: ((conWidth - boxWidth)/2) + "px"
          });
          
          elWidth = $("#bottommenubar").width();
          $("#bottommenubar").css( {
               left: ((conWidth - elWidth)/2) + "px"
          });
          
          elWidth = $("#bottommenubar2").width();
          $("#bottommenubar2").css( {
               left: ((conWidth - elWidth)/2) + "px"
          });
          
          $("#clearbuttonbar").css( {
               left: (((conWidth - boxWidth)/2) - 15) + "px"
          });
          
          $("#footer").width( window.innerWidth - 5 );
           
          
          pos = $("#tabs").offset();  
          height = $("#tabs").height(); 
          
          $("#warning").css( {
               top: (pos.top + height) + "px",
               left: (pos.left - 250) + "px"
          } );
          
          config.stringFromBox.value = "";
          config.stringToBox.value = "";
          
          config.charFromBox.value = "";
          config.charToBox.value = "";
          
          
          $("#curtain").hide();
          
          setTextFromBox("");
          $("#dabox").focus();
          
     };
     
     var setCounter = function setCounter(){
          var length = getTextFromBox().length;
          $("#counter").html('' + length);
          
          maxLength = TextMasterUI.config.charMax || 160;
          charTarget = TextMasterUI.config.charTarget || 102;
          if(typeof(maxLength) === "function"){
            maxLength = maxLength();
          } else {
            
          };
          if((length > 102) && (length <= maxLength)){
               $("#counter").css({
                    "background-color": "#FFD90F"
               });
          } else if(length > maxLength){
               $("#counter").css({
                    "background-color": "#ED2F7F"
               });
          } else {
               $("#counter").css({
                    "background-color": "#FFFFFF"
               });
          };
          
          var remaining = (maxLength - length);
          $("#remainingcounter").html("&nbsp;&nbsp;" + remaining + " left to " + maxLength);
          
          remaining = (charTarget - length);
          $("#remainingcounter2").html("&nbsp;&nbsp;" + remaining + " left to " + charTarget);
          
          var startRange, endRange;
          [startRange, endRange] = getSelRange(TextMasterUI.config.daBox);
          $("#selrange").html("" + startRange + "-" + endRange);
          
     };
     var getTextFromBox = function getTextFromBox(){
          return config.daBox.value || "";
     };
     
    var setTextFromBox = function setTextFromBox(str){
          config.daBox.value = str;
          return str;
     };
     
    var checkCharFormInput = function checkCharFormInput () {
          if(config.charToBox.value.length > config.charFromBox.value.length){
               showWarning("There are extra \"replace with\" chars they will be discarded.");
               config.charToBox.value = config.charToBox.value.substr(0, config.charFromBox.value.length);
          };
     };
     
     var showWarning = function showWarning(str) {
          $("#warning").css({
               visibility: "visible"
          });
          $("#warning").hide();
          $("#warning").html(
               '<span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>' +  str);
          $("#warning").show("slow", function(){
               setTimeout(function(){hideWarning()}, 10000);
          });
     };
     
     var hideWarning = function hideWarning(){
          $("#warning").css({
               visibility: "hidden"
          });
          $("#warning").html();
          $("#warning").hide();
     };
     
     var getCaretPos = function getCaretPos(el) {
          var CaretPos = 0;
          if (document.selection) {
               el.focus ();
               var Sel = document.selection.createRange ();
               Sel.moveStart ('character', -el.value.length);
               CaretPos = Sel.text.length;
          } else if (el.selectionStart || el.selectionStart == '0')
          CaretPos = el.selectionStart;
          return (CaretPos);
     };
     
     var setCaretPos = function setCaretPos(el, pos){
          if(el.setSelectionRange){
               el.focus();
               el.setSelectionRange(pos,pos);
          } else if (el.createTextRange) {
               var range = el.createTextRange();
               range.collapse(true);
               range.moveEnd('character', pos);
               range.moveStart('character', pos);
               range.select();
          };
     };
     
     var getSelRange = function getSelRange(el){
          if (el.selectionStart || el.selectionStart == '0'){
               startPos = el.selectionStart;
               endPos = el.selectionEnd;
          };
          
          return [startPos, endPos];
    };
    
    return {
         init: init
        ,config: config
    };
}());

$("document").ready(function(){ TextMasterUI.init() });
