

$(document).ready(function() { 
  hladat();
  rozcestnik_hp();
  height_page_bg();
  print_kalkulator();
  
  	Cufon.replace('#tag_could_hp a',{ hover:true, fontFamily: 'Nimbus Sans D OT' });
	Cufon.replace('#tag_could_page a',{ hover:true, fontFamily: 'Nimbus Sans D OT' });
	Cufon.replace('.chalkduster',{ fontFamily: 'Chalkduster' });
	
	//var segment = $(":radio[name='segment']:checked");
	var segment = $("input:radio[name='segment']:checked").val();
	//console.log(segment);
});
 


// ----------------- EUROKALKULACKA ------------------------
var konverzni_kurz_euro_skk = 30.126;
var ek_focus_item_id = "input#ek_eur";
// ----------------- EUROKALKULACKA KONEC ------------------------

$(document).ready(function(){

  controls_set_functionality();

  $.ajaxSetup({
    complete: function(XMLHttpRequest, textStatus) {
      controls_set_functionality(); // the options for this ajax request
    }
  });

  // ----------------- GOOGLE ANALYTICS ---------------------
  try {
    var pageTracker = _gat._getTracker("UA-6976808-1");
    pageTracker._trackPageview();
  } catch(err) {}
  // ----------------- GOOGLE ANALYTICS KONEC ---------------------


  $("a.t_out").live("click", function(){
    open($(this).attr("href"));
    return false;
  });

  $('div.showbox').hide();
  $("a.showbox").live("click", function() {    
    var href = $(this).attr("href");
    var box_id = href.substring(href.indexOf('#'));
    if ( $(this).hasClass('show_only') ) {
      $(box_id).show("fast");
    }
    else 
    {    
      
      var heightbox = $(box_id).height();
      
       
      
      if ($(box_id).css('display') == 'block')
      {
        height_page_bg(heightbox,'block');    
      }
      else
      {
        height_page_bg(heightbox,'none');
      }
      
      
      $(box_id).toggle("fast");
    }
    if ( !$(this).hasClass('jump_enabled') ) {
      return false;
    }
    
  });
  $("a.hidebox").live("click", function() {
    var href = $(this).attr("href");
    var box_id = href.substring(href.indexOf('#'));
     var heightbox = $(box_id).height();
      
     if ($(box_id).css('display') == 'block')
     {
       height_page_bg(heightbox,'block');    
     }
     else
     {
       height_page_bg(heightbox,'none');
     }
    $(box_id).hide("fast");
    return false;
  });


  /**
   * ----------------- FORMULARE OBECNE ------------------------
   */
  $("input.numbersonly").live("keypress", function(e){
    return numbersonly(e, false);
  });

  $("input.numbersonly_dec").live("keypress", function(e){
    return numbersonly(e, true);
  });

  $("input.psconly").live("keypress", function(e){
    return psconly(e);
  });

  $("input.static").live("keydown", function(e){
     return false;
  });
  /**
   * ----------------- FORMULARE OBECNE KONEC ------------------------
   */
  
  // ----------------- EUROKALKULACKA ------------------------
  $("input#ek_eur").keyup(function(e){
    ek_refresh("input#ek_eur","input#ek_skk");
    ek_focus_item_id = "input#ek_eur";
  });

  $("input#ek_skk").keyup(function(e){
    ek_refresh("input#ek_skk","input#ek_eur");
    ek_focus_item_id = "input#ek_skk";
  });
  $("input#ek_eur").click(function(){
    ek_focus_item_id = "input#ek_eur";
  });
  $("input#ek_skk").click(function(){
    ek_focus_item_id = "input#ek_skk";
  });

  $("input#ek_skk").keyup(function(e){
    ek_refresh("input#ek_skk","input#ek_eur");
    ek_focus_item_id = "input#ek_skk";
  });
  
  $("button.ek_button").click(function(){
    var value = $(this).html();
    $(ek_focus_item_id).val($(ek_focus_item_id).val()+value);
    if ( ek_focus_item_id=="input#ek_eur" ) {
      ek_refresh("input#ek_eur","input#ek_skk");
    }
    else if ( ek_focus_item_id=="input#ek_skk" ) {
      ek_refresh("input#ek_skk","input#ek_eur");
    }
    return false;
  });
  $("button.ek_clear").click(function(){
    $("input#ek_eur").val('');
    $("input#ek_skk").val('');
    return false;
  });
  
  $("button#ek_SKK-EUR").click(function(){ ek_refresh("input#ek_skk","input#ek_eur"); return false; });
  $("button#ek_EUR-SKK").click(function(){ ek_refresh("input#ek_eur","input#ek_skk"); return false; });
  // ----------------- EUROKALKULACKA KONEC ------------------------

});

