﻿$(document).ready(
	function()
	{
		// hide projects list by js control
		var control = $('<a href="#" class="project-js js-control" id="projectsHeader">Список проектов</a>');
		control.click(
			function()
			{
				$('#project-list ul').toggle();
				$(this).blur();
				
				return false;
			}
		);
		
		$('#project-list').prepend(control);
		
		$('#project-list ul').hide();
	}
);

function filter() {
	updateHeader(
		projects_collection.show(filter_date_type, filter_type, filter_group)
	);
}

function updateHeader( count )
{
	var headerText = 'Список';

	switch ( filter_date_type )
	{
		case PROJECT_DATE_TYPE_COMPLETED:
			headerText += ' выполненных';
		break;
		case PROJECT_DATE_TYPE_CURRENT:
			headerText += ' текущих';
		break;
	}

	switch ( filter_type )
	{
		case PROJECT_TYPE_PIRSMR:
			headerText += ' проектно-изыскательских и строительных';
		break;
		case PROJECT_TYPE_SMR:
			headerText += ' строительных';
		break;
		case PROJECT_TYPE_PIR:
			headerText += ' проектно-изыскательских';
		break;
	}

	headerText += ' проектов';

	switch ( filter_group )
	{
		case 1:
			headerText += ', Москва';
		break;
		case 2:
			headerText += ', Зеленоград';
		break;
		case 3:
			headerText += ', Тульская область';
		break;
	}
	
	headerText += ' (' + count + ')';
	
	$('#projectsHeader').text(headerText);
}

/* константы */
PROJECT_DATE_TYPE_ALL = 0;
PROJECT_DATE_TYPE_COMPLETED = 1;
PROJECT_DATE_TYPE_CURRENT = 2;

PROJECT_TYPE_ANY = 'any';
PROJECT_TYPE_PIR_OR_SMR = null;
PROJECT_TYPE_SMR = 'smr';
PROJECT_TYPE_PIR = 'pir';
PROJECT_TYPE_PIRSMR = 'pir+smr';

/* иконки */
var pirsmrIcon = null;
var pirIcon = null;
var smrIcon = null;

function initialize_icons()
{
	pirsmrIcon = new GIcon();
	pirsmrIcon.image = "http://www.mon-company.ru/i/f/f_red.png";
	pirsmrIcon.shadow = "http://www.mon-company.ru/i/f/f_shadow.png";

	pirsmrIcon.iconSize = new GSize(28, 34);
	pirsmrIcon.shadowSize = new GSize(45, 34);

	pirsmrIcon.iconAnchor = new GPoint(0, 34);
	pirsmrIcon.infoWindowAnchor = new GPoint(9, 2);


	pirIcon = new GIcon();
	pirIcon.image = "http://www.mon-company.ru/i/f/f_white.png";
	pirIcon.shadow = "http://www.mon-company.ru/i/f/f_shadow.png";

	pirIcon.iconSize = new GSize(28, 34);
	pirIcon.shadowSize = new GSize(45, 34);

	pirIcon.iconAnchor = new GPoint(0, 34);
	pirIcon.infoWindowAnchor = new GPoint(9, 2);
	
		
	smrIcon = new GIcon();
	smrIcon.image = "http://www.mon-company.ru/i/f/f_green.png";
	smrIcon.shadow = "http://www.mon-company.ru/i/f/f_shadow.png";

	smrIcon.iconSize = new GSize(28, 34);
	smrIcon.shadowSize = new GSize(45, 34);

	smrIcon.iconAnchor = new GPoint(0, 34);
	smrIcon.infoWindowAnchor = new GPoint(9, 2);
}

/* коллекция проектов */
var projects_collection = null;

/* project class */
function Project(latitude, longtitude, caption, element, type, date_type, project_group_id) {
	this.gmarker = null;

	this.latitude = latitude;
	this.longtitude = longtitude;
	
	this.caption = caption;

	this.type = type;
	this.date_type = date_type;
	this.project_group_id = project_group_id;
	
	this.element = element;
	
	return true;
}


Project.prototype.get_icon_by_type = function(type)
{
	var CurrentIcon;

	switch (type) {
	   case 'pir':
		CurrentIcon = new GIcon(pirIcon);
	   break;
	   case 'smr':
	   	CurrentIcon = new GIcon(smrIcon);
	   break;
	   case 'pir+smr':
	   	CurrentIcon = new GIcon(pirsmrIcon);
	   break;
	}
	
	return CurrentIcon;
}

Project.prototype.get_gmarker = function()
{
	if (this.gmarker == null)
	{
		var marker = new GMarker(
			new GLatLng(
				this.latitude,
				this.longtitude
			),
			{
				icon: this.get_icon_by_type(this.type)
			}
		);
		var caption = this.caption;
		GEvent.addListener(
			marker,
			"click",
			function()
			{ 
				marker.openInfoWindowHtml(caption); 
			}
		);
		
		this.gmarker = marker;
	}
	
	return this.gmarker;
}

Project.prototype.show = function()
{
	this.element.show();
	this.get_gmarker().show();
}

Project.prototype.hide = function()
{
	this.element.hide();
	this.get_gmarker().hide();
}

