// metoda pro nacteni novych vysledku naseptavace mest
var last_subject_serach_place = false;
var subject_serach_place_interval = false;
var subject_place_box = -1;

function updateMatched()  {
  $('#search_matched').load('/suggest/place/', {type: $('#search_type').val(), place: $('#search_place').val()});
}

$(document).ready( function() {

  // naseptavac mest
  $("#location").autocomplete('/suggest/place/', { minChars: 0 });
  $("#place_location").autocomplete('/suggest/place/', { minChars: 0 });
  $("#subject_location").autocomplete('/suggest/place/', { minChars: 0 });

  $('#sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#places_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#subjects_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#events_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#place_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#subject_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });
  $('#event_sport_name').autocomplete('/suggest/sport/', {minChars: 0 });

  // hover zvyraznei instruktora
  $(".boxItems .item").hover(
    function() { $(this).addClass("highlight"); }, 
    function() { $(this).removeClass("highlight"); }
  );
  
  // hover submit buttonu filtru
  $(".filter .submit").hover(
    function() { $(this).addClass("s_highlight"); }, 
    function() { $(this).removeClass("s_highlight"); }
  );
  
  // tabxbox filtru
  $(".hpFilter").tabs();

  // fancybox
  /*$("a.fancybox").live('click', function() {
    $(this).fancybox({
      'zoomSpeedIn': 300, 
      'zoomSpeedOut': 300, 
      'overlayShow' :true,
    });
    $(this).click();
    return false;
  });*/
  
    $("a.fancybox").fancybox({
      'zoomSpeedIn': 300,
      'zoomSpeedOut': 300,
      'overlayShow' :true
    }); 

   $("a.inline-fancy").fancybox({
    'hideOnContentClick': false ,
    'frameWidth': 870,
    'frameHeight': 500
    });

   $("a.iframe-facebook").click(function() {
    return !window.open($(this).attr('href'), 'facebook', 'width=600,height=300');
   });
 });

// Pregenerovani textu ve formularovy prvek
$(function() {        

  if($(".editable_textarea").length) {
    $(".editable_textarea").editable("http://www.example.cz/save.php", { 
      indicator : "<img src='images/indicator.gif'>",
      type   : 'textarea',
      submitdata: { _method: "put" },
      select : true,
      submit : 'Uložit změny',
      cancel : 'Zrušit',
      cssclass : "editable"
    });
  }
  if($(".editable_textile").length) {
    $(".editable_textile").editable("http://www.example.cz/save.php?renderer=textile", { 
      indicator : "<img src='images/indicator.gif'>",
      loadurl   : "http://www.appelsiini.net/projects/jeditable/php/load.php",
      type      : "textarea",
      submit    : "OK",
      cancel    : "Zrušit",
      tooltip   : "Click to edit..."
    });
  }

  if($(".click").length) {
    $(".click").editable("http://www.appelsiini.net/projects/jeditable/php/echo.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Click to edit...",
      submit : 'OK',
      cancel : 'Zrušit',
      style  : "inherit"
    });
  }
  
  /*if($(".dblclick").length) {
    $(".dblclick").editable("http://www.appelsiini.net/projects/jeditable/php/echo.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Doubleclick to edit...",
      event     : "dblclick",
      style  : "inherit"
    });
  };

  $(".mouseover").editable("http://www.appelsiini.net/projects/jeditable/php/echo.php", { 
      indicator : "<img src='images/indicator.gif'>",
      tooltip   : "Move mouseover to edit...",
      event     : "mouseover",
      style  : "inherit"
  });
  
  $("#nosuch").editable("http://www.appelsiini.net/projects/jeditable/php/echo.php", { 
      indicator : "<img src='images/indicator.gif'>",
      type   : 'textarea',
      submit : 'OK'
  });

  /*$(".timepicker").editable("", { 
      indicator : "<img src='images/indicator.gif'>",
      type      : 'timepicker',
      submit    : 'OK',
      cancel    : 'Zrušit',
      tooltip   : "Click to edit..."
  });*/
});

// Vymazani inputu
function swip (elementInput, swipFrom, swipTo)  {
  if (elementInput.value == swipFrom) elementInput.value = swipTo;
}

$.fn.simpleAjaxForm = function(targetURL) {
  var target = $(this);
  $(this).find("form").submit(function() {
    if(!targetURL)
      targetURL = $(this).attr('action');

    var data = $(this).serializeArray();
    target.html('loading');

    $.post(
      targetURL,
      data,
      function(response) {
        target.html(response);

        // rekurzivni zavolani na nove nacteny form
        target.simpleAjaxForm(targetURL);
      }
    );

    return false;
  });
}

var active_comment_block = false;

