jQuery.fn.exists = function(){
   return jQuery(this).length>0;
}

function slideShow() { // homepage slideshow

   if(!$("#slideshow").exists()) { return }
   if($("#slideshow").attr('started')==1) { return }
   $("#slideshow").attr('started',1)


   $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
      $(pager).find('a').removeClass('act').filter('a:eq('+currSlideIndex+')').addClass('act');
   };

   $('#slideshow > ul.images').cycle({
      fx:'scrollLeft',
      speed: 500,
      timeout: 4000,
      pause: true,
      pauseOnPagerHover: true,
      requeueTimeout: 0,
      pager: '#slideshow > ul.thumbs',
      pagerAnchorBuilder: function(idx, slide) {
         return null;
      }
   });
   $('#slideshow > ul.thumbs > li').each(function(i){
      $(this).hover(function() {
         $('#slideshow > ul.images').cycle({
            fx:'scrollLeft',
            speed: 500,
            timeout: 4000,
            pause: true,
            pauseOnPagerHover: true,
            startingSlide: i,
            pager: '#slideshow > ul.thumbs',
            pagerAnchorBuilder: function(idx, slide) {
               return null;
            }
         });
         $('#slideshow > ul.images').cycle('pause');
         return false;
      },
      function () {
         $('#slideshow > ul.images').cycle({
            fx:'scrollLeft',
            speed: 500,
            timeout: 4000,
            pause: true,
            pauseOnPagerHover: true,
            startingSlide: i,
            pager: '#slideshow > ul.thumbs',
            pagerAnchorBuilder: function(idx, slide) {
               return null;
            }
         });
         $('#slideshow > ul.images').cycle('resume');
         return false;
      });
   });
   $('#slideshow > ul.images').show();
}


function inputFocus() { // keeps default text in inputs
   $('.clear-value').focus(function() {
      if (this.value==this.defaultValue) this.value="";
   });
   $('.clear-value').blur(function() {
      if (this.value=="") this.value=this.defaultValue;
   });
}


function discussHover() { // shows user rating in discuss
   $(".comments .item-in").hover(
      function () {
      if($(".my-rating",this).exists()){
         $(".rating", this).hide();
         $(".my-rating", this).show();
        }
      }, function () {
      	if($(".my-rating",this).exists()){
         $(".rating", this).show();
         $(".my-rating", this).hide();
		}
      }
      );
}

function tabs() {
   if($(".tabs").exists()){
      $(".tabs .tabs-content").not(".active").hide();
      $(".tabs .tabs-header ul li a").click(function(){
         $(this).parents(".tabs").children(".tabs-content").hide();
         $(this).parents("ul").children("li").removeClass("active");
         $(this).parents(".tabs").children(".tabs-content").removeClass("active");
         $(this).parent("li").addClass("active");
         var id = $(this).attr("href");
         $(id).fadeIn();
         $(id).addClass("active")
         return false;
      });
   }
}

function tooltips() {
   if($(".tooltip").exists() && $("#tooltip").exists()){
      $(".tooltip").click(function(){
         var text = $(this).attr("title");
         $("#tooltip #tooltip-in").text(text);
         $("#tooltip").css("top",$(this).position().top-20);
         $("#tooltip").fadeIn();
         $("#tooltip").delay(10000).fadeOut();
      });
   }
}

function searchSwitch() {
   if($("#s-type-1").exists() && $("#s-type-2").exists()){
      $("#s-type-1,#s-type-2").click(function(){
      if(
	  	$("#search-inp").attr("value") == $("#s-type-1").attr("title") ||
		  $("#search-inp").attr("value") == $("#s-type-2").attr("title")){
         $("#search-inp").attr("value",$(this).attr("title"));
         }
      });
   }
}

function sidebox() {
   if($("#sidebox").exists()){
      $.waypoints.settings.scrollThrottle = 30;
      $("#sidebox a.close").click(function(){
         $("#sidebox").fadeOut();
         return false;
      });

      if($("#sidebox-show").exists()){
         $('#sidebox-show').waypoint(function(event, direction) {
            if(direction === "down"){
               //		$('#sidebox').toggleClass('hidden', direction === "up");
               $('#sidebox').not(".sidebox-shown").animate({right:'+=400'},1000);
               $('#sidebox').addClass("sidebox-shown");
            }
         }, {offset: '100px'});
         $('#sidebox-hide').waypoint(function(event, direction) {
            if(direction === "down"){
               //		$('#sidebox').toggleClass('hidden', direction === "up");
               $('#sidebox.sidebox-shown').animate({right:'-=400'},1000);
               $('#sidebox').removeClass("sidebox-shown");
            }
         }, {offset: '0'});
      }
   }
}

function linkHover() {
   $("a").not(".symlink").not('[href="#"]').mouseover(function(){
      var href = $(this).attr("href");
      $(this).offsetParent().find('a[href="'+href+'"]').addClass("hover");
   });
   $("a").not(".symlink").not('[href="#"]').mouseout(function(){
      var href = $(this).attr("href");
      $(this).offsetParent().find('a[href="'+href+'"]').removeClass("hover");
   });

   //ads border to img in ads
   //$(".titadv a img").parent("a").addClass("imgborder");
}


function checkboxAndRadio() { // styles checkbox and radio
   $('.chb').screwDefaultButtons({
      checked: "url(img/chb-checked.png)",
      unchecked: "url(img/chb-unchecked.png)",
      width: 20,
      height: 20
   });
   $('.rad').screwDefaultButtons({
      checked: "url(img/rad-checked.png)",
      unchecked: "url(img/rad-unchecked.png)",
      width: 20,
      height: 20
   });
}


function multiSelect() { // let's have all select boxes beautiful
   $(".multi-select").multiselect({
      header: false,
      multiple: false,
      selectedList: 1,
      show: ['blind', 300],
      hide: ['blind', 300]
   });
}


$(document).ready(function() {
   $("#btn-popup").click(function() {
      $(this).toggleClass('act');
      $("#tb-user-info-popup").slideToggle('fast');
   });

   slideShow();
   inputFocus();
   discussHover();
   tabs();
   tooltips();
   searchSwitch();
   linkHover();
   sidebox();
   //checkboxAndRadio();
   if (navigator.userAgent.indexOf('iPad') == -1) { // if iPad, don't style select boxes
      //multiSelect();
   }
});


function gaClick() {
    /* najde vsechny odkazy a prida lepsi analyzu odchozich prokliku pres GA */
    if (typeof _gat=='undefined') { return }

    $('a[gaskip!=1]').each( function() {
        $(this).attr('gaskip',1)
    } ).click( function() {

        if (window.console && window.console.log) {
            window.console.log("click: "+$(this).attr('href'))
        }

        if(!$(this).attr('href')) { return true }
        if($(this).attr('href').substring(0,4)!='http') { return true }

        var hostname = $(this).get(0).hostname
        var category = 'outlink'
        var cat = $(this).attr('gacategory');
        if (cat) {
            category += ' '+cat
        }
        _gat._getTrackerByName()._trackEvent(category, hostname);
        //alert(category+' '+hostname)
        var target = $(this).attr('target') || '';
        if (target=='_blank') {
            //setTimeout('window.open("' + $(this).attr('href') + '")', 100);
            //window.open($(this).attr('href'))
            return true
        }
        else if(target != '') {
            return true
        }
        else {
            setTimeout('document.location = "' + $(this).attr('href') + '"', 100);
        }
        return false;
    } )
}

$(document).ready(function() {
   gaClick();
});
$(window).load( function() {
   /* pro html nactene pozdeji ajaxem */
   gaClick();
} )


