function setEqualHeight( columns )
{
	var tallestcolumn = 0;
	columns.each( function() {
		currentHeight = $( this ).height();
		if ( currentHeight > tallestcolumn ) {
			tallestcolumn = currentHeight;
		}
	} );
	columns.height( tallestcolumn );
}

function showChild() {
	var form = $( '#region-form' );
	var href = form.attr( 'action' ).replace( '/setRegion', '' );
	
	var selector = $( '#select-region' );
	var selected = $( ':selected', selector );
	var regionId = selected.attr( 'id' ).replace( 'region-', '' );
	
	var parentId = selected.attr( 'class' ).match( /parent-\d*/ )[ 0 ]
				   .replace( 'parent-', '' );
	var childs   = selected.attr( 'class' ).match( /childs-\d*/ )[ 0 ]
				   .replace( 'childs-', '' );
	
	location.hash = 'region-' + parentId;
	
	/* check child exists */
	if ( childs == 0 ) {
		form.submit();
		return;
	}
	
	var area = $( '#region-select' );
	area.empty();

	var loading = $( '<div class="loading">Загрузка...</div>' )
				  .appendTo( area );
	/* place loading message in center of holder and show it */
	var ltop = ( area.height() / 2 - loading.height() / 2 ) - 25 + 'px';
	var lleft = ( area.width() / 2 - loading.outerWidth() / 2 ) + 'px';
	loading.css( { top: ltop, left: lleft } ).show();

	// return;

	area.load( href + '?region=' + regionId + '&popup #region-select>select' );
	
	/* show back link */
	$( '#backLink' ).show();
	
	/* get current level */
	var level = $( '#current-level' ).val();
	/* increment level */
	++level;
	/* save level value */
	$( '#current-level' ).val( level );
}

function showParent() {
	var form = $( '#region-form' );
	var href = form.attr( 'action' ).replace( '/setRegion', '' );
	
	var regionId = location.hash.replace( '#parent-', '' );
	
	var area = $( '#region-select' );
	area.empty();

	var loading = $( '<div class="loading">Загрузка...</div>' )
				  .appendTo( area );
	/* place loading message in center of holder and show it */
	var ltop = ( area.height() / 2 - loading.height() / 2 ) + 'px';
	var lleft = ( area.width() / 2 - loading.outerWidth() / 2 ) + 'px';
	loading.css( { top: ltop, left: lleft } ).show();

	area.load( href + '?popup&back #region-select>select'
		, function() {
		
			var selected = $( '#select-region option:first' );
			var parentId = selected.attr( 'class' ).match( /parent-\d*/ )[ 0 ]
						   .replace( 'parent-', '' );
			console.log( parentId );
			location.hash = '#parent-' + parentId;
		}
	);
	
	
	/* get current level */
	var level = $( '#current-level' ).val();
	/* check level */
	if ( level > 0 ) {
		/* decriment level */
		--level;
	}
	if ( level == 0 ) {
		/* hide back link */
		$( '#backLink' ).hide();
	};
	/* save level */
	$( '#current-level' ).val( level );
}

$( document ).ready( function() {
	setEqualHeight( $( ".cols-holder2 > div > div > .box" ) );
	
	/* login box */
	var hideTimeout = null;
	
	$( '.login-link' ).mouseleave( function() {
		if ( typeof hideTimeout == 'numeric' )
			clearTimeout( hideTimeout );
		hideTimeout = setTimeout( function() {
			$( '#popup-login' ).fadeOut();
		}, 500 );
		return false;
	} );
	
	$( '.login-link' ).mouseenter( function() {
		var login = $( this );
		var box   = $( '#popup-login' );
		var offset = login.offset();
		box.css( 'left', offset.left + login.outerWidth() / 2 
							  - box.outerWidth() / 2 );
		box.css( 'top', offset.top + login.outerHeight() + 5 );
		box.fadeIn();
		$( '#login' ).focus();
		return false;
	} );
	
	$( '#popup-login' ).mouseenter( function() {
		if ( typeof hideTimeout == 'number' )
			clearTimeout( hideTimeout );
	} );
	$( '#popup-login' ).mouseleave( function() {
		$( '#popup-login' ).fadeOut();
		return false;
	} );
	
	$( "#switch-location" ).colorbox( { 
		width: "500", 
		// inline: true, 
		href: function() { 
			// return '#' + $( this ).attr( 'class' ).match( /service-\d+/g )[ 0 ]; 
			return $( this ).attr( 'href' ) + '?popup';
		} 
	} );
	
	/*
	 * search support
	 */
	$( '#search' ).val( 'Введите название организации или услуги' )
	
	$( '#search' ).click( function() {
		if ( $( this ).val() == 'Введите название организации или услуги' ) {
			$( this ).val( '' )
		}
	} )
	
	$( '#search' ).blur( function() {
		if ( $( this ).val() == '' ) {
			$( '#search' ).val( 'Введите название организации или услуги' )
		}
	} )
	
	$( '#search' ).autocomplete( {
		source: '/search/search_words',
		minLength: 2,
		select: function( event, ui ) {
			this.value = ui.item ? ui.item.value : ''
			if ( this.value != '' ) {
				$( '#search-form' ).submit()
			}
		}
	} );	
});
