

function checkBlockingSearch(name, count) {
    var str_res = '';
    var elements = $('#'+name+' ');
    jQuery.each(elements, function() {
    	obj = this;
    	if (obj.checked) {
        	str_res = str_res+obj.value+',';
        }
    });

    return str_res;
}

function generateLinkToSearch(sort, page, currency, spec)
{
	if($("#targetCode").val()=='') { alert('Введите город, регион или название отеля'); exit; }
	sort = sort || 0;
	page = page || 1;
	spec = spec || '';

	currency = currency || 'UAH';

	generateLinkToSearchWithFilers(sort, page, currency, spec);

	/*
	var url = '/search';
	if ($('#targetCode').val()){
		url = url + '?p[target]=' + $("#targetCode").val();
	}
	if ($('#targetName').val()){
		url = url + '&p[name]=' + $("#targetName").val();
	}
	if ($("#StartDate").val()) {
		url = url + '&p[start]=' + $("#StartDate").val();
	}
	if ($("#DueDate").val()) {
		url = url + '&p[end]=' + $("#DueDate").val();
	}
	if ($("#isFree") && $("#isFree").attr('checked')) {
		url = url + '&p[free]=1';
	}
	if (sort != 0) {
		url = url + '&sort='+sort;
	}
	if (page != 1) {
		url = url + '&p[page]='+page;
	}
	if (currency != 'UAH') {
		url = url + '&p[currency]='+currency;
	}

	window.location.href = url;
	*/
}

function generateLinkToSearchWithFilers(sort, page, currency, spec)
{
	sort = sort || 0;
	page = page || 1;
	spec = spec || '';

	currency = currency || 'UAH';

	var url = '/search';
	if ($('#targetCode').val()){
		url = url + '?p[target]=' + $("#targetCode").val();
	}
	if ($('#targetName').val()){
		url = url + '&p[name]=' + $("#targetName").val();
	}
	if ($("#StartDate").val()) {
		url = url + '&p[start]=' + $("#StartDate").val();
	}
	if ($("#DueDate").val()) {
		url = url + '&p[end]=' + $("#DueDate").val();
	}
	if ($("#isFree") && $("#isFree").attr('checked')) {
		url = url + '&p[free]=1';
	}
	if (sort != 0) {
		url = url + '&sort='+sort;
	}
	if (page != 1) {
		url = url + '&p[page]='+page;
	}
	if (currency != 'UAH') {
		url = url + '&p[currency]='+currency;
	}
	if (spec != '') {
		url = url + '&f[specialoffer]='+spec;
	}

	if ($('#popular').val()){
		url = url + '&f[popular]=' + $('#popular').val();
	}
	str_prices = checkBlockingSearch('prices_');
	str_category = checkBlockingSearch('category_');
	str_objectfield = checkBlockingSearch('objectfield_');
	str_roomfield = checkBlockingSearch('roomfield_');
	str_specialization = checkBlockingSearch('specialization_');

	if (str_prices) {
		url = url + '&f[prices]=' + str_prices.substring(0, str_prices.length-1);
	}
	if (str_category) {
		url = url + '&f[category]=' + str_category.substring(0, str_category.length-1);
	}
	if (str_objectfield) {
		url = url + '&f[hotel]=' + str_objectfield.substring(0, str_objectfield.length-1);
	}
	if (str_roomfield) {
		url = url + '&f[room]=' + str_roomfield.substring(0, str_roomfield.length-1);
	}
	if (str_specialization) {
		url = url + '&f[spec]=' + str_specialization.substring(0, str_specialization.length-1);
	}
	if ($("#priceMin").val() || $("#priceMax").val()) {
		if (($("#priceMin").val() != $("#min").val()) || ($("#priceMax").val() != $("#max").val()) )
			url = url + '&f[price]=' + $("#priceMin").val() + '-' + $("#priceMax").val();
	}
	window.location.href = url;
}

function showRoomsByObject(objectId, start, end, currency)
{
	currency = currency || 'UAH';
	$("#more_load_"+objectId).show();
	jQuery.post
	(
		'/search/getrooms',
		{'p[objectId]': objectId, 'p[start]': start, 'p[end]': end, 'p[currency]': currency},
		function(data) {
	    	$('#rooms_'+objectId).html(data).show();
	    	$("#hide_"+objectId).show();
	    	$("#in_detail_"+objectId).hide();
	    	$("#more_load_"+objectId).hide();

		}
	);
}

function showAllRooms(objectIds, start, end, currency)
{
	currency = currency || 'UAH';
	var objects = objectIds.split(",");
	$.each(objects, function(n, objectId) { // выбираем содержание по очереди
	 	showRoomsByObject(objectId, start, end, currency)
	});
}



function hideRoomsByObject(objectId)
{
	$('#rooms_'+objectId).hide();
	$("#hide_"+objectId).hide();
	$("#in_detail_"+objectId).show();

}

function hideAllRooms(objectIds)
{
	var objects = objectIds.split(",");
	$.each(objects, function(n, objectId) { // выбираем содержание по очереди
	 	hideRoomsByObject(objectId)
	});

}

