//Dinamic Updating Search variants
//<b>Search results  for: Joint paint + Allergies.</b> View also results for <a href="#">Joint pain (15)</a> or <a href="#">Allergies (8)</a>

function SearchResultsVariantStringClick(link, is_old_results) {
	if(link.css('text-decoration') != 'underline') {
		jQuery('div#UVarianString').find('a').css({
			'text-decoration':'none',
			'color':'#555555'
		});
		link.css({
			'text-decoration':'underline',
			'color':'green'
		});
		if(!is_old_results) {
			//alert('Old results!');
			jQuery.ajax({
				type: 'POST',
				cache: false,
				url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=return_search_medical_result',
				success: function(data) {
					SearchResultGetAndUpdateHotels();
				}
			});
		} else {
			//alert('New result!');
			jQuery.ajax({
				type: 'POST',
				cache: false,
				url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=new_search_medical_result&id=' + is_old_results,
				success: function(data) {
					if(data != '0') {
						SearchResultGetAndUpdateHotels();
					} else {
						alert('Error!');
					}
				}
			});
		}
	}
}

function SearchResultShowVarianString() {
	//jQuery('div#UVarianString').html('Test!');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_updated_variant_string',
		success: function(data) {
			if(data != '0') {
				jQuery('div#UVarianString').html(data);
				jQuery('div#UVarianString').find('a:first').css({
					'text-decoration':'underline',
					'color':'green'
				});
			} else {
				jQuery('div#UVarianString').html('');
			}
		}
	});
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Dinamic Updating Tour Types
function SearchResultsPrepareTourTypes() {
	var BoxCaps = jQuery('div#tt-caps');
	var BoxTables = jQuery('div#tt-tables');
	
	BoxCaps.find('a:first').addClass('current');
	BoxTables.find('div.hotel-tour-type-rooms:first').css('display', 'block');
	
	//Bind Click Event
	BoxCaps.find('a').each(function(i) {
		jQuery(this).click(function() {
			if(!jQuery(this).hasClass('current')) {
				BoxCaps.find('a').removeClass('current');
				jQuery(this).addClass('current');
				BoxTables.find('div.hotel-tour-type-rooms').css('display', 'none');
				BoxTables.find('div.hotel-tour-type-rooms').each(function(j) { if(i == j) { jQuery(this).css('display', 'block'); } });
			}
			return false;
		});
	});
}

function SearchResultGetAndUpdateTourTypes() {
	var UpdateBox = jQuery('div#sp4-capts-tour-types');
	
	jQuery('img#AjaxLoadAnimationStep4').css('display', 'block');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_hotel_tour_types&id=1',
		success: function(data) {
			if(data != '0') {
				UpdateBox.html(data);
				SearchResultsPrepareTourTypes();
			} else {
				UpdateBox.html('Not found!');
			}
			jQuery('img#AjaxLoadAnimationStep4').css('display', 'none');
		}
	});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function SearchResultAjaxSelectHotel(_id) {
	jQuery('img#AjaxLoadAnimationStep4').css('display', 'block');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=select_hotel&id=' + _id,
		success: function(data) {
			jQuery('img#AjaxLoadAnimationStep4').css('display', 'none');
			SearchResultGetAndUpdateTourTypes();
		}
	});
}

function SearchResultLinkClick(_object) {
	if(!_object.hasClass('curr')) {
		SearchResultAjaxSelectHotel(_object.attr('href'));
		
		var list = jQuery('div#sp4-capts-capts a');
		list.each(function() {
			jQuery(this).removeClass('curr');
		});
		_object.addClass('curr');
	}
	
	//Click Show Page
	var list = jQuery('div#sp4-capts-capts a');
	list.each(function(i) {
		//jQuery(this).removeClass('curr');
		if(jQuery(this).hasClass('curr')) {
			var _mm = i;
			jQuery('div#sp4-capts-pages div.one-block').each(function(j) {
				if(j == _mm) {
					jQuery(this).css('display', 'block');
				} else {
					jQuery(this).css('display', 'none');
				}
			});
		}
	});
	return false;
}

