﻿BGMap.SlotApotheken=Class.create({nodraw:false,initialize:function(slotcontrol,options){this.options=Object.extend({gateway:"GatewayApotheken.ashx"},options||{});this.slotcontrol=slotcontrol||null;this.openAbbrs=options.abbrs?$A(options.abbrs.split(BGMap.delimiter)):[];this.slotcontrol.getSlot(this);var title=this.items.title.toLowerCase();BGMap.Event.register(slotcontrol,"slot-"+title,this.toggleLayers.bind(this));BGMap.Event.register(slotcontrol,"slot-logo-"+title,this.toggleLayersWithLogoClicked.bind(this));BGMap.Event.register(slotcontrol,"toggle-layer-"+title,this.toggleLayerNew.bind(this));BGMap.Event.register(slotcontrol,"toggle-nearest-"+title,this.toggleNearest.bind(this));BGMap.Event.register(slotcontrol,"restart-nearest-"+title,this.restartNearest.bind(this));BGMap.Event.register(slotcontrol.map,'mapcenterchange',this.updateOpenLayers.bind(this));BGMap.Event.register(slotcontrol.map,'mapdrop',this.updateOpenLayers.bind(this));BGMap.Event.register(slotcontrol.map,'mapcoordsyschange',this.updateOpenLayers.bind(this,true));BGMap.Event.register(slotcontrol.map,'mapcoordsyschange',this.onCityChange.bind(this));BGMap.Event.register(slotcontrol.map,'zoom',this.updateOpenLayers.bind(this,true));BGMap.Event.register(slotcontrol.map,'zoom',this.onCityChange.bind(this));BGMap.Event.register(null,'positionchange',this.onCityChange.bind(this));this.nearest=options.nearest?$A(options.nearest.split(BGMap.delimiter)):[];this.initSession(this.nearest);},build:function(req,json){if(!json)return;var slot=json;slot.subcats=[{id:1,title:"Nur Notdienst-Apotheken",abbr:[]},{id:2,title:"Alle Apotheken",abbr:[]}];slot.extended=true;slot.layers=[1,2];slot.abbrs=[];slot.abbreviations.each(function(abbr){if(/emergency/.test(abbr.name.toLowerCase()))slot.subcats[0].abbr.push(abbr.id);else slot.subcats[1].abbr.push(abbr.id);slot.abbrs.push(abbr.id);});this.items=slot;this.slotcontrol.slots.push(slot);this.slotcontrol.buildPanel(slot);},initSession:function(nearest){var lastAbbr=this.openAbbrs.last();this.openAbbrs=this.openAbbrs.findAll(function(abbr){return this.items.abbrs.include(abbr)}.bind(this));var subcats=this.convertAbbrsToSubcats();subcats.each(function(subcat){this.initSubcat(subcat);if(subcat==2)$("slot-toggle-1").disabled=true;}.bind(this));if(subcats.length){$(this.items.title.toLowerCase()).checked=true;this.slotcontrol.addOpenSlot(this.items.id);this.slotcontrol.openLayersId=this.slotcontrol.openLayersId.concat(subcats);this.slotcontrol.logoToggle(this.items.id,true);this.updateOpenLayers();};if(this.slotcontrol.map.lineType=="slot"&&subcats.include(1)){this.getNearestPharmacyRequest(this.items.id);this.slotcontrol.map.loadLine(this.LineGeoStart,this.LineGeoEnd,"slot");};this.nearest=this.nearest.findAll(function(nid){return this.items.layers.include(nid)}.bind(this));this.nearest.each(function(list){this.toggleNearest(list,true);}.bind(this));},initSubcat:function(id){$("slot-toggle-"+id).checked=true;},onCityChange:function(newCenter){if(this.openAbbrs.include(1)&&this.slotcontrol.map.lineType=="slot")this.getNearestPharmacy(1,newCenter);},getNearestPharmacy:function(pid,newCenter){var id=pid||this.items.id;var center=newCenter?newCenter.position:null;if(!this.getNearestPharmacyRequest(id,center))return;if(pid){this.slotcontrol.map.loadLine(this.LineGeoStart,this.LineGeoEnd,"slot");}},getNearestPharmacyRequest:function(id,newCenter){var success=false;var center=newCenter||this.slotcontrol.map.rcMarker.position;new Ajax.Request(this.options.gateway,{method:"post",asynchronous:false,parameters:{a:"getnearestemregencypharmacy",sid:sid,id:id,llC:center.toString()},onSuccess:function(req,json){success=!!json;if(!success)return;this.GeoEmergency=new glLongLat(json.longitude,json.latitude);this.LineGeoStart=center;this.LineGeoEnd=this.GeoEmergency;}.bind(this)});return success;},toggleLayersWithLogoClicked:function(show){var open=this.slotcontrol.openedSlots.include(this.items.id);this.toggleLayers(show);if(show&&!open)this.slotcontrol.toggleContent(this.items.id,true);},toggleLayers:function(show,nosavesession){var abbrs=this.getAbbrList();if(show){this.openAbbrs=abbrs;if(!this.slotcontrol.getSlotOpenLayerById2(1))this.slotcontrol.hideMapLayers();this.slotcontrol.addOpenSlot(this.items.id);this.getNearestPharmacy(this.items.id,true);this.slotcontrol.getObjects(abbrs.join(BGMap.delimiter),this.options.gateway,this.combineAbbrs.bind(this,this.convertAbbrsToSubcats()));this.toggleCheckBoxes(true);this.slotcontrol.openAbbrs=this.slotcontrol.openAbbrs.concat(abbrs);if(!this.slotcontrol.openedSlots.include(this.items.id))this.slotcontrol.toggleContent(this.items.id,true);}else{this.items.subcats.each(function(subcat){this.slotcontrol.hideObjects(subcat.id);}.bind(this));this.openAbbrs=[];this.slotcontrol.openAbbrs=this.slotcontrol.openAbbrs.reject(function(abbr){return abbrs.include(abbr)});this.toggleCheckBoxes(false);if(this.slotcontrol.map.lineType=='slot')this.slotcontrol.map.hideLine();this.slotcontrol.removeOpenSlot(this.items.id);};$("slot-toggle-1").disabled=show;$(this.items.title.toLowerCase()).checked=show;},combineAbbrs:function(ids,req,json){var pois=[];this.items.subcats.each(function(subcat,index){var toBePois=json.findAll(function(item){return subcat.abbr.include(item.id);}.bind(this));if(ids.include(subcat.id)){var i=pois.push({id:subcat.id,template:this.items.id});pois[--i].pois=[];toBePois.each(function(toBePoi){pois[i].pois=pois[i].pois.concat(toBePoi.pois);});}}.bind(this));this.slotcontrol.showObjects(req,pois,ids,this.options.gateway,this.buildTooltip);},toggleLayerNew:function(id,nosavesession){var openLayer=this.slotcontrol.getSlotOpenLayerById2(id);if(openLayer)this.toggleLayer(id);else this.toggleNearest(id,nosavesession||false);},toggleLayer:function(id,nosavesession){var openLayer=this.slotcontrol.getSlotOpenLayerById2(id);var abbrs=this.getSubcatById(id).abbr;if(openLayer){if(id==2){this.toggleLayers(false);this.slotcontrol.logoToggle(this.items.id,!openLayer);if(!nosavesession)BGMap.Event.trigger(null,"statechanged");return;};this.slotcontrol.hideObjects(openLayer);if(this.slotcontrol.getSlotOpenNearestById(id))this.toggleNearest(id,true);abbrs.each(function(abbr){this.openAbbrs=this.openAbbrs.without(abbr)}.bind(this));this.slotcontrol.openAbbrs=this.slotcontrol.openAbbrs.reject(function(abbr){return abbrs.include(abbr)});var checked=false;for(var i=0;i<this.items.subcats.length;i++){checked=this.slotcontrol.getSlotOpenLayerById(this.items.subcats[i].id)!=null;if(checked)break;};if(!checked){$(this.items.title.toLowerCase()).checked=false;this.slotcontrol.removeOpenSlot(this.items.id);};if(id==1&&this.slotcontrol.map.lineType=="slot")this.slotcontrol.map.hideLine();this.slotcontrol.hideButton(id);}else{if(id==2){this.toggleLayers(true);this.slotcontrol.logoToggle(this.items.id,!openLayer);if(!nosavesession)BGMap.Event.trigger(null,"statechanged");return;};if(!this.openAbbrs.length){this.slotcontrol.hideMapLayers();};if(!this.slotcontrol.isSlotOpen(this.items.id)){this.slotcontrol.addOpenSlot(this.items.id);BGMap.SlotControl.logStatistics("SlotLayerOn",this.items.id);};if(id==1)this.getNearestPharmacy(id,true);this.openAbbrs=this.openAbbrs.concat(abbrs);this.slotcontrol.openAbbrs=this.slotcontrol.openAbbrs.concat(abbrs);this.slotcontrol.getObjects(abbrs.join(BGMap.delimiter),this.options.gateway,this.combineAbbrs.bind(this,[id]));if(!$(this.items.title.toLowerCase()).checked)$(this.items.title.toLowerCase()).checked=true;if(!this.slotcontrol.openLayersId.include(id)){this.slotcontrol.showButton(id);this.slotcontrol.openLayersId.push(id);}};$("slot-toggle-"+id).checked=!openLayer;this.slotcontrol.logoToggle(this.items.id,!openLayer);if(!nosavesession)BGMap.Event.trigger(null,"statechanged");},updateOpenLayers:function(clear){if(!this.openAbbrs||!this.openAbbrs.length||this.nodraw)return;if(clear){this.slotcontrol.openLayersId.each(function(layerId){layer=this.slotcontrol.getSlotLayerById(layerId);if(layer)layer.clearOverlays();}.bind(this));};this.refreshTimeout=setTimeout(function(){this.slotcontrol.getObjects(this.openAbbrs.join(BGMap.delimiter),this.options.gateway,this.combineAbbrs.bind(this,this.convertAbbrsToSubcats()));delete this.refreshTimeout;}.bind(this),1000||this.slotcontrol.map.layerRefreshTimeout);},hideAll:function(maplayers){if(!maplayers.length)return;this.toggleLayers(false,true);$(this.items.title.toLowerCase()).checked=false;},getAbbrList:function(){var abbrs=[];$A(this.items.subcats).each(function(subcat){abbrs=abbrs.concat(subcat.abbr)});return abbrs;},toggleNearest:function(id,nosavesession){var abbrList;if(id==2)abbrList=this.getAbbrList().join(BGMap.delimiter);else abbrList=this.getSubcatById(id).abbr.join(BGMap.delimiter);var openNearest=this.slotcontrol.getSlotOpenNearestById(id);if(openNearest){this.slotcontrol.closeNearest(id);if(!nosavesession)BGMap.Event.trigger(null,"statechanged");this.nearest=this.nearest.without(id);}else{if(!this.slotcontrol.getSlotOpenLayerById2(id))this.toggleLayer(id,true);this.slotcontrol.getNearest(id,this.options.gateway,abbrList);if(!this.nearest.include(id))this.nearest.push(id);};this.slotcontrol.toggleButton(id);},restartNearest:function(id){var abbrList=this.getSubcatById(id).abbr.join(BGMap.delimiter);this.slotcontrol.getNearest(id,this.options.gateway,abbrList);},convertAbbrsToSubcats:function(){var subcats=[];this.openAbbrs.each(function(abbr){this.items.subcats.each(function(subcat){if(subcat.abbr.include(abbr)&&!subcats.include(subcat.id))subcats.push(subcat.id);});}.bind(this));return subcats;},getSubcatById:function(id){return this.items.subcats.detect(function(subcat){return subcat.id==id});},toggleCheckBoxes:function(checked){this.items.subcats.each(function(subcat){$("slot-toggle-"+subcat.id).checked=checked;});},buildTooltip:function(info){var wrap=$(newElement('div',BGMap.attachPoint));wrap.style.visibility='hidden';wrap.absolutize();wrap.setPosition(new Pos(0,0));wrap.innerHTML=BGMap.ToolTipTemplate["t"+this.options.template];for(var key in info){var domElement=wrap.select(".tooltip-"+key)[0];if(domElement){if(/url/.test(key)){var url=info[key];Event.observe(domElement,"click",function(url){window.open(url);BGMap.SlotControl.logStatistics("SlotTooltipClicked",this.options.template);}.bind(this,url));};domElement.appendChild(document.createTextNode(info[key]));}};var imgClose=wrap.select(".close-button")[0];changeCursor(imgClose,"pointer");imgClose.style.zIndex='1000';this.btnClose=imgClose;this.eventCloseButton=function(event){this.close();Event.stop(event);}.bindAsEventListener(this);Event.observe(this.btnClose,'click',this.eventCloseButton);var content=wrap.lastChild;var dimensions=Element.getDimensions(content);content.style.width=pix(dimensions.width);content.style.height=pix(dimensions.height);this.dimensions={width:parseInt(content.style.width)+3,height:parseInt(content.style.height)+3};wrap.setSize(this.dimensions);BGMap.attachPoint.removeChild(wrap);return wrap;}});