function controls_set_functionality() {

  $("#right_col *:first").css("margin-top","0"); /*kvoli IE6*/

  /**
   * pokud je více tabulek na stránce toto funguje pouze pro první
   * $("table.zoznam tbody tr:first ~ tr:even").addClass('even'); sude riadky pouze v prvej urovni potomkov
   *
   * toto funguje pro více tabulek:
   * $("table.zoznam").children("tbody") je uvedeno proto, že chceme pouze přímé potomky "tbody"
   * pokud by bylo uvedeno $("table.zoznam tbody") zachytily by se i případné vnořené tbody prvky vnořených tabulek
   *
   * tento kód dostačuje pro tabulky, kde nejsou definovány žádné rowspan atributy
   * $("table.zoznam").children("tbody").each(function(){
   *   $(this).children("tr:odd").addClass('even'); sude riadky pouze v prvej urovni potomkov
   * });
  */
  /**
   * tento kód přidá k sudým řádkům tabulky třídy "zoznam" třídu "even"
   * počítá s možným výskytem atributů rowspan
   */
  $("table.zoznam").children("tbody").each(function(){
    var skip_rows = 0; /*počet řádků, které se mají přeskočit - liché*/
    var fold_rows = 0; /*počet řádků, které se mají vysvítit (přiřadit k sudému)*/
    var tr_order = 0; /*pořadí řádku, podle kterého se počítá, je-li lichý nebo sudý*/
    $(this).children("tr").each(function(){ /*zajímají nás řádky pouze v první úrovni potomků*/
      if ( skip_rows>0 ) { skip_rows--; } /*musí být uvedeno >0 ( nestačí if ( fold_rows ) ) - Firefox, Opera*/
      else if ( fold_rows>0 ) { /*musí být uvedeno >0 ( nestačí if ( fold_rows ) ) - Firefox, Opera*/
        $(this).addClass('even');
        fold_rows--;
      }
      else {
        tr_order++;
        var even = (tr_order%2) ? false : true;
        
        /*zjistí se maximální rowspan u jakékoli buňky řádku a podle toho se následující řádky buď přeskakují nebo zahrnují*/
        var rowspan_max = 0;
        $(this).children("th,td").each(function(){
          if ( $(this).attr('rowspan') && parseInt($(this).attr('rowspan'))>rowspan_max ) {
            rowspan_max = parseInt($(this).attr('rowspan'));
          }
        });
        
        if ( even ) {
          $(this).addClass('even');
          fold_rows = rowspan_max - 1;
        }
        else {
          skip_rows = rowspan_max - 1;
        }
      }
    });
  });
  
  /**
   * pokud je více tabulek na stránce toto funguje pouze pro první
   * $("table.zoznam thead tr:last").addClass('last');
   *
   * toto funguje pro více tabulek:
   */
  $("table.zoznam").children("thead").each(function(){
    $(this).children("tr:last").addClass('last');
  });
  
  $("table.zoznam thead th.vertical_stripe").each(function(){
    //var th_pos = $(this).prevAll().length;
    var th_pos = 0;
    var rowspans = new Array();
    $(this).prevAll().each(function(){
      th_pos+= ( $(this).attr('colspan') ) ? parseInt($(this).attr('colspan')) : 1;
      if ( $(this).attr('rowspan') && parseInt($(this).attr('rowspan'))>1 ) { rowspans.push(parseInt($(this).attr('rowspan'))); }
    });
    /**
     * vysvícení dalších <th> v <thead>
     */
    var tr_parent = $(this).parents("tr")[0];
    var tr_idx = 2;
    $(tr_parent).nextAll().each(function(){
      var position = 0;
      for ( i=0; i<rowspans.length; i++ ) {
        if ( tr_idx<=rowspans[i] ) { position++; }
      }
      $(this).children().each(function(){
        if ( position++ == th_pos ) {
          $(this).addClass('vertical_stripe');
        }
      });
      tr_idx++;
    });
    /**
     * vysvícení <td> v <tbody>
     */
    var table_parent = $(this).parents("table")[0];
    $(table_parent).children("tbody").children("tr").each(function(){
      var tds = $(this).children()[th_pos];
      $(tds).addClass('vertical_stripe');
    });
  });



  // ----------------- FORMULARE obecne ------------------------------
  $("input.ddv").focus(function(){
    if ( !$(this).attr('default_value') || $(this).attr('default_value')==$(this).val() ) {
      $(this).attr('default_value',$(this).val());
      $(this).val('');
    }
  });
  $("input.ddv").blur(function(){
    var value = $(this).val();
    var default_value = $(this).attr('default_value');
    if ( value.length==0 && default_value && default_value.length>0 ) {
      $(this).val(default_value);
    }
  });

  /*kulaté rohy v blokových elementech .fields_box_opt*/
  $(".fields_box_opt, .fields_box_obl").each(function(){
    var block_position = $(this).css('position');
    if ( block_position == 'static' ) {
      $(this).css('position','relative');
    }

    /*IE6*/
    if ( $.browser.msie && parseInt($.browser.version) <= 6 && $(this).css('float') == 'none' ) {
      var height = $(this).height();
      $(this).css('height', height + 'px');
    }

    $(this).append('<div class="corner lt"></div><div class="corner lb"></div><div class="corner rt"></div><div class="corner rb"></div>');
  });

  $("button.tisk").click(function(){
    window.print();
    return false;
  });
  // ----------------- FORMULARE obecne KONEC------------------------------


  /**
   * zobrazení hlášek v boxech, které je obsahují
   */
  $('div.js_alert_msg').each(function(){
    if ( !$(this).attr('showed') ) {
      var content = $(this).html();
      var js_content = content.replace(/\\n/,'\n');
      alert(js_content);
      $(this).attr('showed','1');
    }
  });
  
}



