var map, placemark, new_placemark, placemarks = [], marker_style = [], markers = []
    , layers_status ={  wait: true, connected: true, tv: true }, map_observ;

function mapInit() {
	
		map = new YMaps.Map(document.getElementById("map"));
		map.setCenter(new YMaps.GeoPoint(34.107917,44.948595 ), 12, YMaps.MapType.MAP);
		
		$().unload( function () {
			map.destructor();
		});

		map.addControl(new YMaps.TypeControl());
		map.addControl(new YMaps.SmallZoom());
		map.addControl(new YMaps.ScaleLine());
		map.enableScrollZoom();
		
		markersStyles();

		YMaps.Events.observe(map,map.Events.Update, function () {
			loadMarkersToMap();
		});
		YMaps.Events.observe(map,map.Events.MoveEnd, function () {
			loadMarkersToMap();
		});

		loadMarkersToMap();
}

function markersStyles() {

    // markers styles

    var s = new YMaps.Style();
    s.iconStyle = new YMaps.IconStyle("markers#wait");
    s.iconStyle.templateURL="markers#customPoint";
    s.iconStyle.offset=new YMaps.Point(-19, -40);
    s.iconStyle.href="/images/markers_wait.png";
    s.iconStyle.size=new YMaps.Point(38, 40);
    YMaps.Styles.add("markers#wait", s);

    var t = new YMaps.Template();
    t.text = "<div><img alt=\"$[m_name|0]\" style=\"height:$[style.iconStyle.size.y];width:$[style.iconStyle.size.x];\" \n\
src=\"$[style.iconStyle.href]\"\/>&nbsp;</div>";
    YMaps.Templates.add("markers#wait", t);

    marker_style['wait'] = s; // wait

    var s = new YMaps.Style();
    s.iconStyle = new YMaps.IconStyle("markers#connected");
    s.iconStyle.templateURL="markers#customPoint";
    s.iconStyle.offset=new YMaps.Point(-19, -40);
    s.iconStyle.href="/images/markers_connect.png";
    s.iconStyle.size=new YMaps.Point(38, 40);
    YMaps.Styles.add("markers#connected", s);

    var t = new YMaps.Template();
    t.text = "<div><img alt=\"$[m_name|0]\" style=\"height:$[style.iconStyle.size.y];width:$[style.iconStyle.size.x];\" \n\
src=\"$[style.iconStyle.href]\"\/>&nbsp;</div>";
    YMaps.Templates.add("markers#connected", t);

    marker_style['connected'] = s; // connect

    var s = new YMaps.Style();
    s.iconStyle = new YMaps.IconStyle("markers#tv");
    s.iconStyle.templateURL="markers#customPoint";
    s.iconStyle.offset=new YMaps.Point(-19, -40);
    s.iconStyle.href="/images/markers_tv.png";
    s.iconStyle.size=new YMaps.Point(38, 40);
    YMaps.Styles.add("markers#tv", s);

    var t = new YMaps.Template();
    t.text = "<div><img alt=\"$[m_name|0]\" style=\"height:$[style.iconStyle.size.y];width:$[style.iconStyle.size.x];\" \n\
src=\"$[style.iconStyle.href]\"\/>&nbsp;</div>";
    YMaps.Templates.add("markers#tv", t);

    marker_style['tv'] = s; // tv
    
}

function loadMarkersToMap( ) {

    var bounds = map.getBounds(),
        edgeT = bounds.getTop(),
        edgeR = bounds.getRight(),
        edgeB = bounds.getBottom(),
        edgeL = bounds.getLeft();

    for(i in placemarks){  map.removeOverlay(placemarks[i]); }

    placemark = null, markers = [], objects = [], placemarks = [];

    params = { filter: '', edgeT: edgeT, edgeR: edgeR, edgeB: edgeB, edgeL: edgeL };

    $.getJSON(	'/?task=ajax&action=map_markers_json'
                , params 
				, function (data){

                    showMarkers(data);

			});
}

function showMarkers(json) {

    var marker;
    var bounds = [];

    for(i in json) {

		marker = json[i];

		x = placemarks.length;

		placemarks[x] = new YMaps.Placemark(new YMaps.GeoPoint(marker.lng, marker.lat), 
												{style: marker_style[marker.status]} );
		placemarks[x].id = x;
		placemarks[x].m_id = marker.id;
		placemarks[x].m_layer = marker.status;
		placemarks[x].m_status = marker.status;
		placemarks[x].m_lat = marker.lat;
		placemarks[x].m_lng = marker.lng;
		placemarks[x].m_name = marker.name;

		placemarks[x].disableBalloon();

		if( layers_status[placemarks[x].m_layer] )
			map.addOverlay(placemarks[x]);

    }

}

function searchAddress() {
	
	if( !$("input[name=sAddress]").val() ) return false; 
	
    geocoder = new YMaps.Geocoder('Симферополь, ул.' 
						+ ($("input[name=sAddress]").val() + '' ).replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0")  
						, {results: 1});

    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {

        var results = this.length(), item = this.get(0);

        if (results > 0) {

            map.panTo( item.getGeoPoint() );
            map.setZoom(16);

        } else {

            alert('Не найдено!');
        }
    });

    return false;
}

function switchLayer(layer_name) {

    if(layers_status[layer_name]) {

        for(i in placemarks){
            if(placemarks[i].m_layer == layer_name)
                map.removeOverlay(placemarks[i]);
        }

    } else {

        for(i in placemarks){
            if(placemarks[i].m_layer == layer_name)
                map.addOverlay(placemarks[i]);
        }

    }

    layers_status[layer_name] = !layers_status[layer_name];

}

$(document).ready(function(){
    //$('#container').height(document.body.clientHeight - 37);

    $('.servers img').each(function(){
            $(this).mouseover(
                function(){
                    $('.serversText div').each(function(){
                        $(this).hide();
                    });
                    
                    $('.serversText div').eq($(this).attr('alt')).show();
                
            });
    });

    $('.serversText div').mouseout(
                function(){
                    $(this).hide();
    });

    $('.tab tr td').each(function(){
        $(this).hover(
            function(){                
                $(this).parent().addClass('active');
            },
            function(){
                $(this).parent().removeClass('active');
            }
        );
    });

    // $("#resizable").jSize({
        // type: 'top-bottom',
        // c1: '#resizable',
        // c2: '#resizebleCont',
        // staticDiv: '#resizable'
    // });
		    
});
