jQuery.fn.selectOptions = function(value) { 
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;  
				};
			}
		}
	)
	return this;
}

/**
 *  Funkce pro nastavení počtu hvězdiček, které jsou nastavené pro vyhledávání
 */ 
function setStars(count){
  document.getElementById('stars_img').src='/images/rekreacnidomy/stars'+count+'.gif';
  document.getElementById('extended_stars_img').src='/images/rekreacnidomy/stars'+count+'.gif';
  $jq('#stars').val(count);
  $jq('#extended_stars').val(count);
}
/**
 *  Funkce pro zobrazení počtu hvězdiček po najetí myši...
 */ 
function hoverStars(count){
  if (count>0){
    //nastaveni poctu hvezdicek po najeti mysi
    document.getElementById('stars_img').src='/images/rekreacnidomy/stars'+count+'.gif';
    document.getElementById('extended_stars_img').src='/images/rekreacnidomy/stars'+count+'.gif';
  }else {
    //nastaveni poctu hvezdicek podle nastavene hodnoty
    count2=$jq('#stars').val();
    document.getElementById('stars_img').src='/images/rekreacnidomy/stars'+count2+'.gif';
    document.getElementById('extended_stars_img').src='/images/rekreacnidomy/stars'+count2+'.gif';
  }
}

function setEmpty(id){
  $jq('#'+id).val('');
}

// JavaScript Document
  function showSearchDiv(id){
    if (id=="normalSearchDiv"){
      document.getElementById("normalSearchDiv").style.display='block';
      document.getElementById("extendedSearchDiv").style.display='none';
      document.getElementById("countriesSearchDiv").style.display='none';
      document.getElementById("normalSearchDivA").style.background="url(/images/rekreacnidomy/zalozkaH.gif) no-repeat";
      document.getElementById("normalSearchDivA").style.color="yellow";
      document.getElementById("extendedSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("extendedSearchDivA").style.color="white";
      document.getElementById("countriesSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("countriesSearchDivA").style.color="white";
    }else if(id=="countriesSearchDiv") {
      document.getElementById("normalSearchDiv").style.display='none';
      document.getElementById("extendedSearchDiv").style.display='none';
      document.getElementById("countriesSearchDiv").style.display='block';
      document.getElementById("normalSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("normalSearchDivA").style.color="white";
      document.getElementById("extendedSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("extendedSearchDivA").style.color="white";
      document.getElementById("countriesSearchDivA").style.background="url(/images/rekreacnidomy/zalozkaH.gif) no-repeat";
      document.getElementById("countriesSearchDivA").style.color="yellow";
    }else {
      document.getElementById("normalSearchDiv").style.display='none';
      document.getElementById("extendedSearchDiv").style.display='block';
      document.getElementById("countriesSearchDiv").style.display='none';
      document.getElementById("normalSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("normalSearchDivA").style.color="white";
      document.getElementById("extendedSearchDivA").style.background="url(/images/rekreacnidomy/zalozkaH.gif) no-repeat";
      document.getElementById("extendedSearchDivA").style.color="yellow";
      document.getElementById("countriesSearchDivA").style.background="url(/images/rekreacnidomy/zalozka.gif) no-repeat";
      document.getElementById("countriesSearchDivA").style.color="white";
    }
  }
  /**
   * Funkce pro základní kontrolu vyhledávacího formuláře a jeho odeslání
   */     
  function submitSearchForm(id){
    if (id=="normalSearchForm"){
      if (document.getElementById("country").value=="-1"){
        window.alert("Musíte zadat zemi!");
      }else if (document.getElementById("persons").value=="-1"){
        window.alert("Musíte zadat počet osob!");
      }else {
        $jq('#'+id).submit();
      }
    }else {
      if (document.getElementById("extended_country").value=="-1"){
        window.alert("Musíte zadat zemi!");
      }else if (document.getElementById("extended_persons").value=="-1"){
        window.alert("Musíte zadat počet osob!");
      }else {
        $jq('#'+id).submit();
      }
    }
  }



/**
 * Akce pro jQuery po načtení dokumentu
 */ 
function extendedCountryChange(e){
    var country = $jq('#extended_country option:selected').val();
    
    if((country>-1)||(country=='lm1')||(country=='lm2')) {

       $jq.get('/ajax/searchformdestinations?cid='+country, function(data){
          $jq('#extended_destination').html(''); 
          
          $jq('#extended_destination').html(data);
          
          if (e=='first'){
            $jq('#extended_destination').selectOptions(defaultDestination);
          }
          
       });

    } else {
       $jq('#mesta').html('<option value="-1">(nerozhoduje...)</option>');
    }
    
    
  } 
 
$jq(document).ready(function(){

  /**
   * Kód pro nastavení destinací po vybrání země
   */   
   
  $jq('#extended_country').change(extendedCountryChange); 
  
   if (($jq('#extended_country option:selected').val()>-1)||($jq('#extended_country option:selected').val()=='lm1')||($jq('#extended_country option:selected').val()=='lm2')){
    extendedCountryChange('first');
   }
   
   
}); 


function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}