﻿window.onload = load;
var map;
var org_show=[];
var modif=0;

function load()
{
if (GBrowserIsCompatible())
{
    var map = new GMap2(document.getElementById("map"));
    window.map=map;
    window.map.setCenter(new GLatLng(window.lat, window.lng), window.zoom);
    window.map.addControl(new GMapTypeControl());
    window.map.addControl(new GLargeMapControl());
    map.enableScrollWheelZoom();
	window.map.enableDoubleClickZoom();
	window.map.enableContinuousZoom();

GEvent.addListener(map, "mousemove", function(point){$('#gpscoord').html(point.lat().toFixed(5) + ', ' + point.lng().toFixed(5));});

    for(id in org)
    {
       initializePoint(org[id],id);
       org_show[id]=false;
    }
}
}

function openSection(sec)
{
    $('#rightmenu .cat_section').hide();
    if (sec>0) $('#cat_sec'+sec).show('slow');
}

function initOrgSec($sec)
{
  $sec.find('ul li h3').prepend('<img src="/pic/a1.gif">');
  $sec.find('ul li h3~div').append('<div align="right"><a href="#">найти на карте</a></div>');
  $sec.find('ul li h3~div').find('div a').click(function()
  {
    org_id=$(this).parent().parent().parent().find('i').text();
    window.map.setCenter(new GLatLng(org[org_id].lat, org[org_id].lng), 11);
    return false;
  });
}

function initializePoint(pointData,id) {
	var point = new GLatLng(pointData.lat, pointData.lng);
//	var listItem = document.createElement('option');
//    listItem.value=id;
	var visible = false;

	var marker = new GMarker(point);
//    window.gmarkers.push(marker);

//	listItem.innerText = pointData.address;
//    listItem.text = pointData.address;

	var showINFO = function() {
        marker.openInfoWindowHtml('<div style="width:360px;" class="maintext"><div align="center" style="font-size:11pt;"><b>'+pointData.name+'</b></div><div style="height:106px;border-bottom:1px solid #aaaaaa;"><img src="/uploads/images/org_pic/'+id+'_ipic.jpg" width="78" height="99" style="float:left;margin-right:5px;">'+pointData.info+'</div><div style="border-bottom:1px solid #aaaaaa;">Адрес: '+pointData.addr+'<br>Телефон: '+pointData.tel+'<br>GPS координаты: '+(Math.round((pointData.lat)*100000)/100000)+','+(Math.round((pointData.lng)*100000)/100000)+'</div><div align="right"><a href="/katalog_organizaciy/org'+id+'/">подробнее...</a></div></div>');
		window.map.panTo(point);
		return false;
	}

	GEvent.addListener(marker, 'click', showINFO);
//	listItem.onclick = focusPoint;

	pointData.show = function() {
		if (!visible) {
//			document.getElementById('orglist').appendChild(listItem);
			window.map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hide = function() {
		if (visible) {
//			document.getElementById('orglist').removeChild(listItem);
			window.map.removeOverlay(marker);
			visible = false;
		}
	}

}

function openOrgList()
{
  var cat_id;
  var temp='';
  var org_new=[];
  var org_del=[];
  var org_new2=[];
  $('.org_section').remove();

  for(i=0;i<$('.cat_section:visible div ul li input:checked').length;i++)
  {
    cat_id=$('.cat_section:visible div ul li input:checked').eq(i).val();
    if (typeof(cat[cat_id])!='undefined')
    {
    cat_name=$('.cat_section:visible div ul li input[value='+cat_id+']').parent().text();
    temp=temp+'<div class="org_section"><h2>'+cat_name+'</h2><ul>';
        for(i2=0;i2<cat[cat_id].length;i2++)
        {
            temp=temp+'<li><h3>'+org[cat[cat_id][i2]].name+'</h3><div>'+org[cat[cat_id][i2]].addr+'<br>'+org[cat[cat_id][i2]].tel+'</div><i style="display:none;">'+cat[cat_id][i2]+'</i></li>'
            org_new[cat[cat_id][i2]]=true;
        }
    }
    temp=temp+'</ul></div>';
  }
  $('#leftmenu').prepend(temp);
  $('.org_section ul li').hover(function (){$(this).find('img').attr('src','/pic/a2.gif');$(this).find('div').show('fast');},function (){$(this).find('img').attr('src','/pic/a1.gif');$(this).find('div').hide('fast');});
  initOrgSec($('.org_section'));
  for(id in org_show)
  {
     if (typeof(org_new[id])=='undefined') {org_show[id]=false;org_del[id]=true;}
  }
  for(id in org_new)
  {
     if (org_show[id]==false) {org_show[id]=true;org_new2[id]=true;}
  }
  for(id in org_new2)
  {
     org[id].show();
  }
  for(id in org_del)
  {
     org[id].hide();
  }
}

$(document).ready(function()
{

document.body.onresize = function (){
    $('#leftmenu').css('height',(document.body.clientHeight-274+modif)+'px');
    $('#map').css('height',(document.body.clientHeight-194+modif)+'px');
}

    $('#leftmenu').css('height',(document.body.clientHeight-274+modif)+'px');
    $('#map').css('height',(document.body.clientHeight-194+modif)+'px')
    $('.block-menu h2').css('cursor','hand').click(function (){$(this).next().toggle('fast');});
    $('.cat_section div ul li input').attr('checked','checked').click(function(){openOrgList();});
    Nifty('div.tabmenu a', "small transparent top");
/*
    $('.org_section ul li').click(function ()
    {
      if ($(this).find('img').attr('src')=='/pic/a1.gif')
      {
        $(this).find('img').attr('src','/pic/a2.gif');
        $(this).find('div').show('fast');
      }
      else
      {
        $(this).find('img').attr('src','/pic/a1.gif');
        $(this).find('div').hide('fast');
      }
    });
*/
    $('.org_section ul li').hover(function (){$(this).find('img').attr('src','/pic/a2.gif');$(this).find('div').show('fast');},function (){$(this).find('img').attr('src','/pic/a1.gif');$(this).find('div').hide('fast');});


    initOrgSec($('.org_section'));
});

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {modif=9;}
