


$(document).ready(function(){
var urldata = "";
var elements=".subcity a";
var places=".place span.f-name";
var page="",title="",name="",place="";
var x=0,y=0;
var flag=999;
function checkHash(){
    urldata = location.hash;
    TownData = urldata.replace(/(#)*(-[0-9])*/g,"");
    PlaceData = urldata.replace(/(#)*([A-Za-z])*(-)*/g,"");
    if (TownData) {
        showDialog('li.'+TownData+' > a');
        if (PlaceData) setTimeout(function(){changePlace(PlaceData);},900);
    }
}
function changePlace(PlaceIndex){
    $(".info").slideUp(200).parent().removeClass("selected");
    if (flag==PlaceIndex) {flag=999; return false;}
    flag=PlaceIndex;
    $(".info").eq(PlaceIndex).slideDown(200).parent().addClass("selected");
}
function showDialog(selectTown){
    flag=999;
    hideDialog();
    $(selectTown).addClass("current");
    var xTown = $(selectTown).find("ins").css("left");
    xTown = xTown.slice(0,-2);
	offset=$(selectTown).find("ins").offset();
    x=xTown*1+280;
    y=offset.top-10;
	page=$(selectTown).attr("page");
	title=$(selectTown).text();
    setTimeout(function(){
        $("#dialog").html("<div id='close'></div><div class='clear'></div><div id='infotext'>Загружается информация...</div>");
    	$.get("/o/"+page+".html", function(data){$("#infotext").html("<h4>"+title+"</h4>"+data);});
        $("#dialog").css({"left":x+"px", "top":y-101}).fadeIn(500);
     },300);
}
function hideDialog(){
    $(elements).removeClass("current");
	$("#dialog").fadeOut(300);   
}

	// Проверяем есть ли хэш и запускаем если уже есть в ссылкеы
    window.onload = function(){
  		// вызов нужных функций скрипта
  		checkHash();
	}
	
	$(elements).click(function(){
        showDialog(this);        
		return false;
	});
	$("#close").live("click", function(){
        hideDialog();
        return false;
	});
	$(places).live("click", function(){ 
        PlaceClick=$(places).index(this);
        changePlace(PlaceClick);
        return false;
	});	
    $('#firm a').live("click",function(){setTimeout(function(){checkHash();},200);});
});


