$(function(){
	   
	$('body').css('overflow-y','hidden');
	$('#EastMedia_leftScroll').css({float:'left',overflowY:'auto',width:520});
	$('#EastMedia_rightMap').css({float:'right'});

	resizeWindow();

	$(window).resize(function(){
		resizeWindow();
	});
	   
	function resizeWindow() {
		var max_width = 960,
			max_height = 725,
			$window = $(window),
			window_height = $window.height(),
			window_width;
		
		
		if (window_height < max_height) {
			window_height = max_height;
			$('body').css('overflow-y','auto');
		} else {
			$('body').css('overflow-y','hidden');
		}
	
		window_width = $window.width();
	
		if (window_width < max_width) {
			window_width = max_width;
		}
		
		$('#EastMedia').width(window_width - 150).height(window_height - 240);
		$('#EastMedia_leftScroll,#EastMedia_rightMap').height(window_height - 240);
		$('#EastMedia_rightMap').width(window_width - 673);
	}
});


$(window).load(function(){

	var $region = $('#form_search_region'),
		map,
		markerCounter = 1;

	if ($region.val()) {
		
		var bounds = new GLatLngBounds(),
			$rightMap = $('#EastMedia_rightMap'),
			point;

		map = new GMap2($rightMap.get(0));
		map.setUIToDefault();
		map.removeMapType(G_HYBRID_MAP);
		map.removeMapType(G_SATELLITE_MAP);
		map.addControl(new GOverviewMapControl());
		map.addControl(new ResetMap());

		$.getJSON("/json/hotels.php",{
			country : country_copy,
			city : city_copy
		},function(data) {
			if (data.error) {
				if (data.error === 'empty') {
					var geocoder = new GClientGeocoder();
					geocoder.getLocations(city_copy+', '+country_copy,function(data){
						if (data.Status.code == G_GEO_SUCCESS) {
							var box = data.Placemark[0].ExtendedData.LatLonBox;
							bounds.extend(new GLatLng(box.north,box.east));
							bounds.extend(new GLatLng(box.north,box.west));
							bounds.extend(new GLatLng(box.south,box.west));
							bounds.extend(new GLatLng(box.south,box.east));
							setCenterZoomBounds(bounds);
						} else {
							geocoder.getLocations(country_copy,function(data){
								var box = data.Placemark[0].ExtendedData.LatLonBox;
								bounds.extend(new GLatLng(box.north,box.east));
								bounds.extend(new GLatLng(box.north,box.west));
								bounds.extend(new GLatLng(box.south,box.west));
								bounds.extend(new GLatLng(box.south,box.east));
								setCenterZoomBounds(bounds);
							});
							
						}
					});
				}
			} else {
				$.each(data.i,function(i,item) {
					point = new GLatLng(item.x,item.y);
					bounds.extend(point);
					map.addOverlay(createMarker(point,item.i));
					addZoom(point,item.i);					
				});
				setCenterZoomBounds(bounds);				
			}
		});	
	}
			
	function setCenterZoomBounds(bounds) {
		map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
	}
	
	function addZoom(point,id) {
		$('<a/>').text('Zoom map').attr('href','#').addClass('EastMedia_hotel_zoom').click(function(){
			map.setCenter(point,16);
			return false;
		}).appendTo($('#EastMedia_hotel_'+id).find('.EastMedia_hotel_links'));	
	}
	
	function createMarker(point,id) {
	
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = '/images/markers/number-marker.php?number='+markerCounter;
	
		var marker = new GMarker(point,{
			icon : icon
		});
	
		GEvent.addListener(marker, "mouseover", function() {
			window.top.location = '#hotel_'+id;
			
/*
			
			var $hotel = $('#EastMedia_hotel_'+id).animate({backgroundColor:'#ffff90'}),		
				h2 = $hotel.find('h2').text(),
				img = $hotel.find('img'),
				html = '<h2>'+h2+'</h2>';
				
			if (img) {
				html += '<br /><img src="'+img.attr('src')+'" alt="'+h2+'" />';	
			}

			$('div.EastMedia_hotel').animate({backgroundColor:'#FFF'});


			var $hotel = $('#EastMedia_hotel_'+id).animate({backgroundColor:'#ffff64'}),
				$hotel_clone = $hotel.clone().css('background','none');
*/

			var $hotel = $('#EastMedia_hotel_'+id),
				$hotel_clone = $hotel.clone();

			$hotel_clone.find('p:first span:not(.subImage)').remove();
			$hotel_clone.find('.EastMedia_hotel_links').prepend($('<div/>').addClass('EastMedia_clear'));

/*
			$hotel_clone.find('.subImage').each(function(){
				var id = this.className.split(' ');
				id = id[1];
				id = id.split('_')
				id = id[1];
				
				$('<img/>').attr({
					src : '/images/hotels/thumbs/'+id+'.jpg'
				}).addClass('image').css('margin-right','10px').appendTo($hotel_clone.find('p:first'));
			});
*/		
			$hotel_clone.find('.EastMedia_address').css('display','inline');
			
			$hotel_clone.find('.EastMedia_hotel_zoom').click(function(){
				map.setCenter(point,16);
				return false;
			});
			
			
			marker.openInfoWindow($hotel_clone.get(0),{maxWidth:300});
		
		});
	/*
		GEvent.addListener(marker, "dblclick", function() {
			window.top.location = '/hotel.php?id='+id;
		});
	*/
	
		markerCounter++;
	
		return marker;
	}





	$(window).unload(GUnload);
});

function ResetMap() {}
ResetMap.prototype = new GControl();

ResetMap.prototype.initialize = function(map) {
	
	var element = document.createElement("input");
	element.setAttribute('type','button');
	element.value = 'Reset Map';
	element.style.width = '100px';

	$(element).click(function() {
		map.returnToSavedPosition();
		return false;
	}); 
	
	map.getContainer().appendChild(element);
	return element;
};

ResetMap.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(80,7));
};

