/***
* @author 			Oskar Hasinski ( hasinski@popsit.com )
* @package			Search
* @created			02.09.2010
* 
* Copyright and all rights reserved to Oskar Hasinski, Miet24 Gmbh
**/

var		$_compare		= null;

$(document).ready(function () {
	checkMapPins();
	$('#search_results .property_outer .property_info a.more').click(function(){
		$(this).parent().parent().parent().parent().children('.more_content').toggle();
	});
});

selectCompare	= function()
{
	$checked	= $('form#search_results_compare input[type="checkbox"]:checked');
	
	if( $checked.length < 2 )
	{
		$('#compare_selected_bottom, #compare_selected_top, form#search_results_compare a.compare').hide();
	}
	else
	if( $checked.length >= 3 )
	{
		$chebox		= $('form#search_results_compare input[type="checkbox"]:not(:checked)');
		$( $chebox ).attr( 'disabled', true );
	}
	else
	{
		$chebox		= $('form#search_results_compare input[type="checkbox"]:not(:checked)');
		$( $chebox ).attr( 'disabled', false );
		if( $checked.length >= 2 ) $('#compare_selected_bottom, #compare_selected_top, form#search_results_compare a.compare').show();
	}
}

checkMapPins	= function()
{
	$count		= 0;
	
	if( window.gm_locs === undefined || !gm_locs || gm_locs.length < 1 )
	{
		return;
	}
	
	for( $i=0; $i<gm_locs.length; $i++ )
	{
		if( $count >= 4 )
		{
			return;
		}
		if(!gm_locs[$i]['y'])
		{
			$count++;
			findLatLng( gm_locs[$i]['name'], $i);
		}
	}
	
	initGoogleMaps();
}

findLatLng	= function( address, $i )
{
	geocoder.getLatLng(	address, 
			function(point) 
			{
				if(point)
				{
					gm_locs[$i]['x']	= point.x;
					gm_locs[$i]['y']	= point.y;
				}
			});
}

initGoogleMaps	= function()
{
	if( gm_locs.length > 1 )
	{
		var $latlng 		= new google.maps.LatLng(51.1830, 10.0415);
		var $zoom		= 3;
	}
	else
	{
		var $latlng 		= new google.maps.LatLng(gm_locs[0]['y'], gm_locs[0]['x']);
		var $zoom		= 12;
	}
	
	var settings 	= {
			zoom: $zoom,
			center: $latlng,
			mapTypeControl: true,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.HYBRID};
		
	map = new google.maps.Map(document.getElementById("google_map"), settings);
	
	var markers 	= [];
	contents 	= [];
	
	gm_marker = new google.maps.Marker({
		position: new google.maps.LatLng(gm_locs[0]['y'], gm_locs[0]['x']),
		map: map,
		title:gm_locs[0]['name'],
		zIndex: 0});
	
	markers.push(gm_marker);
	
	infowindow = new google.maps.InfoWindow(
			{ content: '<div style="float:left; height:60px; padding-left: 5px;"><a href="'+gm_locs[0]['url']+'" target=_parent><img src="'+gm_locs[0]['img_src']+'" style="width:70px;"></a></div><div style="padding-left:80px; padding-right:5px; font-size:small;"><a href="'+gm_locs[0]['url']+'" target=_parent>'+gm_locs[0]['name']+'</a><br>'+gm_locs[0]['price']+'<br>'+gm_locs[0]['m2']+'</div>'
			});
	
	content = '<div style="float:left; height:60px; padding-left: 5px;"><a href="'+gm_locs[0]['url']+'" target=_parent><img src="'+gm_locs[0]['img_src']+'" style="width:70px;"></a></div><div style="padding-left:80px; padding-right:5px; font-size:small;"><a href="'+gm_locs[0]['url']+'" target=_parent>'+gm_locs[0]['name']+'</a><br>'+gm_locs[0]['price']+'<br>'+gm_locs[0]['m2']+'</div>';
	contents.push(content);
	
	  	google.maps.event.addListener(gm_marker, 'click', function() {
	  		callListener(this);
	});	
	
	if( gm_locs.length > 1 )
	{
		for( $i=1; $i<gm_locs.length; $i++ )
		{
			if(gm_locs[$i]['y'])
			{
				//google.maps.InfoWindow
				gm_marker = new google.maps.Marker({
					position: new google.maps.LatLng(gm_locs[$i]['y'], gm_locs[$i]['x']),
					map: map,
					title:gm_locs[$i]['name'],
					zIndex: $i});
				
				markers.push(gm_marker);
				
				infowindow = new google.maps.InfoWindow(
				      { content: '<div style="float:left; height:60px; padding-left: 5px;"><a href="'+gm_locs[$i]['url']+'" target=_parent><img src="'+gm_locs[$i]['img_src']+'" style="width:70px;"></a></div><div style="padding-left:80px; padding-right:5px; font-size:small;"><a href="'+gm_locs[$i]['url']+'" target=_parent>'+gm_locs[$i]['name']+'</a><br>'+gm_locs[$i]['price']+'<br>'+gm_locs[$i]['m2']+'</div>'
				      });
				
				content	= '<div style="float:left; height:60px; padding-left: 5px;"><a href="'+gm_locs[$i]['url']+'" target=_parent><img src="'+gm_locs[$i]['img_src']+'" style="width:70px;"></a></div><div style="padding-left:80px; padding-right:5px; font-size:small;"><a href="'+gm_locs[$i]['url']+'" target=_parent>'+gm_locs[$i]['name']+'</a><br>'+gm_locs[$i]['price']+'<br>'+gm_locs[$i]['m2']+'</div>';
				contents.push(content);
				
				  google.maps.event.addListener(gm_marker, 'click', function() {
					  callListener(this);
				  });
			}
		}
	}
	
	var markerCluster = new MarkerClusterer(map, markers);
}

callListener = function(marker)
{
	infowindow.setContent(contents[marker.zIndex]);
	infowindow.open(map,marker);
}
