$(function(){var b=7;var h=$("#edit_offer_form");var x=false;var c;function r(){$(".preload").hide();$(".main").show();category=new Array();category_pre_set=false;if($("#category_id").prev("span").length){category_pre_set=true;parent_category_id=($("#category_id").prev("span").attr("id"));$("span",$("#category_id").parent()).click(D)}else{parent_category_id=0;$("#category_id").val("")}category[parent_category_id]=new Array();i=0;k=0;F=0;if(option=$("#category_id").attr("options")){while(i<option.length){if(option[i].value==""){i++;continue}category[parent_category_id][k]={id:option[i].value,title:option[i].text};if(option[i].selected){var F=option[i].value;if($("#district_id")[0]){category[parent_category_id][k].has_district=true}if($("#subway_id")[0]){category[parent_category_id][k].has_subway=true}}i++;k++}}if(!F){F=parent_category_id}type=new Array();type[F]=new Array();i=0;k=0;if(option=$("#type_id").attr("options")){while(i<option.length){if(option[i].value==""){i++;continue}var G=option[i].title.split("|");has_price=0;autotitle=0;if(G[0]!=null&&G[0]!=0){has_price=1}if(G[1]!=null&&G[1]!=0){autotitle=1}type[F][k]={id:option[i].value,title:option[i].text,has_price:has_price,autotitle:autotitle};if(option[i].selected&&autotitle){$("#title_block").hide();$("#title").attr("requred","")}i++;k++}}region=new Array();c=$("#region_id").val();if($("#district_id")[0]){if(region[c]==null){region[c]=new Array()}region[c]["districts"]=new Array();region[c]["district_title"]=$("#district_title").html();region[c]["has_district"]=true;i=0;k=0;if(option=$("#district_id").attr("options")){while(i<option.length){if(option[i].value==""||option[i].value<0){i++;continue}region[c]["districts"][k]={id:option[i].value,title:option[i].text};i++;k++}}}if($("#subway_id")[0]){if(region[c]==null){region[c]=new Array()}region[c]["subways"]=new Array();region[c]["has_subway"]=true;i=0;k=0;if(option=$("#subway_id").attr("options")){while(i<option.length){if(option[i].value==""||option[i].value<0){i++;continue}region[c]["subways"][k]={id:option[i].value,title:option[i].text};i++;k++}}}if($(".step").length){$(".step:eq(0)").click(function(){$("#category_id").fadeOut("200").fadeIn("200").focus()})}property=new Array();propertyHTML=new Array();$("#category_id").change(A);$("#region_id").change(v);$("input",h).change(function(){a()}).blur(function(){a(1)});$(".file",h).change(function(){C(this)});if(useAjax){h.ajaxForm(p)}if(category_pre_set){$("#type_id").change(E).change()}$("#has_not_user_yes").click(function(){$("#quick_registration").show();$("#offer_contacts").hide()});$("#has_not_user_no").click(function(){$("#quick_registration").hide();$("#offer_contacts").show()});$("#price_type").change(n);q();$(":radio","#images .list").click(B);$(":checkbox","#images .list").click(y)}function s(F){if(showErrors(F.errors)){refreshCaptcha();$(":submit, :image",h).removeAttr("disabled")}else{if(F.data!=null&&F.data.save_and_continue!=null){if(F.messages!=null){showMessage(F.messages)}$(":submit, :image",h).removeAttr("disabled");m()}else{if(F.redirect!=null){$("#ajaxstatus").html("Пожалуйста подождите...").show();location.href=F.redirect}else{if(F.messages!=null){showMessage(F.messages)}$("#ajaxstatus").html("Пожалуйста подождите...").show();if(offer_id=$(':input[name="id"]',h).val()){setTimeout(function(){location.href="/offer/"+offer_id+"/"},2000)}else{setTimeout(function(){location.href="/"},2000)}}}}}var g;var p=$.extend({},ajaxOptions||{},{success:s,beforeSubmit:function(G,F){if(G.length==0||!a(null,null,true)){i=0;while(g[i]!=null){g[i]='Не заполнено обязательное поле: "'+g[i]+'"';i++}return !showErrors(g)}else{G[G.length]={name:"is_ajax",value:1};hideMessage();$(":submit, :image",h).attr("disabled","disabled")}}});function m(){$(":input",h).each(function(){switch(this.name){case"category_id":case"type_id":case"region_id":case"has_not_user":break;default:switch(this.type){case"select-one":var F=0;var G=0;while(F<this.options.length){if(this.options[F].defaultSelected){G=F;break}F++}this.options[G].selected="selected";break;case"submit":case"button":case"image":break;default:this.value="";break}break}});$(".del_image").click();a()}function u(){currentimages=0;$(":checkbox","#images .list").each(function(F){if(!this.checked){currentimages++}else{if(F==$(":radio","#images").index($(":radio:checked"))){z()}}});return $(".item","#images").length+currentimages}function C(F){if(u()<b){if(F==null){F=$(".item:last :file","#images")}par=$(F).parent();if(t=par[0].outerHTML){par.after(t).children("label, .del_image").remove()}else{par.after(par.clone()).children("label, .del_image").remove()}$(F).unbind("change");par.next().children(".del_image").click(w);if($(":radio",par).length==0){par.append('<a href="#" class="del_image">удалить</a><label><input type="radio" class="r" name="mainimage"></label>').next().children(":file").val("").change(function(){C(this)});$(":radio",par).click(B);$(".del_image",par).click(w)}y()}}function w(){var F=$(this).parent();F.remove();z();y();return false}function B(){$(":radio","#images").next("span").hide();i=$(":radio","#images").index($(this));if($(":checkbox:eq("+i+"):checked","#images").length){z();return false}else{$(this).after("<span>Основная</span>")}return true}function z(){ind=$(":checkbox","#images .list").index($(":checkbox:not(:checked)"));if(ind!=-1){$(":radio:eq("+ind+")","#images").click()}else{$(".item:first :radio","#images").click()}}function y(){hideMessage();if($(":radio","#images").index($(":radio:checked"))==-1){$(":radio:first","#images").click()}countFields=0;countRadios=0;$(":radio","#images").each(function(G){delCheckbox=$(":checkbox:eq("+G+")","#images");if(!delCheckbox.length||!delCheckbox.attr("checked")){this.value=countRadios++}else{this.value=-1}});var F=$(".item","#images");F.each(function(G){$(":file",this).attr("name","image["+G+"]");if($(":file",this).val()!=""){countFields++}if(u()<7&&F.index(this)==(F.length-1)){$(".del_image:visible",this).hide()}else{$(".del_image:hidden",this).show()}});if(u()>b){if($(":file:last","#images").val()==""){$(":file:last","#images").remove()}else{showErrors("ВНИМАНИЕ! Фотографий не может быть более "+b+". Лишние фотографии будут пропущены.")}}else{if($(".add_images:hidden","#images").length){$(".add_images:hidden","#images").show()}else{if($(":file:last","#images").val()!=""){C()}}}}function A(F){if(x&&!confirm("При смене раздела параметры объявления будут стёрты. Вы уверены?")){x=false;return false}x=false;if(F==null||$(F).attr("tagName")!="SPAN"){categorySelect=$("#category_id");id=categorySelect.val();obj=categorySelect}else{obj=$(F);if(obj.prev().length){id=obj.prev().attr("id")}else{id=0}categorySelect=null}if(id!=null){$(obj).parent().children(".note").hide();showLoad(obj);if(subs=o(id,F)){$(obj).parent().children(".note").show();hideLoad(obj);if(subs!="none"){if(categorySelect!=null){text=categorySelect.attr("options")[categorySelect.attr("selectedIndex")].text;F='<span id="'+id+'"><a href="/offer/add/">'+text+"</a> » </span>";$(categorySelect).after(j(id)).replaceWith(F);$("#category_id").change(A).prev("span").click(D)}else{obj.replaceWith(j(id));$("#category_id").change(A)}}else{current=$("#type_id").val();$("#type_id").replaceWith(f(type[id]!=null?id:0,current));if(property[id]!=null){if(propertyHTML[id]==null){propertyHTML[id]="";i=0;while(property[id][i]!=null){propertyHTML[id]+="<tr>";propertyHTML[id]+="<th";if(property[id][i].req==1){propertyHTML[id]+=' class="req"'}propertyHTML[id]+=">"+property[id][i].title+":";if(property[id][i].req==1){propertyHTML[id]+=' <i class="note">*</i>'}propertyHTML[id]+="</th>";propertyHTML[id]+="<td>"+property[id][i].input+"</td></tr>";i++}}}else{propertyHTML[id]=""}$("tbody","#properties").html(propertyHTML[id]);if(propertyHTML[id]==""){$("#properties").hide()}else{$("#properties").show();$(":input","#properties").change(function(){a()}).blur(function(){a(1)});modifyCheckboxes();modifyRadios();modifySelects();q()}$("#type_id").change(E).change();$("#category_id").blur();$("#properties").focus();a();v();return false}}}a(null,1);return false}function l(){categorySelect=$("#category_id");index=categorySelect.attr("selectedIndex")-1;if(index>-1){if(categorySelect.prev().length){parent_id=categorySelect.prev().attr("id")}else{parent_id=0}if(category[parent_id][index]==null){return}return category[parent_id][index]}}function v(){region_id=$("#region_id").val();if(region_id){cat=l();if(cat.has_district||cat.has_subway){districtObj=$("#district_id");subwayObj=$("#subway_id");if(c!=region_id||(cat.has_district&&!districtObj.length)||(cat.has_subway&&!subwayObj.length)){if(reg=d(region_id,cat.has_district,cat.has_subway)){if(cat.has_district&&reg.has_district!=null&&reg.districts!=null){list=reg.districts;list[list.length]={id:-1,title:"любой"};list[list.length]={id:-2,title:"нет"};html="<tr>";html+="<th";if(cat.has_district==2){html+=' class="req"'}html+=">";html+=reg.district_title+":";if(cat.has_district==2){html+='  <i class="req">*</i>'}html+="</th>";html+="<td>";html+=form_Select("district_id",list,"","Выберите",cat.has_district==2);html+="</tr>";if(districtObj.length){districtObj.parents("tr").replaceWith(html)}else{$("tbody","#properties").prepend(html)}}else{districtObj.parents("tr").remove()}if(cat.has_subway&&reg.has_subway!=null&&reg.subways!=null){html="<tr>";html+="<th>Станция метро:";if(cat.has_subway==2){html+='  <i class="req">*</i>'}html+="</th>";html+="<td>";html+=form_Select("subway_id",reg.subways,"","Выберите",cat.has_subway==2);html+="</tr>";if(subwayObj.length){subwayObj.parents("tr").replaceWith(html)}else{if($("#district_id").length){$("#district_id").parents("tr").after(html)}else{$("tbody","#properties").prepend(html)}}}else{subwayObj.parents("tr").remove()}return true}return true}}}c=region_id;$("#district_id").parents("tr").remove();$("#subway_id").parents("tr").remove()}function d(H,F,G){if(H==null){return false}if(region!=null&&region[H]!=null&&(F==null||region[H].has_district!=null)&&(G==null||region[H].has_subway!=null)){return region[H]}$("#district_id").replaceWith('<span id="district_id" class="loading">Загрузка списка</span>');$("#subway_id").replaceWith('<span id="subway_id" class="loading">Загрузка списка</span>');$.getJSON("/region/get/"+H,function(I){if(!showErrors(I.errors)&&I.obj!=null){region[H]=I.obj;if(I.data!=null&&I.data.districts!=null){region[H].district_title=I.data.district_title;region[H].districts=I.data.districts}if(I.data!=null&&I.data.subways!=null){region[H].subways=I.data.subways}return v()}})}function q(){$(".other_input").each(function(){var F=$(this);if(this.value==""){F.hide()}prev=$(this).prev();if(prev.attr("tagName")=="LABEL"){input=$(":input",prev)}else{input=prev}switch(input.attr("type")){case"select-one":input.change(function(){if(this.value=="other"){F.show()}else{F.hide()}});break;case"checkbox":input.click(function(){if(this.checked){F.show()}else{F.hide()}});break}})}function D(){id=$(this).prev().attr("id");if(id==null){id=0}$(this).nextAll().remove();if(sel=j(id)){$(this).replaceWith(sel);$("#category_id").change(A);a()}else{A(this)}return false}function j(H,F){if(typeof category[H]!="object"||!category[H].length){return false}listArray=category[H];var G="Выберите раздел";return form_Select("category_id",listArray,F,G)}function f(H,F){var G;if(typeof type[H]!="object"||!type[H].length){return false}listArray=type[H];if(listArray.length>1){G="Выберите тип"}else{F=listArray[0].id}return form_Select("type_id",listArray,F,G)}function E(){var F=0;if(this.selectedIndex!=0){F=this.selectedIndex-1}if(type[$("#category_id").val()][F].has_price==1){$("#price_block").show()}else{$("#price_block").hide()}if(type[$("#category_id").val()][F].autotitle==null||type[$("#category_id").val()][F].autotitle==0){$("#title_block").show();$("input#title").addClass("req")}else{$("#title_block").hide();$("input#title").removeClass("req")}a()}function o(G,F){if(G==null){return false}if(category[G]!=null){return category[G]}$.getJSON("/category/get/"+G,function(H){if(H.errors==null){category[G]="none";if(H.data!=null){if(H.data.subs!=null&&H.data.subs.length){category[G]=H.data.subs}if(H.data.types!=null){type[G]=H.data.types}if(H.data.properties!=null){property[G]=H.data.properties}if(H.data.has_district!=null||H.data.has_subway!=null){i=0;while(category[parent_category_id][i]!=null){if(category[parent_category_id][i].id==G){category[parent_category_id][i].has_district=(H.data.has_district!=null);category[parent_category_id][i].has_subway=(H.data.has_subway!=null);break}i++}}}return A(F)}})}function e(G,F){return false}function a(F,H,G){var I=0;var J=0;if(H==true){$("#steps").children(".step").removeClass("done");$("#properties:visible").hide();if(!G){$("#formContent:visible",h).hide()}$(".offer_intro").show();return e(J,F)}g=[];if($("#category_id").val()!=""){$("#steps").children(".step").eq(I++).addClass("done");$("#formContent").show();$(".offer_intro").hide();J=1}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done");g.push("Раздел");return a(0,1,G)}if($("#type_id").val()!=""){$("#steps").children(".step").eq(I++).addClass("done");J=50}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done");g.push("type_id")}reqEmpty=false;$(":input","#properties").each(function(){if((this.type=="checkbox"&&!this.checked)||$(this).val()==null||$(this).val()==""){if($(this).hasClass("req")){reqEmpty=true;g.push($(this).attr("id"))}}else{x=true}});if(!reqEmpty){$("#steps").children(".step").eq(I++).addClass("done");if(property[$("#category_id").val()]!=null){J+=50+property[$("#category_id").val()].length*36}}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done")}if($("input#title").val()!=""||$("input#title").hasClass("req")==false){$("#steps").children(".step").eq(I++).addClass("done");J+=70}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done");g.push("title")}if($("#description").val()!=""){$("#steps").children(".step").eq(I++).addClass("done")}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done");g.push("description")}if($("#quick_registration").length){reqEmpty=false;$(":input.req","#quick_registration").each(function(){if((this.type=="checkbox"&&!this.checked)||$(this).val()==null||$(this).val()==""){reqEmpty=true;g.push($(this).attr("id"))}});if($("#email","#quick_registration").val()==""&&$("#phone","#quick_registration:visible").val()==""){reqEmpty=true;g.push('email" или "<a href="#" onclick="$(\'#phone\').focus()">Телефон</a>')}if(!reqEmpty){$("#steps").children(".step").eq(I++).addClass("done")}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done")}}if($("#captcha_code").val()!=""){$("#steps").children(".step").eq(I++).addClass("done")}else{$("#steps").children(".step:gt("+(I++-1)+")").removeClass("done");g.push("captcha_code")}if(g.length){return e(J,F)}return true}function n(){switch(this.value){case"negotiated":$(".price_from, .price_to, .price, .currency","#price_block").hide();break;case"fixed":$(".price_from, .price_to","#price_block").hide();$(".price, .currency","#price_block").removeClass("dn").show();break;case"from-to":$(".price, .price_from, .price_to, .currency","#price_block").removeClass("dn").show();break}}r()});
