
jQuery.bt.defaults.fill="#DCE5FE";
jQuery.bt.defaults.strokeStyle="#AAD4FF";
jQuery.bt.defaults.positions=['right'];
jQuery.bt.defaults.strokeWidth="3";
jQuery.bt.defaults.cornerRadius=15;
jQuery.bt.defaults.width="300px";

var minZoom=6;
var maxZoom=11;

var map = null;
var markers = [];
var infoBubble = null;
var markerClusterer = null;

var places = {};
var prices = {};

function fb(v) {
	if (window.console) {
		window.console.log(v);
	}
}

function map_ahouse() {

	var pos=null;
	var center=new google.maps.LatLng(38.1130, 24.0313);
	
	if (gm_pos.Lat) {
		center=pos=new google.maps.LatLng(gm_pos.Lat, gm_pos.Lon);
	}

	map = new google.maps.Map($('#map_ahouse')[0],{
		center: center,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
			position: google.maps.ControlPosition.RIGHT_BOTTOM
		},
		scrollwheel: false,
		streetViewControl: false,
		minZoom: minZoom,
		maxZoom: maxZoom,
		zoom: maxZoom
	});

	if (pos) {
		var marker = new google.maps.Marker({
			position: pos,
			map: map
		});
	}
}

function  map_init() {

	maxZoom = 14;
	
	infoBubble = new InfoBubble({
		minWidth: 130
	});

	map = new google.maps.Map($('#map_canvas')[0],{
		center: new google.maps.LatLng(38.1130, 24.0313),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
			position: google.maps.ControlPosition.RIGHT_BOTTOM
		},
		scrollwheel: false,
		streetViewControl: false,
		minZoom: minZoom,
		maxZoom: maxZoom,
		zoom: minZoom
	});

	$.each(gm_pos, function(i, pos){

		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(pos.Lat, pos.Lon),
			map: map,
			title: pos.Tooltip
		});
		marker.Url=pos.Url;
		marker.Price=pos.Price;
		marker.Acount=pos.Acount;
		marker.HouseID=pos.HouseID;
		marker.Region=pos.Region;
		marker.RegionID=pos.RegionID;
		marker.Location=pos.Location;
		marker.LocationID=pos.LocationID;
		marker.Place=pos.Place;
		marker.PlaceID=pos.PlaceID;
		marker.HouseTypeID=pos.HouseTypeID;
		marker.HouseSubTypeID=pos.HouseSubTypeID;

		markers.push(marker);
	});

	markerClusterer = new MarkerClusterer(map, markers, {
		minimumClusterSize: 1,
		zoomOnClick: false
	});

	google.maps.event.addListener(markerClusterer, 'clusterclick', function(cluster) {

		var i;
		var content = [];
		var zoom = map.getZoom();
		var size = cluster.getSize();
		var center = cluster.getCenter();
		var clickedMarkers = cluster.getMarkers();

		if (size > 12 && zoom < maxZoom) {
			map.fitBounds(cluster.getBounds());
			return;
		}

		if (size <= 12) {

			// display houses

			var w=130;
			var cl=0;
			var l=clickedMarkers.length;

			if (l>2) {
				w=250;
				cl=2;
			}
			if (l>4) {
				w=380;
				cl=3;
			}
			if (l>9) {
				w=500;
				cl=4;
			}

			for (i = 0; i < l; i++) {
				var st=(cl>0 && (i%cl)==0) ? ' style="clear:both"' : '';
				content.push('<li'+st+'>'+clickedMarkers[i].title+'</li>');
			}

			infoBubble.setMinWidth(w);
			infoBubble.setContent('<ul>'+content.join('')+'</ul>');
			infoBubble.setPosition(center);
			infoBubble.open(map);

			return;
		}
		
		places = {};
		prices = {};
		
		for (i = 0; i < clickedMarkers.length; i++) {
			if (typeof places[clickedMarkers[i].PlaceID]=='undefined') {
				places[clickedMarkers[i].PlaceID]={
					name:clickedMarkers[i].Place,
					ids: [],
					cnt:1
				};
			}
			else {
				places[clickedMarkers[i].PlaceID].cnt++;
			}
			places[clickedMarkers[i].PlaceID].ids.push(clickedMarkers[i].HouseID);

			var a=clickedMarkers[i].Acount;
			var pos=price_limit(a,clickedMarkers[i].Price);
			if (typeof prices[a]=='undefined') {
				prices[a]={};
			}
			if (typeof prices[a][pos]=='undefined') {
				prices[a][pos]={
					ids:[],
					place: clickedMarkers[i].Location,
					cnt:1
				};
			}
			else {
				prices[a][pos].cnt++;
			}
			prices[a][pos].ids.push(clickedMarkers[i].HouseID);
		}

		var html='<div class="extra_browse">';
		$.each(prices, function(a, pr){
			content = [];
			var t=(a=='Rent') ? 'ενοικίου' : 'αγοράς';
			var url=uri + 'Χάρτης-Ακινήτων/' + ((a=='Rent') ? 'ΕΝΟΙΚΙΑΖΕΤΑΙ' : 'ΠΩΛΕΙΤΑΙ') + '/Περιοχή/';
			
			content[0]='<li class="title">Τιμές '+t+'</li>';
			$.each(pr, function(i, v) {
				var gmp=gm_prices[a][i];
				var b64=$.base64.encode(v.ids.join(','));
				var url_part=(gmp.min==gmp.max) ? gmp.max : gmp.min+'-'+gmp.max;
				content[i]='<li><a href="'+url+v.place+'/Τιμές/'+url_part+'/'+b64+'">'+gmp.title+' ( '+v.cnt+' )</a></li>';
			});
			html+='<ul>'+content.join('')+'</ul>';
		});

		var l=0;
		content = [];
		content.push('<li class="title">Περιοχές</li>');
		$.each(places, function(i, v){
			var b64=$.base64.encode(v.ids.join(','));
			content.push('<li><a href="'+uri+'Χάρτης-Ακινήτων/ΕΝΟΙΚΙΑΖΕΤΑΙ-ΠΩΛΕΙΤΑΙ/Περιοχή/'+v.name+'/'+b64+'">'+v.name+' ( '+v.cnt+' )</a></li>');
			l++;
		});
		if (l<=10) {
			html+='<ul style="width:290px;float:none;clear:both">'+content.join('')+'</ul>';
		}
		
		infoBubble.setMinWidth(300);
		infoBubble.setContent(html);
		infoBubble.setPosition(center);
		infoBubble.open(map);
	});
}

