/*
// jQuery translations
//
// Version 1.0 beta
//
// Paulius Rimavicius
// (http://clipper.lt/)
// 06 April 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=62 for more information
//
// Usage: $('#control_id').translations( options, callback )
//
// Licensing & Terms of Use
// 
// CopyLeft 
*/

if(jQuery) (function($)
{
    $.extend($.fn, {
    translations: function(o, callback) 
    {
        $(this).mouseover( function(e)
        {
            $(this).translations_add_edit();
        });
        /*$(this).mouseout( function(e)
        {
            $(this).translations_remove_edit();
        });*/
    },
    translations_add_edit: function(o, callback) 
    {
        $(this).each( function()
        {
           span = $(this);
           poss = span.offset();
           //alert(poss.left);
           html = '<div style="z-index:1;position:absolute;top:'+poss.top+'px;left:'+poss.left+'px;background-color:#fff;color:#000;border:1px solid #000;" id="'+$(span).attr('id')+'_div"><a href="javascript:;" onclick="$(\'#'+$(span).attr('id')+'_edit_div\').show();return false;">[edit]</a></div>';
           html += '<div onclick="return false;" style="padding:10px;color:#f00;z-index:2;display:none;position:absolute;top:'+poss.top+'px;left:'+poss.left+'px;background-color:#fff;border:1px solid #000;" id="'+$(span).attr('id')+'_edit_div"><b>Language: '+$(span).attr('lang')+'</b><br /><br /><textarea style="width:500px;height:200px;" lang="'+$(span).attr('lang')+'" id="'+$(span).attr('id')+'_edit">'+$(span).html()+'</textarea><br /><input type="button" value="save" onclick="$(\'#'+$(span).attr('id')+'_edit\').translations_save()" /></div>';
           $(span).after(html);
        });
    },
    translations_remove_edit: function(o, callback) 
    {
        $(this).each( function()
        {
            span = $(this);
            $("#"+$(span).attr('id')+"_div").remove();
            $("#"+$(span).attr('id')+"_edit_div").remove();
        });
    },
    translations_save: function(o, callback) 
    {
        //perduodame edit field'a 
        id = this.attr('id');
        value = this.val();
        
        span_id = "";
        if (id.substring(id.length-5)=='_edit')
        {
            span_id = id.substring(0,id.length-5);
        }
        //alert(span_id);
        
        str = "lang="+this.attr('lang');
	    str += "&key="+id;
	    str += "&value="+value;
        
        
	    //alert(str);
	    $.ajax({
	      type: "POST",
	      url: "/index.php/translations/save_translation",
	      data: str,
	      cache: false,
	      success: function(html)
	      {
	         //alert(html);
	         //$("#"+id+"_div").hide();
	         $("#"+span_id+"_div").remove();
             $("#"+span_id+"_edit_div").remove();
	         $("#"+span_id).html(value);
	         
	      }
	    });
    }
    });

})(jQuery);