/* JavaScript file */

//========================================================
// Функции от Дрынкина А.В.

$(document).ready(function() {

			$("a#schema_proezd").fancybox({
				'width'				: '75%',
				'height'			: '75%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});
			
			$("a.fancybox").fancybox({'showNavArrows' : true});
			
  });


function open_catalog_image(img_url)
{
	var width = '800';
	var height = '700';
	window.open( '/pic.php?w=' + width + '&h=' + height + '&pic_fname=' + img_url, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes ,resizable=no");
}



function open_js_sform_add_params()
{
	var type_id = $(this).val();
	var current_rows = add_params_arr[type_id];
	$(all_params).css({display: 'none'});
	
	if ($.browser.msie)
	{
		$(current_rows).css({display: 'block'});
	}
	else
	{
		$(current_rows).css({display: 'table-row'});
	}
	
	
}

var not_change = false;
function js_sform_add_params_setup()
{
	//$("form#arenda_form #view_filter\\[type_id\\]").change(open_js_sform_add_params).change();
	//$("form#arenda_form #view_filter\\[district_id\\]").change(open_js_sform_add_params2).change();
	//$("form#arenda_form #view_filter\\[direction_id\\]").change(open_js_sform_add_params3).change();
	
	$("form#arenda_form input[name='vnr']").click(change_form);
	$("form#arenda_form select[name='view_filter[direction_id]']").change(change_form);
	$("form#arenda_form select[name='view_filter[district_id]']").change(change_form);
	$("form#arenda_form select[name='view_filter[estate_id]']").change(change_form);
	

	not_change = true;
	$("form#arenda_form input[name='vnr']").filter(':checked').click();
	
	//$("form#arenda_form select[name='view_filter[direction_id]']").change();
	
	//$("form#arenda_form #view_filter\\[district_id\\]").change(open_js_sform_add_params2).change();
	//$("form#arenda_form #view_filter\\[direction_id\\]").change(change_form).change();
}

function change_form()
{
	var name = $(this).attr('name');
	var val = $(this).val();
	//alert(val);
	switch (name)
	{
		case 'vnr':
			if ( val == 1 ) // Зарубежье
			{
				var current_rows = '#country';
				show_table_row($(current_rows));
				
				
				switch_select('#types_rus', '#types_vnr');
				
				hide_regions();
				//switch_select('#region_rus', '#region_vnr');
				
				
				//$('#region').find('select').val('');
				//$('#region').css({display: 'none'});
				
				
				//$('#metro').find('select').attr('selectedIndex', 0);
				empty_val('#metro');
				$('#metro').css({display: 'none'});
				
				empty_val('#estate');
				$('#estate').css({display: 'none'});
				
				empty_val('#estate_mo');
				$('#estate_mo').css({display: 'none'});
				
				//$('#mo_direction').find('select').attr('selectedIndex', 0);
				empty_val('#mo_direction');
				$('#mo_direction').css({display: 'none'});
				
				
				if ($('#currency_changed').val() == '0')
				{
					$('#curens').find('select').val('Евро');
					$('#curens').find('select option').eq(0).attr('selected', '1'); //Евро
					//$('#curens').find('select').val(0);
					$('#curens').find('select').eq(0).selectedIndex = 0;
					$('#curens').find('select').attr('selectedIndex', 0);
				}
				
			}
			else
			{
				//alert(val);
				//var current_rows = '#region';
				//show_table_row($(current_rows));
				
				switch_regions(3); // Россия
				
				
				//$('#country').find('select').attr('selectedIndex', 0);
				empty_val('#country');
				$('#country').css({display: 'none'});
				
				switch_select('#types_vnr', '#types_rus');
				if ($("form#arenda_form input[name='vnr']").filter(':checked').val() == 0) // Россия
				{
				
					if ( $("form#arenda_form select[name='view_filter[direction_id]'] option:selected").val() == 1 ) // Москва
					{
						if ( $("form#arenda_form select[name='view_filter[estate_id]'] option:selected").val() != 0 ) 
						{
							empty_val('#types_rus');
							$('#types_rus').css({display: 'none'});
						}
						else
						{
							var current_rows = '#types_rus';
							show_table_row($(current_rows));
						}
					}
				}				
				
				
				//switch_select('#region_vnr', '#region_rus');
				
				//$('#mo_direction').find('select').val('');
				//$('#mo_direction').css({display: 'none'});
				//alert(val);
					
				if ($('#currency_changed').val() == '0')
				{
					$('#curens').find('select').val('Рубли');
					$('#curens').find('select option').eq(2).attr('selected', '1'); // Рубли
					//$('#curens').find('select').val(2);
					$('#curens').find('select').eq(0).selectedIndex = 2;
				}
			}

			//alert(name);
			break;
		case 'view_filter[district_id]': //страна
			if ( val != 0 ) 
			{
				switch_regions(val); 
			}
			else
			{
				hide_regions();
			}
			
			break;

		case 'view_filter[estate_id]': //поселок
			//alert(1);
			if ($("form#arenda_form input[name='vnr']").filter(':checked').val() == 0) // Россия
			{
			
				if ( $("form#arenda_form select[name='view_filter[direction_id]'] option:selected").val() == 1 ) // Москва
				{
					if ( val != 0 ) 
					{
						empty_val('#types_rus');
						$('#types_rus').css({display: 'none'});
					}
					else
					{
						var current_rows = '#types_rus';
						show_table_row($(current_rows));
					}
				}
			}
			
			break;
			
			
		case 'view_filter[direction_id]':
			//alert(val);
			if ($("form#arenda_form input[name='vnr']").filter(':checked').val() == 0) // Россия
			{
			
				if ( val == 1 ) // Москва
				{
					var current_rows = '#metro';
					show_table_row($(current_rows));
					
					var current_rows = '#estate';
					show_table_row($(current_rows));
					
					//$('#region').find('input').val(null);
					//$('#region').css({display: 'none'});
					
					
					//$('#mo_direction').find('select').attr('selectedIndex', 0);
					empty_val('#mo_direction');
					$('#mo_direction').css({display: 'none'});
					
					empty_val('#estate_mo');
					$('#estate_mo').css({display: 'none'});
				}
				else if ( val == 2 ) // Подмосковье
				{
					//alert(val);
					var current_rows = '#mo_direction';
					show_table_row($(current_rows));
					
					var current_rows = '#estate_mo';
					show_table_row($(current_rows));
					
					
					//$('#country').find('select').attr('selectedIndex', 0);
					empty_val('#country');
					$('#country').css({display: 'none'});
					
					//$('#metro').find('select').attr('selectedIndex', 0);
					empty_val('#metro');
					$('#metro').css({display: 'none'});
					
					empty_val('#estate');
					$('#estate').css({display: 'none'});
					//alert(val);
						
				}
				else
				{
					
					//$('#country').find('select').attr('selectedIndex', 0);
					empty_val('#country');
					$('#country').css({display: 'none'});
					
					//$('#metro').find('select').attr('selectedIndex', 0);
					empty_val('#metro');
					$('#metro').css({display: 'none'});
					
					empty_val('#estate');
					$('#estate').css({display: 'none'});
					
					empty_val('#estate_mo');
					$('#estate_mo').css({display: 'none'});
					
					//$('#mo_direction').find('select').attr('selectedIndex', 0);
					empty_val('#mo_direction');
					$('#mo_direction').css({display: 'none'});
				}
				
			}
			
			break;
			
			
		
	}
	
	
	
	not_change = false;
}

function switch_select(hide_type, show_type)
{
	//$(hide_type).find('select').attr('selectedIndex', 0);
	empty_val(hide_type);
	var attr_name = $(hide_type).find('select').attr('name');
	$(hide_type).find('select').attr('name', '');
	$(hide_type).css({display: 'none'});
	
	show_table_row($(show_type));
	$(show_type).find('select').attr('name', attr_name);
	
}


function switch_regions(cid)
{
	hide_regions();
	show_table_row($('#region_' + cid));
	$('#region_' + cid).find('select').attr('name', 'view_filter[direction_id]');
}

function empty_val(selector)
{
	if ( not_change == false)
	{
		$(selector).find('select').attr('selectedIndex', 0);
	}
}

function hide_regions()
{
	if (not_change)
	{
		$('.regions:not(:has(select:visible))').css({display: 'none'});
		$('.regions:not(:has(select:visible))').find('select').attr('name', '');
		
	}
	else
	{
		
		$('.regions').css({display: 'none'});
		$('.regions').find('select').attr('name', '');
		empty_val('.regions');
		//$('.regions').find('select').attr('selectedIndex', 0);
	}
}


function show_table_row(row)
{
	if ($.browser.msie)
	{
		row.css({display: 'block'});
	}
	else
	{
		row.css({display: 'table-row'});
	}
}


function open_js_sform_add_params2()
{
	var country_id = $(this).val();
	var current_rows = '#region';
	if ( country_id == '3') // Россия
	
	{	
		if ($.browser.msie)
		{
			$(current_rows).css({display: 'block'});
		}
		else
		{
			$(current_rows).css({display: 'table-row'});
		}
	}
	else
	{
		$(current_rows).css({display: 'none'});
	}
	
	
}

function open_js_sform_add_params3()
{
	var country_id = $(this).val();
	var current_rows = '#mo_direction';
	if ( country_id == '2') // Московская область
	
	{	
		if ($.browser.msie)
		{
			$(current_rows).css({display: 'block'});
		}
		else
		{
			$(current_rows).css({display: 'table-row'});
		}
	}
	else
	{
		$(current_rows).css({display: 'none'});
	}
	
	
}

var map, marker, infowindow;
function load_map(g_lat, g_lng, name) 
{
		var z = 15;

		var myLatlng = new google.maps.LatLng(g_lat, g_lng);
		var myOptions = {
			zoom: z,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			panControl: false,
			scrollwheel: false,
			streetViewControl: false
		  }
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		marker = new google.maps.Marker({position: myLatlng, draggable: false, map: map});
		marker.setTitle(name);
		setTimeout("open_iw('"+name+"');", 1000);
		//marker.setVisible(true);
};
function open_iw(name)
{
	infowindow = new google.maps.InfoWindow();
	infowindow.setContent('<div style="">' + name + '</div>');
	infowindow.open(map, marker);
}



//========================================================

