

$(document).ready(function(){
	var LOCATION = new Array();
	var INFO = new Array();
	var TYPE = new Array();
	
	
	
	// hoteltype,hotelinfo
	start();
	function start()
	{
		if($('#searchbox').attr("class")=="searchbox")
		{
				LOCATION = locationcheckbox();
				INFO = infocheckbox();
				TYPE = typecheckbox();
				ajaxSEARCH(LOCATION,INFO,TYPE);
		}
		
	}
	    // TOGGLE SUCHE .
	$('#toggle_search').click(function() {
	  	$('#searchbox').slideToggle('fast', function() {  });
		$('#search_results').slideToggle('fast', function() {  });
	});
	    // TOGGLE Favlist .
	$('#togglefavlist').click(function() {
		$('.favlist_content').slideToggle('fast', function() {  });
		return false;
	});
	

// FANCY BOX
	$("a#single_image").fancybox({
		'titlePosition' 	: 'inside'
	});
	$("a.group_image").fancybox({
		'titlePosition' 	: 'inside'
	});
	



	
// BEI STATUS ÄNDERUNG Funktionsaufruf
	$(".hotellocations[type=checkbox]").change(function(){	start() });
	$(".hotelinfo[type=checkbox]").change(function(){	start() });
	$(".hoteltype[type=checkbox]").change(function(){	start()});
	
	function locationcheckbox()
	{
		var LOCATION = new Array();
		$('.hotellocations[type=checkbox]').each(function() {
			
		var CONTENT = $(this).val();
		/*ist gecheckt */
		if($(this).is(':checked')) 
			{
				/*IST NICHT IM ARRAY */
				if(in_array(CONTENT,LOCATION)) 
				{ }
				else
				{
					LOCATION.push($(this).val());
				}
				
			}
			/* ist nicht gecheckt*/
			else					 
			{
				if(in_array(CONTENT,LOCATION)) 
				{ 
					removeItem(LOCATION,CONTENT)
				}
			}
	   	
			
		});
	//	 alert('LOCATION'+LOCATION);
		return LOCATION;
	
//		ajaxSEARCH(LOCATION,'','');
		
	}
	
	function infocheckbox()
	{
		$('.hotelinfo[type=checkbox]').each(function() {
			
		var CONTENT = $(this).val();
		/*ist gecheckt */
		if($(this).is(':checked')) 
			{
				/*IST NICHT IM ARRAY */
				if(in_array(CONTENT,INFO)) 
				{ }
				else
				{
					INFO.push($(this).val());
				}
				
			}
			/* ist nicht gecheckt*/
			else					 
			{
				if(in_array(CONTENT,INFO)) 
				{ 
					removeItem(INFO,CONTENT)
				}
			}
	   	
		});
//		alert("INFO"+INFO.length);
	//	ajaxSEARCH('',INFO,'');
	return INFO;
	}

	function typecheckbox()
	{
		$('.hoteltype[type=checkbox]').each(function() {
			
		var CONTENT = $(this).val();
		/*ist gecheckt */
		if($(this).is(':checked')) 
			{
				/*IST NICHT IM ARRAY */
				if(in_array(CONTENT,TYPE)) 
				{ }
				else
				{
					TYPE.push($(this).val());
				}
				
			}
			/* ist nicht gecheckt*/
			else					 
			{
				if(in_array(CONTENT,TYPE)) 
				{ 
					removeItem(TYPE,CONTENT)
				}
			}
	   	
		});
//		alert("TYPE"+TYPE.length);
	//	ajaxSEARCH('','',TYPE);
	return TYPE;
	}

	


function in_array(item,arr) {
for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
return false;
}

//remove item (string or number) from an array
function removeItem(originalArray, itemToRemove) {
	var j = 0;
	while (j < originalArray.length) {
	//	alert(originalArray[j]);
		if (originalArray[j] == itemToRemove) {
			originalArray.splice(j, 1);
	} else { j++; }
}
	//	assert('hi');
return originalArray;
}


});


function ajaxSEARCH(hotellocations,hotelinfo,hoteltype)
{
//	alert("LOCATION----"+hotellocations+"---- INFO ----"+hotelinfo+"----TYPE ----"+hoteltype);
//	alert(hotelinfo);

		$.ajax({
		  	url: "./ajax/",
			type: "POST",
		   	data: "hotellocation=1,"+hotellocations+"&hotelinfo=1,"+hotelinfo+"&hoteltype=1,"+hoteltype,
		  	cache: true,
		  success: function(html){
		    $(".search_results").html(html);
			
			scrollto();
		  }
		
		});
	
}

	// Scroll to

function scrollto()
{
	url = window.location;
	if(url.hash)
	{
	jump = url.hash.substr(1);
	new_position = $('#'+jump).offset();
	window.scrollTo(new_position.left,new_position.top-150);
	$('#'+jump).addClass('active');	
	}
}
	