/**
 * @Name: ek_refresh(source_id,target_id)
 * @Dependent on: jQuery library, function add_thousand_sep, function number_format, function round
 * @Author: Petr Novotný
 * @Date: 20.9.2008
 * @Description: Aktualizuje údaje v Eurokalkulačce
 * @param string source_id   ID prvku s hodnotou, která má být přepočítaná
 * @param string target_id   ID prvku kam se uloží přepočítaná hodnota
 */
function ek_refresh(source_id,target_id) {
  //hodnota ze zdrojového prvku
  var cislo = $(source_id).val();
  //odstranění počátečních nul (za kterými následuje číslo), čárek a teček, převedení čárky na desetinnou tečku, odstranění mezer
  cislo = cislo.replace(/^0+(\d)/g, '$1').replace(/^[,\.]+/g, '').replace(/,/g, '.').replace(/ /g, '');

  if ( is_float(cislo) ) {
    var numberparts = cislo.split('.',2); //rozdělení na celočíselnou a desetinnou část
    var formated_str = add_thousand_sep(numberparts[0],' '); //přidá se oddělovač tisíců do celočíselné části
    if ( numberparts.length==2 ) { formated_str+= "," + numberparts[1]; } //pokud existuje desetinná část, přidá se k řetězci
    $(source_id).val(formated_str); //zobrazí se upravený řetězec

    //výpočet přepočtu
    var result = ( source_id=="input#ek_eur" ) ? round(cislo * konverzni_kurz_euro_skk,2) : round(cislo / konverzni_kurz_euro_skk,2);
    
    $(target_id).val(number_format(result,2,',',' ')); //výsledek se zformátuje a zobrazí
  }
  else if ( cislo.length==0 ) { //pokud je ve zdrojovém prvku prázdný řetězec, cílový prvek se vynuluje
    $(target_id).val('');
  }
}

