﻿BGMap.SlotFleurop=Class.create({nodraw:false,initialize:function(slotcontrol,options){this.options=Object.extend({gateway:"GatewayFleurop.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.toggleLayerNew.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(null,"zoom",this.onCityChange.bind(this));BGMap.Event.register(null,"positionchange",this.onCityChange.bind(this));BGMap.Event.register(null,'convertcoords',this.convertLineCoords.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.extended=true;slot.abbrs=[];slot.subcats=slot.abbreviations;slot.abbreviations.each(function(abbr,i){slot.subcats[i].title=abbr.name;slot.subcats[i].abbr=[abbr.id];slot.abbrs.push(abbr.id);});slot.layers=slot.abbrs;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));this.openAbbrs.each(function(subcat){this.initSubcat(subcat);}.bind(this));if(this.openAbbrs.length){this.open=true;$(this.items.title.toLowerCase()).checked=true;this.slotcontrol.addOpenSlot(this.items.id);this.slotcontrol.openLayersId=this.slotcontrol.openLayersId.concat(this.openAbbrs);this.slotcontrol.logoToggle(this.items.id,true);this.updateOpenLayers();};if(this.slotcontrol.map.lineType=="slot"&&this.openAbbrs.length){this.getNearestPoiRequest(this.items.id,this.openAbbrs.last());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){if($("slot-toggle-"+id))$("slot-toggle-"+id).checked=true;},convertLineCoords:function(from,to){if(this.slotcontrol.map.lineType=="slot"&&this.openAbbrs.length){this.LineGeoStart=convertCoords(this.LineGeoStart,from,to);this.LineGeoEnd=convertCoords(this.LineGeoEnd,from,to);}},onCityChange:function(newCenter){if($(this.items.title.toLowerCase()).checked&&this.slotcontrol.map.lineType=="slot")this.getNearestPoi(this.items.id,newCenter);},getNearestPoi:function(pid,aid,newCenter){var id=pid||this.items.id;var center=newCenter?newCenter.position:null;if(!this.getNearestPoiRequest(id,aid,center))return;if(pid){this.slotcontrol.map.loadLine(this.LineGeoStart,this.LineGeoEnd,"slot");}},getNearestPoiRequest:function(id,aid,newCenter){var success=false;var center=newCenter||this.slotcontrol.map.rcMarker.position;new Ajax.Request(this.options.gateway,{method:"post",asynchronous:false,parameters:{a:"getnearestobject",sid:sid,id:id,aid:aid,llC:center.toString()},onSuccess:function(req,json){success=!!json;if(!success)return;this.GeoEmergency=new glLongLat(json.longitude,json.latitude);this.LineAbbr=json.abbreviationid;if(!aid&&(this.LineAbbr!=parseInt(this.openAbbrs.last()))){this.openAbbrs=this.openAbbrs.without(this.LineAbbr);this.openAbbrs.push(this.LineAbbr);};this.LineGeoStart=center;this.LineGeoEnd=this.GeoEmergency;}.bind(this)});return success;},toggleLayersWithLogoClicked:function(show){this.open=this.slotcontrol.openedSlots.include(this.items.id);this.toggleLayers(show);if(show&&!this.open)this.slotcontrol.toggleContent(this.items.id,true);},toggleLayers:function(show,nosavesession){var abbrs=this.getAbbrList();if(show){this.open=true;this.openAbbrs=abbrs;this.slotcontrol.hideMapLayers();this.slotcontrol.addOpenSlot(this.items.id);this.getNearestPoi(this.items.id,"",true);abbrs=this.openAbbrs;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.open=false;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);};$(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 banner=$("a0a4b8bcamaprs");if(this.items.subcats.length==1){this.toggleLayer(this.items.subcats[0].id);if(this.slotcontrol.getSlotOpenLayerById2(this.items.subcats[0].id))this.toggleNearest(this.items.subcats[0].id,nosavesession||false);if(banner)banner.noreload=!!this.slotcontrol.getSlotOpenLayerById2(this.items.subcats[0].id);}else{this.toggleLayer(id);this.toggleNearest(id,nosavesession||false);if(banner)banner.noreload=!!this.slotcontrol.getSlotOpenLayerById2(id);}},toggleLayer:function(id,nosavesession){var openLayer=this.slotcontrol.getSlotOpenLayerById2(id);var abbrs=this.getSubcatById(id).abbr;if(openLayer){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.logoToggle(this.items.id,!openLayer);this.slotcontrol.removeOpenSlot(this.items.id);};if(openLayer==this.LineAbbr&&this.slotcontrol.map.lineType=="slot"){this.slotcontrol.map.hideLine();};this.slotcontrol.hideButton(id);}else{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);};this.getNearestPoi(this.items.id,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.slotcontrol.openLayersId.include(id)){this.slotcontrol.showButton(id);this.slotcontrol.openLayersId.push(id);}};$(this.items.title.toLowerCase()).checked=!openLayer;this.slotcontrol.logoToggle(this.items.id,!openLayer);if($("slot-toggle-"+id))$("slot-toggle-"+id).checked=!openLayer;else{this.slotcontrol.logoToggle(this.items.id,!openLayer);$("slot-"+this.items.id).select(".small-panel-content")[0].style.display=(!openLayer?"block":"none");};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;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){if(this.items.subcats.length>1)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 domElements=wrap.select(".tooltip-"+key);$A(domElements).each(function(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("» mehr Informationen"));}else domElement.appendChild(document.createTextNode(info[key]));}.bind(this));};var desc=wrap.select(".tooltip-desc")[0];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;}});


