(function(b){var a=function(){var e={type:null,checker:null,unchecker:null,branchaction:null,leafaction:null,icon_check:"/media/images/checked.png",icon_halfcheck:"/media/images/halfchecked.png",icon_uncheck:"/media/images/unchecked.png",icon_expand:"/media/images/expanded.png",icon_collapse:"/media/images/collapsed.png",xbullet:"foxytree-xbullet",abullet:"foxytree-abullet",ulloader:"foxytree-load",row:"foxytree-row",leaf:"foxytree-leaf",branch:"foxytree-branch",flagcheck:"foxytree-checked",ajax:"foxytree-ajaxer",expand:"foxytree-expanded",grab:"foxytree-grabbed",flaghalfcheck:"foxytree-halfchecked",singleshot:false,multishot:false,singleselection:false,preload:false,load:null,collect:false,row_class:".foxytree-row",ulloader_class:".foxytree-load"};var p,m;var o=function(s){var r=0,q=0,t=b(s).siblings("li").andSelf();t.each(function(){if(b(this).hasClass(p.flagcheck)||b(this).hasClass(p.flaghalfcheck)){r++}if(b(this).hasClass(p.flaghalfcheck)){q++}});if(r==t.length&&q==0){return 1}else{if(r>=1){return 2}}return 0},i=function(r,s){var q=b("> ul",r);if(q.is("ul")){q.each(function(){if(b(this).is(":hidden")||r.hasClass(p.flagcheck)){b(this).slideDown().addClass(p.expand);b(this).parent("li").find("> "+p.row_class+" .ibullet").filter(":first").attr("src",p.icon_expand)}else{if(b(this).is(":visible")||r.hasClass(p.flagcheck)==false){b(this).slideUp().removeClass(p.expand);b(this).parent("li").find("> "+p.row_class+" .ibullet").filter(":first").attr("src",p.icon_collapse)}}b("> li",q).each(function(){j(b(this),s);i(b(this),s)})})}return false},d=function(s){var q=s.parents("li:first");if(q.is("li")){var r=o(s);if(r==1){j(q,1);d(q)}else{if(r==2){j(q,2);d(q)}else{if(r==0){j(q,0);d(q)}}}}},j=function(r,s){var q=b("> "+p.row_class+" .thebullet",r);if(s==1){if(r.hasClass(p.flaghalfcheck)){r.removeClass(p.flaghalfcheck)}q.attr("src",p.icon_check);r.addClass(p.flagcheck)}else{if(s==2){if(r.hasClass(p.flaghalfcheck)==false){if(r.hasClass(p.flagcheck)){r.removeClass(p.flagcheck)}q.attr("src",p.icon_halfcheck);r.addClass(p.flaghalfcheck)}}else{if(s==0){if(r.hasClass(p.flaghalfcheck)){r.removeClass(p.flaghalfcheck)}else{r.removeClass(p.flagcheck)}q.attr("src",p.icon_uncheck)}}}return},n=function(q){var r=false;if(q.hasClass(p.flagcheck)){if((typeof p.unchecker=="function")&&p.singleshot==false){p.unchecker(q)}j(q,0);i(q,0)}else{if(p.singleselection){b("."+p.flagcheck).each(function(){j(b(this),0);i(b(this),0)})}if(p.singleshot==true){b(this).find("."+p.flagcheck).each(function(){b(this).removeClass(p.flagcheck);b("> "+p.row_class+" .thebullet",this).attr("src",p.icon_uncheck);if(p.unchecker!=null){p.unchecker()}});if(p.checker!=null){p.checker(q.attr("id").substr(4),b(p.row_class+"> span",q).html())}}else{if(typeof p.checker=="function"&&p.singleselection==false){p.checker(q)}}j(q,1);i(q,1);r=true}d(q);if(p.singleshot==false&&p.collect==true){if(c()==false){throw new Error("Tree error!")}}else{if(p.singleshot==false&&typeof p.collect=="function"){if(p.collect()==false){throw new Error("Tree error!")}}}},h=function(q){if(b("> ul",q).length>0){if(p.branchaction){p.branchaction(b(q).attr("id").substr(4))}if(b("> ul",q).hasClass("prodfield")){if(b("> ul",q).children("li").length>0){if(b("> ul",q).children("li").hasClass(p.ajax)){l(b("> ul",q).children("li"));b("> ul",q).children("li").removeClass(p.ajax)}}}if(b("> ul",q).hasClass(p.expand)){b("> ul",q).slideUp().removeClass(p.expand);b("> "+p.row_class+" .ibullet",q).attr("src",p.icon_collapse)}else{b("> ul",q).slideDown().addClass(p.expand);b("> "+p.row_class+" .ibullet",q).attr("src",p.icon_expand)}}else{n(q)}},k=function(r){var q;b("li "+p.row_class+" span",r).click(function(){q=b(this).parents("li:first");h(q);return false});b("li "+p.row_class+" .ibullet",r).click(function(){q=b(this).parents("li:first");h(q);return false});b("li "+p.row_class+" .thebullet",r).click(function(){q=b(this).parents("li:first");n(q);return false})},c=function(){var t=0,r=0,u=0,v=4,q=new Array(),s=false;var w=b("."+p.flagcheck);m=new Array();w.each(function(x){u=this.id.substr(v);if(q.length>0&&r==q[q.length-1]){s=false}if(s==false){m.push(u);q.pop();r=0}if(b("> ul",this).length>0&&b(this).hasClass(p.flagcheck)&&s==false){q.push(b(this).find("."+p.flagcheck).length+1);s=true}(!s?r=0:r++)});return true},l=function(r){var q=b.trim(b(r).text());var s=b(r).parent();b(r).html($loading);b.ajax({type:"GET",url:q,success:function(t){b("> li",s).remove();s.append(t);k(s);b("> .thebullet",s)}})},g=function(){var u=null,w=null;if(p.load){var t=p.load,v=(p.mount==0)?0:p.mount.data["products"];for(var s in t){if(t[s].constructor.toString().indexOf("Array")!=-1){for(var r in t[s]){li=document.createElement("li");li.id="prod_"+t[s][r].pid;li.className="leaf";w=document.createElement("div");w.className=p.row;u=document.createElement("span");u.innerHTML=t[s][r].name;if(v==0){for(var q=0;q<v.length;q++){if(v[q]["pid"]==t[s][r].pid){w.className+=" foxytree-mount"}}}w.appendChild(u);li.appendChild(w);b("#cat_"+s+" "+p.ulloader_class).append(li)}}}}return false},f=function(){var q=p.load();if(q!=null){q.parents("ul").each(function(){if(b(this).is(":hidden")){b(this).slideDown().addClass(p.expand)}});n(q)}};return{init:function(q){p=b.extend({},e,q||{});element=b(this);m=new Array();if(b("li "+p.row_class,element).is("div")==false){var r=document.createElement("div");r.className=p.row;r.innerHTML=b("li",element).html();b("li",element).html(r)}b("li "+p.row_class,element).each(function(){if(b("img .ibullet",this).length>0==false){var s,t;s=document.createElement("img");s.src=p.icon_uncheck;s.alt="";s.className="thebullet";t=document.createElement("img");t.src=p.icon_collapse;t.alt="";t.className="ibullet";if(p.singleshot){if(b(this).siblings("ul").length>0){b(this).prepend(b(t));b(this).parent("li").addClass(p.branch)}else{b(this).prepend(b(s))}}else{b(this).prepend(b(s));if(b(this).siblings("ul").length>0){b(this).prepend(b(t));b(this).parent("li").addClass(p.branch)}else{b(this).parent("li").addClass(p.leaf)}}}b(this).siblings("ul").hide();if(p.type=="open"){b(this).children("li").children("ul").each(function(){b(this).show().addClass(p.expand).parent("li").children("div").children(".ibullet").attr("src",p.icon_expand)})}});if(p.singleshot==true&&p.load!=null){f()}k(element);if(p.preload==true&&p.load!=null){m=p.load;b.each(p.load,function(t,s){if(b("#cat_"+s).length>0){n(b("#cat_"+s))}})}},get_values:function(){return((m.length>0)?m:0)},get_leaves:function(r){var q=[],s=null;s=b(this).children("li").filter("."+p.flagcheck+",."+p.flaghalfcheck);s.each(function(t){q.push({cid:this.id.substr(r-1),branches:new Array()});b(this).find("."+p.flagcheck+",."+p.flaghalfcheck).filter("."+p.leaf).each(function(u){q[t].branches.push({pid:this.id.substr(r),path:new Array()});b(this).parents("li").each(function(){q[t].branches[u].path.push(this.id.substr(r-1))});q[t].branches[u].path.reverse()})});return q}}}();b.fn.extend({foxyTree:a.init,foxyTreeValues:a.get_values,foxyTreeLeaves:a.get_leaves})})(jQuery);