/**
 * jQuery Plugin : XMap (Gmap setup by xml file)
 * 
 * @file: jquery.xmap.pack.js
 * @version: 0.3.3
 * @author Mizugame <contact*picsmate.net> (*を@に変更して送信してください)
 * @since  2008-03-24
 * 
 *   Copyright (c) 2008 picsmate.net
 *   Licensed under GPL (http://opentechpress.jp/opensource/article.pl?sid=07/09/02/130237)
 *   
 *   Changelog:
 *     2008-3-24 version 0.3.3
 * 
 * 
 * 2009-04-24
 * 3945文字目
 * anchor={width:width!=0?Math.floor(width/2):0,height:height!=0?Math.floor(height/2):0}
 * anchor={width:width!=0?Math.floor(width/2):0,height:height!=0?0:0}
 * 
 */
(function($j){$j.XMap={version:'0.3.3'};if(GBrowserIsCompatible()){$j(window).unload(GUnload)}var _xmap=function(element){var self=this;self.$map=new GMap2(element);self.$geo=new GClientGeocoder();self.$element=element;return self};_xmap.prototype={$load:function(uri){var self=this;if(uri){$j.ajax({url:uri,type:'GET',dataType:'xml',success:function(xml){self.$setting.call(self,xml)},error:function(){return false}})}return self},$setting:function(xml){var self=this,xObj=$j(xml),show=xObj.find('show');self.$info={$xml:xObj,$center:self.$getCenterInfo(show.find('center')),$control:self.$getControlInfo(show.find('control')),$option:self.$getOptionInfo(show.find('option')),$icons:xObj.find('icons > icon'),$markers:xObj.find('markers > marker')};self.$positioning(self.$info.$center).$setControls(self.$info.$control).$setIcons(self.$info.$icons).$setMarkers(self.$info.$markers).$setOptions(self.$info.$option).$setCenterMarker();return self},$getCenterInfo:function(jObject){var self=this;return{lat:jObject.attr('lat')||"35.681391",lng:jObject.attr('lng')||"139.766103",address:jObject.attr('address')||false,zoom:parseInt(jObject.attr('zoom'))||13,icon:self.$setIcon(jObject.find('img'),jObject.find('shadow'))}},$getControlInfo:function(jObject){return{large:(jObject.find('large').attr('value')=='true'),small:(jObject.find('small').attr('value')=='true'),zoom:(jObject.find('zoom').attr('value')=='true'),type:(jObject.find('type').attr('value')=='true'),overview:(jObject.find('overview').attr('value')=='true'),scale:(jObject.find('scale').attr('value')=='true'),_large:new GLargeMapControl(),_small:new GSmallMapControl(),_zoom:new GSmallZoomControl(),_type:new GMapTypeControl(),_overview:new GOverviewMapControl(),_scale:new GScaleControl()}},$getOptionInfo:function(jObject){return{wheelZoom:(jObject.find('wheelZoom').attr('value')=='true')}},$getMarkerInfo:function(jObject){var self=this,lat=jObject.attr('lat')||false,lng=jObject.attr('lng')||false,point=(lat!==false||lng!==false)?new GLatLng(lat,lng):null,options=jObject.find('option');return{name:jObject.attr('name'),point:point,address:jObject.attr('address'),option:{icon:self.$icons[options.find('icon').attr('name')]||null,title:options.find('title').text()||null,draggable:(options.find('drag').attr('value')=='true')},html:jObject.find('html').text()}},$positioning:function(info){var self=this;self.$map.setCenter(new GLatLng(info.lat,info.lng),info.zoom);if(info.address){self.$geo.getLatLng(info.address,function(point){if(point)self.$map.setCenter(point,info.zoom)})}return self},$setControls:function(info){var self=this;if(info.large===true)self.$map.addControl(info._large);if(info.small===true)self.$map.addControl(info._small);if(info.zoom===true)self.$map.addControl(info._zoom);if(info.type===true)self.$map.addControl(info._type);if(info.overview===true)self.$map.addControl(info._overview);if(info.scale===true)self.$map.addControl(info._scale);return self},$switchControl:function(names){var self=this,info=self.$info.$control;if(names.constructor==String){var name=names.toLowerCase();switch(name){case'large':case'small':case'zoom':case'type':case'overview':case'scale':(!(info[name]=!info[name]))?self.$map.removeControl(info['_'+name]):self.$map.addControl(info['_'+name]);break}}else{for(name in names){if(names[name].constructor==String)self.$switchControl(names[name])}}return self},$setIcons:function(jObject){var self=this;self.$icons={};jObject.each(function(i){var element=$j(this);self.$saveIcon(self.$setIcon(element.find('img'),element.find('shadow')),element.attr('name'))});return self},$setIcon:function(img,shadow){var self=this;if(img){var icon=self.$newIcon(img.attr('src'),img.attr('width'),img.attr('height'));if(shadow&&icon.$shadow){icon.$shadow(shadow.attr('src'),shadow.attr('width'),shadow.attr('height'))}}return icon||null},$newIcon:function(src,width,height){var width=parseInt(width)||30,height=parseInt(height)||30,anchor={width:width!=0?Math.floor(width/2):0,height:height!=0?Math.floor(height):0},icon=new GIcon();icon.image=src;icon.iconSize=new GSize(width,height);icon.iconAnchor=new GPoint(anchor.width,anchor.height);icon.infoWindowAnchor=new GPoint(anchor.width,0);icon.$shadow=function(src,width,height){var self=this,width=parseInt(width)||30,height=parseInt(height)||30;self.shadow=src;self.shadowSize=new GSize(width,height)};return icon},$getIcon:function(name){return(name!=undefined)?this.$icons[name]:this.$icons},$saveIcon:function(icon,name){var self=this;if(icon)self.$icons[name||'icon']=icon;return self},$setMarkers:function(jObject){var self=this;self.$clearMarker();jObject.each(function(i){var info=self.$getMarkerInfo($j(this));if(info.address){self.$geo.getLatLng(info.address,function(point){var position=point||info.point;if(position){self.$saveMarker(self.$printMarker(position,info.option,info.html),info.name)}})}else{self.$saveMarker(self.$printMarker(info.point,info.option,info.html))}});return self},$printMarker:function(point,option,html){var self=this,marker=new GMarker(point,option);if(html){GEvent.addListener(marker,"click",function(){this.openInfoWindowHtml(html)})}self.$map.addOverlay(marker);return marker},$getMarker:function(name){return(name!=undefined)?this.$markers[name]:this.$markers},$saveMarker:function(marker,name){var self=this;if(marker)self.$markers[name||'marker']=marker;return self},$centerMarker:function(){var self=this,info=self.$info.$center;if(info.icon){var marker=new GMarker(self.$map.getCenter(),info.icon);self.$map.addOverlay(marker);info.marker=marker}return self},$setCenterMarker:function(){var self=this;if(self.$info.$center.icon){self.$centerMarker().$moveCenterIconEvent()}return self},$removeMarker:function(overlay){var self=this;self.$map.removeOverlay(overlay);return self},$clearMarker:function(){var self=this;self.$map.clearOverlays();self.$markers={};return self},$setOptions:function(info){var self=this;if(info.wheelZoom===true){self.$wheelEvent(self.$element,function(e){var self=this;if(e.detail){var status=e.detail*-1;e.preventDefault()}else{var status=e.wheelDelta;e.returnValue=false}self.$map.setZoom(self.$map.getZoom()+(status<0?1:-1))})}return self},$moveCenterIconEvent:function(){var self=this,info=self.$info.$center;if(info.icon){GEvent.addListener(self.$map,"move",function(){info.marker.setPoint(this.getCenter())})}return self},$wheelEvent:function(element,fn){var self=this,call=function(e){return fn.call(self,e)};if(element.onmousewheel){element.onmousewheel=call}else if(element.addEventListener){element.addEventListener('DOMMouseScroll',call,false)}else{element.attachEvent('onmousewheel',call)}return self}};$j.fn.XMap=function(uri){if(!uri)return this[0]._XMap||false;if(!GBrowserIsCompatible())return false;var self=this;self.each(function(){var self=this;self._XMap=new _xmap(self);self._XMap.$load(uri)});return self};$j.fn._GMap2=function(){return this[0]._XMap.$map};$j.fn._Geo=function(){return this[0]._XMap.$geo}})(jQuery);