/**
 * @Name: number_format(number,decimals,dec_point,thousand_sep)
 * @Dependent on: function add_thousand_sep
 * @Author: Petr Novotný
 * @Date: 20.9.2008
 * @Description: Vrátí číslo (jako řetězec) v požadovaném formátu, inspirováno stejnojmennou PHP funkcí
 * @param float number          Číslo
 * @param int decimals          Počet desetinných míst
 * @param string dec_point      Oddělovač desetinných míst
 * @param string thousand_sep   Oddělovač tisíců
 */
function number_format(number,decimals,dec_point,thousand_sep) {

  number = parseFloat(number);
  
  if ( !decimals ) { decimals = 0; }
  if ( !dec_point ) { dec_point = ','; }
  if ( !thousand_sep ) { thousand_sep = ''; }

  var multi = Math.pow(10,decimals); //Zaokrouhlení na požadovaný počet desetinných míst
  var rounded = Math.round(number*multi) / multi;
  
  var rounded_str = rounded + ""; //převod na řetězec
  var numberparts = rounded_str.split('.',2); //rozdělení na celočíselnou a desetinnou část
  var intpart_str = numberparts[0]; //celočíselná část
  var decpart_str = '';
  if ( numberparts[1] ) { decpart_str = numberparts[1]; } //pokud je desetinná část
  else { //pokud není desetinná část, tak se vytvoří
    for ( var i=1;i<=decimals;i++ ) { decpart_str+= '0'; }
  }
  
  if ( thousand_sep.length ) { //pokud se požaduje oddělovač tisíců
    intpart_str = add_thousand_sep(intpart_str,thousand_sep); //přidá se oddělovač tisíců
  }
  
  var formated = intpart_str;
  if ( decimals ) { formated+= dec_point + decpart_str; }

  return formated;
}

/**
 * @Name: add_thousand_sep(number,thousand_sep)
 * @Author: Petr Novotný
 * @Date: 20.9.2008
 * @Description: Přidá oddělovač tisíců do celého čísla
 * @param int number            Číslo
 * @param string thousand_sep   Oddělovač tisíců
 */
function add_thousand_sep(number,thousand_sep) {

  var number_str = parseInt(number) + "";
  if ( !thousand_sep ) { thousand_sep = ''; }

  if ( thousand_sep.length ) {
    var number_str_new = "";
    var tmp = number_str;
    var last_trio = "";
    while ( tmp.length>3 ) {
      last_trio = tmp.substring(tmp.length-3);
      number_str_new = ( number_str_new.length ) ? last_trio + " " + number_str_new : last_trio;
      tmp = tmp.substring(0,tmp.length-3);
    }
    number_str = ( number_str_new.length ) ? tmp + " " + number_str_new : tmp;
  }
  
  return number_str;
}


//pouze čísla a případně desetinný oddělovač
function numbersonly(e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;

  keychar = String.fromCharCode(key);

  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;
  else if ( ("0123456789").indexOf(keychar) > -1 )
    return true;
  else if ( dec && ((".,").indexOf(keychar) > -1) ) {
    return true;
  }
  else
   return false;
}


/**
 * @Name: is_int(tested_value)
 * @Author: Petr Novotný
 * @Date: 28.3.2003
 * @Description: Zjistí, zda je hodnota typu integer
 * @param float|string tested_value   Testovaná hodnota
 */
function is_int(tested_value) {
  var tmp;
  tmp = parseInt(tested_value) + "";
  if ( tested_value.length==0 ) { return false; }
  else if ( tmp.length!=tested_value.length || isNaN(tmp) ) { return false; }
  else { return true; }
}

