jQuery(document).ready(function() {
		 		
	$('.ac_results').bgiframe();
			
	//Calendar
	if ( $(".date-pick").length > 0 ) {
		$('.date-pick').datePicker({clickInput:true})		
		$(".date-pick:input").change(count_leaving_date);
		$(".nights").change(count_leaving_date);
	}
	
	//City find ajax
	if ( $("#CityAjax").length > 0 ) {	
		jQuery("#CityAjax").autocomplete(
			"system/application/libraries/autocomplete_cities_ajax.php",
			{
				delay:0,
				minChars:1,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:false
			}
		);
	}
	
	//On city select to "other" provide text input
	$("#SelectCity").change(
		function(){
			if ($(this).val()=='other') {				
				$("#SelectCity").remove();
				$("#CityAjax").show();
				$("#CityAjax").val('');
				$("#CityAjax").focus();
			}
		}
	);
			
	//Select number of rooms
	$(".rooms").change(
		function(){
			rooms=$(this).val();
			if (rooms>0) {
				for(i=1;i<=4;i++) {
					if  (i>rooms) $(".room"+i).hide(); else $(".room"+i).show(); 					
				}
			}
		}
	);		
	
	//Select room size	
	$(".size1").change(select_room_size);
	$(".size2").change(select_room_size);
	$(".size3").change(select_room_size);
	$(".size4").change(select_room_size);
																		
	
	//H.lt room selection	
	$(".check").click(function(){
		var price=$(this).attr("price");
		var totalprice=$(this).attr("totalprice");		
		var room_identifier=$(this).attr("room_identifier");	
		var room=$(this).attr("room");
				
		var data=$("."+room_identifier).html();	
		
		$('.'+room+'_data').html(data);
		$('.'+room+'_price').html(price);
		$('.'+room+'_totalprice').attr({value : totalprice});
		
		var sum=0;
		$(".totalprices").each(function(){
			sum=sum+$(this).attr("value")*1;			
		});
		$('.total_price').html(sum);		
	});
	
	
	//CVC info 
	$(".cvc_info_show").toggle(function() {		
		$("#cvc_desc").show('slow');
		event.preventDefault();
	}, function() {
		$("#cvc_desc").hide('slow');
		event.preventDefault();
	});
	
	//Go up
	$(".goup").click(function(event){		
		$("html,body").scrollTop(0);		
		event.preventDefault();		
	});
				
	//Request answer
	$(".request_answer").click(function() {		
		if ($('.request_answer').attr('checked')) {
			$("#request_answer_email").show();		
		} else {
			$("#request_answer_email").hide();
		}		
	});
								
});


function count_leaving_date() {	
	$(".leaving").load("system/application/libraries/leaving_count.php", {date: $(".date-pick").val(), nights: $(".nights").val()} );
}

function select_room_size() {	
	size=$(this).val();
	
	var parent1=$(this).parent();
	var parent2=parent1.parent();
		
	for(i=1;i<=4;i++) {		
		$(".beds"+i,parent2).hide();
	}	
	$(".beds"+size,parent2).show();
}


function findValue(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;	
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	//return row[0] + " (id: " + row[1] + ")";
	return row[0];
}

function lookupAjax(){
	var oSuggest = jQuery("#CityAjax")[0].autocompleter;
	oSuggest.findValue();
	return ret_false();	
}

function lookupLocal(){
	var oSuggest = jQuery("#CityLocal")[0].autocompleter;
	oSuggest.findValue();
	return ret_false();
}	


