(function($){
var last="";
var orgsrc="";
  $.fn.emoticate = function(options){
  
  $("#iconreset").hide();

    var defaults = ({
      replacediv:   'replaceme', 
      image_path:   '/images/emoticons/', 
      speed:        500, 
      icon:         'smiley',
      iconfield:	'icon',
      curicon: '0'
    });
    var options = $.extend(defaults, options);
    var newimage="";

    return this.each(function() {
      var select = $('select', this);
      var area = $("#" + defaults.iconfield);
      var icon = $("#" + defaults.icon, this);
      
      var emo ='<div class="emoticon-box">';
      $('option', select).each(function () {
        var option    = $(this);
        var emocode        = option.val();
        var title    = option.text();
        var link = $.fn.emoticate.emoticlick(title, emocode, defaults.image_path);

	if (defaults.curicon>0 && emocode==defaults.curicon)
	{
		newimage = defaults.image_path + title;
	}

        emo += '<div style="float:left;">'
            + link
            + '</div>';
      }); //end option
      
     
      emo += '</div>';
      orgsrc = icon.attr('src');

      select.remove(); //get rid of the select to make way for emotification
      $("#" + defaults.replacediv, this).html(emo); //put the emotification HTML in the right div
      $("#" + defaults.replacediv, this).hide(); //hide the emoticons
      
      var listdiv = $("#" + defaults.replacediv, this); //reset the var so it will work in the icon click.. this probably could be done better


	if (newimage!="")
	{
		icon.attr('src',newimage);
		$("#iconreset").show();
	}

      //bind the link for every a tag that is in the replaced div
      $('a', this).bind('click', function() {
        $.fn.emoticate.insertme(area, $(this).attr('id'),$("img", this).attr('src'),icon,$(this));
        listdiv.hide(defaults.speed);
        return false;
      }); //end bind
      
      $("#iconreset").bind('click',function() {
      	$.fn.emoticate.reset(area,icon);
    	$("#iconreset").hide();
    	
      });

      //click the icon
      $(icon, this).bind('click', function (){
      if (listdiv.is(':visible'))
      {
      	listdiv.hide(defaults.speed);
      }
      else
      {

        listdiv.show(defaults.speed);
       }
      });

    }); //end select

  }; //end function


$.fn.emoticate.reset = function(iconField,replace)
{
replace.attr('src',orgsrc);
iconField.val(0);
if (last!="")
{
	$("img", last).css("background", "transparent");
}
last="";
}

  $.fn.emoticate.emoticlick = function(id,emocode, image_path){
    return '<a href="#" id="' + emocode +'"><img src="' + image_path + id + '"></a>';
  };

$.fn.emoticate.insertme = function(iconField,iconId,img,replace,link){
$("#iconreset").show();
iconField.val(iconId);
$("img", link).css("background", "red");
if (last!="")
{
	$("img", last).css("background", "transparent");
}

last = link;

replace.attr('src',img);

  
  };

})(jQuery);