function refreshFilters(currency)
{
	currency = currency || 'UAH';
	$('#filters_load').show();
	$("#filters").hide();

	var free =0;
	if ($("#isFree") && $("#isFree").attr('checked')) {
		free =1;
	}

	var popular = 0;
	if ($('#popular').val()){
		popular = $('#popular').val();
	}
	str_prices = checkBlockingSearch('prices_');
	str_category = checkBlockingSearch('category_');
	str_objectfield = checkBlockingSearch('objectfield_');
	str_roomfield = checkBlockingSearch('roomfield_');
	str_specialization = checkBlockingSearch('specialization_');

	var prices = '';
	if (str_prices) {
		prices = str_prices.substring(0, str_prices.length-1);
	}

	var category = '';
	if (str_category) {
		category = str_category.substring(0, str_category.length-1);
	}
	var hotel = '';
	if (str_objectfield) {
		hotel = str_objectfield.substring(0, str_objectfield.length-1);
	}
	var room = '';
	if (str_roomfield) {
		room = str_roomfield.substring(0, str_roomfield.length-1);
	}
	var specialization = '';
	if (str_specialization) {
		specialization = str_specialization.substring(0, str_specialization.length-1);
	}
	var price = '';
	if ($("#priceMin").val() || $("#priceMax").val())
	{
		if (($("#priceMin").val() != $("#min").val()) || ($("#priceMax").val() != $("#max").val()) )
			var price = $("#priceMin").val() + '-' + $("#priceMax").val();
	}

	jQuery.post
	(
		'/ajax_functions/search.php',
		{'action': 'get_filters',
		 'p[target]': $("#targetCode").val(), 'p[start]': $("#StartDate").val(), 'p[end]': $("#DueDate").val(),
		 'p[currency]': currency, 'p[free]': free,
		 'f[category]': category, 'f[hotel]': hotel, 'f[room]': room, 'f[prices]': prices, 'f[price]': price, 'f[popular]': popular,
		 'f[spec]': specialization
		},
		function(data) {
			$('#filters_load').hide();
	    	$("#filters").html(data).show();
		}
	);
}

function getFilters(prices, price, popular, category, hotel, room, spec, currency)
{
	currency = currency || 'UAH';
	$('#filters_load').show();
	$("#filters").hide();
	var free =0;
	if ($("#isFree") && $("#isFree").attr('checked')) {
		var free =1;
	}

	jQuery.post
	(
		'/search/getfilters/',
		{'action': 'get_filters',
		 'p[target]': $("#targetCode").val(), 'p[start]': $("#StartDate").val(), 'p[end]': $("#DueDate").val(),
		 'p[currency]': currency, 'p[free]': free,
		 'f[category]': category, 'f[hotel]': hotel, 'f[room]': room, 'f[prices]': prices, 'f[price]': price, 'f[popular]': popular, 'f[specialoffer]': spec
		},
		function(data) {
			$('#filters_load').hide();
	    	$("#filters").html(data).show();
		}
	);

}

function addToMemorize(objectId)
{
	jQuery.post
	(
		'/search/addtomemorize',
		{'action': 'addtomemorize',
		 'objectId': objectId
		},
		function(data) {
			$('#add_to_memorize_'+objectId).hide();
	    	$('#delete_from_memorize_'+objectId).show();
	    	//$('#hotel_'+objectId).attr("class","nz-result nz-favourite");
	    	$('#hotel_'+objectId).addClass('nz-favourite');
	    	if (data){
	    		$('#memorize_div').show();
	    		$('#memorize_a').attr("href", '/search/memorize?p[target]=m-'+data);
	    		var ctn = data.split(',').length;
	    		$('#memorize_a').text("Мое избранное ("+ctn+")");
	    	} else {
	    		$('#memorize_div').hide();
	    	}
		}
	);
}

function deleteFromMemorize(objectId)
{
	jQuery.post
	(
		'/search/deletefrommemorize',
		{'action': 'delete_from_memorize',
		 'objectId': objectId
		},
		function(data) {
			$('#add_to_memorize_'+objectId).show();
	    	$('#delete_from_memorize_'+objectId).hide();
	    	$('#hotel_'+objectId).removeClass('nz-favourite');
	    	if (data){
	    		$('#memorize_div').show();
	    		$('#memorize_a').attr("href", '/search?action=memorize&p[target]=m-'+data);
	    		var ctn = data.split(',').length;
	    		$('#memorize_a').text("Мое избранное ("+ctn+")");
	    	} else {
	    		$('#memorize_div').hide();
	    	}
		}
	);
}

function isViewing(objectId)
{
	$('#isViewing_'+objectId).show();
}

function changeInputPrice(index, value)
{
	$("#slider-range").slider( "values" , index, value );
	refreshFilters();
}

function showStar(num)
{
	for(var i=1; i<num+1; i++){
		document.getElementById('pop_'+i).src= "/images/rating_fill.png";
	}
	for(var i=5; i>num; i--){
		document.getElementById('pop_'+i).src = "/images/rating_empty.png";
	}
	return true;
};

function hideStar(scaleId, num)
{
	var inputTag = document.getElementById("popular");
	getStar(parseInt(inputTag.value));
	return true;
};

function getStar(num)
{
	for(var i=1; i<num+1; i++){
		document.getElementById('pop_'+i).src= "/images/rating_fill.png";
	}
	for(var i=5; i>num; i--){
		document.getElementById('pop_'+i).src = "/images/rating_empty.png";
	}
	var inputTag = document.getElementById("popular");
	inputTag.value = num;
	return true;
};

function clearDates()
{
    jQuery.post('/search/cleardates', {'action': 'clearDates'}, function(){
        var tmp = new Array();      // два вспомагательных
        var tmp2 = new Array();     // массива
        var param = new Array();
        var get = location.search;  // строка GET запроса
        if(get != '') {
            tmp = (get.substr(1)).split('&');   // разделяем переменные
            for(var i=0; i < tmp.length; i++) {
                tmp2 = tmp[i].split('=');       // массив param будет содержать
                param[tmp2[0]] = tmp2[1];       // пары ключ(имя переменной)->значение
            }
        }
        if(param['p[target]']){
            window.location.href = 'search?p[target]=' + param['p[target]'];
        }
        else{
            window.location.reload();
        }
    });
}
