var map = null;
var center;
var loaded_markers = new Array();

window.onunload = function () {
	if ( map ) {
		GUnload ();
	}
}

function LoadNewMarkers ( id )
{
	var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    
    var data = {
    	xl: southWest.lat (),
    	yl: southWest.lng (),
    	xr: northEast.lat (),
    	yr: northEast.lng (),
    	id: id
    };

    client_request ( 'hotels', 'get_points', '', 'GET', data, 'AfterMoveMap' );
}

function CreateMarker ( point, cloud, markerOptions )
{
	var marker = new GMarker ( point, markerOptions );

	var dist = center.distanceFrom ( point );
	
	GEvent.addListener ( marker, 'click', function () {
		marker.openInfoWindowHtml ( cloud );
	} );
	
	return marker;
}


function AfterMoveMap ( data )
{
	var baseIcon = new GIcon ();
	baseIcon.image = "http://viahotel.ru/images/blue.png";
	baseIcon.iconSize = new GSize ( 37, 32 );
	baseIcon.iconAnchor = new GPoint ( 10, 32 );
	baseIcon.infoWindowAnchor = new GPoint ( 9, 2 );
	baseIcon.infoShadowAnchor = new GPoint ( 18, 25 );

	markerOptions = { 
		icon: baseIcon
	};
	
	for ( var i = 0; i < data.points.length; i++ )
	{
		if ( loaded_markers[data.points[i].id] != 1 )
		{
			var point = new GLatLng ( data.points[i].lat, data.points[i].lng );
			var marker = CreateMarker ( point, data.points[i].cloud, markerOptions );
			
			map.addOverlay ( marker );
			
			loaded_markers[data.points[i].id] = 1;
		}
	}
}

function ShowHotelMap ( lat, lng, id, cloud )
{
	
	if ( document.getElementById( 'hotel_map' ).style.display == 'block' )
	{
		document.getElementById( 'hotel_map' ).style.display = 'none';
	} else {
		document.getElementById( 'hotel_map' ).style.display = 'block';
	}
	
	if ( !map )
	{
		setTimeout ( function () { GetHotelMap ( lat, lng, id, cloud ); }, 100 );
	}
}

function GetHotelMap ( lat, lng, id, cloud )
{
	if ( GBrowserIsCompatible () )
	{
		function MyZoomControl () {}
		
		MyZoomControl.prototype = new GControl();

		MyZoomControl.prototype.initialize = function ( map )
		{
			var container = document.createElement ( 'div' );
		
			var zoomInImg = document.createElement ( 'img' );
			this.setButtonStyle_ ( zoomInImg, 'plus' );
			container.appendChild ( zoomInImg );
			GEvent.addDomListener ( zoomInImg, 'click', function()
			{
				map.zoomIn ();
			} );
		
			var zoomOutImg = document.createElement ( 'img' );
			this.setButtonStyle_ ( zoomOutImg, 'minus' );
			container.appendChild ( zoomOutImg );
			GEvent.addDomListener ( zoomOutImg, 'click', function ()
			{
				map.zoomOut ();
			} );
		
			map.getContainer().appendChild ( container );
			return container;
		}
		
		MyZoomControl.prototype.getDefaultPosition = function ()
		{
			return new GControlPosition ( G_ANCHOR_TOP_LEFT, new GSize ( 7, 7 ) );
		}
		
		MyZoomControl.prototype.setButtonStyle_ = function ( button, type )
		{
			button.setAttribute ( 'src', '/images/' + type + '.png' );
			button.setAttribute ( 'width', '40' );
			button.setAttribute ( 'height', '40' );
			
			button.style.marginBottom    = '3px';
			button.style.cursor 		 = 'pointer';
			button.style.display 		 = 'block';
		}
		
		map = new GMap2 ( document.getElementById( 'google_map' ) );
		
		map.addControl ( new MyZoomControl () );
				
		var mapTypeControl = new GMapTypeControl ();
		var topRight = new GControlPosition ( G_ANCHOR_TOP_RIGHT );
		map.addControl ( mapTypeControl, topRight );
		
		center = new GLatLng ( lat, lng );
		
		GEvent.addListener ( map, 'moveend', function () {
			LoadNewMarkers ( id );
		} );
		
		GEvent.addListener ( map, 'zoomend', function () {
			LoadNewMarkers ( id );
		} );
		
		map.setCenter ( center, 15 );
		
		var marker = new GMarker ( center );

		map.addOverlay ( marker );
		
		marker.openInfoWindowHtml ( cloud );
		
		GEvent.addListener ( marker, 'click', function () {
			marker.openInfoWindowHtml ( cloud );
		} );
	}
}

