$(document).ready(function(){
	var geoResult;
	
	var map = new YMaps.Map(document.getElementById('bigMap'));
	map.setCenter( new YMaps.GeoPoint(56.027511,54.79852),12,YMaps.MapType.MAP );
	map.addControl(new YMaps.SmallZoom());
	map.addControl(new YMaps.ToolBar());
	
	$('.trigger').click(function(){
		var id = $(this).attr('alt');
		if($('#'+id).css('display')=='none')
		{
			$('#'+id).show();
		}else{
			$('#'+id).hide();
		}
	});
	
	$('.hand').click(function(){
		var cid = $(this).attr('alt');
		var pid = $(this).attr('name');
		map.removeAllOverlays();
		
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle();
		s.iconStyle.offset = new YMaps.Point(-10, -10);
		s.iconStyle.href = "/images/map/icon_"+pid+".gif";
		s.iconStyle.size = new YMaps.Point(20, 20);
		
		
		$.post(
			"/handlers/yp/xml.php?id="+cid,
			{},
			function(data)
			{
				if(data!='error')
				{
            		var arr = explode("###", data);
					for (var key in arr) {
    					var firm = arr [key];
    					firm = explode("|||",firm);
    					
						addmarkeronmap(firm[0],firm[1],firm[2],s,firm[3])
					}
				}
			}
		);
	});

	function addmarkeronmap(id,title,adress,s,note)
	{
		map.removeOverlay(geoResult);
		var geocoder = new YMaps.Geocoder(adress, {results: 1, boundedBy: map.getBounds()});

		YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
			if (this.length()) {
				marker = this.get(0);
				var coords = marker.getGeoPoint();
				var marker = new YMaps.Placemark(coords,{draggable:0,style:s,hideIcon:false})
				var link = document.createElement('a');
				link.href = '/'+id+'/firm/yp.html';
				link.innerHTML = title;
				
				var addnote = document.createElement('a');
				var delnote = document.createElement('a');
				delnote.innerHTML = 'Удалить из блокнота';
				addnote.innerHTML = 'Добавить в блокнот';

				if(note==1){
					addnote.className = "hidden";
					delnote.className = "";
				}else{
					addnote.className = "";
					delnote.className = "hidden";
				}

				addnote.onclick= function (){
					$.post(
						"/note_func/yp.html",
						{
							id: id,
							td: "add"
						},
						function(data)
						{
							addnote.className = "hidden";
							delnote.className = "";
						}
					);
				}
				
				delnote.onclick= function (){
					$.post(
						"/note_func/yp.html",
						{
							id: id,
							td: "del"
						},
						function(data)
						{
							addnote.className = "";
							delnote.className = "hidden";
						}
					);
				}
				
				var contdiv = document.createElement('div');
				var div = document.createElement('div');
				div.appendChild(addnote);
				div.appendChild(delnote);
				
				contdiv.appendChild(link);
				contdiv.appendChild(div);
				
				marker.openBalloon(contdiv,{maxWidth:150});
				map.addOverlay(marker);
			}
		});
	}
});

function explode( delimiter, string ) {
    var emptyArray = { 0: '' };
 
    if ( arguments.length != 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
 
    return string.toString().split ( delimiter.toString() );
}