$(document).ready(function() {
  $("a.zoom").fancybox();
  
  if(openedElement!='') showHideExtendedMenu(openedMain, openedElement, openedParent);
  
  $('#user_country_id').bind('change', changeUserCountry);
  
  $("#q").autocomplete({
		serviceUrl: url + 'index.php?choice=search&action=jsonSearchGood',
		minChars:3,
		maxHeight:'500px',
		width: '300px',
		noCache: false, 
		onSelect:
			function(value, data){
			var goodUrl = url + 'index.php?choice=good&action=getGood&good_id=' + data.good_id;
				window.location.replace(goodUrl);
			},
			fnFormatResult:
			function(value, data, currentValue){
				var goodUrl = url + 'index.php?choice=good&action=getGood&good_id=' + data.good_id;
				var str = '<div class="image"><a href="' + goodUrl + '"><img src="' + url + 'images/goods/mini' + data.good_id + '.jpg"/></a></div>'
				+ '<div class="detail">'
					+ '<div class="title"><a href="' + goodUrl + '">' + data.title + '</a></div>' 
					+ '<div class="price">' + data.priceView + '</div>'
				+ '</div>';
				return str;
			}

  });  
  
});


  function showHideExtendedMenu(main_id, element_id, parent_id){
			var display = $('#' + element_id).css('display');
			//alert(display);
			if(display=='none'){
				$('[id^=good_cat_]').css('display', 'none');
				if(main_id!=''){ 
					$('[id^=main_good_cat_]').css('background-image', 'url(images/button_cat.gif)');
					$('#' + main_id).css('background-image', 'url(images/button_cat2.gif)');
				}	

				if(parent_id!='') $('#' + parent_id).css('display', 'block');
				$('#' + element_id).css('display', 'block');

			} else {
				$('#' + element_id).css('display', 'none');
				if(main_id!='')$('#' + main_id).css('background-image', 'url(images/button_cat.gif)');
			}
	}
  
  function changeUserCountry(){
	  var user_country_id = $('#user_country_id').attr('value');
	  var newLocation = window.location.href + '?' + "user_country_id=" + user_country_id;
	  if(window.location.href.indexOf('?')!=-1){
		  newLocation = window.location.href + '&' + "user_country_id=" + user_country_id; 
	  }
	  window.location.replace(newLocation);
	  
  }
  
  function getMyOrdersAfterSuccessOrder(){
	  event.preventDefault();
	  parent.$.fancybox.close();
	  alert("good");
  }
