$(function() {
	initOptionBlocks();
	
	/* Show types */
	$('.showTypeSwitcher').click(function() {
		var type = $(this).attr('rel');
		$('input#showType').val(type);
		return false;
	});	
});
/* autosuggest */
$(function() {
    if ($('.autoSuggest input[type=text]').length > 0) {
		var autoSuggestOptions = {
			selectedItemProp: "name",
			searchObjProps: "name",
			queryParam : 'string',
			minChars: 2,
			startText: "Zadejte město, okres nebo kraj",
			emptyText: "Žádný záznam",
			retrieveLimit : 10,
			asHtmlID : 10
		};
	
		$('.autoSuggest input[type=text]').autoSuggest('/ajax/autosuggest/', autoSuggestOptions);
	}
});
/*function visitedOfferHighlight(elm) {
	try {
		if ($("table.dotsTable a:visited").length > 0) {
			$("table.dotsTable a:visited").parent().parent().addClass("visitedOffer");
		}
		if (typeof(elm) != 'undefined') {
			$(elm).parent().parent().addClass("visitedOffer");
		}
	} catch (e){
	}
}

$(document).ready(function(){
	visitedOfferHighlight();//highlight already visited offers
	$("table.dotsTable a").unbind('mouseup.visitedOfferHighlight').bind('mouseup.visitedOfferHighlight', function(){//higlight currenty clicked offer(eg using third button -> new window)
		visitedOfferHighlight($(this));
	});
});*/
$(function() {
	try {
		$('.offerContact .textarea').focus(function() {
			$(".captcha").css("display","block")
		})
	} catch (e){
	}
})

$(function() {
	
	$('a.as-close').click(function(){
		var value = $(this).prev().val();
		
		$('.as-values').val($('.as-values').val().replace(value + ",", ""));
		$('.as-input').focus();
		
		$(this).parent().remove();
		
		autoSuggestInput.reset();
		return false;
	});
	
});

