if (self.parent.frames.length != 0)
	self.parent.location.replace(document.location.href);

function em(box, server, zone) {
	var at = String.fromCharCode(64);
	document.write('<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>');
}

/*** GEO *********************************************************************/
var geoClickedIndex = 0;

function RequestGeoList(type, id) {
	ToggleGeoLoading(true);
	geoClickedIndex = type;
	DropSelectsAfter(geoClickedIndex);
	$.get('/ajax/geo/', {id: id}, function(data){ParseResponceGeo(data);}, "text");
}

function ParseResponceGeo(data) {
	ToggleGeoLoading(false);
	if (data.length > 0) {
		var select, pair, list;

		if (!$('#geo'+geoClickedIndex).length)
			$('#geo-container').append('<select id="geo'+geoClickedIndex+'" size="12" onchange="javascript:RequestGeoList('+(geoClickedIndex+1)+', this.options[this.selectedIndex].value)"></select>');

		list = data.split(";");
		for (var i=0; i<list.length; i++) {
			pair = list[i].split(":");
			$('#geo'+geoClickedIndex).append('<option value="'+pair[0]+'">'+pair[1]+'</option>');
		}
	} else {
		$('#selected-geo-popup').text($('#geo'+(geoClickedIndex-1)+' option:selected').text());
		$('#geo-submit').removeAttr('disabled');
		var select = $('#geo'+geoClickedIndex);
   	    if (select)
   	    	select.remove();
	}
}

function GeoSelectorOk() {
	select = $('#geo'+(geoClickedIndex-1)+' option:selected');
	SetGeoSelection(select.val(), select.text());
	GeoSelectorHide();
}

function DropSelectsAfter(type) {
	var select;
    for (var i=10; i>type; i--){
        select = $('#geo'+i);
        if (select)
	        select.remove();
    }
	$('select#geo'+type+' option').remove();
}

function ToggleGeoLoading(in_progress) {
	if (in_progress)
		$('#geo-loading').show();
	else
		$('#geo-loading').hide();
	$('#geo-submit').attr('disabled', 'disabled')
	$('#selected-geo-popup').html('&nbsp;');
}

function RequestGeoCurrentRegion(ip) {
	$.get('/ajax/geo/', {ip: ip}, function(data){ParseResponceGeoCurrentRegion(data);}, "text");
}

function ParseResponceGeoCurrentRegion(data) {
	var pair = data.split(";");
	if (pair.length == 2) {
		SetGeoSelection(pair[0], pair[1]);
		$('#geo-current-region-control').innerText = 'изменить';		
	}
}

function SetGeoSelection(id, name) {
	$('#geo-current-region-id-input').attr('value', id);
	$('#geo-current-region-name-input').attr('value', name);
	$('#geo-current-region').text(name);
}

/*** LOGIN *******************************************************************/
function set_login_form_position() {
	var coord, form, icon;
	form = document.getElementById('login-form');
	icon = document.getElementById('a-login');
	if (icon && form) {
		coord = get_position(icon);
		form.style.top = (coord["top"])+"px";
		var left = coord["left"]-icon.offsetWidth-50; // +icon.offsetWidth
		if (left>920) left = 920;
		form.style.left = left+"px";
	}
}
function toggle_login_form() {
	var obj = document.getElementById("login-form");

	if (obj.style.display=="block") {
		obj.style.display="";
	} else {
		obj.style.display="block";
		obj.getElementsByTagName("input")[1].focus();
	}
	set_login_form_position();
	return false;
}

function login_form_hide() {
	setTimeout('document.getElementById("login-form").style.display="";', 100);
}

function get_position(element) {
	var left = element.offsetLeft;
	var top = element.offsetTop;
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
		left += parent.offsetLeft - parent.scrollLeft;
		top += parent.offsetTop - parent.scrollTop
	}
	return {top: top, left: left};
}

/*** OVERLABELS **************************************************************/
function initOverLabels() {
	if (!document.getElementById)
		return;
	var labels, id, field;
	// Set focus and blur handlers to hide and show 
	// LABELs with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i=0; i<labels.length; i++) {
		if (labels[i].className == 'overlabel') {
			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id)))
				continue;
 
			// Change the applied class to hover the label 
			// over the form field.
			labels[i].className = 'overlabel-apply';
 
			// Hide any fields having an initial value.
			if (field.value !== '')
				hideLabel(field.getAttribute('id'), true);
 
			// Set handlers to show and hide labels.
			field.onfocus = function() {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function() {
				if (this.value === '')
					hideLabel(this.getAttribute('id'), false);
			};
 
			// Handle clicks to LABEL elements (for Safari).
			labels[i].onclick = function() {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id)))
					field.focus();
			};
		}
	}
}
 
function hideLabel(field_id, hide) {
	var field_for, labels = document.getElementsByTagName('label');
	for (var i=0; i<labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-9999px' : '0px';
			return true;
		}
	}
}

/*** DOCUMENT EVENTS *********************************************************/
$(document).ready(function(){
	initOverLabels();
});
