// SmileyTest.js
// Javascript and jQuery code for Ning Networks
// Copyright 2009, B K Services Inc.  
// Tested with IE7 and FF3 and works.
// Known issue: Inserts at end of any text, not at cursor.
// A license to use this code is granted ONLY to members of TheNingMaster.com
// For more information, Email us: bill@bkserv.com

// ----- This will be in a separate file called MoreSmileyData.js -----
// var moreSmileys = { 
//  "path" : "http://www.bkserv.net/images/", 
//  "count" : "2",
//  "smiley" :
//    [
//        { "code" : "8)", "name" : "glasses" },
//        { "code" : "=:)", "name" : "hair" }
//    ]
// }

// Built-in Smileys:
var smileyName = ['Grin', 'Smile', 'Frown', 'Tongue'];
var smileyCode = [':D', ':\)', ':\(', ':P'];
var smileyRegex = [':D', ':\\)', ':\\(', ':P'];
var numSmileys = 4;
var imgStart = "<img src='http://www.bkserv.net/images/";
var imgEnd = ".gif' />";

// Global variables:
var moreOpen = false;


function showSmileys() {
// Smiley Injection - replace :D with grin, :) with smile, :( with frown, :P with Tongue.

  // First, replace codes in existing comments/posts:
  //  Profile pages: dl.comment dd
  //  Photo pages: dl.comment dd
  //  Video pages: dl.comment dd
  //  Forum reply: dl.discussion dd
  //  Group pages: dl.comment dd
  //  Event pages: dl.comment dd
//  x$("#comments dd, .comment dd, dd .description").each(function () {  // ORIG
  x$("dl.comment dd, dl.discussion dd").each(function () {
    var commentText = x$(this).html();

    var ix;
    var r;

    if (window.moreSmileys !== undefined) {
        // Turn moreSmileys codes into img tags:
        for (ix = 0; ix < moreSmileys.count; ix++) {
          r = new RegExp(encodeRE(moreSmileys.smiley[ix].code),"g");
          commentText = commentText.replace(r, imgStart + moreSmileys.smiley[ix].name + imgEnd);
        }
    }
    
    // Turn Built-in Smileys codes into img tags:
    for (ix = 0; ix < numSmileys; ix++) {
      r = new RegExp(smileyRegex[ix],"g");
      commentText = commentText.replace(r, imgStart + smileyName[ix] + imgEnd);
    }

    x$(this).html(commentText);
  });

  // Second, bind to mouseup so we call this again a second after a new comment/post is added:
  x$("input[value='Add Comment'], input[value='Add Reply']").mouseup(function() {
  // x$("p#add-comment").append('<span style="color:#F00;"> Mouse up.</span>');
    setTimeout("showSmileys()", 2000);
  });
}

function addToTextareaORIG(n) {
  var s = smileyCode[n];
  // textarea is named comment except for Forum, then its description
  x$('textarea[name="comment"], textarea[name="description"]').append(s);
}

function addToTextarea(n) {
  // textarea is named comment except for Forum, then its description

  var s ="";
  if (n < 4) 
    s = smileyCode[n];
  else
    if (window.moreSmileys !== undefined) s = moreSmileys.smiley[n - 4].code;
    
    
  var text = ' ' + s + ' ';

  var browser=navigator.appName;
  if (browser=="Microsoft Internet Explorer") {
    x$('textarea[name="comment"], textarea[name="description"]').append(text);
  }
  else {
    var iItem = 1;
    var areas = document.getElementsByName("description");
    if (areas.item(iItem) == null || areas.item(iItem).type != "textarea") { 
      areas = document.getElementsByName("comment");
      iItem = 0;
      if (areas.item(iItem) == null || areas.item(iItem).type != "textarea") { 
	    return -1;
  	  }
    }
    var txtarea = areas.item(iItem);
    txtarea.value  += text;
    txtarea.focus();
  } 
}

function buildSmileyBar() {
  var sStart = " <a onclick='addToTextarea(";
  var sMid = ")'>";
  var sOut = "";

  var ix;
  for (ix = 0; ix < numSmileys; ix++) {
    sOut += sStart + ix + sMid + imgStart + smileyName[ix] + imgEnd + "</a>";
  }

  return sOut;
}

function getMoreSmileys() {
  var sStart = " <a onclick='addToTextarea(";
  var sMid = ")'>";
  var sOut = "";

  if (window.moreSmileys !== undefined) {
      var ix;
      for (ix = 0; ix < moreSmileys.count; ix++) {
        sOut += sStart + (ix + 4) + sMid + imgStart + moreSmileys.smiley[ix].name + imgEnd + "</a>";
      }
  }
  
  return sOut;
}

function showMore() {
    if (moreOpen==false) {
        x$('span#smileyBar').after('<div id="moreSmileys">MORE Smileys | <a onclick="closeMore()">Close</a><br />' + getMoreSmileys() + '<br /></div>');
        moreOpen=true;
    }
}

function closeMore() {
    x$('div#moreSmileys').remove();
    moreOpen=false;
}
        
function encodeRE(s) { 
    return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); 
}

// +++++++++++++++++++++++++++++++++++ READY FUNCTION ++++++++++++++++++++++++++++++++++++++++++++++++++
x$(document).ready(function() {

var smileyBar = buildSmileyBar();

  // Inject the Smiley Bar:
  x$("input[value='Add Comment'], input[value='Add Reply']")
    .before("<span id='smileyBar' style='float:left'> Click to Insert: " + smileyBar + " | <a onclick='showMore()'>more</a> </span>");

  showSmileys();

});

