var $j = jQuery.noConflict();
var selectForm = 1;
var defaultValue = '';
$j(function(){
	if(selectForm == 1) 
	$j(".search_submit").click(function(){
		$j('.required').remove();
		var theImmobilientyp = $j("#searching_immobilientyp").val();
		var theSearchText = $j("#searching_text").val();
		
		if(theSearchText == 'Stadt oder PLZ' || theSearchText == ''){
			$j("#searching_text").after('<br/><span class="required" style=" position: absolute; left: 12px; bottom: 23px;color:red; font-size: 11px;">Das ist ein Pflichtfeld</span>');
			return false;
		}
		
		var saveSearch = 0;
		if($j("#saving_search").attr('checked')) saveSearch = 1;
		
		$j("#search_text"+selectForm).val(theSearchText);
		$j("#immobilientyp"+selectForm).val(theImmobilientyp);
		$j("#saving_search"+selectForm).val(saveSearch);
    	$j("#vermarktungsart"+selectForm).val(selectForm);
		
		
		$j("#searchForm"+selectForm).submit();
	});
	
	$j(".search_step_submit").click(function(){
		
		$j('.required').remove();
		var theSearchText = $j("#search_region").val();
		if(theSearchText == 0){
			$j("#search_region").after('<br/><span class="required" style=" position: absolute; left: 15px; bottom: 4px;color:red; font-size: 11px;">Das ist ein Pflichtfeld</span>');
			return false;
		}
		var saveSearch = 0;
		if($j("#saving_search_step").attr('checked')) saveSearch = 1;
		
		$j("#search_step_text").val(theSearchText);
		$j("#search_step_save").val(saveSearch);

		$j("#searchStepForm").submit();
		
	});
	
	$j("#reset-form").click(function(){
		$j('.required').remove();
		$j("#searching_immobilientyp").find('option').eq(0).attr('selected','selected');
		$j("#searching_text").val('Stadt oder PLZ');
		
		$j(".search_price_from").val('von');
		$j(".search_price_to").val('bis');
		
		$j(".search_rooms_from").val('0');
		$j(".search_rooms_to").val('0');
		
		$j(".search_area_from").val('von');
		$j(".search_area_to").val('bis');
		return false;
	});
	
	clickTab = function(obj,tab,type){
		switch(tab){
			case 'tab1':
				selectForm = 1;
				$j(obj).parent().parent().attr('class','search_tab1');
				$j("#searching_immobilientyp").replaceWith('<select  class="search_select" id="searching_immobilientyp" >'+
						'<option value="wohnung">Wohnungen</option>'+
						'<option value="haus">Häuser</option>'+
						'<option value="wg">WG</option>'+
						'<option value="gewerbe">Gewerbe</option></select>');
				if(type == 'nostep'){
					$j('.filter_area form').each(function(){
			            if($j(this).attr('id') == 'searchForm1'){
			            	$j('#searchForm1').show();
			            }else{
			            	$j(this).hide();
			            }
			        });
				}else{
					$j('.step_filter_area form').each(function(){
			            if($j(this).attr('id') == 'searchStepForm1'){
			            	$j('#searchStepForm1').show();
			            }else{
			            	$j(this).hide();
			            }
			        });
				}
				
				break;
			case 'tab2':
				selectForm = 2;
				$j(obj).parent().parent().attr('class','search_tab2');
				$j("#searching_immobilientyp").replaceWith('<select  class="search_select" id="searching_immobilientyp" >'+
						'<option value="wohnung">Wohnungen</option>'+
						'<option value="haus">Häuser</option>'+
						'<option value="gewerbe">Gewerbe</option>'+
						'<option value="grundstueck">Grundstücke</option></select>');
				if(type == 'nostep'){
					$j('.filter_area form').each(function(){
			            if($j(this).attr('id') == 'searchForm2'){
			                $j('#searchForm2').show();
			            }else{
			                $j(this).hide();
			            }
			        });
		        }else{
		        	$j('.step_filter_area form').each(function(){
			            if($j(this).attr('id') == 'searchStepForm2'){
			                $j('#searchStepForm2').show();
			            }else{
			                $j(this).hide();
			            }
			        });
		        }
				break;
			case 'tab3':
				break;
			case 'tab4':
				break;
		}
	}
    
    $j(".search_box input[type='text']").each(function(){
    	$j(this).focus(function(){
    		if($j(this).val() == 'Stadt oder PLZ' || $j(this).val() == 'von' || $j(this).val() == 'bis'){
    			defaultValue = $j(this).val();
    			$j(this).val('');
    		}
    	}).blur(function(){
    		if($j(this).val() == '') 
    			$j(this).val(defaultValue);
    		else
    			$j('.required').remove();
    	});
    });
    
	$j(".entry-gallery-body a[rel]").overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.6
		}
	});
	
	$j(".as-title-page select").each(function(){
		$j(this).change(function(){
			$j("#list_sorttype").val($j(this).val());
			$j("#navForm").submit();
		});
	});
	
});

function toNextStep(obj,type){
	if($j(obj).prev().val() == 'Stadt oder PLZ' || $j(obj).prev().val() == '') {
		$j(obj).prev().focus();
		$j('.required').remove();
		$j(obj).after('<br/><span class="required" style=" position: absolute; left: 15px; bottom: 4px;color:red; font-size: 11px;">Das ist ein Pflichtfeld</span>');
		return false;
	}else{
		$j('.required').remove();
		$j("#form_step_search_text").val($j(obj).prev().val());
		$j("#form_step_search_type").val(type);
		$j("#form_step1").submit();
	}
}

function toPage(page){
	$j("#list_offset").val(page-1);
	$j("#navForm").submit();
}

function clickExternalLink(inEntryId,inActionUrl,inType){
	var actionUrl = inActionUrl;
	$j.post(actionUrl,{entryId:inEntryId,linkType:inType});
	return true;
}