function SearchResultPrepare() {
	//Links
	var list = jQuery('div#sp4-capts-capts a');
	list.each(function() {
		jQuery(this).click(function() {
			return SearchResultLinkClick(jQuery(this));
		});
		jQuery(this).removeClass('curr');
	});
	jQuery('div#sp4-capts-capts a:first').addClass('curr');
	
	//Pages
	jQuery('div#sp4-capts-pages div.one-block').each(function() {
		jQuery(this).css('display', 'none');
	});
	jQuery('div#sp4-capts-pages div.one-block:first').css('display', 'block');
}

function SearchResultUpdateHotels(data) { jQuery('div#sp4-capts-capts').html(data); }
function SearchResultUpdatePages(data) { jQuery('div#sp4-capts-pages').html(data); }

function SearchResultGetAndUpdateHotels() {
	jQuery('img#AjaxLoadAnimationStep4').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_hotels',
		success: function(data) {
			SearchResultUpdateHotels(data);
			//SearchResultPrepare();
			jQuery('img#AjaxLoadAnimationStep4').css('display', 'none');
			SearchResultGetAndUpdatePages();
		}
	});
}

function SearchResultGetAndUpdatePages() {
	jQuery('img#AjaxLoadAnimationStep4').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_pages',
		success: function(data) {
			SearchResultUpdatePages(data);
			SearchResultPrepare();
			SearchResultGetAndUpdateTourTypes();
			jQuery('img#AjaxLoadAnimationStep4').css('display', 'none');
		}
	});
}

function SearvhResultsAjaxAddAndRefresh(_id, _object) {
	jQuery('img#AjaxLoadAnimationStep4').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=add_some_medcine&id=' + _id,
		success: function(data) {
			jQuery('img#AjaxLoadAnimationStep4').css('display', 'none');
			
			if(data != '1') {
				alert('Error: ' + data);
				_object.fadeIn('fast');
			} else {
				//ClearAll
				_object.css({
					'color':'red',
					'text-decoration':'underline'
				});
				_object.fadeIn('slow');
				jQuery('div#sp4-capts-capts').html('');
				jQuery('div#sp4-capts-pages').html('');
				SearchResultGetAndUpdateHotels();
			}
		}
	});
}

function SearvhResultsBindSRAddMedcine() {
	jQuery('a.add-sr-for-add').each(function() {
		jQuery(this).click(function() {
			var _id = jQuery(this).attr('href');
			SearvhResultsAjaxAddAndRefresh(_id, jQuery(this));
			jQuery(this).fadeOut('slow');
			return false;
		});
	});
}

function SearchResultAddOption(_object) {
	jQuery('img#AjaxLoadAnimationStep5').css('display', 'block');
	var _id = _object.attr('name');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=add_some_option&id=' + _id,
		success: function(data) {
			if(data != '0') {
				SearchResultShowVarianString();
				SearchResultGetAndUpdateHotels();
			} else {
				alert('Error!');
			}
			jQuery('img#AjaxLoadAnimationStep5').css('display', 'none');
		}
	});
}

function SearchResultRemoveOption(_object) {
	jQuery('img#AjaxLoadAnimationStep5').css('display', 'block');
	var _id = _object.attr('name');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-search-results.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=remove_some_option&id=' + _id,
		success: function(data) {
			if(data != '0') {
				SearchResultShowVarianString();
				SearchResultGetAndUpdateHotels();
			} else {
				alert('Error!');
			}
			jQuery('img#AjaxLoadAnimationStep5').css('display', 'none');
		}
	});
}

function SearchResultBindOptions() {
	var CheckBoxList = jQuery('input[type=checkbox].hotel-option-checkbox');
	CheckBoxList.each(function() {
		jQuery(this).click(function() {
			if(jQuery(this).attr('checked')) {
				SearchResultAddOption(jQuery(this));
			} else {
				SearchResultRemoveOption(jQuery(this));
			}
		});
	});
}

jQuery(document).ready(function() {
	SearchResultBindOptions();
	SearchResultGetAndUpdateHotels();
	//SearvhResultsBindSRAddMedcine();
	SearchResultShowVarianString();
});
