$G.Catalog=function(){var j=250;var i=400;var v,s,x;var g=$(),y=$(),f=$();var k="local";var u,B;var z=document.title;function q(E,D){var F=(((E.type=="branch")&&(!D))||(!E.parent))?E.id:E.parent.id;o(E.id);$G.Catalog.Table.load(F,E.id);p(F);}function C(D){var E=D.id;$G.Catalog.Tree.selectById(E);o(E);}function e(){var D=window.location.hash.substring(2);if(/\D/.test(D)){D="";}return D;}function o(D){if((!D)||($G.Catalog.Tree.getRoot().id==D)){D="";}window.location.hash="/"+D;u=window.location.hash;n();}function n(){B=window.setInterval(function(){if((u)&&(window.location.hash!=u)){window.clearInterval(B);var D=e();if(D){$G.Catalog.Tree.selectById(D,true);}else{$G.Catalog.Tree.getRoot().select();}}},i);}function t(D){k=D;y=$(document.getElementById("eshop-search"));y.find("a").click(function(){w();return false;});y.find('input[type="text"]').keydown(function(E){if(E.which==13){l();}});y.find('input[type="submit"]').click(function(){l();});}function w(){y.find('input[type="text"]').val("");if(k=="global"){$G.Catalog.Tree.getRoot().clear();$G.Catalog.Tree.getRoot().open(true);}else{$G.Catalog.Table.resetFilter();}}function l(){var E=y.find('input[name="code"]').val().trim();var D=y.find('input[name="name"]').val().trim();if((E=="")&&(D=="")){return w();}if(k=="global"){$G.getJSON({_do:"eshop_search",root:$G.Catalog.Tree.getRoot().id,code:E,name:D},a);}else{$G.Catalog.Table.filter(E,D);}}function a(E){if((E)&&(E.length)){$G.Catalog.Tree.construct(E);$G.Catalog.Tree.openAll();$G.Catalog.Tree.getFirstLeaf().select(false,true);}else{var D=new $G.Window({align:"center-center",width:300,height:36,animation:true});D.attachEventListener("draw",function(F){F.$content.html('<div class="eshop-search-null">По вашему запросу ничего не найдено</div>');});D.show();}}function b(D){$G.getJSON({_do:"eshop_to_basket",id:D},d);}function r(E,D){$G.getJSON({_do:"eshop_change_count",id:E,count:D},c);}function d(D){if(D.basket){m(D.basket);}if(v){if(D.add){$G.Catalog.Table.inBasket(D.add,1);}if(D.remove){$G.Catalog.Table.inBasket(D.remove,0);}}if(s){if(D.remove){$G.Catalog.BasketTable.deleteRow(D.remove);}}}function c(D){if(D.basket){m(D.basket);}if((D.row)&&(s)){$G.Catalog.BasketTable.updateRow(D.row);}}function A(D){$G.getJSON({_do:"eshop_basket",cnt:(D)?"1":"0"},m);}function m(G){var F=$G.GetPrice(G.price,true)||0;var E=G.count+" "+$G.GetCountStr(G.count);if(G.count>0){g.addClass("basket-full");}else{g.removeClass("basket-full");}g.find(".count").html(E);g.find(".price").html(F+" руб.");if(G.content){for(var D=0;D<G.content.length;D++){$G.Catalog.Table.inBasket(G.content[D],1);}}}function h(){$("#cnt-wrap h1 a").click(function(){$G.Catalog.Tree.getRoot().select();return false;});}function p(H){var G="";var D=$G.Catalog.Tree.getRoot();var F=D.getChildById(H);if((F)&&(F!=D)){G=F.name.replace(/<[^>]+>/g," ")+" - ";}document.title=G+z;var E="";if(F){while(F.parent){E='<td><a href="#/'+F.id+'">'+F.name+"</a></td>"+E;F=F.parent;}E="<tr>"+E+"</tr>";}f.html(E);}return{init:function(){var D=$("#cnt-wrap h1");var F=$(document.createElement("span")).html("<table><tbody></tbody></table>").appendTo(D);f=F.find("tbody");D.hover(function(){F.stop().animate({width:f.width()},j,"swing");},function(){F.stop().animate({width:0},j,"swing");});if(window._TREE_NODE_ID!=window._TREE_CAT_ID){var H=D.find("a").get(0);if(H){window.location=H.href+"#/"+window._TREE_NODE_ID;return;}}g=$(document.getElementById("basket-block"));g.find("ins").click(function(){if(g.hasClass("basket-full")){window.location=g.find("a").get(0).href;}});v=document.getElementById("eshop-table");var E=document.getElementById("eshop-tree");if(E){A();n();t("global");var G=e();$G.Catalog.Tree.init(E,window._TREE_CAT_ID,G);$G.Catalog.Tree.attachEventListener("select",q);h();if(v){$G.Catalog.Table.init(v);$G.Catalog.Table.attachEventListener("select",C);$G.Catalog.Table.attachEventListener("basket",b);}}else{if(v){t("local");$G.Catalog.Table.initExt(v);$G.Catalog.Table.attachEventListener("basket",b);A(true);}else{A();}}s=document.getElementById("eshop-basket-table");if(s){$G.Catalog.BasketTable.init(s);$G.Catalog.BasketTable.attachEventListener("delete",b);$G.Catalog.BasketTable.attachEventListener("change_count",r);}x=document.getElementById("eshop-form");if(x){$G.Catalog.Form.init(x);}}};}();$G.Catalog.WRAP_HEIGHT_OFFSET=72;$G.GetCountStr=function(a){if(((a%100)<10)||((a%100)>20)){switch(a%10){case 1:return"товар";case 2:case 3:case 4:return"товара";}}return"товаров";};$G.GetPrice=function(g,b){if(g==0){return"";}g=Math.floor(g*100)/100;var a=Math.floor(g*18+0.5)/100;if(!b){g+=a;}var j=Math.floor(g).toString();var h=Math.floor((g-parseInt(g)+0.001)*100).toString();var c=j.length;var e=0;var f="";while(e<c){var d=((e==0)&&(c%3>0))?c%3:3;f+="<span>"+j.substr(e,d)+"</span>";e+=d;}if(h.length<2){h="0"+h;}f+=","+h;return f;};$G.Catalog.Tree=function(){var e=300;var c;var b=new $G.EventDispatcher();var h;function g(){if(!h){h=$("#cnt-wrap .eshop-h");}var i=Browser.getScrollBody();c._$.css({height:document.documentElement.clientHeight-h.height()-$G.Catalog.WRAP_HEIGHT_OFFSET,overflow:"auto"});
$(i).animate({scrollTop:h.offset().top-($G.Catalog.WRAP_HEIGHT_OFFSET/2)},e,"swing");}function d(){if(jQuery.browser.opera){$(".eshop-tree").css({position:"absolute"});}g();}function f(i){return c.getChildById(i);}function a(i){$G.getJSON({_do:"eshop_tree",open:"open",id:i},function(j){c.constructChilds(j,true,true,i);});}return{init:function(j,i,k){c=new $G.Catalog.Tree.Item(null,"branch",i,"root");c._$=$(j).scrollTop(0).empty();if(k){a(k,true);}else{c.open(true);}d();$(window).resize(function(){g();});},attachEventListener:function(j,i){b.attachEvent(j,i);},removeEventListener:function(j,i){b.removeEvent(j,i);},getRoot:function(){return c;},select:function(j,i){b.dispatchEvent("select",j,i);},construct:function(i){c.constructChilds(i,true);},openAll:function(){c.openAll();},getFirstLeaf:function(){return c.getFirstLeaf();},selectById:function(k,i){var j=f(k);if(j){j.select(true,i);}else{a(k);}}};}();$G.Catalog.Tree.Item=function(c,b,d,a){this.parent=c;this.type=b;this.id=d;this.name=a;this.childs=new Array();this.isRoot=(this.parent==null);this.closed=true;};$G.Catalog.Tree.Item.SLIDE_DURATION=200;$G.Catalog.Tree.Item.prototype={loadChilds:function(c,b){var a=this;if(this._loading){return;}this._loading=true;$G.getJSON({_do:"eshop_tree",id:this.id},function(d){a._loading=false;a.constructChilds(d,c,b);});},constructChilds:function(g,f,e,b){var a=true;this.childs.splice(0,this.childs.length);for(var c=0;c<g.length;c++){var d=new $G.Catalog.Tree.Item(this,g[c]["type"],g[c]["id"],g[c]["name"]);d._root=this._root||this;d.__childsData=g[c].childs;this.childs.push(d);}if(f){this.drawChilds();}for(var c=0;c<this.childs.length;c++){if(this.childs[c].__childsData){this.childs[c].constructChilds(this.childs[c].__childsData,f);}}if(e){if(b){var h=this.getChildById(b);if(h){h.parent.open(false,true);h.select(false,true);}}else{this.select();}}},draw:function(){var a=this;if(!this.isRoot){var b=$(document.createElement("a")).attr("href","#/"+this.id).html(this.name).click(function(){a.select(false,true);return false;});this._$=$(document.createElement("li")).addClass(this.type).addClass("closed").append(b).append(document.createElement("em"));$(document.createElement("ins")).click(function(){a.toggle();}).appendTo(this._$);}if(this.type=="branch"){this._$ul=$(document.createElement("ul")).hide().appendTo(this._$);}this._drawn=true;},drawChilds:function(){if(!this._drawn){this.draw();}this._$ul.hide().empty();for(var a=0;a<this.childs.length;a++){this.childs[a].draw();this.childs[a]._$.appendTo(this._$ul);}if(!this.closed){this.open();}},select:function(b,a){this.makeSelected();if((this.type=="branch")&&(!this.childs.length)){this.loadChilds(true);}$G.Catalog.Tree.select(this,a);if(b){if(this.type=="branch"){this.open();}else{this.parent.open();}}},makeSelected:function(){var a=this._root||this;a._$ul.find("li.active").removeClass("active");if(!this.isRoot){this._$.addClass("active");var e=this._$.offset().top;var b=a._$.offset().top;var c=a._$.scrollTop();var f=e-b;if(f>0){f=(e+this._$.find("a").height())-(b+a._$.height())+20;if(f<0){f=0;}}if(f){a._$.stop().animate({scrollTop:c+f},$G.Catalog.Tree.Item.SLIDE_DURATION);}}else{a._$.stop().animate({scrollTop:0},$G.Catalog.Tree.Item.SLIDE_DURATION);}},toggle:function(){if(this.closed){this.open();}else{this.close();}},open:function(c,a,b){if(this.type=="leaf"){return;}if((this.parent)&&(!b)){this.parent.open(false,a);}this.closed=false;if(!this.childs.length){this.loadChilds(true,c);}else{this._$.addClass("open").removeClass("closed");if(a){this._$ul.show();}else{this._$ul.slideDown($G.Catalog.Tree.Item.SLIDE_DURATION);}if(c){this.select();}}},openAll:function(b){if((this.childs)&&(this.childs.length>0)){this.open(false,true,b);}for(var a=0;a<this.childs.length;a++){this.childs[a].openAll(true);}},close:function(){this.closed=true;this._$.removeClass("open").addClass("closed");this._$ul.slideUp($G.Catalog.Tree.Item.SLIDE_DURATION);},clear:function(){if(this._$ul){this._$ul.empty();}this.childs.splice(0,this.childs.length);},getChildById:function(c){for(var a=0;a<this.childs.length;a++){if(this.childs[a].id==c){return this.childs[a];}else{var b=this.childs[a].getChildById(c);if(b){return b;}}}return null;},getFirstLeaf:function(){if((!this.childs)||(!this.childs.length)||(this.type=="leaf")){return this;}return this.childs[0].getFirstLeaf();}};$G.Catalog.Table=function(){var e=200;var f="/dgallery/";var c="/datasheet/";var w=$(),p=$(),s=$(),m=$();var r=false,a=false;var j=new $G.EventDispatcher();var d=[];var k=0,x=0;var u=false;function h(){s.css({height:document.documentElement.clientHeight-$("#cnt-wrap .eshop-h").height()-$G.Catalog.WRAP_HEIGHT_OFFSET,overflow:"auto"});}function A(){var I=s.offset();var B=p.find("td");if(!m.length){m=$(document.createElement("div")).addClass("eshop-static-header").appendTo(document.body);if(jQuery.browser.mozilla){m.css("left",-1);}B.each(function(){m.append($(document.createElement("div")).html(this.innerHTML));});}var H=m.find("div");var F=B.get(0);
var C=10;var E=F.offsetHeight-10;var G=s.scrollTop();for(var D=0;D<B.length;D++){$cell=B.eq(D);var I=$cell.offset();H.eq(D).css({left:I.left,top:I.top+G,width:$cell.get(0).offsetWidth-C,height:E});}}function z(){h();}function l(){r=(p.find("td.ass-count").length>0);a=(p.find("td.weight").length>0);}function n(B){u=false;d=B;q();if(k){v(k);}}function q(){w.empty();s.scrollTop(0);for(var G=0;G<d.length;G++){var P=d[G];var K=$(document.createElement("tr")).appendTo(w);if(P.child_quant>0){K.addClass("j");}var D="";var J=P.ass_count;if(J==0){J="";}var F=(r)?'<td class="cont">'+J+"</td>":"";var I=P.weight;if(I==0){I="";}else{I=I.replace(".",",");}var M=(a)?'<td class="cont">'+I+"</td>":"";var C="";if(P.dwg){C+='<div class="dwg"><a title="Чертежи" rel="dwg" href="'+f+P.dwg+'/"><ins><em></em></ins></a></div>';}if(P.datasheet){C+='<div class="datasheet"><a title="Описание" rel="datasheet" href="'+c+P.datasheet+'/"><ins><em></em></ins></a></div>';}var E=(P.count>0)?P.count:"под заказ";if(P.price==0){E="";}var L="";if(P.price>0){var B=(P.in_basket)?"/i/catalog/tba.png":"/i/catalog/tb.png";var O=(P.in_basket)?"Убрать из корзины":"Добавить в корзину";L+='<a rel="to-basket" href="#'+P.id+'"><img src="'+B+'" title="'+O+'" width="24" height="21" alt="o" class="png"></a>';}var N=P.name;if(P.description!==""){N+="<br><small>"+P.description+"</small>";}var H='<td class="n">'+(G+1)+'</td><td class="code">'+P.code+'</td><td class="i">'+C+'</td><td class="name">'+N+"</td>"+F+M+'<td class="price">'+$G.GetPrice(P.price)+'</td><td class="count">'+E+'</td><td class="btn">'+L+"</td>";K.html(H);if(P.child_quant>0){K.click(o);}else{K.click(o);}}A();i();}function i(){w.find("a").click(y);}function b(){d.splice(0,d.length);w.find('a[rel="to-basket"]').each(function(){var C=this.href.split("#")[1];var B=this.parentNode.parentNode.getElementsByTagName("td");d.push({id:C,code:B[1].innerHTML,name:B[2].innerHTML});$(this).click(y);});}function o(C){var B=$(this).index();if(B>-1){j.dispatchEvent("select",d[B]);}}function y(D){var B=this.getAttribute("rel");var C=this.href.split("#")[1];switch(B){case"datasheet":case"dwg":case"scheme":$G.Popup.show(this,800,500);break;case"to-basket":j.dispatchEvent("basket",C);break;}return false;}function v(B){var F=-1;for(var D=0;D<d.length;D++){if(d[D].id==B){F=D;break;}}var E=w.find("tr").removeClass("selected");if(F>-1){var H=E.eq(F).addClass("selected");var J=H.offset().top;var G=s.offset().top;var C=s.scrollTop();var I=J-G-p.height();if(I>0){I=(J+H.height()*2)-(G+s.height());if(I<0){I=0;}}if(I){s.stop().animate({scrollTop:C+I},e);}}else{s.scrollTop(0);}}function g(C){for(var B=0;B<d.length;B++){if(d[B].id==C){return B;}}return -1;}function t(E,D){var B=g(E);if(B>-1){var C=w.find("tr").eq(B).find('a[rel="to-basket"] img').get(0);if(C){if(D){C.src="/i/catalog/tba.png";C.title="Убрать из корзины";}else{C.src="/i/catalog/tb.png";C.title="Добавить в корзину";}}}}return{init:function(B){s=$(B);p=s.find("thead");w=s.find("tbody").empty();l();z();$(window).resize(function(){h();A();});},initExt:function(B){s=$(B);w=s.find("tbody");b();},load:function(C,B){if(u){return;}if(x!=C){u=true;x=C;k=(B!=C)?B:0;$G.getJSON({_do:"eshop_list",id:C},n);}if(B){v(B);}},inBasket:function(C,B){t(C,B);},filter:function(D,B){D=D.toLowerCase();B=B.toLowerCase();var E=w.get(0).getElementsByTagName("tr");for(var C=0;C<d.length;C++){if(((!D)||(d[C].code.toLowerCase().indexOf(D)>-1))&&((!B)||(d[C].name.toLowerCase().indexOf(B)>-1))){E[C].style.display="table-row";}else{E[C].style.display="none";}}},resetFilter:function(){var C=w.get(0).getElementsByTagName("tr");for(var B=0;B<d.length;B++){C[B].style.display="table-row";}},attachEventListener:function(C,B){j.attachEvent(C,B);},removeEventListener:function(C,B){j.removeEvent(C,B);}};}();$G.Catalog.BasketTable=function(){var d=$(),i=$();var b=[];var f=new $G.EventDispatcher();function h(){b.splice(0,b.length);d.find('a[rel="del"]').each(function(){var j=this.href.split("#")[1];b.push({id:j});$(this).click(a);var k=this.parentNode.parentNode;g(k,j);});}function g(m,o){var l=$("td",m).eq(2);var k=l.html();var j=document.createElement("input");j.value=j.defaultValue=k;var n=$(j).change(function(p){c(this,o);}).keydown(function(p){if(p.which==13){c(this,o);p.preventDefault();return false;}});l.empty().append(n);}function a(k){var j=this.href.split("#")[1];f.dispatchEvent("delete",j);return false;}function c(j,l){var k=parseInt(j.value);if(k>0){j.defaultValue=j.value;f.dispatchEvent("change_count",l,k);}else{j.value=j.defaultValue;}}function e(k){for(var j=0;j<b.length;j++){if(b[j].id==k){return j;}}return -1;}return{init:function(j){i=$(j);d=i.find("tbody");h();},deleteRow:function(k){var j=e(k);if(j>-1){b.splice(j,1);d.find("tr").eq(j).remove();d.find("tr").each(function(l){$(this).find("td").first().html(l+1);});}},updateRow:function(k){var j=e(k.id);if(j>-1){d.find("tr").eq(j).find("td").each(function(l){switch(l){case 4:this.innerHTML=$G.GetPrice(k.price*k.count,true);break;case 5:this.innerHTML=$G.GetPrice(k.price*k.count);
break;}});}},attachEventListener:function(k,j){f.attachEvent(k,j);},removeEventListener:function(k,j){f.removeEvent(k,j);}};}();$G.Catalog.Form=function(){var l=300;var k=2500;var d=300;var b;var m=$();var h=new Array();var a=false;function f(){m.submit(c);m.find("label.required").each(function(){var n=document.getElementById(this.getAttribute("for"));if(n){h.push(n.name);}});}function j(){var s=false;var n=m.serializeArray();var r={};m.find("label").removeClass("error");var q,o;for(var p=0;p<n.length;p++){if((h.indexOf(n[p].name)==-1)||(/\S/.test(n[p].value))){r[n[p].name]=n[p].value;}else{m.find('input[name="'+n[p].name+'"]').siblings("label").addClass("error");s=true;}if(n[p].name=="pass"){q=n[p].value;}if(n[p].name=="pass2"){o=n[p].value;}}if(((q)||(o))&&(q!=o)){m.find('input[name="pass"],input[name="pass2"]').siblings("label").addClass("error");s=true;g("Пароль и подтверждение не совпадают");}return(s)?null:r;}function c(o){if(!a){var n=j();if(n){a=true;m.find('input[type="submit"]').addClass("progress");$G.getJSON(n,e);}else{$(Browser.getScrollBody()).animate({scrollTop:m.offset().top-20},l,"swing");}}return false;}function e(p){a=false;m.find('input[type="submit"]').removeClass("progress");if(p.errors){m.find("label").removeClass("error");for(var o=0;o<p.errors.length;o++){m.find('input[name="'+p.errors[o]+'"]').siblings("label").addClass("error");}}if(p.errort){g(p.errort);}if(p.c){i(p.c);}if(p.ok=="edit"){var n=new $G.Window({align:"center-center",width:300,height:36,animation:true});n.attachEventListener("draw",function(q){q.$content.html('<div class="eshop-search-null">Спасибо. Ваши данные изменены.</div>');});n.show();}if(p.pdf){window.location=p.pdf;}}function g(o){var n=m.find(".error-text").html(o).show();window.setTimeout(function(){n.fadeOut();},k);}function i(o){var n=$(document.createElement("div")).addClass("text").html(o).hide().insertAfter(m);m.slideUp(d);n.slideDown(d);}return{init:function(n){b=n;m=$(b);f();}};}();