$(document).ready(function() {
  /*
   * Komentare - Nova odpoved
   */
  $('.add_comment_overlay').live('click', function() {
    active_comment_block = {
      'model': $(this).attr('rel').substr(0, $(this).attr('rel').lastIndexOf('_')),
      'scope': $(this).attr('rel').substr($(this).attr('rel').lastIndexOf('_')+1)
    };
    return showOverlayContent(false, $(this).attr('href'), 720, 450);
  });

  /*
   * Komentare - Odpovedet na existujici prispevek
   */
  $('.reply_comment_overlay').live('click', function() {
    var el = $(this).parents('.discussion').find('.add_comment_overlay').attr('rel');
    active_comment_block = {
      'model': el.substr(0, el.lastIndexOf('_')),
      'scope': el.substr(el.lastIndexOf('_')+1)
    };

    return showOverlayContent(false, $(this).attr('href'), 720, 450);
  });                                             
  /*
   * Live search na HP
   */     
  var previousSerialize = new Array();
  function _checkVal() {
    $('.liveSearch').each(function() {
      //alert($(this).attr('id'));
      //alert($(this).attr('id'));
      if($(this).serialize() != previousSerialize[$(this).attr('id')]) {
        // obnoveni poctu nalezenych vysledku
        //alert($(this).attr('action'));
        var parent = $(this);
        if($(this).hasClass('liveSerachForm'))
         var action = $('#'+$(this).attr('id')+'_submit').attr('action');
        else
         var action = $(this).attr('action');

        var data = $(this).serialize()/*.replace(/form_id/, 'form_ad')*/;
        $.post(
          action,
          data,
          function(response) {
            var results = parseInt(response);
            if(isNaN(results))
             results = 0;
  
            parent.find('.results strong').text(results);
          }
        );
        previousSerialize[$(this).attr('id')] = $(this).serialize();
      }
    });
  }
  // v pravidelnych intervalech budeme kontrolovat, zda nedoslo ke zmene formulare

  _checkVal();                                    
  var interval = setInterval(_checkVal, 500);    

  /*
   * Email obfuscate
   */
  $('a.js-email').each(function() {
    $(this).attr('href', $(this).attr('href').replace('(zavinac)', '@'));
    $(this).text($(this).text().replace('(zavinac)', '@'));
  });
  $('span.js-email').each(function() {
    $(this).text($(this).text().replace('(zavinac)', '@'));
  });
  
  $('.clickable-item').click(function(){
    document.location = $(this).find('a.detail').attr('href');
    return false;
   });
   
  $(".clickable-item").hover(function(){ 
    $(this).addClass("clickable-item-hover"); 
  },function(){ 
    $(this).removeClass("clickable-item-hover"); 
  });
   
  $('.article-author').click(function(e){
    document.location = $(this).find('a').attr('href');
    return false;
   });
           /*
    $(function(){
      $('#mainMenu .menu-blog a').click(function(){
      window.open(this.href);
      return false;
      });
    });  */   
});

/*
 * Pruhovane tabulky
 */
$(document).ready(function() {
	$('table.stripped tr').each(function(i) {
		if(!(i%2))
			$(this).addClass('odd');
		});
});

/*
 * Hover tabulky
 */
if($.browser.msie && $.browser.version < 7) {
	$(document).ready(function() {
		$('table.hover tr').hover(function(){$(this).addClass('hover');}, function(){$(this).removeClass('hover');});
	});
}

/*
 * Klikatelne tabulky
 */
$(document).ready(function() {
	$('table.clickable tr').click(function() {
		var a = $(this).find('a');
		if(a.length) {
			document.location = a[0].href;
		}
	});
	$('table.clickable tr a').click(function(event) {
		event.stopPropagation();
	});
});


/* ----------------------------------------------------------------
 * FILE: core.js
 * ----------------------------------------------------------------
 * Created: 30-12-08 (dd-mm-yy)
 * Website: Elevate Web Design
 * ---------------------------------------------------------------
*/
/*
$core = {

    context : false,
    
    getContext: function() {
        if(this.context === false) {
            this.context = $("#content");
        }
        return this.context;
    },
    
    init : function() {        
        $('body').addClass('js');
        
        this.prepareScrollTo();
        if($('#latest-work').length > 0) this.prepareLatestWork();
    },
    
    prepareLatestWork : function() {
        if($.browser.msie && $.browser.version < 7.0) {
            $('#latest-work li .details').remove();
        } else {    
            $('#latest-work li').hover(function() {
                $(this).children('div.details').stop(true, true).fadeIn().removeClass('hide');
            }, function() {
                $(this).children('div.details').stop(true, true).fadeOut().addClass('hide');
            });
        }
    },

    prepareScrollTo : function() {
        $('a[href^="#"]', this.getContext()).click(function(e) {  
            e.preventDefault();
            $('html, body').animate({ 
                scrollTop: $($(this).attr("href")).offset().top 
            }, 500);
        });        
    }    

}

$(function() {
    $core.init();
});
*/



//  Mapa
function map(howMuch, element) {
  if(howMuch < 8) {
    document.getElementById('map_cz').style.backgroundPosition = "0px -"+(howMuch*202)+"px";
  } else {
    document.getElementById('map_cz').style.backgroundPosition = "-324px -"+((howMuch-7)*202)+"px";
  }
 
 if(element) {
  if(howMuch!=0 && subject_place_box!=howMuch){
   $('#hp-places-box').html('<img src="'+BreezyCMS_URL+'public/images/loader.gif" />');
   $.get($(element).attr('href'), function(data){$('#hp-places-box').html(data)});
  }

  subject_place_box = howMuch;
 }
}


