var helper = new function(){
	this.show = function( item ){
		var o = document.getElementById( 'helper' );
		o.innerHTML = item.attributes.getNamedItem( 'help' ).value;
		o.style.display = 'block';
		var pos = getAbsPos( item );
		o.style.left = pos.x + pos.w / 2 + 'px';
		o.style.top = pos.y + pos.h + 'px';
	}
	
	this.hide = function(){
		document.getElementById( 'helper' ).style.display = 'none';
	}
	this.attach = function( el ){
		el.onmouseover = function(evt){
			helper.show( el, evt || event );
		}
		el.onmouseout = function(){
			helper.hide( );
		}
	}
	this.init = function(){
		var i, coll = document.getElementsByTagName( '*' );
		for( i = 0; i < coll.length; i ++ ){
			if( coll[ i ].attributes && coll[ i ].attributes.getNamedItem( 'help' ) ){
				helper.attach( coll[ i ] );
			}
		}
	}
}

function getAbsPos( obj ){
	var x = obj.offsetLeft;
	var y = obj.offsetTop;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	while( obj = obj.offsetParent ){
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}
	return {'x':x,'y':y,'h':h,'w':w};
}

function catalogueSearch(type_search) {	
	if (type_search == 'region') {
		$.ajax({
		url: "catalogue_search.php",
		global: false,
		type: "POST",
		data: ({action: 'search_region', type_object: $('#selectType').val()}),
		dataType: "txt",
		success: function(response){
			if ($.trim(response) == 'error_type_object_id') {
				$('#str_region_search').html('<span class=\"description_label\">[ Сначала выберите тип объекта ]</span>');
				$('#str_point_search').html('<span class=\"description_label\">[ Сначала выберите тип объекта ]</span>');
			} else {
				$('#str_region_search').html(response);
				$('#str_point_search').html('<span class=\"description_label\">[ Для отображения списка населённых пунктов выберите регион ]</span>');
			}
		},
		beforeSend: function() {
			$('#str_region_search').html('<img src="/images/loading.gif" alt="Загрузка" title="Пожалуйсте, подождите, идёт загрузка" />');
		}
		});
	} else if (type_search == 'point') {
	
		$.ajax({
		url: "catalogue_search.php",
		global: false,
		type: "POST",
		data: ({action: 'search_point', type_object: $('#selectType').val(), regionid: $('#selectRegion').val()}),
		dataType: "txt",
		success: function(response){
			if ($.trim(response) == 'error_point_id') {
				$('#str_point_search').html('<span class=\"description_label\">[ Для отображения списка населённых пунктов выберите регион ]</span>');
			} else {
				$('#str_point_search').html(response);

			}
		},
		beforeSend: function() {
			$('#str_point_search').html('<img src="/images/loading.gif" alt="Загрузка" title="Пожалуйсте, подождите, идёт загрузка" />');
		}
		});
		
	}
}

function resetSearchForm() {
	$('#selectType > #first_option').attr('selected', 'selected');
	$('#str_region_search').html('<span class=\"description_label\">[ Сначала выберите тип объекта ]</span>');
	$('#str_point_search').html('<span class=\"description_label\">[ Сначала выберите тип объекта ]</span>');
}
