/*!
* Twinstone TDI (http://wiki.twinstone.org/display/TDI)
* Version: 1.7.6
*
* Copyright 2013 Etnetera a.s. http://www.etnetera.cz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if(typeof jQuery=="undefined"){throw ("Missing dependency: jQuery!");}var TDI=function($){return{};}(jQuery);TDI.Ajax=function($){var _delegateSelectors={linkClick:"a.ajaxlink, a.tdi, a.infuse",formSubmit:"form.ajaxform, form.tdi, form.infuse",formButtonActivate:"form.ajaxform [type=submit], form.tdi [type=submit], form.infuse [type=submit]",fieldChange:"select.ajaxselect, select.tdi, select.infuse, input[type=checkbox].tdi, input[type=checkbox].infuse, input[type=radio].tdi, input[type=radio].infuse",fieldSubmit:"input[type=text].tdi, input[type=text].infuse"};function _bindUI(){$(document).delegate(_delegateSelectors.formSubmit,"submit",_onBeforeFormSubmit).delegate(_delegateSelectors.linkClick,"click",_onBeforeLinkClick).delegate(_delegateSelectors.formButtonActivate,"click",_onFormButtonActivate).delegate(_delegateSelectors.fieldChange,"change",_onFieldChange).delegate(_delegateSelectors.fieldSubmit,"keydown",_onFieldSubmit);$(window).on("unload",_unbindUI);$.event.special["tdi:ajax:beforeLinkClick"]={_default:_onLinkClick};$.event.special["tdi:ajax:beforeFormSubmit"]={_default:_onFormSubmit};}function _unbindUI(evt){$(document).undelegate(_delegateSelectors.linkClick,"click",_onLinkClick).undelegate(_delegateSelectors.formSubmit,"submit",_onBeforeFormSubmit).undelegate(_delegateSelectors.formButtonActivate,"click",_onFormButtonActivate).undelegate(_delegateSelectors.fieldChange,"change",_onFieldChange).undelegate(_delegateSelectors.fieldSubmit,"keydown",_onFieldSubmit);}function _onBeforeLinkClick(evt){evt.preventDefault();$(this).trigger("tdi:ajax:beforeLinkClick",{link:$(this)});return false;}function _onLinkClick(evt){TDI.Ajax.send(evt.target);}function _onBeforeFormSubmit(evt){evt.preventDefault();$(this).trigger("tdi:ajax:beforeFormSubmit",{form:$(this)});return false;}function _onFormSubmit(evt){TDI.Ajax.send(evt.target);}function _onFormButtonActivate(evt){var $button=$(this),$form=$(this.form);$form.data("_submitButton",$button);if($button.attr("name")){$form.find("input.submit-action").remove();$("").attr("type","hidden").attr("name",$button.attr("name")).attr("value",$button.attr("value")).addClass("submit-action").appendTo($form);}}function _onFieldChange(evt){TDI.Ajax.send($(this).data("ajax-url")?this:this.form);}function _onFieldSubmit(evt){if(evt.keyCode===13&&$(this).data("ajax-url")){evt.preventDefault();TDI.Ajax.send(this);}}_bindUI();return{send:function(elm,callbacks){callbacks=callbacks||{};var $elm=$(elm),name=$elm.attr("name"),value=$elm.val(),confirm=$elm.data("confirm"),related=$elm.closest($elm.data("related-ancestor")||"").add($($elm.data("related-element")||"")).add($($elm.attr("rel")||"")).add($($elm.data("_submitButton")||"")),triggerGroup=$($elm.data("trigger-group")||""),url=$elm.data("ajax-url")||$elm.attr("href")||$elm.attr("action"),xhrFields=$elm.data("ajax-xhr-fields")||{},data={};if((url===""||url===undefined)&&value){url=value;}if(name&&value){if($elm.is("input[type=checkbox]")&&$elm.prop("checked")===false){data[name]=0;}else{data[name]=value;}}if($elm.is("[disabled], .disabled")){return false;}if(confirm&&!window.confirm(confirm)){return false;}var _options={beforeStart:function(){var res=callbacks.beforeStart&&callbacks.beforeStart.apply(this,arguments);if(typeof res==="undefined"||res===true){$elm.add(related).addClass("loading");triggerGroup.each(function(){var $trigger=$(this);if(!$trigger.hasClass("disabled")&&!$trigger.prop("disabled")){$trigger.addClass("disabled").prop("disabled",true);$trigger.data("_disabled",true);}});related.get(0)&&related.addClass("loading-target");callbacks.start&&callbacks.start.apply(this,arguments);return true;}return false;},beforeEnd:function(){var res=callbacks.beforeEnd&&callbacks.beforeEnd.apply(this,arguments);if(typeof res==="undefined"||res===true){$elm.add(related).removeClass("loading");triggerGroup.each(function(i,$trigger){var $trigger=$(this);if($trigger.data("_disabled")===true){$trigger.removeClass("disabled").prop("disabled",false);$trigger.data("_disabled",false);}});related.get(0)&&related.removeClass("loading-target");callbacks.end&&callbacks.end.apply(this,arguments);}},data:data,trigger:$elm,xhrFields:xhrFields};if($elm.is("form")){_options.end=function(){$elm.data("_submitButton",null);$elm.find("input.submit-action").remove();};if($elm.find("input[type=file]").length>0){TDI.Ajax.Request.sendForm($elm[0],_options);}else{_options.data=$elm.serialize();_options.method=$elm.attr("method");TDI.Ajax.Request.send(url,_options);}}else{TDI.Ajax.Request.send(url,_options);}}};}(jQuery);TDI.Ajax.Request=function($){return{send:function(url,options){options=options||{};options.url=TDI.Ajax.Request.ajaxifyUrl(url);$.ajax({url:options.url,xhrFields:options.xhrFields||{},type:options.method||"GET",async:!options.sync,data:options.data||"",dataType:"xml",beforeSend:function(xhr,settings){var res=options.beforeStart&&options.beforeStart(xhr,settings,options);if(typeof res==="undefined"||res===true){$(document).trigger("tdi:ajax:_start",{xhr:xhr,settings:settings,options:options});options.start&&options.start(xhr,settings,options);return true;}return false;},success:function(data,textStatus,xhr){$(document).trigger("tdi:ajax:_success",{data:data,textStatus:textStatus,xhr:xhr,options:options});options.success&&options.success(data,textStatus,xhr,options);},error:function(xhr,textStatus,error){$(document).trigger("tdi:ajax:_error",{xhr:xhr,textStatus:textStatus,error:error,options:options});options.error&&options.error(xhr,textStatus,error,options);},complete:function(xhr,textStatus){var res=options.beforeEnd&&options.beforeEnd(xhr,textStatus,options);if(typeof res==="undefined"||res===true){$(document).trigger("tdi:ajax:_end",{xhr:xhr,textStatus:textStatus,options:options});options.end&&options.end(xhr,textStatus,options);}}});},sendForm:function(form,options){options=options||{};var $form=$(form),$submitButton=$form.data("_submitButton"),url=$form.data("ajax-url")||$form.attr("action");options.url=TDI.Ajax.Request.ajaxifyUrl(url);var res=options.beforeStart&&options.beforeStart($form,options);if(res===false){return false;}$(document).trigger("tdi:ajax:_start",{xhr:$form,settings:null,options:options});options.start&&options.start($form,options);if($submitButton){$submitButton.addClass("loading");}var iframeName="form_iframe_"+(new Date()).getTime(),iframe;if(document.documentMode&&document.documentMode<=8){iframe=document.createElement('