//Open Object Popup
function openObjectPopup(href) {
	var href = href + ',iestuff.'+(new Date()).getTime();		
	popup_size=windowSize()-120;		
	jQuery('#object_info').load(href).css("height",popup_size).show();		
	if (href.match(/#map/)=='#map') map=1; else map=0;
	if (href.match(/#comments/)=='#comments') comments=1; else comments=0;
	return ret_false();
}

//Send friend popup
function sendFriend(href,id) {
	$("#"+id).show().load(href);
	return ret_false();
}


//Paging
function changePage(page_no,direction) {	  
	  
	  if (!isdefined('current_page')) current_page=1;
	  	  
	  if (direction) {
		if (direction=='+') var page_no=current_page-0+1;  		    		
		if (direction=='-') var page_no=current_page-0-1;		
	  }
	 	 
	  if (page_no<total_pages) $(".next_page").show(); else $(".next_page").hide();
	  if (page_no>1) $(".previous_page").show(); else $(".previous_page").hide();
		 
	  $(".page"+current_page).hide();
	  
	  $(".page"+page_no).show();	  
	  		  
	  $("#page_link_"+current_page).removeClass("selected");
	  $("#page_link_"+page_no).addClass("selected");
	  
	  current_page=page_no;
	  return ret_false();
}

//Photos
function changePhoto(photo,id,photo_nr,direction) {
	  if (direction) {
		var total_photos=$("#object_"+id+" #dots_"+id).attr('nrphotos');		
		current_photo_nr=$("#object_"+id+" #photo_"+id).attr('nr');		
		if (direction=='+') {
			var photo_nr=current_photo_nr-0+1;
			if (photo_nr>=total_photos) var photo_nr=0;
		}
		if (direction=='-') {
			var photo_nr=current_photo_nr-0-1;			
			if (photo_nr<0) var photo_nr=total_photos-0-1;			
		}
		var photo=$("#object_"+id+" .photo_"+id+"_"+photo_nr).attr('href');		
	  }
	  
	  $("#object_"+id+" #photo_"+id).attr({src : photo});
	  $("#object_"+id+" #photo_"+id).attr({nr : photo_nr});	  

	  return ret_false();
}    

	

//Send friend popup
function sendFriend(href,id) {
	$("#"+id).show().load(href);
	return ret_false();
}


//Paging
function changePage(page_no,direction) {
	   
	  if (!isdefined('current_page')) current_page=1;
	  	  
	  if (direction) {
		if (direction=='+') var page_no=current_page-0+1;  		    		
		if (direction=='-') var page_no=current_page-0-1;		
	  }
	 	 
	  if (page_no<total_pages) $(".next_page").show(); else $(".next_page").hide();
	  if (page_no>1) $(".previous_page").show(); else $(".previous_page").hide();
		 
	  $(".page"+current_page).hide();	  
	  $(".page"+page_no).show();	  
	  		  
	  $("#page_link_"+current_page).removeClass("selected");
	  $("#page_link_"+page_no).addClass("selected");
	  
	  current_page=page_no;
	  
	  $(".goup").trigger("click");
	  return ret_false();
}

//Photos
function changePhoto(photo,id,photo_nr,direction) {
	  if (direction) {
		var total_photos=$("#object_"+id+" #dots_"+id).attr('nrphotos');		
		current_photo_nr=$("#object_"+id+" #photo_"+id).attr('nr');		
		if (direction=='+') {
			var photo_nr=current_photo_nr-0+1;
			if (photo_nr>=total_photos) var photo_nr=0;
		}
		if (direction=='-') {
			var photo_nr=current_photo_nr-0-1;			
			if (photo_nr<0) var photo_nr=total_photos-0-1;			
		}
		var photo=$("#object_"+id+" .photo_"+id+"_"+photo_nr).attr('href');		
	  }
	  
	  $("#object_"+id+" #photo_"+id).attr({src : photo});
	  $("#object_"+id+" #photo_"+id).attr({nr : photo_nr});	  
	  $("#object_"+id+" #dots_"+id+" a").removeClass("selected");
	  $("#object_"+id+" .photo_"+id+"_"+photo_nr).addClass("selected");
	  return ret_false();
}    

		
	
function windowSize() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	  //window.alert( 'Width = ' + myWidth );
	  //window.alert( 'Height = ' + myHeight );
	  return myHeight;
}



function isdefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function ret_false() {
	try
	 {
	  // VISTA IE 7
	  window.event.returnValue = false;
	 } catch (error)
	 {
	  // ALL OTHER (MOZILLA, OPERA, SAFARI, IE 6 & 7 ON XP)
	  return false;
	 }	
}

