jQuery(document).ready(function(){
	var c_city = getCookie('city');
	if (c_city == undefined) {
		var StartEl = City.i1;
	} else {
		var StartEl = City[c_city];
	}

	var ContactsPanelHTML = 
		'<div class="city"></div>'
		+ '<div class="phone"></div>'
		+ '<div class="person"></div>'
		+ '<div class="address"></div>'
		+ '<div class="email"></div>'
		+ '<div class="map"></div>'
		;
	$(ContactsPanelHTML).appendTo('#ContactsPanel');
	ChangeCity(StartEl);

	$('<div id="ContactsLayout" class="iepng"><div class="ctrl"><img width="13" height="13" border="0" src="/img/x.gif" title="Закрыть" alt="Закрыть" /></div><div class="f_tt"/><div class="f_r iepng"><div class="f_rr"/><div class="f_b iepng" style="margin-bottom: 0pt;"><div class="f_bb"><div/></div><div class="f_l iepng"><div class="f_ll"><div/></div><div class="f_c"></div></div></div></div></div>').appendTo('body');
	$('#ContactsLayout .ctrl img').click(function(){
		$('#ContactsLayout').hide();
	});

	var ContactsLayoutHTML = '<ul class="city_list">';
	for (id in City) {
		ContactsLayoutHTML += '<li class="'+id+'">'+ City[id]['city']+'</li>';
	}
	ContactsLayoutHTML += '</ul>';
	$(ContactsLayoutHTML).appendTo('.f_c');
	$('#ContactsLayout ul.city_list li').click(function(){
		ChangeCity(City[$(this).attr('class')]);
		$('#ContactsLayout').hide();
	});

	$('#ContactsPanel div.city').click(function(){
		$('#ContactsLayout').toggle();
	});

});

function ChangeCity(id) {
	for (key in City) {
		//alert(City[key]['city'] + ' ' + id.city);
		if (City[key]['city'] == id.city) {
			setCookie('city', key, '/');
			//alert(key);
		}
	}
	$('#ContactsPanel .city').html('<span>' + id.city + '</span><img src="/images/arr.png" height="11" width="7" alt="" /><p>(выберите город)</p>');
	$('#ContactsPanel .phone').text(id.phone);
	$('#ContactsPanel .person').text(id.person);
	$('#ContactsPanel .address').text(id.address);
	$('#ContactsPanel .email').html('Адрес электронной почты:<a href="mailto:' + id.email + '">'+id.email+'</a>');
	$('#ContactsPanel .map').html('<a href="' + id.map + '" target="_blank">схема проезда</a>');
}

//function setCookie (name, value, expires, path, domain, secure) {
function setCookie (name, value, path, domain, secure, expires) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


