// JavaScript Document
$(function() {
	//$('#je-menu-action-menus li').live('click',function(){selectMenuMove(this)});
	$('#je-restaurant-search-cuisine').change(function(){
	  var arrCuisine = new Array();
	  var values = $('#je-restaurant-search-cuisine-values').val();
	  if (values != '') arrCuisine = values.split(',');
	  if ($.inArray($(this).val(), arrCuisine) != -1 || $(this).val() == 0) return false;
		$('#je-restaurants-search-cuisine-select').append('<div class="sel-element" id="cuisine-'+ $(this).val() +'"><span class="je-rest-select-text">' + $(this).children('option:selected').text() +  '</span><span class="je-rest-select-action"><a href="javascript:delCuisine('+$(this).val()+')">X</a></span></div>');
		arrCuisine.push($(this).val());
		$('#je-restaurant-search-cuisine-values').val(arrCuisine.join(','));
		$('#cuisine-' + $(this).val()).slideDown(150);
		$(this).clearForm();
	});
	
	$('#je-restaurant-search-service').change(function(){
	  var arrService = new Array();
	  var values = $('#je-restaurant-search-service-values').val();
	  if (values != '') arrService = values.split(',');
	  if ($.inArray($(this).val(), arrService) != -1 || $(this).val() == 0) return false;
		$('#je-restaurants-search-service-select').append('<div class="sel-element" id="service-'+ $(this).val() +'"><span class="je-rest-select-text">' + $(this).children('option:selected').text() +  '</span><span class="je-rest-select-action"><a href="javascript:delService('+$(this).val()+')">X</a></span></div>');
		arrService.push($(this).val());
		$('#je-restaurant-search-service-values').val(arrService.join(','));
		$('#service-' + $(this).val()).slideDown(150);
		$(this).clearForm();
	});
	
  $('#je-restaurant-search-city-zip').keypress(function (e) {
  	$('#je-restaurants-search-city-value').val('');
  });	
	
 if ($('#je-restaurant-search-city-zip').length != 0) {
  $('#je-restaurant-search-city-zip').autocomplete({ 
   	serviceUrl:'index.php?mod=restaurants&act=view&what=quick&sub=cityregion2',
    minChars:1, 
    maxHeight:200,
    width:179,
   	onSelect: function(value, data){
		$('#je-restaurants-search-city-value').val(data);
		if ($.cookie('restaurantsLocationCityId') == data) return true;
		$.cookie('restaurantsLocationCityId',data, {path: "/", expires: 365});
		$.cookie('restaurantsLocationCityValue', $('#je-restaurant-search-city-zip').val(), {path: "/", expires: 365});
		$('#je-restaurants-inner-container').hide();
		$('#je-restaurants-loading').show();
		sendAjax('mod=restaurants&act=view');
	},
	beforeSend : function () {
		dataAutoComp  = {regionid : $('#je-restaurant-search-region').val()}
	}
		
   });
  }
  
  if ($.cookie('restaurantsLocationCityValue') != null) {
	   $('#je-restaurant-search-city-zip').val(decodeURI($.cookie('restaurantsLocationCityValue')));
  }
	
  if ($.cookie('restaurantsLocationCityId') != null) {
	   $('#je-restaurants-search-city-value').val(decodeURI($.cookie('restaurantsLocationCityId')));
  }
	
 /* $("#je-restaurants-loading").ajaxComplete(function(){
		$(this).height(300);
		$('#je-restaurants-loading').css({backgroundPosition:'50% 50%'});
  });*/

});

