var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}



document.onclick = jsddm_close;

function findValue(li) {
	if( li == null ) return alert("Nie znaleziono miasta");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

}

function displayWindow(url, width, height) {
    var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
}


function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	
	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#city")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function lookupLocal(){
	var oSuggest = $("#CityLocal")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

$(document).ready(function() {
	
	$('#jsddm > li').bind('mouseover', jsddm_open)
	   $('#jsddm > li').bind('mouseout',  jsddm_timer)
	
 // hides the slickbox as soon as the DOM is ready
 // (a little sooner than page load)
  $('#slickbox').hide();
 // shows the slickbox on clicking the noted link
  $('a#slick-show').click(function() {
 $('#slickbox').show('slow');
 $('#slick-show').hide();
 $('#slick-hide').show();
 return false;
  });
 // hides the slickbox on clicking the noted link
  $('a#slick-hide').click(function() {   
 $('#slickbox').hide('fast');
  $('#slick-hide').hide();
 	$('#slick-show').show();
 	$('input[class=adv]').attr('checked', false);
 return false;
  });
 // toggles the slickbox on clicking the noted link
  $('a#slick-toggle').click(function() {
 $('#slickbox').toggle(400);
 return false;
  }),
  
  $("#city").autocomplete(
			"http://www.zaginal.pl/cities/get_cities",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
  
  $("#miasto").autocomplete(
			"http://www.zaginal.pl/cities/get_cities",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);

		
});