/**
 * @Name: is_float(tested_value)
 * @Author: Petr Novotný
 * @Date: 20.9.2008
 * @Description: Zjistí, zda je hodnota typu float
 * @param float|string tested_value   Testovaná hodnota
 */
function is_float(tested_value) {
  var tmp;
  tmp = parseFloat(tested_value) + "";
  if ( tested_value.length==0 ) { return false; }
  else if ( tmp.length!=tested_value.length || isNaN(tmp) ) { return false; }
  else { return true; }
}


/**
 * @Name: round(number,decimals)
 * @Author: Petr Novotný
 * @Date: 20.9.2008
 * @Description: Zaokrouhlení na požadovaný počet desetinných míst, inspired on web
 * @param float number          Číslo
 * @param int decimals          Počet desetinných míst
 */
function round(number,decimals) {
  number = parseFloat(number);
  var multi = Math.pow(10,decimals);
  return Math.round(number*multi) / multi;
}

//pouze čísla a mezera
function psconly(myfield, e) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;

  keychar = String.fromCharCode(key);

  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;
  else if (((' 0123456789').indexOf(keychar) > -1))
    return true;
  else
   return false;
}



function height_page_bg(heightbox,display)
{
  var showheigt = $('.showbox').height();
  var celkove = 0;
         
  
   
  if(showheigt)
  {
    
   
    $('div.showbox').each(function(index) {
       celkove += $(this).height();
    });
   
    
   
    if(heightbox > 0)
    {

      if(display == 'block')
      {   
        var height_page = $(document).height() - 1024 - heightbox;

  
        if(height_page < 0)
        {
           height_page = 0;
        }        
      }
      else
      {            
        var height_page = $(document).height() - 1024 + heightbox + 50;
 
      }
    }
    else
    {
      
      var height_page = $(document).height() - 1024 - celkove;
    }
   

  }
  else if(heightbox > 0)
  {
     if(display == 'none')
     {
        var act_heith     = $('#repeat_main_container').height();     
        var height_page = act_heith - heightbox + 20;
     }
     else if(display == 'block')
     {
        var act_heith     = $('#repeat_main_container').height();     
        var height_page = act_heith + heightbox - 20;
     } 
     else
     {
      var height_page = heightbox + 80;
     }
     
  }
  else 
  {
     var height_page = $(document).height() - 893;     
  }
  

  $('#repeat_main_container').css('height',height_page);
  
}

function lightBoxactiv()
{
   $('div.cloned a.light_box').removeClass('light_box')

   $('.bg_ossd_repeat').each(function(index) 
   {
      $('.box_poc_'+index+' .items a.light_box').lightBox();  
   });
  
  
}



 
function scroll()
{
  // initialize scrollable together with the navigator plugin
$(".scrollable").scrollable({circular: true, mousewheel: true}).navigator().autoscroll({
  	interval: 3000
  });
}


function hladat()
{
  var defaulthladat = $('#hladat').attr('value');
  
  
  $('#hladat').click(function() {          
    $('#hladat').attr('value',''); 
  })                               
}


function rozcestnik_hp()
{

  
  
    $("#rozcestnik_content div").hover(function () 
    {    
      var activ_rozcestnik = $(this).attr('id');

      
      
      $( '#' + activ_rozcestnik + ' .rozcestnik_podmenu').css('display','block');
      var id = $( '#' + activ_rozcestnik + ' .bg_name_section_hp').attr('id');
      $( '#' + activ_rozcestnik + ' .bg_name_section_hp').addClass(id + '_white');
      
           
    },
    
    function () {
     
      var activ_rozcestnik = $(this).attr('id');

      $( '#' + activ_rozcestnik + ' .rozcestnik_podmenu').css('display','none');
       var id = $( '#' + activ_rozcestnik + ' .bg_name_section_hp').attr('id');
      $( '#' + activ_rozcestnik + ' .bg_name_section_hp').removeClass(id + '_white'); 
    
    });  
 
}



function print_kalkulator()
{

   $("#tisk_kalk_platby").click(function(){
    window.print();
    return false;
  });
  
  
}
