function userLogin() {
    
    if (!($('#loginForm_userEmail').val())) {
	var errorHtml = "<ul class='form_errors'><li>заполните поле E-mail</li></ul>";
	$('.form_errors').html(errorHtml);
	return;
    }

    if (!($('#loginForm_user_password').val())) {
	var errorHtml = "<ul class='form_errors'><li>введите пароль</li></ul>";
	$('.form_errors').html(errorHtml);
	return;
    }

    $.post(URL + 'sending-query/users/login',
	$( '#userAuthForm' ).serialize(),
	function(data) {
	    
	
	if ($('error', data).length != 0) {
	    var errorHtml = "<ul class='form_errors'><li>" + $('error', data).text() + "</li></ul>";
	    $('.form_errors').html(errorHtml);
	} else {
		window.location = URL + 'users/';
	    
	}
    });
}

function loadDistrictDropbox(country_id, obj) {
	$.get( URL + 'index.php?mod=jquery&submod=districts_selector',
		{ countryId : country_id },
		function (data) {
			var districts = $( 'root', data ).text();
			$('#country').html( $(obj).text() );
			$('#district').html('Выберите');
			if( districts != '' ) {
				$('#districtDropbox').html(districts);
				$('#countryDropbox').hide();
			}

		}, 'xml');
	
	
}

$('body').live('click',function(e){ 
	e=e||event;
  var target=e.target||e.srcElement;
  var list=document.getElementById('districtSelectorLink');
  var name=document.getElementById('districtDropbox');
  if(name&&list){
    if(target !=name && target != list) {
		var parent=target;
		while(parent.parentNode&&parent!=list)parent=parent.parentNode;//Вот эта проверка
		if(parent !=list){
			$(name).hide();
		}
    }
  }
  
  var list=document.getElementById('countrySelectorLink');
  var name=document.getElementById('countryDropbox');
  if(name&&list){
    if(target !=name && target != list) {
		var parent=target;
		while(parent.parentNode&&parent!=list)parent=parent.parentNode;//Вот эта проверка
		if(parent !=list){
			$(name).hide();
		}
    }
  }
});


function fix_variants() 
{
    if ($.browser.msie && parseInt(BrowserDetect.version) == 7) 
	{
		$('table.variant_list td').each(function(i) 
		{
			$(this).html('<div class="variant_height" rel="'+i+'">'+$(this).html()+'</div>');
		});

		$('table.variant_list span.date').each(function(i) 
		{

			var pTop = $('table.variant_list td:eq('+i+')').height() - $('table.variant_list div.variant_height[rel='+i+']').height();
			//$(this).css({paddingTop: pTop+'px'});
			$(this).attr('rel', i);
        });

        $('table.variant_list a.variant_more').each(function(i) 
		{
			var pTop = $('table.variant_list td:eq('+i+')').height() - $('table.variant_list div.variant_height[rel='+i+']').height() + parseInt($('table.variant_list span[rel='+i+']').css('padding-top'));

			//$(this).css({paddingTop: pTop+10+'px'});
        });
    }
    else 
	{
		$('table.variant_list td').each(function(i) 
		{

			if ($.browser.msie && parseInt(BrowserDetect.version) < 7) 
			{
				$(this).wrapInner('<div class="variant_height" rel="'+i+'" style="height: 1px;"></div>');
			}
			else
			{
				$(this).wrapInner('<div class="variant_height"></div>');
			}
		});

		if ($.browser.msie && parseInt(BrowserDetect.version) < 7) 
		{
			
			$('table.variant_list span.date').each(function(i) 
			{

				var pTop = $('table.variant_list td:eq('+i+')').height() - $('table.variant_list div.variant_height[rel='+i+']').height();
				$(this).css({paddingTop: pTop+'px'});
				$(this).attr('rel', i);
			});

			$('table.variant_list a.variant_more').each(function(i) 
			{
				var pTop = $('table.variant_list td:eq('+i+')').height() - $('table.variant_list div.variant_height[rel='+i+']').height() + parseInt($('table.variant_list span[rel='+i+']').css('padding-top'));

				//$(this).css({paddingTop: pTop+10+'px'});
			});
		}
		else 
		{
			$('table.variant_list span.date').each(function(i) 
			{
				var pTop = $(this).parents('td').height() - $(this).parents('div.variant_height').height();
				$(this).css({paddingTop: pTop-1+'px'});
				$(this).attr('rel', i);
			});
			$('table.variant_list a.variant_more').each(function(i) 
			{
				var pTop = $(this).parents('td').height() - $(this).parents('div.variant_height').height() + parseInt($('table.variant_list span[rel='+i+']').css('padding-top'));
				//$(this).css({paddingTop: pTop+10+'px'});
			});
		}
    }
}
  
  
function deleteAd(type,adCode) 
{
    if( prompt( 'ВНИМАНИЕ!\nЕсли вы действительно хотите удалить это предложение, введите его код:' ) != adCode )
	{
        return false;
    }

    document.location = '/users/ad-delete/' + type + '-' + adCode + '/';
}

function showFeedbackDialog() {
    $( '#feedbackDialogMessage' ).hide();
    $( '#feedbackDialogError' ).html( '' );
    $( '#feedbackDialogControls' ).show();

    $( '#ie6frame' ).show();
    $( '#overlay' ).show();
    var feedbackDialog = new Boxy( $( '#feedbackDialog' ),
	{
	    title: 'Форма обратной связи',
	    draggable: false,
	    closeText: 'закрыть',
	    afterHide: function() {
		$( '#feedbackError' ).html( '' );
		$( '#overlay' ).hide();
		$( '#ie6frame:visible' ).hide();
	    }
	} );
}

