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


$(function () {
 if ($('.cat-item.current-cat ul, .cat-item.current-cat-parent ul').length) {
		$('#header').addClass('high');
	}
});



function slideShow() { // homepage slideshow
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
		$(pager).find('a').removeClass('act').filter('a:eq('+currSlideIndex+')').addClass('act'); 
	};
	$('#slideshow > ul.images').cycle({pauseOnPagerHover: 1, fx:'scrollLeft',speed: 500, timeout: 4000, requeueTimeout: 0, pager: '#slideshow > ul.thumbs', pagerAnchorBuilder: function(idx, slide) { return null;}});                
	$('#slideshow > ul.thumbs > li').each(function(i){
		$(this).mouseenter(function() {
			$('#slideshow > ul.images').cycle({fx:'scrollLeft',speed: 500, timeout: 0, startingSlide: i, pager: '#slideshow > ul.thumbs', pagerAnchorBuilder: function(idx, slide) { return null;}}); 
			return false; 
		}).mouseleave(function() {
                         $('#slideshow > ul.images').cycle({fx:'scrollLeft',speed: 500, timeout: 4000, startingSlide: i, pager: '#slideshow > ul.thumbs', pagerAnchorBuilder: function(idx, slide) { return null;}}); 
  });        
                
	});
        
	$('#slideshow > ul.images').show();
}



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



function jCarousel() { // wallpapers
	jQuery('#jcar-1').jcarousel({				// carousel for posters with text
		scroll: 4,						// number of items to scroll
		animation: 700,					// speed of paging
		easing: "easeInOutQuad"				// animation effect
	});
}



function gallery() { // article gallery
	if ($('.ad-gallery').length) {
		var galleries = $('.ad-gallery').adGallery();
		$('#switch-effect').change(function() {
			galleries[0].settings.effect = $(this).val();
			return false;
		});
		$('#toggle-slideshow').click(function() {
			galleries[0].slideshow.toggle();
			return false;
		});
	}
}



function showForm() { // show form for comments
	$('span.add').click(function() {
		$('.main-box form').slideToggle('slow');
	});
}

function getWallpapers(){        
    var url = 'http://sportstream.tiscali.cz/wp-content/themes/SportStream/iQuery_wallpapers.php?width=' + screen.width + '&height=' + screen.height;  
    $('.showWallpapers').load(url, {}, function () {
      // callback, ktery ma byt proveden, az se ajax request vykona
      jCarousel();
    });   
    
}

function skin() {
   if($(".skin").exists()){
      $.waypoints.settings.scrollThrottle = 30;
         $("#footer").waypoint(function(event, direction) {
            if(direction === "down"){
               $('.skin').addClass("skin-absolute");
							 $("#bg-envelope").addClass("skin-relative");
            }
            else if(direction ==="up"){
            	 $('.skin').removeClass("skin-absolute");
               $("#bg-envelope").removeClass("skin-relative");
						}
         }, {offset: '100px'});
   }
}

$(document).ready(function() {                
        getWallpapers();
	inputFocus();
	gallery();
	showForm();
	skin();
});

