/**
 * Pig Latin Translator
 * Copyright (C) 2009 Paul Carduner
 * http://www.carduner.net/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var word = /([a-zA-Z]+)(.*)/;
var wordWithVowel = /^([a-zA-Z]*?)([aeiouAEIOU][a-zA-Z]*)(.*)/;
var betweenWord = /([^a-zA-Z]+)(.*)/;

function toLatin(s, withDashes){
  var result = "";
  var parts = [null, s];

  function eatNonWord(w){
    parts = w.match(betweenWord);
    if (parts){
      result += parts[1];
    }
  }

  function eatWord(w){
    parts = w.match(wordWithVowel);
    if (parts){
      if (w.match(/^[A-Z].*/)){
        parts[2] = parts[2][0].toUpperCase()+parts[2].slice(1);
        if (parts[1])
          parts[1] = parts[1][0].toLowerCase()+parts[1].slice(1);
      }
      result += parts[2]+(withDashes? '-':'')+parts[1]+'ay';
      eatNonWord(parts.pop());
    } else if (w){
      parts = w.match(word);
      if (parts){
        result += parts[1]+(withDashes? '-':'')+'ay';
        eatNonWord(parts.pop());
      }
      else{
        result += w+(withDashes? '-':'')+'ay';
      }
    }
  }


  for (var count=0; count < 10000 && parts && parts.length > 1; count++){
    eatWord(parts.pop());
  }
  return result;
}

function toEnglish(s){
  var result = "";
  var word = /(.*?)-([a-zA-Z]*?)ay(.*)/;
  var parts = [null, s];
  function eatNonWord(w){
    parts = w.match(betweenWord);
    if (parts){
      result += parts[1];
    }
  }

  function eatWord(w){
    parts = w.match(word);
    if (parts){
      if (w.match(/^[A-Z].*/)){
        if (parts[2])
          parts[2] = parts[2][0].toUpperCase()+parts[2].slice(1);
        if (parts[1])
          parts[1] = parts[1][0].toLowerCase()+parts[1].slice(1);
      }
      result += parts[2]+parts[1];
      eatNonWord(parts.pop());
    }
  }
  for (var count=0; count < 10000 && parts && parts.length > 1; count++){
    eatWord(parts.pop());
  }
  return result;
}

$(document).ready(
  function(){
    $("#help-text").hide();
    $("#ok").click(
      function(){
        $("#help-text").fadeOut("slow");
      });
    $("#help").fadeTo(0,.2).hover(
      function(){
        $(this).fadeTo("slow", 1);
      },
      function(){
        $(this).fadeTo("slow", .2);
      }).click(
        function(){
          $("#help-text").fadeIn("slow");
        });
    $("#withDashes").toggle(
      function(){
        $("#withDashes .mark").html("&nbsp;");
        $("#input").keyup();
      },
      function(){
        $("#withDashes .mark").html("*");
        $("#input").keyup();
      });
    $("#input").click(
      function(){
        $(this).val("").unbind("click");
      });
    $("#header").show(
      "slide",
      {direction: "up"},
      "slow",
      function(){
        $("#input-wrap").show(
          "slide",
          {direction:"left"},
          "slow",
          function(){
            $("#output-wrap").show(
              "slide",
              {direction:"right"},
              "slow",
              function(){
                $("#footer").fadeIn(3000);
              });
          });
      });
    var input = $("#input");
    input.keyup(
      function(e){
        var val = input.val();
        var trans;
        if (val.match(/-[a-zA-Z]*?ay/g)){
          trans = toEnglish(val);
          $("#language").html("English");
          $("#latinOptions").fadeTo("fast",.1);
        } else {
          trans = toLatin(val, $("#withDashes .mark").html() === "*");
          $("#language").html("Pig Latin");
          $("#latinOptions").fadeTo("fast",1);
        }
        if (trans != input.val()){
          $("#output").html(trans);
        }
      });

    input.keyup();
  });

