/**
 * Search.Map
 * 
 * @license GNU General Public License v2
 * @author Towers Planning Co.,Ltd.
 * @version 1.0
 * @see http://www.towers-p.co.jp/
 * @see http://mootools.net
 * @since 2009
 */

var Search;
if (!Search) {Search = {}};

Search.Map = new Class({

	Implements: [Events, Options],

	"options": {
		"data": {},
		"lat": "",
		"lng": ""
	},

	show: false,

	initialize: function(element, options) {
		this.setOptions(options);
		this.element = element;

		this.loading = "loading";
		this.assetPath = "http://www.rental-of.jp/skin/images/";
		this.request = new Request({
			"url": "map.php",
			"method": "get",
			"onRequest": this.onRequest.bind(this),
			"onSuccess": this.onSuccess.bind(this)
		})

		this.index = 0;
		this.markers = [];
		this.informations = [];

		if (GBrowserIsCompatible()) {
			this.createMap(new GLatLng(this.options.lat, this.options.lng));
			this.setBounds();
			this.request.send(this.getQueryString());
		}
	},

	getQueryString: function(){
		var query = new Array();
		query.push("swlat=");
		query.push(this.southWest.lat());
		query.push("&");
		query.push("swlng=");
		query.push(this.southWest.lng());
		query.push("&");
		query.push("nelat=");
		query.push(this.northEast.lat());
		query.push("&");
		query.push("nelng=");
		query.push(this.northEast.lng());
		return query.join("");
	},
	
	createNormalIcon: function() {
		var icon = this.createGUIIcon();
		icon.image = this.assetPath + "img_maker.png";
		return icon;
	},

	createSpecialIcon: function() {
		var icon = this.createGUIIcon();
		icon.image = this.assetPath + "img_special_maker.png";
		return icon;
	},

	createGUIIcon: function() {
		var guyIcon = new GIcon(G_DEFAULT_ICON);
		guyIcon.iconSize	 = new GSize(40, 50);
		guyIcon.iconAnchor	 = new GPoint(25, 35);
		guyIcon.imageMap	 = [26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,	16,20, 16,14, 19,13, 22,8];
		guyIcon.shadow		 = null;
		return guyIcon;
	},

	setBounds: function() {
		this.bounds		= this.map.getBounds();
		this.southWest	= this.bounds.getSouthWest();
		this.northEast	= this.bounds.getNorthEast();
	},

	onRequest: function() {
		var p = new Element("p", {"id": this.loading, "html": "検索しています"});
		p.inject(this.element);
	},

	onSuccess: function(responseText, responseXML) {
		$(this.loading).dispose();
		var markers = this.markers; 
		for (var i = 0; l = markers.length, i < l; i++) {
			GEvent.clearListeners(markers[i]);
			this.map.removeOverlay(markers[i]);			
			markers[i].remove();
			this.informations[i].dispose();
		}
		
		this.markers = [], this.informations = [], this.informations = [];
		
		var response = eval("(" + responseText + ")");
		var data = response.data;

		if (data) {
			var normals = [], specials = [];
			for (var i = 0; l = data.length, i < l; i++) {
				(data[i].sid) ? specials.push(data[i]) : normals.push(data[i]);
			}
			specials = normals.extend(specials);
			
			for (var i = 0; l = specials.length, i < l; i++) {
				var article = specials[i];
				var point = new GLatLng(article.lat, article.lng);
				var options = {zIndexProcess: function() { return i; }}
				options.icon = (article.sid) ? this.createSpecialIcon() : this.createNormalIcon();

				var marker = new GMarker(point, options);
				
				this.map.addOverlay(marker);
				this.markers.push(marker);
				this.informations.push(this.createInformatonWindow(article,i));

				GEvent.addListener(marker, "click", function(index) {
					this.informations[index].setStyle("display", "");
					var marker = this.markers[index];
					marker.openInfoWindow(this.informations[index]);
				}.bind(this, [i]))
			}
			if (!this.show) {
				var last = this.markers.getLast();
				this.map.setCenter(last.getLatLng());
			}
			this.show = true
		}
	},

	createMap: function(point) {
		var map = new GMap2(this.element);
		map.setCenter(point,13);
		map.addControl(new GSmallMapControl());
		this.map = map;

		GEvent.addListener(this.map, "dragend", function() {
			this.setBounds();
			this.request.send(this.getQueryString());
		}.bind(this));
	},

	createInformatonWindow: function(a, index) {
		var point = new GLatLng(a.lat, a.lng);
		var dl = new Element("div", {"class": "mapInfo"});

		var dd  = new Element("div", {"class":"image"});
		var img = new Element("img", {"src": a.image});
		img.inject(dd);
		dl.adopt(dd);

		var dd = new Element("div", {"class":"cont","html": "<a href='"+ a.url +"'>" + a.name + "</a><br />" + "住所：" + a.address + "<br />" + "賃料：" + a.rent});
		dl.adopt(dd);

		dl.inject(document.body);
		dl.setStyle("display", "none");
		
		return dl;
	}

});



window.addEvent("domready", function() {

	var specialControl	= $("specialControlList"), searchMap = $("searchMap");
	
	var regionSelect = $("regionSelect");
	var companySelect = $("companySelect");
	
	var ca = $("contents_Are");
	var footer = $("footer");
	
	var h = ca.getStyle("height").replace("px","").toInt();
	var t = ca.getStyle("top").replace("px","").toInt();
	
	if(footer){
		footer.setStyles({
			position: 'absolute',
			top: h+t,
			left: 0,
			width: '100%'
		});
	}
	
	if(regionSelect){
		regionSelect.addEvent("change",function(){
			location.href= "/"+this.value+"/";
		});
	}
	
	if(companySelect){
		companySelect.addEvent("change",function(){
			location.href = "/"+this.value+"/";
		});
	}
	
	if (specialControl) {
		this.index = 1;
		this.cache = [];
		this.cache.push(specialControl.get("html"));

		this.hasCache = function(index) {
			return (index <= this.cache.length) ? true : false; 
		}

		this.renderer = function(index) {
			if (!this.hasCache(index)) {
				new Request.HTML({
					"url": "recommend.php",
					"onSuccess": function(tree, elements, html, js) {
						if (html) {
							specialControl.set("html", html);
							this.cache.push(html);
						} else {
							this.index--;
							this.last = true;
						};
					}.bind(this)
				}).send("p=" + (index) + "&" + "l=5");
			} else {
				specialControl.set("html", this.cache[index - 1]);
			}
		}

		$("controlBtnNext").addEvent("click", function() {
			if (!this.last || this.index < this.cache.length) {
				this.index++;
				this.renderer(this.index);
			}
		}.bind(this));

		$("controlBtnBack").addEvent("click", function() {
			if ((this.index - 1) > 0) {
				this.index--;
				this.renderer(this.index);
			}
		}.bind(this));
	}

	if (searchMap) {
		new Search.Map(searchMap, {"lat": 35.68377079037763, "lng": 139.74978851789524});
	}
});