function price_limit(acount,price) {
	var pos=null;
	price=parseInt(price);
	$.each(gm_prices[acount],function(i,v){
		if (price<=i) {
			pos=i;
			return false;
		}
	});
	return pos;
}

function filterMap() {

	var cAcount='|',cHouseType='|',cLocation='|';
	$('input:checked','#map_options').each(function() {
		var cl=$(this).val().split('-');
		switch (cl[0]) {
			case 'Acount':
				cAcount=cAcount+cl[1]+'|';
				break;
			case 'HouseTypeID':
				cHouseType=cHouseType+cl[1]+'|';
				break;
			case 'LocationID':
				cLocation=cLocation+cl[1]+'|';
				break;
		}
	});

	var cnt=0;
	var new_markers=new Array();

	for (var i=0; i<markers.length; i++) {
		var show=1;
		if (cAcount.indexOf('|'+markers[i].Acount+'|')==-1) show=0;
		if (cHouseType.indexOf('|'+markers[i].HouseTypeID+'|')==-1) show=0;
		if (cLocation.indexOf('|'+markers[i].LocationID+'|')==-1) show=0;
		if (show) {
			new_markers.push(markers[i]);
			cnt++;
		}
	}

	infoBubble.close();
	markerClusterer.clearMarkers();
	markerClusterer.addMarkers(new_markers);
}

function show_extra_browse(a) {
	var html='';
	var url_part='';
	var url=a.attr('href');
	$.ajax({
		url: uri+'?browse='+a.attr('id'),
		dataType: "json",
		async: false,
		success: function(response) {
			html='<div class="extra_browse">';

			var cnt_ul=0;
			$.each(response.prices,function(a,prices){
				html+='<ul><li class="title">Τιμές '+(a=='Rent'?'ενοικίου':'αγοράς')+'</li>';
				$.each(prices,function(i,price){
					url_part=(price.min==price.max) ? price.max : price.min+'-'+price.max;
					html+='<li><a href="'+url+'/Tιμή/'+url_part+'">'+price.title+' ( '+price.count+' )</a></li>';
				});
				html+='</ul>';
				cnt_ul++;
			});

			if (response.heatings) {
				$.each(response.heatings,function(a,heatings){
					html+='<ul'+(cnt_ul==2?' style="clear:both;"':'')+'><li class="title">Θέρμανση</li>';
					$.each(heatings,function(heating,cnt){
						html+='<li><a href="'+url+'/Θέρμανση/'+heating+'">'+heating+' ( '+cnt+' )</a></li>';
					});
					html+='</ul>';
					cnt_ul++;
				});
			}

			if (response.bedrooms) {
				$.each(response.bedrooms,function(a,rooms){
					html+='<ul><li class="title">Δωμάτια</li>';
					$.each(rooms,function(room,cnt){
						var t=room+' '+(room==1 ? 'δωμάτιο' : 'δωμάτια');
						html+='<li><a href="'+url+'/Δωμάτια/'+room+'">'+t+' ( '+cnt+' )</a></li>';
					});
					html+='</ul>';
					cnt_ul++;
				});
			}
			
			/*html+='<ul><li class="title">Εμβδόν</li>';
			$.each(response.floors,function(i,floor){
				url_part=(floor.min==floor.max) ? floor.max : floor.min+'-'+floor.max;
				html+='<li><a href="'+url+'/Eμβαδόν/'+url_part+'">'+floor.title+' ( '+floor.count+' )</a></li>';
			});
			html+='</ul>';*/

			html+='</div>';
		}
	});
	return html;
}