function ChangePlaceType ()
{
	var pt = document.getElementById( 'place_type_field' ).value;
	if ( pt == 2 )
	{
		document.getElementById( 'place_type_field' ).value = 1;
	} else {
		document.getElementById( 'place_type_field' ).value = 2;
	}
	
	document.getElementById( 'show_prices_button' ).click();
}

function OrderFromMap ()
{
	if ( document.getElementById( 'order_mbutton' ) )
	{
		document.getElementById( 'hotel_map' ).style.display = 'none';
		document.getElementById( 'order_mbutton' ).click();
	}
}

function ShowPubPhoto ( id )
{
	var data = {
		id: id
	}
	
	client_request ( 'hotels', 'show_photo', '', 'GET', data, 'AfterShowPhoto' );
}

function AfterShowPhoto ( data )
{
	document.getElementById( 'photos_cont' ).innerHTML = data.content;
}

function ShowAllPhotos ( id )
{
	var data = {
		id: id
	}
	
	client_request ( 'hotels', 'show_all_photos', '', 'GET', data, 'AfterShowAllPhotos' );
}

function AfterShowAllPhotos ( data )
{
	document.getElementById( 'photos_cont' ).innerHTML = data.content;
}

function ShowPrices ( id )
{
	var data = {
		id: id
	}
	
	document.getElementById( 'prices_rooms_select' ).innerHTML = '<div style="margin: 15px 0; font-weight: bold;">Æäèòå, èäåò çàãðóçêà öåí...</div>';
	
	client_request ( 'hotels', 'show_prices', '', 'POST', data, 'AfterShowPrices', false, 'prices_form' );
}

function AfterShowPrices ( data )
{
	document.getElementById( 'prices_rooms_select' ).innerHTML = data.content;
}

function GetOrder ()
{
	if ( document.getElementById( 'hotel_map' ).style.display == 'block' )
	{
		document.getElementById( 'hotel_map' ).style.display = 'none';
	}
	
	client_request ( 'hotels', 'get_order', '', 'POST', '', 'AfterGetOrder', false, 'order_form' );
}

function AfterGetOrder ( data )
{
	if ( data.error )
	{
		document.getElementById( 'order_form_error' ).innerHTML = data.error;
	} else {
		document.getElementById( 'prices_date_select' ).style.display = 'none';
		document.getElementById( 'prices_rooms_select' ).style.display = 'none';
		document.getElementById( 'prices_container' ).innerHTML = data.content;
	}
}

function GetMoreOrder ()
{
	client_request ( 'hotels', 'get_more_order', '', 'POST', '', 'AfterGetMoreOrder', false, 'order_blank' );
}

function AfterGetMoreOrder ( data )
{
	if ( data.errors )
	{
		document.getElementById( 'order_form_errors' ).innerHTML = '<div class="errors">' + data.errors + '</div>';
	} else {
		document.getElementById( 'prices_container' ).innerHTML = data.content;
	}
}

function FinishOrder ()
{
	client_request ( 'hotels', 'finish_order', '', 'POST', '', 'AfterFinishOrder', false, 'order_pdata' );
}

function AfterFinishOrder ( data )
{
	if ( data.errors )
	{
		document.getElementById( 'order_form_errors' ).innerHTML = '<div class="errors">' + data.errors + '</div>';
	} else {
		document.getElementById( 'prices_container' ).innerHTML = data.content;
		pageTracker._trackPageview('/order_done');
	}
}

function CloseOrder ()
{
	document.getElementById( 'prices_container' ).innerHTML = '';
	document.getElementById( 'prices_date_select' ).style.display = 'block';
	document.getElementById( 'prices_rooms_select' ).innerHTML = '';
	document.getElementById( 'prices_rooms_select' ).style.display = 'block';
	document.getElementById( 'show_prices_button' ).click();
}

function BackRoomsSelect ()
{
	document.getElementById( 'prices_date_select' ).style.display = 'block';
	document.getElementById( 'prices_rooms_select' ).style.display = 'block';
	document.getElementById( 'prices_container' ).innerHTML = '';
	document.getElementById( 'order_form_error' ).innerHTML = '';
}

function GetNumStrEnd ( cnt, e1, e2, e3 )
{
	var end_str = '';
	var converted = false;
	var cnts = eval ( "'" + cnt + "'" );
	
	if ( cnt > 10 )
	{
		var end = cnts[cnts.length - 2] + cnts[cnts.length - 1];
		
		if ( end == '11' || end == '12' || end == '13' || end == '14' )
		{
			end_str = e3;
			converted = true;
		}
	}
	
	if ( !converted )
	{
		var end = cnts[cnts.length - 1];
		
		if ( end == '1' )
		{
			end_str = e1;
		} else if ( end == '2' || end == '3' || end == '4' ) {
			end_str = e2;
		} else {
			end_str = e3;
		}
	}
	
	return end_str;
}
