function ZoneController(){function t(){n()}function n(){e=[]}function r(e,t){var n=funcObj.groupCon.getGroupZoneMaster($(t).attr("master")),r=$(t).children("member");if(n!=undefined&&n!=null&&r.length>0){if(n==e){var i=[];for(var u=0;u<r.length;u++){var a=funcObj.groupCon.getGroupZoneMaster($(r[u]).text());i.push(a)}i=deDupe(i),s(n,i,!1,!1,!1)}}else o(e,!1,!0)}function i(e,t,n,r,i){funcObj.message.closeMessage(),s(e,t,n,r,i)}function s(t,n,r,i,s){var u=!1,f=c(t),l=n.indexOf(t);l!=-1&&n.splice(l,1);var h=n.length-1;for(h;h>=0;h--){var p=funcObj.groupCon.getGroupByUID(n[h]);p&&p.groupMaster!==n[h]&&n.splice(h,1)}if(f!=null)if(r){var d=f.slaves.indexOf(n[0]);i&&d!=-1?(u=!0,f.slaves.splice(d,1)):!i&&d==-1&&(u=!0,f.slaves.push(n[0]))}else{if(f.slaves.length!=n.length)u=!0;else for(var v=0;v<n.length;v++)if(f.slaves.indexOf(n[v])==-1){u=!0;break}f.slaves=n.slice()}else u=!0,f=new Zone(t,n,!0),e.push(f);for(var m=e.length-1;m>=0;m--)if(e[m].master!=t)for(var g=n.length-1;g>=0;g--){var y=e[m].slaves.indexOf(n[g]);if(y!=-1){u=!0,e[m].slaves.splice(y,1);if(e[m].slaves.length==0){o(e[m].master,!1,!1);break}}if(e[m].master==n[g]){u=!0,o(n[g],!1,!1);break}}f.slaves.length==0&&o(t,!1,!1),u&&(f.syncDevices(),funcObj.deviceTray.updateTrayView(!0)),s&&(r?i?f.slaves.length==0?a(f.master,null,"remove"):a(f.master,[n[0]],"removeSlave"):f.slaves.length==1?a(f.master,[n[0]],"add"):a(f.master,[n[0]],"addSlave"):a(f.master,f.slaves,"add"))}function o(t,n,r){var i=!1;for(var s=e.length-1;s>=0;s--)if(e[s].master==t){e.splice(s,1),i=!0;break}i&&r&&funcObj.deviceTray.updateTrayView(!0),n&&a(t,null,"remove")}function u(t,n,r){if(r)o(t,!0,!0);else{for(var i=e.length-1;i>=0;i--)e[i].master!=t&&e.splice(i,1);s(t,n,!1,!1,!0)}}function a(e,t,n){switch(n){case"add":if(t!=undefined&&t!=null&&t.length>0){var r="",i=funcObj.groupCon.getGroupZoneUID(e),s=funcObj.devCon.getDeviceIP(e);if(i){r+=funcObj.messageCon.getContent("zoneMember",{MEMBER:i,MEMBERIP:s});for(var o=0;o<t.length;o++){var u=funcObj.groupCon.getGroupZoneUID(t[o]),a=funcObj.devCon.getDeviceIP(t[o]);u&&(r+=funcObj.messageCon.getContent("zoneMember",{MEMBER:u,MEMBERIP:a}))}funcObj.devCon.sendMsg(e,"setZone","newZone",{MASTER:i,MEMBERS:r},null,!1)}}break;case"remove":var i=funcObj.groupCon.getGroupZoneUID(e);i&&funcObj.devCon.sendMsg(e,"setZone","clearZone",{MASTER:i},null,!1);break;case"addSlave":if(t!=undefined&&t!=null&&t.length>0){var i=funcObj.groupCon.getGroupZoneUID(e),s=funcObj.devCon.getDeviceIP(e),u=funcObj.groupCon.getGroupZoneUID(t[0]),a=funcObj.devCon.getDeviceIP(t[0]);i&&u&&funcObj.devCon.sendMsg(e,"addZoneSlave","zoneSlaveUpdate",{MASTER:i,MASTERMEMBER:i,MASTERMEMBERIP:s,MEMBER:u,MEMBERIP:a},null,!1)}break;case"removeSlave":if(t!=undefined&&t!=null&&t.length>0){var i=funcObj.groupCon.getGroupZoneUID(e),s=funcObj.devCon.getDeviceIP(e),u=funcObj.groupCon.getGroupZoneUID(t[0]),a=funcObj.devCon.getDeviceIP(t[0]);i&&u&&funcObj.devCon.sendMsg(e,"removeZoneSlave","zoneSlaveUpdate",{MASTER:i,MASTERMEMBER:i,MASTERMEMBERIP:s,MEMBER:u,MEMBERIP:a},null,!1)}}}function f(){return e}function l(e){showDebug===2&&console.log("dev_tray.js: getZoneSlavesCount: device id: "+e);var t=0,n=f();for(var r=0;r<n.length;r++)n[r].master==e&&(t=n[r].slaves.length);return t}function c(t){var n=null,r=$.grep(e,function(e){return e.master==t});return r.length>0&&(n=r[0]),n}function h(e){var t=!1;return c(e)!=null&&(t=!0),t}function p(t){var n=null;for(var r=0,i=e.length;r<i;r++)for(var s=0,o=e[r].slaves.length;s<o;s++)e[r].slaves[s]==t&&(n=e[r].master);return n}var e;this.getZones=f,this.getZone=c,this.parseZone=r,this.refUpdateZone=i,this.updateZone=s,this.allZone=u,this.removeZone=o,this.isMaster=h,this.findMasterOfSlave=p,this.getZoneSlavesCount=l,t()};