(function($){var functions={add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'},wpList;wpList={settings:{url:ajaxurl,type:'POST',response:'ajax-response',what:'',alt:'alternate',altOffset:0,addColor:'#ffff33',delColor:'#faafaa',dimAddColor:'#ffff33',dimDelColor:'#ff3333',confirm:null,addBefore:null,addAfter:null,delBefore:null,delAfter:null,dimBefore:null,dimAfter:null},nonce:function(element,settings){var url=wpAjax.unserialize(element.attr('href')),$element=$('#'+settings.element);return settings.nonce||url._ajax_nonce||$element.find('input[name="_ajax_nonce"]').val()||url._wpnonce||$element.find('input[name="_wpnonce"]').val()||0;},parseData:function(element,type){var data=[],wpListsData;try{wpListsData=$(element).data('wp-lists')||'';wpListsData=wpListsData.match(new RegExp(type+':[\\S]+'));if(wpListsData){data=wpListsData[0].split(':');}}catch(error){}
return data;},pre:function(list,settings,action){var $element,backgroundColor,confirmed;settings=$.extend({},this.wpList.settings,{element:null,nonce:0,target:list.get(0)},settings||{});if(typeof settings.confirm==='function'){$element=$('#'+settings.element);if('add'!==action){backgroundColor=$element.css('backgroundColor');$element.css('backgroundColor','#ff9966');}
confirmed=settings.confirm.call(this,list,settings,action,backgroundColor);if('add'!==action){$element.css('backgroundColor',backgroundColor);}
if(!confirmed){return false;}}
return settings;},ajaxAdd:function(element,settings){var list=this,$element=$(element),data=wpList.parseData($element,'add'),formValues,formData,parsedResponse,returnedResponse;settings=settings||{};settings=wpList.pre.call(list,$element,settings,'add');settings.element=data[2]||$element.prop('id')||settings.element||null;settings.addColor=data[3]?'#'+data[3]:settings.addColor;if(!settings){return false;}
if(!$element.is('[id="'+settings.element+'-submit"]')){return!wpList.add.call(list,$element,settings);}
if(!settings.element){return true;}
settings.action='add-'+settings.what;settings.nonce=wpList.nonce($element,settings);if(!wpAjax.validateForm('#'+settings.element)){return false;}
settings.data=$.param($.extend({_ajax_nonce:settings.nonce,action:settings.action},wpAjax.unserialize(data[4]||'')));formValues=$('#'+settings.element+' :input').not('[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]');formData=typeof formValues.fieldSerialize==='function'?formValues.fieldSerialize():formValues.serialize();if(formData){settings.data+='&'+formData;}
if(typeof settings.addBefore==='function'){settings=settings.addBefore(settings);if(!settings){return true;}}
if(!settings.data.match(/_ajax_nonce=[a-f0-9]+/)){return true;}
settings.success=function(response){parsedResponse=wpAjax.parseAjaxResponse(response,settings.response,settings.element);returnedResponse=response;if(!parsedResponse||parsedResponse.errors){return false;}
if(true===parsedResponse){return true;}
$.each(parsedResponse.responses,function(){wpList.add.call(list,this.data,$.extend({},settings,{position:this.position||0,id:this.id||0,oldId:this.oldId||null}));});list.wpList.recolor();$(list).trigger('wpListAddEnd',[settings,list.wpList]);wpList.clear.call(list,'#'+settings.element);};settings.complete=function(jqXHR,status){if(typeof settings.addAfter==='function'){settings.addAfter(returnedResponse,$.extend({xml:jqXHR,status:status,parsed:parsedResponse},settings));}};$.ajax(settings);return false;},ajaxDel:function(element,settings){var list=this,$element=$(element),data=wpList.parseData($element,'delete'),$eventTarget,parsedResponse,returnedResponse;settings=settings||{};settings=wpList.pre.call(list,$element,settings,'delete');settings.element=data[2]||settings.element||null;settings.delColor=data[3]?'#'+data[3]:settings.delColor;if(!settings||!settings.element){return false;}
settings.action='delete-'+settings.what;settings.nonce=wpList.nonce($element,settings);settings.data=$.extend({_ajax_nonce:settings.nonce,action:settings.action,id:settings.element.split('-').pop()},wpAjax.unserialize(data[4]||''));if(typeof settings.delBefore==='function'){settings=settings.delBefore(settings,list);if(!settings){return true;}}
if(!settings.data._ajax_nonce){return true;}
$eventTarget=$('#'+settings.element);if('none'!==settings.delColor){$eventTarget.css('backgroundColor',settings.delColor).fadeOut(350,function(){list.wpList.recolor();$(list).trigger('wpListDelEnd',[settings,list.wpList]);});}else{list.wpList.recolor();$(list).trigger('wpListDelEnd',[settings,list.wpList]);}
settings.success=function(response){parsedResponse=wpAjax.parseAjaxResponse(response,settings.response,settings.element);returnedResponse=response;if(!parsedResponse||parsedResponse.errors){$eventTarget.stop().stop().css('backgroundColor','#faa').show().queue(function(){list.wpList.recolor();$(this).dequeue();});return false;}};settings.complete=function(jqXHR,status){if(typeof settings.delAfter==='function'){$eventTarget.queue(function(){settings.delAfter(returnedResponse,$.extend({xml:jqXHR,status:status,parsed:parsedResponse},settings));}).dequeue();}};$.ajax(settings);return false;},ajaxDim:function(element,settings){var list=this,$element=$(element),data=wpList.parseData($element,'dim'),$eventTarget,isClass,color,dimColor,parsedResponse,returnedResponse;if('none'===$element.parent().css('display')){return false;}
settings=settings||{};settings=wpList.pre.call(list,$element,settings,'dim');settings.element=data[2]||settings.element||null;settings.dimClass=data[3]||settings.dimClass||null;settings.dimAddColor=data[4]?'#'+data[4]:settings.dimAddColor;settings.dimDelColor=data[5]?'#'+data[5]:settings.dimDelColor;if(!settings||!settings.element||!settings.dimClass){return true;}
settings.action='dim-'+settings.what;settings.nonce=wpList.nonce($element,settings);settings.data=$.extend({_ajax_nonce:settings.nonce,action:settings.action,id:settings.element.split('-').pop(),dimClass:settings.dimClass},wpAjax.unserialize(data[6]||''));if(typeof settings.dimBefore==='function'){settings=settings.dimBefore(settings);if(!settings){return true;}}
$eventTarget=$('#'+settings.element);isClass=$eventTarget.toggleClass(settings.dimClass).is('.'+settings.dimClass);color=wpList.getColor($eventTarget);dimColor=isClass?settings.dimAddColor:settings.dimDelColor;$eventTarget.toggleClass(settings.dimClass);if('none'!==dimColor){$eventTarget.animate({backgroundColor:dimColor},'fast').queue(function(){$eventTarget.toggleClass(settings.dimClass);$(this).dequeue();}).animate({backgroundColor:color},{complete:function(){$(this).css('backgroundColor','');$(list).trigger('wpListDimEnd',[settings,list.wpList]);}});}else{$(list).trigger('wpListDimEnd',[settings,list.wpList]);}
if(!settings.data._ajax_nonce){return true;}
settings.success=function(response){parsedResponse=wpAjax.parseAjaxResponse(response,settings.response,settings.element);returnedResponse=response;if(true===parsedResponse){return true;}
if(!parsedResponse||parsedResponse.errors){$eventTarget.stop().stop().css('backgroundColor','#ff3333')[isClass?'removeClass':'addClass'](settings.dimClass).show().queue(function(){list.wpList.recolor();$(this).dequeue();});return false;}
if('undefined'!==typeof parsedResponse.responses[0].supplemental.comment_link){var $submittedOn=$element.find('.submitted-on'),$commentLink=$submittedOn.find('a');if(''!==parsedResponse.responses[0].supplemental.comment_link){$submittedOn.html($('<a></a>').text($submittedOn.text()).prop('href',parsedResponse.responses[0].supplemental.comment_link));}else if($commentLink.length){$submittedOn.text($commentLink.text());}}};settings.complete=function(jqXHR,status){if(typeof settings.dimAfter==='function'){$eventTarget.queue(function(){settings.dimAfter(returnedResponse,$.extend({xml:jqXHR,status:status,parsed:parsedResponse},settings));}).dequeue();}};$.ajax(settings);return false;},getColor:function(element){return $(element).css('backgroundColor')||'#ffffff';},add:function(element,settings){var $list=$(this),$element=$(element),old=false,position,reference;if('string'===typeof settings){settings={what:settings};}
settings=$.extend({position:0,id:0,oldId:null},this.wpList.settings,settings);if(!$element.length||!settings.what){return false;}
if(settings.oldId){old=$('#'+settings.what+'-'+settings.oldId);}
if(settings.id&&(settings.id!==settings.oldId||!old||!old.length)){$('#'+settings.what+'-'+settings.id).remove();}
if(old&&old.length){old.before($element);old.remove();}else if(isNaN(settings.position)){position='after';if('-'===settings.position.substr(0,1)){settings.position=settings.position.substr(1);position='before';}
reference=$list.find('#'+settings.position);if(1===reference.length){reference[position]($element);}else{$list.append($element);}}else if('comment'!==settings.what||0===$('#'+settings.element).length){if(settings.position<0){$list.prepend($element);}else{$list.append($element);}}
if(settings.alt){$element.toggleClass(settings.alt,($list.children(':visible').index($element[0])+settings.altOffset)%2);}
if('none'!==settings.addColor){$element.css('backgroundColor',settings.addColor).animate({backgroundColor:wpList.getColor($element)},{complete:function(){$(this).css('backgroundColor','');}});}
$list.each(function(index,list){list.wpList.process($element);});return $element;},clear:function(elementId){var list=this,$element=$(elementId),type,tagName;if(list.wpList&&$element.parents('#'+list.id).length){return;}
$element.find(':input').each(function(index,input){if($(input).parents('.form-no-clear').length){return;}
type=input.type.toLowerCase();tagName=input.tagName.toLowerCase();if('text'===type||'password'===type||'textarea'===tagName){input.value='';}else if('checkbox'===type||'radio'===type){input.checked=false;}else if('select'===tagName){input.selectedIndex=null;}});},process:function(elementId){var list=this,$element=$(elementId||document);$element.on('submit','form[data-wp-lists^="add:'+list.id+':"]',function(){return list.wpList.add(this);});$element.on('click','[data-wp-lists^="add:'+list.id+':"], input[data-wp-lists^="add:'+list.id+':"]',function(){return list.wpList.add(this);});$element.on('click','[data-wp-lists^="delete:'+list.id+':"]',function(){return list.wpList.del(this);});$element.on('click','[data-wp-lists^="dim:'+list.id+':"]',function(){return list.wpList.dim(this);});},recolor:function(){var list=this,evenOdd=[':even',':odd'],items;if(!list.wpList.settings.alt){return;}
items=$('.list-item:visible',list);if(!items.length){items=$(list).children(':visible');}
if(list.wpList.settings.altOffset%2){evenOdd.reverse();}
items.filter(evenOdd[0]).addClass(list.wpList.settings.alt).end();items.filter(evenOdd[1]).removeClass(list.wpList.settings.alt);},init:function(){var $list=this;$list.wpList.process=function(element){$list.each(function(){this.wpList.process(element);});};$list.wpList.recolor=function(){$list.each(function(){this.wpList.recolor();});};}};$.fn.wpList=function(settings){this.each(function(index,list){list.wpList={settings:$.extend({},wpList.settings,{what:wpList.parseData(list,'list')[1]||''},settings)};$.each(functions,function(func,callback){list.wpList[func]=function(element,setting){return wpList[callback].call(list,element,setting);};});});wpList.init.call(this);this.wpList.process();return this;};})(jQuery);