function initOptionBlocks() {
	
	$(".optionsBlock .cont").unbind('click').click(function(e){
		$(".optionsBlock").removeClass("opened");
		$(this).parent().addClass("opened");
		$(".optionsCont").slideUp(100);
			if ($(this).parent().find(".optionsCont").is(":hidden")){
				$(this).parent().find(".optionsCont").slideDown(300);
			} else {
				$(".optionsBlock").removeClass("opened");
				$(".optionsCont").slideUp(100);
			}
		e.stopPropagation();
	});
	
	$(".optionsCont").unbind('click').click(function(e){
		$(this).parent().find(".fakeFade")
			.stop()
			.css({display: "block", opacity: 1})
			.fadeOut(1000);
		e.stopPropagation();
	});
	
	$(".optionsCont input.textField").unbind('blur').blur(function(e){
		
		/* Add selected item */
		var identifier = $(this).parents('div.column:eq(0)').attr('id');
		var fieldIdentifier = $(this).attr('name');
		
		/* Remove selected item */
		$('#' + fieldIdentifier + 'Item').next('br').remove();
		$('#' + fieldIdentifier + 'Item').remove();
		
		var val = $(this).val();
		if (val != '') {
			var title = $('label[for=' + fieldIdentifier + ']').text();
			if (title != '') {
				title = title + ' ' + val;
			} else {
				title = val;
			}
			var selectedItem = $('<span></span>').attr('id', fieldIdentifier + 'Item').text(title);
			$('#' + identifier + ' .cont').append(selectedItem).append($('<br/>'));
		} else {
			
		}
		
		if ($('#' + identifier + ' .cont span').length > 1) {
			$('#' + identifier + ' .cont .defaultText').hide();
		} else {
			$('#' + identifier + ' .cont .defaultText').show();
		}
	});
	
	$(".optionsBlock .inputField").unbind('click').click(function(e){
		
		var identifier = $(this).parents('div.column:eq(0)').attr('id');
		var val = $(this).val();
		var title = $('label[for=' + identifier + val + ']').text();
		
		if ($(this).is(':checked')) {
			if ($(this).attr('type') == 'radio') {
				/* Remove selected items */
				var elm = $('#' + identifier + ' .cont .defaultText').clone(true);
				$('#' + identifier + ' .cont').html('').append(elm.show());
			}
			/* Add selected item */
			var selectedItem = $('<span></span>').attr('id', identifier + val + 'Item').text(title);
			$('#' + identifier + ' .cont').append(selectedItem);
		} else {
			/* Remove selected item */
			$('#' +  identifier + val + 'Item').remove();
		}
		
		if ($('#' + identifier + ' .cont span').length > 1) {
			$('#' + identifier + ' .cont .defaultText').hide();
		} else {
			$('#' + identifier + ' .cont .defaultText').show();
		}
		
		if ($(this).attr('type') == 'radio') {
			/* Hide optionsCont */
			$('#' + identifier + ' .optionsBlock').removeClass("opened");
			
			$('#' + identifier + ' .optionsCont').slideUp(100);
			
			if ($(this).attr('name') == 'estateType') {
				addSearchComponents('box');
			}
		}
	});
	
	$(".optionsBlock .confirm").unbind('click').click(function(e){
		if ($(this).parent().find('.textField').length > 0) {
			/* Remove all items */
			var elm = $(this).parents('div.optionsBlock:eq(0)').find('.defaultText').clone(true);
			$(this).parents('div.optionsBlock:eq(0)').find('.cont').html('').append(elm.show());
			$(this).parent().find('.textField').each(function() {
				/* Add selected item */
				var identifier = $(this).parents('div.column:eq(0)').attr('id');
				var fieldIdentifier = $(this).attr('name');
				var val = $(this).val();
				if (val != '') {
					var title = $('label[for=' + fieldIdentifier + ']').text();
					if (title != '') {
						title = title + ' ' + val;
					} else {
						title = val;
					}
					var selectedItem = $('<span></span>').attr('id', fieldIdentifier + 'Item').text(title);
					$('#' + identifier + ' .cont').append(selectedItem).append($('<br/>'));
					$('#' + identifier + ' .cont  .defaultText').hide();
				}
			});
		}
		
		/* Hide optionsCont */
		$(this).parent().parent().removeClass("opened");
		$(this).parent().slideUp(100);
		return false;
	});
	
	$(".optionsBlock .reset").unbind('click').click(function(e){
		/* Remove selected items */
		var elm = $(this).parent().parent().find('.defaultText').clone(true);
		$(this).parent().parent().find('.cont').html('').append(elm.show());
		
		/* Reset selection */
		$(this).parent().find(':input').each(function() {
			if ($(this).attr('type') == 'text') {
				$(this).val('');
			}			
			if ($(this).is(':checked')) {
				$(this).removeAttr('checked');
			}			
			if ($(this).is(':selected')) {
				$(this).removeAttr('selected');
			}
		});
		
		/* Hide optionsCont */
		$(this).parent().parent().removeClass("opened");
		$(this).parent().slideUp(100);
		return false;
	});
	
	$(".optionsBlock .close").unbind('click').click(function(e){
		
		/* Remove selected items */
		var elm = $(this).parent().parent().find('.defaultText').clone(true);
		$(this).parent().parent().find('.cont').html('').append(elm.show());
		
		/* Reset selection */ 
		$(this).parent().find(':input').each(function() {
			if ($(this).attr('type') == 'text') {
				$(this).val('');
			}			
			if ($(this).is(':checked')) {
				$(this).removeAttr('checked');
			}			
			if ($(this).is(':selected')) {
				$(this).removeAttr('selected');
			}
		});
		
		/* Remove class */
		$(this).parent().find('.optionsCont').slideUp(100);
		$(this).parent().removeClass("opened");
		
		/* Hide filter block */ 	
		$(this).parent().parent().hide(300);		
		
		/* Show filter label */ 	
		var identifier = $(this).parent().parent().attr('id');
		$('a[rel=' + identifier + ']').show();
		
		return false;
	});	
	
}

$(document).bind('click', function(e) {
	$(".optionsCont").slideUp(100);
	$(".optionsBlock").removeClass("opened");
});