// jquery EasySlider na HP
$(document).ready(function(){
  if($('#hp_top_slider').length) {
    $('#hp_top_slider').easySlider({
      prevId: 'easySlider_prev',
      prevText: 'Předchozí',
      nextId: 'easySlider_next',
      nextText: 'Následující'});
  };
});

// záložky
$(document).ready(function() {
  if (!$('ul.event-tabs').length)
    return;

  var hiddenClass = 'event-tab-content-hidden';
  var wasTabActivated = false;
  $('ul.event-tabs a').each(function() {
    $(this).click(function(e) {
      $('.event-tab-content').addClass(hiddenClass);
      var id = this.href.substr(this.href.indexOf('#'));

      $(id).removeClass(hiddenClass);
      $(this).parent().parent().find('li.active').removeClass('active');
      $(this).parent().addClass('active');

      if(id == '#tab-doprava') {
        initialize();
      }

      if (e.which != undefined)
        window.location.hash = id.substr(5);
      return false;
    });
    if (this.href.substr(this.href.indexOf('#')) == '#tab-' + window.location.hash.substr(window.location.hash.indexOf('#') + 1)) {
      $(this).trigger('click');
      wasTabActivated = true;
    }
  });
  if (!wasTabActivated)
    $('ul.event-tabs a:eq(0)').trigger('click');
});

/**
 * Hlasování o komentářích u instruktorů
 */
$(document).ready(function() {
  var f = function(form, option) {
    form.append('<input type="hidden" name="value" value="' + option + '"/>');
    var commentId = form.find('input[name="comment_id"]').val();
    var data = form.serializeArray();

    $.post(
      BreezyCMS_URL + 'instruktori/comment_score/' + commentId,
      data,
      function(response) {
        form.html(response);
      }
    );
    form.addClass('closed');
  }

  $('form.improved-comments').each(function() {
    var form = $(this);
    form.find('.vote-up').click(function() {
      f(form, 1);
    });
    form.find('.vote-down').click(function() {
      f(form, -1);
    });
    form.submit(function() {
      return false;
    });
  });
});

/**
 * Globální vyhledávání
 */
$(document).ready(function() {
  if ($('#global-search').length) {
    var txt = {'subject': 'instruktorů', 'place': 'provozoven', 'event': 'akcí'};
    $('#global-search .search-type').click(function() {
      $('#global-search .results span').text(txt[$('#search_type').val()]);
    });

    $('#global-search select').click(function() {
      $('#global-search .results span').text(txt[$('#search_type').val()]);
    });
    $('#global-search select').trigger('click');

    $('#global-search-show-more').click(function() {
      $('#global-search .bottom').slideToggle();
      return false;
    });
    
    if (! $('#fulltext').val()) {
      $('#global-search .bottom').hide();
    }
  }

  $('.global-search-open-sportlist').click(function() {
    showOverlayContent(false, BreezyCMS_URL + 'suggest/sport_selectlist/', 730,430);
    var input = $(this).parent().find('input');
    window.sportSelectListTarget = input;
  });

  $('.global-search-open-sportlist').each(function() {
    var input = $(this).parent().find('input');
    if (!input.val()) {
      input.val('sport nebo aktivitu');
      input.focus(function() {
        if (!$(this).data('activated')) {
          $(this).val('');
          $(this).data('activated', true);
        }
      });
    }
  });
  
  // Instruktoři ve vašem okolí na HP
  $('#hpNearbySubjects .form').click(function() {
    $('#hpNearbySubjectsInput').focus();
  });
});

/**
 * Doporučení na HP
 */
$(document).ready(function() {
  $('#hp-recommend-tabs a').click(function() {
    var div = $(this).attr('href').replace(/.*#/, '');
    $('.hp-recommend-active-tab').hide();
    $('#' + div).show();
    $('#' + div).addClass('hp-recommend-active-tab');
    $('#hp-recommend-tabs .active').removeClass('active');
    $(this).parent().addClass('active');
    return false;
  });
  $('.boxHPRecommend').hide();
  $('#hp-recommend-tabs a:first').trigger('click');
});


/**
 * Fotky v galerii pouze první řádek
 */
$(document).ready(function() {
  if($('.gallery .overflowed').length) {
    $('.gallery .overflowed').after('<p class="clear"><a href="" class="white" onclick="$(this).parent().prev(\'.gallery .overflowed\').fadeIn();$(this).remove();return false;">zobrazit všechny fotografie</a></p>');
    $('.gallery .overflowed').hide();
  };
});

/*
 * datepicker
 */
$(document).ready(function() {
    $('#global-search .dateButton').datepicker({ changeMonth: true, changeYear: true, yearRange: '2010:2011'});
});
