(function($){$.transcript={setupForEditing:function(sourceTextElement,editingElement,wordIdPrefix,beginIndex,endIndex){$.transcript.splitWords(sourceTextElement,editingElement,wordIdPrefix,beginIndex,endIndex,function(wordSpan){$(wordSpan).hover(function(){$(this).addClass("highlightWord");},function(){$(this).removeClass("highlightWord");});$(wordSpan).mouseup(function(){var currentlyEditing=$("#editTranscript").data("currentlyEditing");if($(this).attr("id")==currentlyEditing){return;}
var editingElement;if(currentlyEditing){editingElement=$("#"+currentlyEditing);}
if(editingElement&&$(editingElement).data("editing")=="true"){var wordIndex=$.transcript.getWordIndex($(this));$.transcript.applyEdit(editingElement,wordIndex);}else{$.transcript.beginEdit($(this));}
return false;});});},beginEdit:function(element){if($(element).data("editing")=="true"){return;}
var id=$(element).attr("id");$("#editTranscript").data("currentlyEditing",id);$(element).removeClass("highlightWord");$(element).data("editing","true");$(element).data("originalHtml",$(element).html());$(element).data("originalText",$(element).text());var inputElement=$.transcript.createInput(element);$(element).html("");$(element).append(inputElement);$(element).bind("keydown",function(e){var key=e.keyCode||e.which;if(key==13){e.preventDefault();$.transcript.applyEdit(this,null);}else if(key==9){e.preventDefault();if(e.shiftKey){$.transcript.applyEdit(this,"previous");}else{$.transcript.applyEdit(this,"next");}}else if(key==27){e.preventDefault();$.transcript.cancelEdit(this);}});if(beginEditCallback){beginEditCallback($.transcript.getWordIndex(element));}
inputElement.focus();inputElement.select();},createInput:function(element){var inputWidth=$(element).width();if(inputWidth<30){inputWidth=30;}
var inputHeight=$(element).height();var result=$("<input type='text' class='Detail_Input_Active' id='input_"+$(element).attr("id")+"' autocomplete='off' style='width:"+inputWidth+"px; height: "+inputHeight+"px; display:inline;'>");$(result).val($(element).text());return result;},cancelEdit:function(element){$(element).html($(element).data("originalHtml"));$.transcript.cleanupElementData(element);if(finishedEditingCallback){finishedEditingCallback();}},cleanupElementData:function(element){$(element).removeData("originalHtml");$(element).removeData("originalText");$(element).removeData("editing");$("#editTranscript").removeData("currentlyEditing");},getWordIndex:function(element){var result=$(element).attr("id");if(result){result=result.substring(result.indexOf("_")+1);}
return result;},applyEdit:function(element,goTo){var wordIndex=$.transcript.getWordIndex(element);var oldValue=$(element).data("originalText").replace(/\u00A0/g," ");var newValue=$(element).children(":first").val().replace(/\u00A0/g," ");if(oldValue==newValue){$(element).html($(element).data("originalHtml"));}else{var textToSet=newValue.replace(/\u0020/g,"\u00A0");$(element).html(textToSet);}
var transcriptText="";var preText=$("#editTranscript").data("preString");if(preText){transcriptText+=preText;}
var editText=$("#transcriptEditDiv").text();if(editText){transcriptText+=editText;}
var postText=$("#editTranscript").data("postString");if(postText){transcriptText+=postText;}
transcriptText=transcriptText.replace(/\u00A0/g," ");$("#transcriptText").val(transcriptText);$.transcript.cleanupElementData(element);if(oldValue!=newValue||goTo){updateTranscriptWord(wordIndex,oldValue,newValue,goTo);}
if(!goTo&&finishedEditingCallback){finishedEditingCallback();}},splitWords:function(sourceTextElement,destinationElement,wordIdPrefix,beginIndex,endIndex,perWordFunction){var text=$(sourceTextElement).val();var textToSplit;if(beginIndex>-1&&endIndex>-1){textToSplit=text.substring(beginIndex,endIndex);}else if(beginIndex>-1){textToSplit=text.substring(beginIndex);}else if(endIndex>-1){textToSplit=text.substring(0,endIndex);}else{textToSplit=text;}
$(destinationElement).html("");if(beginIndex>0){var preString=text.substring(0,beginIndex);$("#editTranscript").data("preString",preString);preString=preString.replace(/\u0020$/,"&nbsp;");$(destinationElement).append("<div id='transcriptPreEdit' style='display:inline;'>"+preString+"</div>");}else{$("#editTranscript").data("preString","");}
var editDiv=$("<div id='transcriptEditDiv' style='display:inline'></div>");$(destinationElement).append(editDiv);var pattern=/[\w']+/;if(pattern.source&&!pattern.global){var patternMods=(pattern.ignoreCase)?"ig":"g";patternMods+=(pattern.multiline)?"m":"";pattern=new RegExp(pattern.source,patternMods);}
var spaces=textToSplit.split(pattern),words=textToSplit.match(pattern);if(spaces[0]==''){spaces.shift();}
if(words[0]==''){words.shift();}
if(spaces[spaces.length-1]==''){spaces.pop();}
if(words[words.length-1]==''){words.pop();}
var startsWithSpace=textToSplit.indexOf(spaces[0])==0;var index=0;for(var i=0;i<Math.max(spaces.length,words.length);i++){var array=startsWithSpace?spaces:words;if(array[i]){$.transcript.createWordSpan(wordIdPrefix+(index++),array[i],startsWithSpace,editDiv,perWordFunction);}
array=startsWithSpace?words:spaces;if(array[i]){$.transcript.createWordSpan(wordIdPrefix+(index++),array[i],!startsWithSpace,editDiv,perWordFunction);}}
if(endIndex>-1){var postString=text.substring(endIndex);$("#editTranscript").data("postString",postString);postString=postString.replace(/\u0020/,"&nbsp;");$(destinationElement).append("<div id='transcriptPostEdit' style='display:inline;'>"+postString+"</div>");}else{$("#editTranscript").data("postString","");}},createWordSpan:function(id,text,isSpace,editDiv,perWordFunction){if(text&&text!=''){if(isSpace){text=text.replace(/\u0020/g,"&nbsp;");}
var s="<span id='"+id+"' class='editableWord'>"+text+"</span>";if(isSpace){s+="<wbr/>";}
var wordSpan=$(s);$(editDiv).append(wordSpan);if(perWordFunction){perWordFunction(wordSpan);}}}}})(jQuery);