// JavaScript Document
$(function() {// <------- OnLoad
	$("#tabs").tabs().find(".ui-tabs-nav").sortable({axis:'x'});
	$('#contact_feedback').contactable({
	 		subject: 'A Feeback Message'
	 	});

	$(".abovemsg").mouseover(function() {
			$(this).feedback($(this).val(), {duration: 3000, above: true,below: true  });
	}).mouseout(function(){
			$('ul.facilities,li.a p,li.a div').remove(); 
	});

	$(" ul.data li.a input").mouseover(function() {
		id = $(this).val();
			$('#disc-'+id).hide(function(){$('#fac-'+id).show('slow');});
	}).mouseout(function(){
				$('#fac-'+id).hide(function(){$('#disc-'+id).show('slow');});
	});

	$("#keyword").focus(function() {
	if( this.value == this.defaultValue ) {
		this.value = "";
	}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

	$('#top_hotel-head h3.top_spa').click(function(){
		$('#top_hotel-thailand #top_hotel-head').css("background-image", "url(photo/thailand-13_b.png)"); 
		AjaxPage('#top_hotel-thailand #list','top_spa.php');
	});
	$('#top_hotel-head h3.top_hotel').click(function(){
		$('#top_hotel-thailand #top_hotel-head').css("background-image", "url(photo/thailand-13.png)");
		AjaxPage('#top_hotel-thailand #list','top_hotel.php');  
	});

	$('#agency_boxlist input.agency,#result_list input.promo').click(function(){
		SearchPromotion();
	});

	$('#search_box #city').change(function(){
		$('#my_area_id').load(Cache('action.php?action=chk_box_area&city_my_id='+$(this).val()));
		$('#hotel_box').load(Cache('action.php?action=chk_box_hotel&city_my_id='+$(this).val()));
		//SearchHotel();
	});
	
	$('select#currency,select#sort_by,select#per_page').change(function(){
		//SearchHotel();
	});
	
	
	
	$('#all_agency').click(function(){
		if($('#all_agency').is(':checked')){
			$('#agency input.agency').attr('checked',true);
		}else{
			$('#agency input.agency').attr('checked',false);
		}
		SearchPromotion();
	});

	$('#agency_boxlist input').attr('checked',true);
	$('#result_list input').attr('checked',true);
	$('#promo_all').click(function(){
		if($('#promo_all').is(':checked')){
			$('#result_list input').attr('checked',true);
		}else{
			$('#result_list input').attr('checked',false);
		}
		SearchPromotion();
	});
	
	$("div#hotel_list #search_box ul li select.fix").mousedown(function(){
        if($.browser.msie) {
                $(this).css("width","auto");
        }
	});
	$("div#hotel_list #search_box ul li select.fix").change(function(){
			if ($.browser.msie) {
					$(this).css("width","113px");
			}
	});
});

function ChangeAreaBox(area_no){
	
	$.ajax({
		  type: 'POST',
		  url: 'action.php',
		  data:'action=chk_box_area_hotel&city_my_id='+$('select#city').val()+'&area_no='+area_no+'&cache='+Math.random(),
		  success: function(data) {
			//  alert(data);
			$('#hotel_box').html(data);
		//	SearchHotel();
		  }
		});
	
	//$('#hotel_box').load(Cache('action.php?action=chk_box_area_hotel&city_my_id='+$('select#city').val()+'&area_no='+area_no));
}
function Cache(action){
	if(action.indexOf("?") < 0){
		var	parameter = '?';
	}else{
		var parameter = '&';
	}  
	return action+parameter+'cache='+Math.random();
}

function AjaxPage(div_id,action){
		$("#"+div_id).html('<img src=\"photo/loading.gif\"/> Loading...');
			$("#"+div_id).load(Cache(action));
}
function GetPage(div_id,action){
	$(div_id).html('<div class=\"loading\"> Loading...</div>');
	$(div_id).load(Cache(decodeURIComponent(action)));
}
function ToPage(page){
	location.href = page;
}

function SearchHotel(){
		var aff = [];
		$("input[name^='aff_name']").each(function() {
			if (this.checked) { aff.push(this.value); }
		});
		
		var promo = [];
		$("input[name^='promo']").each(function() {
			if (this.checked) { promo.push(this.value); }
		});	
	if(promo.length==5){promo='';}
	var data = $("#search_box form").serialize();
	$('#hotel_list div#list').html('<div class=\"loading\"> Loading...</div>');
		$.ajax({
		  type: 'POST',
		  url: 'hotel_list_data.php',
		  data: decodeURIComponent(data)+'&aff='+aff+'&promo='+promo+'&currency_name='+$('form #currency :selected').text()+'&keyword='+$('#keyword').val()+'&cache='+Math.random(),
		  success: function(hotel_list) {
			$('#hotel_list div#list').html(hotel_list);
			
			location_search = $('#search_box h2 select :selected').text().toUpperCase();
			if(location_search ==""){key='All Location';}
			else{key="'"+location_search+"'";}
			$('#hotel_search h3').html($('#count_hotel').val()+' Results found in '+key);
		  }
		});
}

function Vote(city){
	$.ajax({
		  type: 'POST',
		  url: 'vote.php',
		  data: 'v_no='+city+'&cache='+Math.random(),
		  success: function(data) {
			$('#vote_list').html(data);
		  }
		});
}

function SearchPromotion(){
		var aff = [];
		$("input[name^='aff_name']").each(function() {
			if (this.checked) { aff.push(this.value);}
		});
		
		var promo = [];
		$("input[name^='promo']").each(function() {
			if (this.checked) { promo.push(this.value); }
		});	
		count_aff=aff.length;
		count_promo=promo.length;
	if(count_aff!=5){
			$('#agency_boxlist input#all_agency').attr('checked',false);
	}else{
			$('#agency_boxlist input#all_agency').attr('checked',true);
	}
	if(count_promo!=5){
		$('#result_list input#promo_all').attr('checked',false);
	}else{
		$('#result_list input').attr('checked',true);
	}

	//if(promo.length==5){	$('#result_list input').attr('checked',true);}
	if(count_aff>=1){
	//alert(aff.length);
		if(promo.length==5){promo='';}
		//if(promo.length<5){$('#result_list input#promo_all').attr('checked',false);}

		var data = $("#search_box form").serialize();
		$('#hotel_list div#list').html('<div class=\"loading\"> Loading...</div>');
		$.ajax({
		  type: 'POST',
		  url: 'hotel_list_data.php',
		  data: decodeURIComponent(data)+'&aff='+aff+'&promo='+promo+'&cache='+Math.random(),
		  success: function(hotel_list) {
			$('#hotel_list div#list').html(hotel_list);

			location_search = $('#search_box h2 select :selected').text().toUpperCase();
			if(location_search ==""){key='All Location';}
			else{key="'"+location_search+"'";}
			$('#hotel_search h3').html($('#count_hotel').val()+' Results found in '+key);

		  }
		});
	}
	
}

function SearchKeyword(){
		if($('#keyword').val()!="Hotel in Thailand"){
			location.href  = 'HotelSearch-'+$('#search #keyword').val().replace(" ", ",");
		}else{
			alert('Please input keyword');
		}
}

function ChangeLocation(){
		var link = 'hotels-'+$('select#city :selected').text();


		$.ajax({
		  type: 'POST',
		  url: 'action.php',
		  data: ({
			action:"ReCityURL", 
			city_name :$('select#city :selected').text(),
			cache: Math.random()
		 }),
			success: function(data) {
				//alert('hotels-'+data+'.'+$('select#city').val());
					location.href = 'hotels-'+data+'.'+$('select#city').val();
		   }
		});


}
