(function($){$.decibel={rpc:{},view:{},widget:{DChildObjectsWidget:{},DLinkedObjectsWidget:{},DTextWidget:{},RichTextWidget:{}},regional:{}};$.decibel.loadedScripts=new Array();$.decibel.regional.changeEditingLanguage=function(language,force){if(language==$.decibel.regional.editingLanguage&&!force){return;}
var eventData={'language':language,previousLanguage:$.decibel.regional.editingLanguage,flag:$.decibel.regional.availableLanguages[language].flag,name:$.decibel.regional.availableLanguages[language].name,localName:$.decibel.regional.availableLanguages[language].localName};$.decibel.regional.editingLanguage=language;$.cookie('app-decibel-regional-editinglanguage',language,{path:'/'});$(document).trigger('app-decibel-regional-changeLanguage',eventData);}
$.decibel.rpc.callSync=function(procedure,parameters,ajaxOptions){procedure=procedure.replace(/app/,$.decibel.adminPath.replace(/\//,'')).replace(/\\/g,'/');if(procedure.charAt(procedure.length-1)!='/'){procedure=procedure+'/';}
var options={url:$.decibel.url+procedure,cache:false,error:$.decibel.rpc._handleError,success:$.decibel.rpc._handleSuccess,data:parameters,type:'POST',async:false};options=$.extend(ajaxOptions,options);return $.ajax(options);}
$.decibel.rpc.call=function(procedure,handler,parameters,context,ajaxOptions){procedure=procedure.replace(/app/,$.decibel.adminPath.replace(/\//,'')).replace(/\\/g,'/');if(procedure.charAt(procedure.length-1)!='/'){procedure=procedure+'/';}
var options={url:$.decibel.url+procedure,cache:false,error:$.decibel.rpc._handleError,success:$.decibel.rpc._handleSuccess,data:parameters,type:'POST'};options=$.extend(ajaxOptions,options);var request=$.ajax(options);$.decibel.rpc._requests[$.decibel.rpc._requests.length]={request:request,success:handler,procedure:procedure,parameters:parameters,context:context}}
$.decibel.rpc.requestsQueued=function(){return($.decibel.rpc._requests.length>0);}
$.decibel.rpc._requests=new Array();$.decibel.rpc._getRequestInformation=function(request){for(var i in $.decibel.rpc._requests){if($.decibel.rpc._requests[i].request===request){return $.decibel.rpc._requests[i];}}
return null;}
$.decibel.rpc._removeRequest=function(request){for(var i in $.decibel.rpc._requests){if($.decibel.rpc._requests[i].request===request){$.decibel.rpc._requests.splice(i,1);break;}}
if($.decibel.rpc._requests.length==0&&$.decibel.debugMode){$.ajax({url:$.decibel.url+$.decibel.adminPath+'decibel/debug/DGetDebugConsole/',cache:false,success:$.decibel.rpc._showDebugConsole,type:'POST'});}}
$.decibel.rpc._showDebugConsole=function(result){if(result){$('head').append(result);}}
$.decibel.rpc._handleSuccess=function(data,textStatus,request){var rpc=$.decibel.rpc._getRequestInformation(request);$.decibel.rpc._removeRequest(request);rpc.success.call(rpc.context,data,rpc.parameters);}
$.decibel.rpc._handleError=function(request,textStatus,errorThrown){if($.decibel.debugMode){var errorMessage=errorThrown?errorThrown:'Decibel Web Platform RPC Error: '+textStatus;if(typeof(console.error)!='undefined'){console.error(errorMessage);}else{alert(errorMessage);}}
$.decibel.rpc._removeRequest(request);}
$.decibel.widget.updateAutoComplete=function(event,data,formatted){var id=$(this).attr('id').replace(/_autocomplete/,'').replace(/\[/g,'\\[').replace(/\]/g,'\\]');if(typeof(data)!='undefined'){$('#'+id).val(data.value);}else{$('#'+id).val('');}}
$.decibel.widget.DChildObjectsWidget.init=function(){$(this).find('button').click($.decibel.widget.DChildObjectsWidget.add);var tags=$(this).find('.tags');tags.find('.tag .edit').click($.decibel.widget.DChildObjectsWidget.edit);tags.find('.tag .delete').click($.decibel.widget.DChildObjectsWidget.unlink);if($(this).find('.add .tag .ui-icon').length){tags.sortable({axis:'y'});}
var widget=$(this);$(document).bind('decibel-model-DModelAction-performStep',function(){$.decibel.widget.DChildObjectsWidget.submit.call(widget);});}
$.decibel.widget.DChildObjectsWidget.add=function(){var widget=$(this).parents('.app-decibel-widget-dchildobjectswidget');var parent=widget.parents('form').find('input[name="decibel-model-DModelAction-ids\[\]"]').val();var availableLanguages=new Array();$('input[name="availableLanguages\[\]"]:checked').each(function(){availableLanguages[availableLanguages.length]=$(this).val();})
ajaxParams={parent:parent,qualifiedName:widget.find('input[name="qualifiedName"]').val(),'availableLanguages':availableLanguages,id:0};$.showLoadingMessage();$.decibel.rpc.call('app/decibel/model/DGetEditHtml',$.decibel.widget.DChildObjectsWidget.showEditPopup,ajaxParams,widget.find('.add .tag'));}
$.decibel.widget.DChildObjectsWidget.edit=function(){var widget=$(this).parents('.app-decibel-widget-dchildobjectswidget');var tag=$(this).parents('.tag');var parent=widget.parents('form').find('input[name="decibel-model-DModelAction-ids\[\]"]').val();var availableLanguages=new Array();$('input[name="availableLanguages\[\]"]:checked').each(function(){availableLanguages[availableLanguages.length]=$(this).val();})
ajaxParams={parent:parent,qualifiedName:widget.find('input[name="qualifiedName"]').val(),'availableLanguages':availableLanguages,id:tag.find('.childId').val()};if(ajaxParams.id.indexOf('_')===0){ajaxParams.id=0;}
tag.find('.fields input,select,textarea').each(function(){ajaxParams[$(this).attr('name')]=$(this).val();});$.showLoadingMessage();$.decibel.rpc.call('app/decibel/model/DGetEditHtml',$.decibel.widget.DChildObjectsWidget.showEditPopup,ajaxParams,tag);}
$.decibel.widget.DChildObjectsWidget.showEditPopup=function(output){var displayName=$(this).parents('.app-decibel-widget-dchildobjectswidget').find('input[name="displayName"]').val();var newChild=$(this).find('.childId').val();newChild=(newChild==0||newChild.indexOf('_')===0);var popup=$('#App_ChildObjectsSelector_popup');popup.find('.error').html('');popup.find('.content').html(output);popup.dialog('option','title',(newChild?'Edit ':'New ')+displayName);popup.dialog('option','width','600px');popup.dialog('open');$('#ui-dialog-title-App_ChildObjectsSelector_popup').after($('#decibel-regional-DMultiLingual-language'));popup.get(0).link=$(this);$.hideLoadingMessage();}
$.decibel.widget.DChildObjectsWidget.canSave=function(){var popup=$('#App_ChildObjectsSelector_popup');popup.find('.app-decibel-widget-dtextwidget .multilingual, .app-decibel-widget-richtextwidget .multilingual').each(function(){var multiLingual=$(this);multiLingual.siblings('input,textarea').change()});var tag=popup.get(0).link;var widget=tag.parents('.app-decibel-widget-dchildobjectswidget');var parent=widget.parents('form').find('input[name="decibel-model-DModelAction-ids\[\]"]').val();var availableLanguages=new Array();$('input[name="availableLanguages\[\]"]:checked').each(function(){availableLanguages[availableLanguages.length]=$(this).val();})
var ajaxParams={parent:parent,qualifiedName:widget.find('input[name="qualifiedName"]').val(),'availableLanguages':availableLanguages,id:tag.find('.childId').val()};if(ajaxParams.id.indexOf('_')===0){ajaxParams.id=0;}
popup.find('input:not(:radio),input:radio:checked,select,textarea').not(':disabled').each(function(){if($(this).attr('name')){ajaxParams[$(this).attr('name')]=$(this).val();}});$.showLoadingMessage();$.decibel.rpc.call('app/decibel/model/DCanSave',$.decibel.widget.DChildObjectsWidget.save,ajaxParams,tag);}
$.decibel.widget.DChildObjectsWidget.save=function(result){var popup=$('#App_ChildObjectsSelector_popup');var tag=$(this);var widget=tag.parents('.app-decibel-widget-dchildobjectswidget');if(!result.success){popup.find('> .error').html(result.__toString);var qualifiedName=widget.find('input[name="qualifiedName"]').val().replace(/\\/g,'-');for(var field in result.messages){$('#'+qualifiedName+'-'+field).parents('.app-decibel-widget-dwidget').addClass('error');}}else{var id=tag.find('.childId').val();if(id==0){var newChildCount=widget.find('input[name="newChildCount"]');tag=tag.clone();tag.find('.childId').val('_'+newChildCount.val());tag.find('.edit').click($.decibel.widget.DChildObjectsWidget.edit);tag.find('.delete').click($.decibel.widget.DChildObjectsWidget.unlink);widget.find('.tags').append(tag);newChildCount.val(parseInt(newChildCount.val(),10)+1);}
tag.find('.name').html(result.objectTitleField);var field=tag.find('.fields').html('');popup.find('input:not(:radio),input:radio:checked,select,textarea').not(':disabled').each(function(){if($(this).attr('name')){field.append('<input type="hidden" name="'+$(this).attr('name')+'" value="'+($('<div/>').text($(this).val()).html().replace(/"/g,'&quot;'))+'" />');}});popup.dialog('close');}
$.hideLoadingMessage();}
$.decibel.widget.DChildObjectsWidget.unlink=function(){var widget=$(this).parents('.app-decibel-widget-dchildobjectswidget');var model=widget.find('.add button').text().replace(/Add /,'');if(confirm('Are you sure you want to remove this '+model+'?')){ajaxParams={};ajaxParams.id=$(this).parents('.tag').find('input.childId').val();if(ajaxParams.id.indexOf('_')===0||ajaxParams.id==0){$.decibel.widget.DChildObjectsWidget.unlinkResult.call($(this));}else{$.showLoadingMessage();$.decibel.rpc.call('app/decibel/model/DCanDelete',$.decibel.widget.DChildObjectsWidget.unlinkResult,ajaxParams,$(this));}}}
$.decibel.widget.DChildObjectsWidget.unlinkResult=function(result){if(result!=null&&!result.success){var message=result.object+' could not be deleted.\n';for(var i=0;i<result.messages.length;i++){message+='- '+result.messages[i]+'\n';}
alert(message);}else{$(this).parents('.tag').remove();}
$.hideLoadingMessage();}
$.decibel.widget.DChildObjectsWidget.submit=function(){var popup=$('#App_ChildObjectsSelector_popup');popup.find('.content').html('');var field=$(this).find('input[name="field"]').val();var tags=$(this).find('.tags .tag');if(tags.length){var position=0;tags.each(function(){var id=$(this).find('.childId').val();$(this).find('.fields').append('<input type="hidden" name="child_position" value="'+position+++'" />');$(this).find('.fields input').each(function(){var childField=$(this).attr('name');if(childField.indexOf('[')>-1){childField="["+childField.replace('[','][');}else{childField="["+childField+"]";}
$(this).attr('name',field+'['+id+']'+childField);});});}else{$(this).append('<input type="hidden" name="'+field+'" value="" />');}}
$.decibel.widget.DChildObjectsWidget.onPopupClose=function(){$('#decibel_menu').after($('#decibel-regional-DMultiLingual-language'));}
$.decibel.widget.DLinkedObjectsWidget.init=function(){$(this).find('button').click(function(){var add=$(this).parents('.add');var linkId=add.find('input[type="hidden"]');var linkName=add.find('input[type="text"]');var model=add.find('.model').text();if(linkId.val()==''){alert('Please select a '+model+' to add.');return false;}
var widget=$(this).parents('.app-decibel-widget-dlinkedobjectswidget');if(widget.find('.tags input[value="'+linkId.val()+'"]').length){alert('The selected '+model+' has already been added.')
linkId.val('');linkName.val('');return false;}
tag=add.find('.tag').clone();tag.find('.name').text(linkName.val());tag.find('input').val(linkId.val()).prop('disabled',false);tag.find('img').click($.decibel.widget.DLinkedObjectsWidget.unlink);widget.find('.tags').append(tag);linkId.val('');linkName.val('');return true;});var tags=$(this).find('.tags');tags.find('.tag img').click($.decibel.widget.DLinkedObjectsWidget.unlink);if($(this).find('.add .tag .ui-icon').length){tags.sortable({axis:'y'});}}
$.decibel.widget.DLinkedObjectsWidget.unlink=function(){$(this).parents('.tag').remove();}
$.decibel.widget.DTextWidget.autocompleteOptions={focus:function(event,ui){$(this).val(ui.item.label);return false;},change:function(event,ui){if($(this).autocomplete('option','mustMatch')&&!ui.item){$(this).autocomplete('selectFirstItem');}},select:function(event,ui){var hiddenId='#'+$(this).attr('id').replace(/_autocomplete/,'');$(this).val(ui.item.label);$(hiddenId).val(ui.item.value);$(hiddenId).change();return false;}};$.decibel.widget.DTextWidget.autocomplete=function(input,options){options=$.extend($.decibel.widget.DTextWidget.autocompleteOptions,options);input.autocomplete(options).focus($.decibel.widget.DTextWidget.selectAll).mouseup($.decibel.widget.DTextWidget.selectAll).keypress($.decibel.widget.DTextWidget.onEnterPress);input.data('autocomplete')._renderItem=$.decibel.widget.DTextWidget.render;input.data('autocomplete').selectFirstItem=$.decibel.widget.DTextWidget.selectFirstItem;}
$.decibel.widget.DTextWidget.selectAll=function(event){$(this).select();event.stopPropagation();event.preventDefault();return false;};$.decibel.widget.DTextWidget.onEnterPress=function(event){if(event.which==13){$(this).blur();}};$.decibel.widget.DTextWidget.render=function(ul,item){return $('<li></li>').data('item.autocomplete',item).append('<a>'+item.label+'</a>').appendTo(ul);};$.decibel.widget.DTextWidget.selectFirstItem=function(){var input=$(this.element);var hiddenInput=$('#'+input.attr('id').replace(/_autocomplete/,''));var match,matches=this.menu.element.find('li');var li=matches.first();for(i=1;i<matches.length;++i){match=matches.eq(i);if(match.text().toLowerCase()==input.val().toLowerCase()){li=match;break;}}
if(input.val()){var matcher=new RegExp(input.val().replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),'i');if(matcher.test(li.text())){var data=li.data('item.autocomplete');input.val(data.label);hiddenInput.val(data.value);}else{input.val('');hiddenInput.val('');}}};$.decibel.widget.RichTextWidget.disable=function(editor){var s=editor.settings,DOM=tinymce.DOM,d=editor.getDoc();if(!s.readonly){if(!tinymce.isIE){try{d.designMode='Off';}catch(ex){}}else{b=editor.getBody();DOM.hide(b);b.contentEditable=false;DOM.show(b);}
s.readonly=true;var i=1,toolbar;while(toolbar=DOM.get(editor.id+'_toolbar'+i)){toolbar.style.display='none';++i;}
DOM.get(editor.id).disabled=true;}}
$.decibel.widget.RichTextWidget.enable=function(editor){var s=editor.settings,DOM=tinymce.DOM,d=editor.getDoc();if(s.readonly){if(!tinymce.isIE){try{d.designMode='On';d.designMode='Off';d.designMode='On';}catch(ex){}}else{b=editor.getBody();DOM.hide(b);b.contentEditable=true;DOM.show(b);}
s.readonly=false;var i=1,toolbar;while(toolbar=DOM.get(editor.id+'_toolbar'+i)){toolbar.style.display='block';++i;}
DOM.get(editor.id).disabled=false;}}
$.decibel.widget.RichTextWidget.pasteCleanup=function(pl,o){o.content=o.content.replace(/<title>.*?<\/title>/gi,'').replace(/<(\/?)div[^>]*>/gi,'<$1p>').replace(/<!--[^\0]*?-->/gi,'').replace(/<style[^>]*>[^<]*<\/style>/gi,'');}
$.fn.originalHtml=$.fn.html;$.fn.html=function(htmlString){if(htmlString!==null&&typeof(htmlString)!='undefined'){if(typeof(htmlString)=='object'&&typeof(htmlString.html)=='string'){this.originalHtml(htmlString.html);var src,tags='';for(i in htmlString.scriptIncludes){if(typeof(htmlString.scriptIncludes[i])=='function'){continue;}
src=htmlString.scriptIncludes[i].src;if($.inArray(src.replace(/(\.[0-9]+)?\.js(\?.*)?/,'.js'),$.decibel.loadedScripts)==-1){tags+='<script type="text/javascript" src="'+src+'"></script>';$.decibel.loadedScripts[$.decibel.loadedScripts.length]=src.replace(/(\.[0-9]+)?\.js(\?.*)?/,'.js')}}
for(i in htmlString.scripts){if(typeof(htmlString.scripts[i])=='function'){continue;}
tags+='<script type="text/javascript">'+htmlString.scripts[i].code+'</script>';}
for(i in htmlString.stylesheetIncludes){if(typeof(htmlString.stylesheetIncludes[i])=='function'){continue;}
tags+='<link type="text/css" rel="stylesheet" href="'+htmlString.stylesheetIncludes[i].href+'" media="'+htmlString.stylesheetIncludes[i].media+'" />';}
var styles='';for(i in htmlString.styles){if(typeof(htmlString.styles[i])=='function'){continue;}
styles+=htmlString.styles[i].selector+'{ '+htmlString.styles[i].value+' }';}
if(styles!=''){tags+='<style type="text/css">'+styles+'</style>';}
$('body').append(tags);$(document).trigger('contentReady',[$(this)]);if($.decibel.regional.editingLanguage){$.decibel.regional.changeEditingLanguage($.decibel.regional.editingLanguage,true);}
return this;}else{return this.originalHtml(htmlString);}}
return this.originalHtml();}
$.fn.limitInputCharacters=function(availableCharacters){$(this).keypress(function(event){if(event.which==0||event.which==8||event.ctrlKey){return true;}
keyCharacter=String.fromCharCode(event.which);if(availableCharacters.indexOf(keyCharacter)==-1){event.stopPropagation();return false;}
return true;});}
$(document).bind('contentReady',function(event,content){content.find('script[src]').each(function(){$.decibel.loadedScripts[$.decibel.loadedScripts.length]=$(this).attr('src').replace(/(\.[0-9]+)?\.js(\?.*)?/,'.js');});if($.decibel.regional.availableLanguages&&$($.decibel.regional.availableLanguages).length){var editingLanguage=$.cookie('app-decibel-regional-editinglanguage');if(editingLanguage&&$.decibel.regional.availableLanguages[editingLanguage]){$.decibel.regional.changeEditingLanguage(editingLanguage,true);}else{$.decibel.regional.changeEditingLanguage($.decibel.regional.currentLanguage,true);}}
content.find('#app-decibel-model-DModel-defaultLanguage').change(function(){$('.app-decibel-regional-availablelanguages.'+$(this).val()).prop('checked',true);$(document).trigger('changeAvailableLanguages',[$(this).val(),true]);});content.find('.app-decibel-regional-availablelanguages').change(function(){var checkbox=$(this);var defaultLanguage=$('#app-decibel-model-DModel-defaultLanguage').val();if(!checkbox.prop('checked')&&checkbox.val()==defaultLanguage){checkbox.prop('checked',true)
return;}
$(document).trigger('changeAvailableLanguages',[checkbox.val(),checkbox.prop('checked')]);}).change();content.find('.app-decibel-widget-dwidget').find('input,textarea,select,checkbox').focus(function(){$(this).parents('.app-decibel-widget-dwidget').addClass('focussed');}).blur(function(){$(this).parents('.app-decibel-widget-dwidget').removeClass('focussed');});content.find('.app-decibel-widget-dwidget.error').find('input,textarea,select,checkbox').keypress(function(){$(this).parents('.app-decibel-widget-dwidget').removeClass('error');}).change(function(){$(this).parents('.app-decibel-widget-dwidget').removeClass('error');});content.find('.app-decibel-widget-dtextwidget .multilingual, .app-decibel-widget-richtextwidget .multilingual').each(function(){var multiLingual=$(this);multiLingual.siblings('input,textarea').blur(function(){multiLingual.find('#'+$(this).attr('id')+'_'+$.decibel.regional.editingLanguage).val($(this).val());})});content.find('.app-decibel-widget-dbooleanwidget-checkbox').each(function(){$(this).find('input[type="checkbox"]').click(function(){$(this).parents('.app-decibel-widget-dbooleanwidget-checkbox').find('input[type="hidden"]').val($(this).prop('checked')?1:0);})});content.find('.app-decibel-widget-dlinkedobjectswidget').each($.decibel.widget.DLinkedObjectsWidget.init);content.find('.app-decibel-widget-dchildobjectswidget').each($.decibel.widget.DChildObjectsWidget.init);});$(document).bind('changeAvailableLanguages',function(event,language,available){if(!available&&language==$.decibel.regional.editingLanguage){var defaultLanguage=$('#app-decibel-model-DModel-defaultLanguage').val();$.decibel.regional.changeEditingLanguage(defaultLanguage);}
$('#decibel-regional-DMultiLingual-language .'+language).toggle(available);});})(jQuery);$.getScript=function(url,callback,preloadedCallback){if(preloadedCallback==null&&callback){preloadedCallback=callback;}
url=url.replace(/(\.[0-9]+)?\.js(\?.*)?/,'.js');if($.inArray(url,$.decibel.loadedScripts)>=0){if(preloadedCallback){preloadedCallback();}
return false;}
$.decibel.loadedScripts[$.decibel.loadedScripts.length]=url;return true;};$.loadStylesheet=function(href,media){if(media==null){media='all';}
$('head').append('<link rel="stylesheet" type="text/css" media="'
+media+'" href="'+href+'" />');}
jQuery.fn.sort=function(){return this.pushStack([].sort.apply(this,arguments),[]);};jQuery.fn.sortOptions=function(sortCallback){jQuery('option',this).sort(sortCallback).appendTo(this);return this;};jQuery.fn.sortOptionsByText=function(){var byTextSortCallback=function(x,y){var xText=jQuery(x).text().toUpperCase();var yText=jQuery(y).text().toUpperCase();return(xText<yText)?-1:(xText>yText)?1:0;};return this.sortOptions(byTextSortCallback);};jQuery.fn.sortOptionsByValue=function(){var byValueSortCallback=function(x,y){var xVal=jQuery(x).val();var yVal=jQuery(y).val();return(xVal<yVal)?-1:(xVal>yVal)?1:0;};return this.sortOptions(byValueSortCallback);};if(typeof(jQuery.fn.prop)=='undefined'){jQuery.fn.prop=function(name,value){return $(this).attr(name,value);}}
