$(document).ready(function(){

	if($('#x1').val() == ''){
		$('#x1').removeClass('onclick');
		$('#x1').val( lang['inp_x1'] );
		$('#x1').focus(function(){ $(this).val('').addClass('onclick'); });
	}
	if($('#x2').val() == ''){
		$('#x2').removeClass('onclick');
		$('#x2').val( lang['inp_x2'] );
		$('#x2').focus(function(){ $(this).val('').addClass('onclick'); });
	}

	$('.SearcherContent form').submit(function(){
		if($('#x1').val() == lang['inp_x1']){
			$('#x1').val('');
		}
		if($('#x2').val() == lang['inp_x2']){
			$('#x2').val('');
		}
		return true;
	});

	$('.SearchRes tr:not(.Ads, .Open, .OpenContent)').mouseout(function(){ $(this).removeClass('MouseOver') });
	$('.SearchRes tr:not(.Ads, .Open, .OpenContent)').mouseover(function(){ $(this).addClass('MouseOver') });

	$('.SearchRes tr:not(.Ads, .OpenContent)').click(function(){
		var id = $(this).attr('id').replace('p_', '');
		if($(this).hasClass('Open')){
			$(this).removeClass('Open');
			$('.SearchRes tr[id=i'+ id +']').hide();
		}else{
			$(this).addClass('Open');

			if( $('#i'+ id).length == 1 ){
				$('#i'+ id).show();
			}else{
				$.ajax({
				url: '/personas/?id='+ id,
				dataType: 'json',
				success: function(data){
						var info = '';
						if(data.address == 1){
							info += '<li class="mail">'+ lang['address'] +'</li>';
						}
						if(data.phone == 1){
							info += '<li class="phone">'+ lang['phone'] +'</li>';
						}

						var buf = '<td colspan="3"><div class="inside"><h3>'+ lang['info'] +':</h3>'
								+ '<ul class="list">'+info+'</ul>';
						if( user_logged == 1 ){
							buf += '<a class="show" onclick="ps_buy(\''+ id +'\')">'+ lang['show'] +'</a>'
								 + '<span>'+ lang['price'] +'</span>';
						}else{
							buf += '<a class="show" href="'+ url_registration +'">'+ lang['register'] +'</a>'
								 + '<span>'+ lang['register_info'] +'</span>';
						}
						buf += '</div></td>';

						$('#i'+ id).html( buf );
					},
				beforeSend: function(){
						var row = document.createElement('tr');
						$(row).addClass('OpenContent')
							.attr('id', 'i'+ id)
							.css('background', '#FFF')
							.html( '<td colspan="3"><div class="inside"><img src="/img/indicator2.gif" /></div></td>' );
						$('#p_'+id).after(row);
					}
				});
			}
		}
	});

	//$('#SortX').change(function(){
	//	window.location = '?o=' + $('#SortX').val();
	//});

});


function ps_buy(id){
	$.ajax({
		url: '/personas/?id='+ id +"&buy" ,
		dataType: "json",
		success: function(data){
			if( data.error == 1 ){
				$('span', '#i'+id).html( lang['error'] );
			}else{
				var info = '';
				if( data.phone != null ){
					info += '<li>'+ lang['phone'] +':<br />'+ data.phone +'</li>';
				}
				if( data.address != null ){
					info += '<li>'+ lang['address'] +':<br />'+ data.address +'</li>';
				}
				$('#i'+id).html('<td colspan="3"><div class="inside"><h3>'+ lang['info'] +':</h3>'
					+'<ul class="box">'+ info +'</ul></td>');
			}
		}
	});
}