function searchRestaurant (string, value) {
	var sendString;
	
	if ( $(document).height() > $('body').height() ){
		var hB = Math.round($('body').height() / 3);
		$('#je-restaurants-loading').height($(document).height());
		$('#je-restaurants-loading').css({'backgroundPosition':'50% ' + hB + 'px' });
	}
	
	$('#je-restaurants-inner-container').hide();
	$('#je-restaurants-loading').show();
	$('#je-restaurant-search-actions button').attr('disabled',true);
	if (!string && !value) {
		sendString='mod=restaurants&act=view&what=search&' + $('#je-restaurants-search').serialize() + '&page=first';
	}
	else {
		var t = '';
		t = 'je-restaurant-search-'+string+'-values=' + value;
		switch (string){
			case 'service':
				$('#je-restaurant-search-service').val(value);
				$('#je-restaurant-search-service').change();
			break;
			case 'cuisine':
				$('#je-restaurant-search-cuisine').val(value);
				$('#je-restaurant-search-cuisine').change();
			break;
			case 'price':
				$('#je-restaurant-search-price-values').val(value);
			break;
			case 'payment':
				$('#je-restaurant-search-payment-values').val(value);
			break;
		}
		sendString='mod=restaurants&act=view&what=search&' + t + '&page=first';
	}
	
	sendAjax(sendString);
}

function nextSearchPage(id) {
	
	var hB = Math.round($('body').height() / 3);
	$('#je-restaurants-pagination-loading').height($('#je-restaurants-search-results').height());
	$('#je-restaurants-pagination-loading').css({'backgroundPosition':'50% ' + 150 + 'px' });

	var id = (parseInt(id) + 1);
	var sendString='mod=restaurants&act=view&what=search&' + $('#je-restaurants-search').serialize() + '&page='  +id + location.search.replace('?','&');
	$('#je-restaurants-search-results').hide();
	$('#je-restaurants-pagination-loading').show();
	sendAjax(sendString);
}


function clearSearchRestForm() {
	$('#je-restaurants-search').clearForm();
	$('.sel-element').remove();
}
function changeSearchElements(string,value){
	
	switch (string){
			case 'services':
				$('#je-restaurant-search-service').val(value);
				$('#je-restaurant-search-service').change();
			break;
			case 'cuisines':
				$('#je-restaurant-search-cuisine').val(value);
				$('#je-restaurant-search-cuisine').change();
			break;
			case 'prices':
				$('#je-restaurant-search-price-values').val(value);
			break;
			case 'payments':
				$('#je-restaurant-search-payment-values').val(value);
			break;
			case 'name':
				$('#je-restaurant-search-name').val(value);
			break;
			case 'address':
				$('#je-restaurant-search-address').val(value);
			break;
			case 'chef':
				$('#je-restaurant-search-chef').val(value);
			break;
		}
	
}
function delCuisine(id) {
	var values = $('#je-restaurant-search-cuisine-values').val();
	var arrCuisine = values.split(',');	
	var pos = $.inArray(id.toString(), arrCuisine);
	arrCuisine.splice(pos,1);
	$('#je-restaurant-search-cuisine-values').val(arrCuisine.join(','));
	$('#cuisine-' + id).slideUp(300, function(){
		$('#cuisine-' + id).remove();
	});
}

function delService(id) {
	var values = $('#je-restaurant-search-service-values').val();
	var arrService = values.split(',');	
	var pos = $.inArray(id.toString(), arrService);
	arrService.splice(pos,1);
	$('#je-restaurant-search-service-values').val(arrService.join(','));
	$('#service-' + id).slideUp(300, function(){
		$('#service-' + id).remove();
	});
}

function backToMainPage() {
	$('#je-restaurants-data-container').hide();
	$('#je-restaurants-loading').show();
	sendAjax('mod=restaurants&act=view');
}

function initPaginationRestaurants(id, wall_entries, start) {
  $(id).pagination(wall_entries, {
      num_edge_entries: 2,
	  prev_text: je_prev,
	  next_text: je_next,
	  current_page:start,
	  link_to:"#",
      num_display_entries: 4,
      callback: nextSearchPage,
      items_per_page:1
  });
}

function viewRestaurantMenu(tab,id){
	
if($('#'+tab).css('display') == 'none'){
	$('.je-restaurant-tab').css("display","none");
		$('#'+tab).css("display","block");
}
$('ul.je-restaurant-menu-actions li').removeClass('je-menu-active-left-tab');

$('li.'+tab).addClass('je-menu-active-left-tab');

return;
}



