$(document).ready(function(){
/* --------------------------------------------------------------------------------------------- */
	var default_height = 443;
	var default_width = 660;
	var rusmap_url = '/public/common/js/rusmap';

	initializeMap();

/* --------------------------------------------------------------------------------------------- */
	function initializeMap(){
		$('#searchlocations').hide();
		//$('#map').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				backgroundImage: ''
			});
			$('#map').animate({height: default_height}, "slow");
			loadMap();
		});				
	}
	
/* --------------------------------------------------------------------------------------------- */	
	function loadMap(id){
		var id_str='';
		if (!id){id='';}
		if (id)
		{
			id_str =  '_'+id;	
		}
		//alert(id);
		
		$('#map').load('/realiz/rusmap' + id_str, {}, function(){
		if (id) 
		{
			// если открыли округ


			//place bullets
			$('a.bullet').each(function(){
				var coords = $(this).attr('rel').split('-');
				$(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
					   .hide()
					   .fadeIn()
					   .click(function(){showPopup($(this).attr('id'));});
			});

			$('.map_dialog').dialog({
	 			autoOpen: false,
	 			//show: 'slide',
	 			//show: 'blind',
	 			//hide: 'explode',
				bgiframe: true,
				//height: 140,
				modal: true
			});					
			
			//добавим кнопку назад
			$('<a id="mapback" href="javascript:void(0)"><span>Вернуться назад &gt;</span></a>')
				.appendTo($('#map'))
				.click(function(){
					initializeMap();	
				});
			$('.popup_dealer_list').click(function(){
											var ids = $(this).attr('id').split('_',2);
											var town_id = ids[0]
											var dealer_id = ids[1];
											$('#'+town_id+"-box").dialog( 'close' );
											goToDealer(dealer_id);
											return false;
										});		
		}
		else 
		{
			// иначае показываем общую карту России
			$('.imgmap').maphilight();
			$('area').click(function() {
				
			var id = $(this).attr('id');

			switch (id)
			{   
				case 'centralnyi' :
					map_width = 660;       
					map_height = 550;
					zwidth = 117;
					zheight = 97;
					zleft = 19;
					ztop = 191;					      
					break;   
				case 'uzhnyi' :      
					map_width = 660; 
					map_height = 660;
					zwidth = 104;
					zheight = 106;
					zleft = -8;
					ztop = 269;										      
					break;		
				case 'severozapadnyi' :
					map_width = 660;
					map_height = 443;
					zwidth = 235;
					zheight = 157;
					zleft = 38;
					ztop = 97;										      
					break;
				case 'uralskyi' :   
					map_width = 660;
					map_height = 770;
					zwidth = 161;
					zheight = 189;
					zleft = 147;
					ztop = 144;					      
					break;
				case 'privolzhskyi' :
					map_width = 660;				       
					map_height = 600;
					zwidth = 133;
					zheight = 122;
					zleft = 62;
					ztop = 216;					      
					break;
				case 'sibirskyi' :
					map_width = 660;				       
					map_height = 770;
					zwidth = 277;
					zheight = 323;
					zleft = 213;
					ztop = 69;					      
					break;
				case 'dalnevostochnyi' :
					map_width = 660;				       
					map_height = 850;
					zwidth = 297;
					zheight = 382;
					zleft = 366;
					ztop = -9;					      
					break;
				case 'kazachstan' : 
					map_width = 660;				      
					map_height = 443;
					zwidth = 240;
					zheight = 161;
					zleft = 63;
					ztop = 290;					      
					break;
				default :
					map_width = 660;				 
					map_height = 443;
					zwidth = 0;
					zheight = 0;
					zleft = 0;
					ztop = 0;
			}
			$('<img src="/public/common/js/rusmap/img/rusmap_' + id + '.jpg" id="zoom_part"/>').css({
				border: 'none',
				position: 'absolute',
				width: zwidth + 'px',
				height: zheight + 'px',
				top: ztop + 'px',
				left: zleft + 'px'})
				.appendTo('#map')
				.siblings()
				.fadeOut('slow');
				$('#zoom_part').animate({
						width: map_width+'px',
						height: map_height+'px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){});;
				

				$('#map').animate({height: map_height}, "slow", '', function(){
					$(this).empty()
						   .css({backgroundImage: 'url(/public/common/js/rusmap/img/rusmap_' + id + '.jpg)'});
						   
					$('<a id="mapback" href="javascript:void(0)"><span>Вернуться назад &gt;</span></a>')
						.appendTo($('#map'))
						.click(function(){
						initializeMap()
						});
						
					loadMap(id);
				
				})
 
		  	return false; 
		});
		// area click end	
						
		}

		$('#map').fadeIn();
		});
		// #map load function end
		
		$('#info_container').load('/realiz/rusmap' + id_str + 'info', {}, function(){
			$('#dealers_info_data').accordion({
				collapsible: true,
				clearStyle: true,
				autoHeight: true,
				active: false
				});
				
			$("._dealers_info_data_table tr").hover(
   				function()
   				{
    				$(this).addClass("highlight");
   				},
   				function()
   				{
    				$(this).removeClass("highlight");
   				});
 
 			var bghoverColor = '#f5f5f5';
 			var bgColor = '#ffffff';
 			$(".dealers_info_data_table tr").hover(
 				function()
   				{
                    $(this).children('td').animate({backgroundColor: bghoverColor}, 300);
                },
   				function()
   				{
                    $(this).children('td').animate({backgroundColor: bgColor}, 300);
   				});			 
			
	
		});
		// #info_container load function end
	}
/* --------------------------------------------------------------------------------------------- */

	function showPopup(id)
	{
/*		$('#map div.popup').fadeOut(); 
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn();
		$('a.close').click(function(){
			$(this).parent().fadeOut();
		});
*/		
		var boxid = '#' + id + '-box';

		$(boxid).dialog('open');
		$('.dealers_desc_hdr').blur();
		
/*		$('#map div.popup').hover(
      				function () {}, 
      				function () {
        				$(this).fadeOut();
      				}
    			);
*/
	}
	
/* --------------------------------------------------------------------------------------------- */
	function goToDealer (id)
	{
	n = $('.dealers_desc_hdr').index($('#'+id+'_acc'));
	$('#dealers_info_data').accordion( 'activate' , n );
	$('#'+id+'_acc').focus();	
	}
				
});






















