var params = new Object();
var data_width = new Object();

function edytuj_profil(){
	if ($('#button_profil').val() == 'Edytuj profil') {
		$('span[name=profil_write]').show();
		$('.profil_description').hide();
		$('span[name=profil_edit]').hide();
		$('#button_profil').val('Zapisz zmiany');
		$('#edit_password').hide();
		$('.profil_title_l,.profil_title').each(function () {
			data_width[$(this).attr('id')] = $(this).css('width');
		});
		$('.profil_title').css('width','72px');
		$('.profil_title_l').css('width','55px');
		$('.profil_title').css('padding-top','5px');
		$('.profil_title_l').css('padding-top','5px');
		$('#cont_activity').css('padding-top','5px');
		$('#button_dodatkowy').show();
		$('#button_profil_anuluj_url').show();
		$('a').each(function () {
			if ($(this).attr('name')  != 'action_link') {
				params[$(this).attr('id')] = $(this)[0].href;
				$(this)[0].href = "javascript:prompt_change('"+$(this)[0].href+"');";
			}
		});

	} else {
		write_profil();
	}
}

function prompt_change(link) {
	if (confirm('Czy napewno chcesz opuścić profil? Dokonane przez Ciebie zmiany nie zostaną zapisane.')) self.location.href=link;
}

function edytuj_profil_hide(){
	$('span[name=profil_write]').hide();
	$('.profil_description').show();
	$('span[name=profil_edit]').show();
	$('#button_profil').val('Edytuj profil');
	$('#edit_password').show();
	$('#button_dodatkowy').hide();
	$('.profil_title').css('width','auto');
	$('.profil_title_l,.profil_title').each(function () {
		$(this).css('width',data_width[$(this).attr('id')]);
	});
	$('.profil_title').css('padding-top','0px');
	$('.profil_title_l').css('padding-top','0px');
	$('#cont_activity').css('padding-top','0px');
	$('#button_profil_anuluj_url').hide();
	$('a').each(function () {
		if ($(this).attr('name') != 'action_link') {
			$(this)[0].href = params[$(this).attr('id')];
		}
	});
}

function write_profil() {
	var text = new Array();
	var www = /([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/;
	text[1] = '<strong>Niepoprawny adres www.</strong>';
	val=0;

	form_ok = true;

	if ($('#f_www').val()!='' && $('#f_www').val()!='http://' && !www.test($('#f_www').val())) {
		$('#f_www').addClass('error');
		form_ok = false;
		val=1;
	}

	var params = new Object();
	$('.profil_form_description').each(function () {
		params['questionaire['+$(this).attr('id')+']'] = $(this).val();
		$('#cont_'+$(this).attr('id')).html($(this).val());
	});

	if (form_ok) {
		//	$('#a_'+pole)[0].innerHTML = '<img src="/img/limetka_ajax.gif" />';
		$('#cont_lname').html($('#f_lname').val());
		$('#cont_fname').html($('#f_fname').val());
		$('#cont_city').html($('#f_city').val());
		$('#cont_occupation').html($('#f_occupation').val());
		if ($('#f_www').val()=='http://') {
			$('#f_www').val('');
			$('#cont_www').html('')
		} else {
			$('#cont_www').html('<a href="'+$('#f_www').val()+'">'+$('#f_www').val()+'</a>');
		}
		$('#cont_signature').html($('#f_signature').val());
		$('#cont_age').html($('#f_day').val()+' '+$('#f_month :selected').val()+' '+$('#f_year').val());

		params['lname'] = $('#f_lname').val();
		params['fname'] = $('#f_fname').val();
		params['city'] = $('#f_city').val();
		params['age'] = $('#f_year').val()+'-'+$('#f_month :selected').text()+'-'+$('#f_day').val();
		params['occupation'] = $('#f_occupation').val();
		params['www'] = $('#f_www').val();
		params['signature'] = $('#f_signature').val();
		params['user_id'] = p_user_id;
		//ajax_it('error_area','profil_operacje',params,function() {
		ajax_it('profil_operacje',params,function() {
			edytuj_profil_hide();
		});
	} else {
		show_error(text[1]);

	}
}

function zaznacz_date(pozycja) {
	$('#f_month_'+pozycja)[0].selected = "selected";
}

function border_edit($id) {
	$("#"+$id).css('border','1px solid #75D9F4');
}

function border_edit_out($id) {
	$("#"+$id).css('border','1px solid #fff');
}

function show_error_2(html) {
	$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#000'  });
	$.blockUI(html, { width: '360px', border: '8px solid #B3C704', padding: '0px' });
	show_error(html);
}

function require_to_friend(user_id) {
	var params = Object();
	params.user_id = user_id;
	params.action = 'oczekuj';
	ajax_it('friends_require',params,function(response) {
		$('#button_require_url').hide();
		show_error(response.params[0]);
	});
}

function friend_accept_users(user_id) {
	var params = Object();
	params.user_id = user_id;

	ajax_it('friends_accept',params,function() {
		$('#action_'+user_id).hide();
		$('#action_del_'+user_id).show();
	});

}

function friend_rejected_users(user_id) {
	var params = Object();
	params.user_id = user_id;

	ajax_it('friends_rejected',params,function() {
		$('#user'+user_id).slideUp('slow');
		$('#action_'+user_id).hide();
		$('#action_del_'+user_id).show();
	});
}

function friend_del_users(user_id) {
	var params = Object();
	params.user_id = user_id;

	ajax_it('friends_delete',params,function() {
		$('#user'+user_id).slideUp('slow');
	});
}

function friend_cancel_users(user_id) {
	var params = Object();
	params.user_id = user_id;

	ajax_it('friends_cancel',params,function() {
		$('#user'+user_id).slideUp('slow');
	});
}

function delete_profil(user_id) {
	if (confirm("Czy napewno chcesz usunąć swój profil? Twoje dane zostana bezpowrotnie usunięte i nie będziesz już mieć mozliwości korzystania z serwisu.")) {
		var params = Object();
		params.user_id = user_id;
		ajax_it('profil_delete',params,function(response) {
			if (response.data[0]==1) {
				$('#profile').html('<h1>Twój profil został usunięty!</h1>');
				setTimeout('location.href = '+site_base+'wyloguj/',2000);
			} else {
				window.alert('Ok, profil usunięty!');
			}
		});
	}
}


$().ready(function (){
	$('input').focus(function(){
		$(this).addClass('bg50');
	}).blur(function(){
		$(this).removeClass('bg50');
	});
	anchor = window.location.href.split("#");
	if ((0 < anchor.length) && ('edycja' == anchor[1])) {
		edytuj_profil();
	}
});

function submit_file() {
	$('#f_upload').hide();
	$('#ajax_form').show();
	$('#f_upload').submit();
}