function feedbackDialogChange() {
    var selected = $('#feedbackType').val();
    if (selected == 'feedback') {
        $('#captchaCell').show();
        $('#captchaCode').show();
        $('#viewFeedbacksLink').css('display','block');
    } else {
        $('#captchaCell').hide();
        $('#captchaCode').hide();
        $('#viewFeedbacksLink').css('display', 'none');
    }
}


function toggleCountryDropbox() {
	var closeFunc = function()
	{
		//$( '#ie6frameSearch:visible' ).hide();
		if( $.browser.msie && parseInt(BrowserDetect.version) == 6) {
		$( '#fast_search_form select' ).css( 'visibility', 'visible' );
		}
		$( '#countryDropbox' ).hide();
	};

	if( $( '#countryDropbox' ).is( ':visible' ) ) {	
		closeFunc();
	} else {
		if( $.browser.msie && parseInt(BrowserDetect.version) == 6) {
			 $('#drop_menu_box').hide();
			 $( '#fast_search_form select' ).css( 'visibility', 'hidden' );
		}
		//$( '#ie6frameSearch:hidden' ).show();
		
		$( '#countryDropbox' ).show();
	}
}

function toggleDistrictDropbox()
{
	var closeFunc = function()
	{
		//$( '#ie6frameSearch:visible' ).hide();
		if( $.browser.msie && parseInt(BrowserDetect.version) == 6) {
		$( '#fast_search_form select' ).css( 'visibility', 'visible' );
		}
		$( '#districtDropbox' ).hide();
	};

	if( $( '#districtDropbox' ).is( ':visible' ) ) {	
		closeFunc();
	} else {
		if( $.browser.msie && parseInt(BrowserDetect.version) == 6) {
			 $('#drop_menu_box').hide();
			 $( '#fast_search_form select' ).css( 'visibility', 'hidden' );
		}
		//$( '#ie6frameSearch:hidden' ).show();
		

		$( '#districtDropbox' ).show();
	}
}

function linkStatictics(link, pageName) {
	var pageName;	
	$(".breadcrumbs strong").each(function() {
		pageName = $(this).text();
	});

	if (!pageName) {
		$(".breadcrumbs a").each(function() {
			pageName = $(this).text();
		});
	}
	
	$.get(
		URL + "sending-query/users/ajax-statistics",
	{
		pageName : pageName,
		addr     : $(link).attr('href')
	});
}

$(document).ready(function()
{
	//статистика переходов по ссылкам
	
	pageAddress = window.location.href;

	
	if (URL) {
		pattern = URL;
	} else 
		pattern = 'promin.ru';

	$("a").each(function() {
		var url = $(this).attr('href');
		if (url) {
			if(url.indexOf(pattern) < 0) {
				if(url.indexOf('http://') + 1) {
					$(this).bind('mouseup', function() {
						linkStatictics(this);
					});
				}
			}
		}
	});
	

    $('.rounded').corner("3px");
    $('.rounded5').corner("5px");
    $('.rounded7').corner("7px");
    $('.rounded8').corner("10px");


	if( $( '#ie6frame' ).length != 0 )
	{
		$( '#ie6frame' ).css( 'height', $(document).height() + 'px' );
	}

	/* IE6 */
	if( $.browser.msie && parseInt(BrowserDetect.version) == 6 )
	{
		$( document ).pngie();
	}


	/* IE6, 7 */
    if( $.browser.msie && parseInt(BrowserDetect.version) < 8 )
	{
		var resizeConferenceLabel = function() 
		{
			if( $( '.conferenceBar' ).length != 0 )
			{
				$( '.conferenceBar .labelBg' ).height( $( '.conferenceBar' ).height() + 2 );
			}
		};

		resizeConferenceLabel();
		$( window ).resize( resizeConferenceLabel );
    }

	// делаем оверлей для диалогов частично прозрачным, 
	// выставляем ему правильные размеры
	$( '#overlay' ).click( function() 
						   {
							   $( '#overlay' ).hide();
							   if( Boxy.get( '#feedbackDialog' ) ) Boxy.get( '#feedbackDialog' ).hide();
							   if( Boxy.get( '#adMapDialog' ) ) Boxy.get( '#adMapDialog' ).hide();
							   if( Boxy.get( '#agencyMapDialog' ) ) Boxy.get( '#agencyMapDialog' ).hide();
							   if( Boxy.get( '#servicePricecutDialog' ) ) Boxy.get( '#servicePricecutDialog' ).hide();
							   if( Boxy.get( '#yourPriceHandlerDialog' ) ) Boxy.get( '#yourPriceHandlerDialog' ).hide();
							   if( Boxy.get( '#adComplainDialog' ) ) Boxy.get( '#adComplainDialog' ).hide();
						   } );

	$( '#overlay' ).css( {
							'opacity': '0.5',
							'height': $(document).height() + 'px'
						 } );
});

$(document).ready(function()
{
    $('.autocomplete').each(function()
	{
		var rel = $(this).attr('rel');
		
		if(typeof(rel)!='undefined') 
		{
			var options = eval('('+rel+')');
			
			if(typeof(options)!='undefined' && typeof(options.acurl)!='undefined') 
			{
				var acurl = options.acurl;
			}
			
		}
		
		$(this).autocomplete(acurl,
		{
			minChars: 2,
			delay: 200,
			cacheLength:10,
			matchSubset: true,
			matchCase: false,
			matchContains: true,
			mustMatch: false,
			selectFirst: false,
			extraParams: {},
			multiple: (typeof(options)!='undefined' && typeof(options.acmultiple)!='undefined')?options.acmultiple:true,
			multipleSeparator: ', ',
			autofill: true,
			max: 20,
			scroll: true,
			scrollHeight: 180			
		});
		
	});
});
