$(document).ready(function(){
		
	var formData = null;
	
	//Init active location
	active_map_location();
	
	//Init datepicker
	ui_datepicker();
	
	// Init accordion
	accordion();

	// Show checkboxes for Stockholm as default
	show_checkboxes(1);
	
	// Init search form
	search();
	
	//
	get_location(1);

	// Load result
	load_result();
	
	get_spaces_count(1);
	
    $("#AjaxLoading").ajaxSend(function(){
	 	   $(this).show();
	 	 });

	$("#AjaxLoading").ajaxStop(function(){
	  	  	$(this).hide();
	  	 }); 
	
});

/**
 * Datepicker
 * 
 */
function ui_datepicker()
{
	$("#contracts_finalized").datepicker($.extend({}, 
			$.datepicker.regional["sv"], { 
				dateFormat: $.datepicker.ISO_8601
			})).attr("readonly", "readonly");
}

/**
 * Active map location
 * 
 */
function active_map_location()
{
	
	// Set first tab active as default
	$('ul#MapLocation li a:first').addClass('active');

	// This code executes when a tab is clicked
	$('ul#MapLocation li a').click(function() {
		
		// Clear map
		map.clear();
		
		//Clear RandomizedResultList div
		$('#RandomizedResultList').empty();
		
		// Remove active class from all tabs.
		$('ul#MapLocation li a').removeClass('active');
		
		// Add active class to current tab
		$(this).addClass('active');	 
		
		// Split id values into an array.
	    var id_value  = this.id.split(';');
	    
	    // Get location code
		var location_code = id_value[0];
		
		// Create a array with x and y coordinates.
		var coorinates = [id_value[1],id_value[2]];
		
		$('#OfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		$('#NoOfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');		
		
		
		/**
		 * 
		 * Call function
		 * 
		 */

		// Show checkboxes related to location, (1: Stockholm, 14: Göteborg and 12: Skåne
		show_checkboxes(location_code);
		
		// Execute function map_position
		map_position(coorinates);
		
		//get result for current location
		get_location(location_code);
		
		// Reset search form
		reset_search_form();
		
		// Testing new function
		get_spaces_count(location_code);
		
	});
}

/**
 * Show metazones
 * 
 */
function show_metazones()
{
	var chb = $('#SearchForm :checkbox');
	
	if(chb.fieldValue().length > 0)
	{		
		// Draw zones on the map
		get_mzone(chb.fieldValue());
	}
}
/**
 * Update CheckboxList div, in app/views/elements/search.form.ctp
 * 
 */
function show_checkboxes(location_code)
{
	$('#MzoneCheckboxList').load('pages/get_mzone_descr/' + location_code);
}
/**
 * Search form
 * 
 */
	function search()
	{
		var options = { 
			    target: '#RandomizedResultList',
			    //type: 'POST',
			    dataType: null,
			    beforeSubmit: function() {
					clear_map();
					$(window).scrollTo(0,600,{onAfter:function(){
						$('#Accordion').accordion('activate', -1);
					}});
				},
			    success: function() {
					get_search_criteria();
					get_search_form_count();					
				}
			};	
		
		$('#SearchForm').ajaxForm(options);
	}
	
	
/**
 * Get seach criteria
 * 
 */
	function get_search_criteria(town)
	{
		var querystring = $('#SearchForm').formSerialize();
	
		$.post("selections/get_search_criteria", querystring,
				function(data) {
			
					if(data.FloorArea != 'Alla')
					{
						data.FloorArea = data.FloorArea + ' m<sup>2</sup>';
					}
					
					$('#SearchCriteria #FloorArea').html(data.FloorArea);
					$('#SearchCriteria #SelectedMetaZones').text(data.SelectedMetaZones);
					$('#SearchCriteria #DateTo').text(data.DateTo);
			
					get_mzone(data.SelectedMetaZoneCodes);
			
				}, "json");
	}
	
/**
 * Get town
 * 
 */
	function get_location(town_code)
	{				
		$.post("selections/current_location", {town: town_code},
				  function(data) {						
					
					var codes = [];
					var descr = [];
					
					$.each(data,function(i, item){
						codes[i] = item.Metazone.code;
						descr[i] = item.Metazone.descr;
					});					

					// Draw zones on map
					get_mzone(codes);
					
				  }, "json");
		var myDate = new Date();
		
		var d = myDate.getDate();
		var m = myDate.getMonth() + 1;
		var y = myDate.getFullYear() + 10;
		
		var date = y + '-' + m + '-' + d;
		
		$('#SearchCriteria #FloorArea').text('Alla');
		$('#SearchCriteria #SelectedMetaZones').text('Alla');
		$('#SearchCriteria #DateTo').text(date);
		
		load_result();
	}

/**
 * Load result
 * 
 */
	function load_result()
	{
		var querystring = $('#SearchForm').formSerialize();
		$('#RandomizedResultList').load('selections/get_result',querystring);
	}
/**
 * Get spaces count
 * 
 */
	function get_spaces_count(location)
	{
		$('#OfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		$('#NoOfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');		

		$.post('selections/get_spaces_count_location',{location_code: location},
				function(data){
					$('#OfficeCount').text(data.OfficeCount);
					$('#NoOfficeCount').text(data.NoOfficeCount);
				},
				"json"
		);
	}
/**
 * Get search form count
 * 
 */
	function get_search_form_count()
	{
		$('#OfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		$('#NoOfficeCount').html('<span class="ajax-loader-small">&nbsp;&nbsp;&nbsp;&nbsp;</span>');		

		var querystring = $('#SearchForm').formSerialize();
		
		//var querystring;
		var location = $('#Town').val();
		
		$.post('selections/get_spaces_count', querystring, function(data){
			
			$('#OfficeCount').text(data.OfficeCount);
			$('#NoOfficeCount').text(data.NoOfficeCount);
			
		},"json");
	}
/**
 * Reset search form
 * 
 * 
 */
	function reset_search_form()
	{
		$('#SearchForm').resetForm();
	}