$(document).ready(function(){
	$('.login').click(function() {
		$('.login-box').slideToggle();
	});

	$('.login-cancel').click(function() {
		$('.login-box').slideToggle();
		return false;
	});

	$('.login-ok').click(function() {
		if ($('#_email').val() == '') {
			$('#_email').css('background', '#FFCCCC');

			if ($('#_password').val() == '') {
				$('#_password').css('background', '#FFCCCC');
			}

			return false;
		}

		if ($('#_password').val() == '') {
			$('#_password').css('background', '#FFCCCC');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "ajax/login.php",
			data: ({
				email: $('#_email').val(),
				password: $('#_password').val()
			}),
			async: false,
			success: function(msg) {
				if (msg == '') {
					$('.login-box div.error').toggle();
					return false;
				} else {
					$('#form-login').submit();
					return true;
				}
			}
		});

		return false;
	});

	$('.search span').click(function() {
		$('.search input').focus();
	});

	// clear input field
	$('.setclear').each(function() {
		var default_value = this.value;
		$(this).focus(function(){
			if(this.value == default_value) {
				this.value = '';
			}
		});

		$(this).blur(function(){
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});

	$('.show-more-text').click(function() {
		$('.more-text', $(this).parent()).slideToggle();
		$(this).remove();
	});

	$('.add-favourite').change(function(){
		var params = $.parseJSON(htmlspecialchars_decode($(this).val()));
		if ($(this).is(':checked')){
			params.status = 1;
		}else{
			params.status = 0;
		}
		$.ajax({
			type: "POST",
			url: "ajax/save_favourite.php",
			data: params,
			async: false,
			dataType: 'json',
			success: function(result){
				var messageBox = $('#message-box-'+params.formname);
				if (result.code==-2){
					messageBox.html(unescape("Um das Objekt zu Ihren Favoriten hinzuf%FCgen zu k%F6nnen m%FCssen Sie sich einloggen."));
				}else if (result.code==0){
					messageBox.html('Das Objekt wurde erfolgreich zu Ihren Favoriten gespeichert.');
				}else if(result.code=1){
					messageBox.html('Das Objekt wurde erfolgreich aus den Favoriten entfernt.');
				}else{
					messageBox.html(result.message);
				}
				messageBox.fadeIn();
			}
		});
	});
	
	// Galerie Gebotebox
	var currentId = null;
	$('div.show-infos').hover(function(){
		if (currentId != null && currentId != $(this).attr('id')) $('.lot-and-bid-information', $('#'+currentId)).fadeOut();
		currentId = $(this).attr('id');
		$('.lot-and-bid-information', $(this)).fadeIn();
	});
	$('div.show-infos img.preview-image').load(function() {});
	$('.show-gallery-gebote-box').overlay({
		top: 150,
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.8
		},
		onLoad: function(e) {
			$('.bid-overlay-notice-box', this.getOverlay()).hide();
			if (!$('.lot-and-bid-information', this.getOverlay()).length) {
				var currentElement = $('#'+this.getOverlay().attr('rel')).clone();
				$('.goto-buttons', currentElement).remove();
				$('img', currentElement).not('a img').remove();
				$('img', currentElement).css('height', '100px');
				$('.close-x', currentElement).show();
				this.getOverlay().prepend(currentElement.html());
				$('input[name=gebot]', this.getOverlay()).select().focus();

				$('div.close-x a.close').click(function(){
					$('a.button.close', $(this).parent().parent()).click();
				});
			}
		}
	});
});