/* collection of projects */
function Projects(gmap)
{
	this.gmap = gmap;

	this.projects = new Array();
	this.showed_projects = null;
	
	this.date_type = null;
	this.type = null;
	this.project_group_id = null;

	this.clear_showed_projects = function()
	{
		this.showed_projects = new Array();
	}

	this.getGMapZoomLevel = function(bounds)
	{
		var result = this.gmap.getBoundsZoomLevel(bounds);
		if (result > 13)
		{
			result = 13;
		}	
		return result;
	}

	this.draw = function(bounds)
	{
		if ( bounds instanceof GLatLngBounds )
		{
			this.gmap.setCenter(bounds.getCenter());
			this.gmap.setZoom(
				this.getGMapZoomLevel(bounds)
			);
		}
		
		for(var currentIndex = 0, length = this.showed_projects.length; currentIndex < length; currentIndex++)
		{
			this.gmap.addOverlay(
				this.showed_projects[currentIndex].get_gmarker()
			);
		}
	}
		
	this.project_allowed_to_show = function(project_item)
	{
		return (
			this.project_allowed_to_show_by_group(project_item) &&
			this.project_allowed_to_show_by_date(project_item) &&
			this.project_allowed_to_show_by_type(project_item)
		);
	}

	this.project_allowed_to_show_by_group = function(project_item)
	{
		return (this.project_group_id == project_item.project_group_id);
	}
	
	this.project_allowed_to_show_by_date = function(project_item)
	{
		return (this.date_type == PROJECT_DATE_TYPE_ALL || project_item.date_type == this.date_type);
	}

	this.project_allowed_to_show_by_type = function(project_item)
	{
		return (
			(this.type == PROJECT_TYPE_ANY) ||
			(this.type == PROJECT_TYPE_PIR_OR_SMR ? project_item.type != PROJECT_TYPE_PIRSMR : project_item.type == this.type)
		);		
	}
	
	return true;
}

Projects.prototype.show = function(date_type, type, project_group_id)
{
	this.date_type = date_type;
	this.type = type;	
	
	var need_to_change_viewport = (this.project_group_id != project_group_id);
	this.project_group_id = project_group_id;
	
	var sw_lat = 0, sw_lng = 0, ne_lat = 0, ne_lng = 0;
	var start_values_assigned = false;
	
	var current_project;
	var current_lat, current_lng;
	
	this.clear_showed_projects();
		
	for(var currentIndex = 0, length = this.projects.length; currentIndex < length; currentIndex++ )
	{
		current_project = this.projects[currentIndex];
		
		current_project.get_gmarker().closeInfoWindow();

		if( this.project_allowed_to_show(current_project) )
		{
			if ( need_to_change_viewport )
			{
				current_lat = current_project.latitude;
				current_lng = current_project.longtitude;
				
				if (!start_values_assigned)
				{
					sw_lat = current_lat;
					sw_lng = current_lng;
					
					ne_lat = current_lat;
					ne_lng = current_lng;
					
					start_values_assigned = true;
				}
				else
				{
					if (current_lat < sw_lat)
						sw_lat = current_lat;
					if (current_lng < sw_lng)
						sw_lng = current_lng;
					
					if (current_lat > ne_lat)
						ne_lat = current_lat;
					if (current_lng > ne_lng)
						ne_lng = current_lng;				
				}
			}
			
			current_project.show();
			this.showed_projects.push(current_project);
			
			//this.gmap.addOverlay(current_project.gmarker);
		}
		else
		{
			current_project.hide();
		}

	}
	
	if ( need_to_change_viewport )
	{
		this.draw(
			new GLatLngBounds(
				new GLatLng(
					sw_lat,
					sw_lng
				),
				new GLatLng(
					ne_lat,
					ne_lng
				)
			)		
		);
	}
	else
	{
		this.draw(false);
	}
	
	return this.showed_projects.length;
}

Projects.prototype.add = function(project_item)
{
	if (project_item instanceof Project)
	{
		this.projects.push(project_item);
	}
};

/* применяем стили к карте только если включён яваскрипт */
function apply_google_map_styles()
{
	$('div#projects-map').css('width', '100%');
	$('div#projects-map').css('height', '400px');
}

/* отображаем форму фильтра только при включённом яваскрипте */
function display_filter_form()
{
	$('form#ppf').show();				
}

/* изменяем размеры карты при изменении размеров окна браузера */
function add_autoresize_to_google_map()
{
	resize_target = $('div#projects-map');
	x = resize_target.x();
	y = resize_target.y() + 10;

	$(window).resize(projects_resize);

	projects_resize();
}

function projects_resize() {
	var clientHeight = getClientHeight();
	var clientWidth = getClientWidth();

	var height = clientHeight - y;
	var width = clientWidth - x;
	
	if (height < width)
		changeSize(height);
	else
		changeSize(width);

	return true;
}

function changeSize( size ) {
	$(resize_target).height( size < 300 ? 300 : size );
}

function getClientHeight() {
	var clientHeight = document.body.clientHeight;

	if (document.documentElement.clientHeight && (clientHeight > document.documentElement.clientHeight))
		clientHeight = document.documentElement.clientHeight;
	if (clientHeight > $(window).height())
		clientHeight = $(window).height();
	if (clientHeight > $(document).height())
		clientHeight = $(document).height();
	
	return clientHeight;
}

function getClientWidth() {
	return document.body.clientWidth;
}
