(function($) {

var SidePanelPosition = {
	init: function(){
		screenWidth = $(window).width();
		containerWidth = $("#main_container").outerWidth(true);
		columnRight = $("#column_right");
		columnRightWidth = $("#column_right").outerWidth(true);
		posX = (screenWidth - containerWidth) / 2;
		if(screenWidth > containerWidth){
			posType = 'fixed';
			right = posX;
		}else{
			posType = 'absolute';
			right = 0;
		}
		
	}
}; 

var CaptchaText = {
	init: function(){
		$('#recaptcha_instructions_image').text('Przepisz tekst z obrazka:');
		$('#recaptcha_instructions_error').text('Niepoprawny tekst');
	}
};

var LinkRollovers = {
	init: function(){
		$('#links a.url').hover(function(){
			$(this).parent().parent().find('.thumb').addClass('hover');
			$(this).parent().parent().find('.url').addClass('hover');
		},function(){
			$(this).parent().parent().find('.thumb').removeClass('hover');
			$(this).parent().parent().find('.url').removeClass('hover');
		});


	}
};

var PaginationRollovers = {
	init: function(){
		$('.pagination li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
};

var CompNavButtons = {
	update: function(){
		if($('.ranking th.hide').length > 0)
		{
			$('.showOlder').removeClass('inactive');
		}
		else
		{
			$('.showOlder').addClass('inactive');
		}
		if($('.ranking th.hideEnd').length > 0)
		{
			$('.showNewer').removeClass('inactive');
		}
		else
		{
			$('.showNewer').addClass('inactive');
		}
	}
}


$(document).ready(function(){
	CaptchaText.init();
	LinkRollovers.init();
	PaginationRollovers.init();
	SidePanelPosition.init();
	
	$('.noJS').removeClass('noJS');
	
	//detect mobile browsers on iphone, ipad and ipod
	var deviceAgent = navigator.userAgent.toLowerCase();
    var mobile = deviceAgent.match(/(iphone|ipod|ipad)/);
	
	// side panel control, disable on mobile
	if(!mobile){		
		$(window).scroll(function() {		
			if ($(window).scrollTop() > 17) {
				columnRight.css({
					position: posType,
					right: right,
					top: '17px'
				});
			}
			else {
				columnRight.css({
					position: posType,
					right: right,
					top: '17px'
				});
			}
		});
		$(window).resize(function() {
			SidePanelPosition.init();
			columnRight.css({
					position: posType,
					right: right
				});
		});
	}

	// slownik scrolling
	$('#slowniczek .main_content').localScroll();
	
	// news ticker
	if($('#js-news').length > 0){
		$('#js-news').ticker({
			controls: false,        // Whether or not to show the jQuery News Ticker controls
			titleText: 'Z ostatniej chwili: ',   // To remove the title set this to an empty String
			displayType: 'reveal', // Animation type - current options are 'reveal' or 'fade'
			direction: 'ltr',       // Ticker direction - current options are 'ltr' or 'rtl'
			pauseOnItems: 6000,    // The pause on a news item before being replaced
			fadeInSpeed: 600,      // Speed of fade in animation
			fadeOutSpeed: 300      // Speed of fade out animation
		});
	}

	
	// banners panel 1
	if ($('.ad_panel1 .banners1').length > 0) {
			$('.ad_panel1 .banners1').cycle({ 
					fx: 'fade', 
					timeout: 10000,
					speed: 1500,
					pause: 1
				});
			}
			
	// banners panel 2
	if ($('.ad_panel2 .banners2').length > 0) {
			$('.ad_panel2 .banners2').cycle({ 
					fx: 'fade', 
					timeout: 10000,
					speed: 1500,
					pause: 1
				});
			}		
			
	
	// banners news pages
	if ($('.bannersnews1').length > 0) {
			$('.bannersnews1').cycle({ 
					fx: 'fade', 
					timeout: 10000,
					speed: 1500,
					pause: 1
				});
			}
			
	
	// main nav sub menus
	$('#main_nav .main').hover(
		function(){
			// check if every panel is closed
			//console.log(('#main_nav li').length);
			li = $(this);
			if(!($(this).hasClass('active'))){
				li.addClass('active');
			
		//		link.find('.sub_nav').delay(150).fadeIn(300);
			}
		},
		function(){
			link = $(this);
			link.removeClass('active');
		//	$('.sub_nav').fadeOut(100, function(){
			  		
		//	  });
		});
	
	
	var mylist = $('#main_nav .kluby .sub_nav');
	var listitems = mylist.children('li').get();
	listitems.sort(function(a, b) {
	   var compA = $(a).text().toUpperCase();
	   var compB = $(b).text().toUpperCase();
	   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
	})
	$.each(listitems, function(idx, itm) { mylist.append(itm); });

	
	$('.pagination li').click(function(e){
		var url = $(this).find('a').attr('href');
		if(url){
		$(this).addClass('click');
		location.href=url;
		}
	});
	
	$('.showOlder').click(function(e){
			e.preventDefault();						   	
			if($('.ranking th.hide').length > 0 )
			{
				$('.ranking th.hide:last').removeClass('hide');
				$('.ranking th.comp:not(.hideEnd):last').addClass('hideEnd');
				$('.ranking tr').each(function(r){
					$(this).find('td.hide:last').removeClass('hide');					   
					$(this).find('td.comp:not(.hideEnd):last').addClass('hideEnd');							   
				});
			}
			CompNavButtons.update();
		});
		
		$('.showNewer').click(function(e){
			e.preventDefault();						   	
			if($('.ranking th.hideEnd').length > 0)
			{
				$('.ranking th.hideEnd:first').removeClass('hideEnd');
				$('.ranking th.comp:not(.hide):not(.hideEnd):first').addClass('hide');
				$('.ranking tr').each(function(r){
					$(this).find('td.hideEnd:first').removeClass('hideEnd');					   
					$(this).find('td.comp:not(.hide):not(.hideEnd):first').addClass('hide');							   
				});
			}
			CompNavButtons.update();
		});
	
	
	/* centering pagination
	var pagination_wrapper = $('.pagination').width();
	var pagination = $('.pagination ul').width();
	var margin = (pagination_wrapper - pagination)/2+5;
	$('.pagination ul').css('margin-left', margin);
	*/
	

	
	$("a.thumb").hover(
	  function () {
		$(this).find('span').animate({
			opacity: 1
		  }, 400, function() {
			// Animation complete.
		  });
	  }, 
	  function () {
		$(this).find('span').animate({
			opacity: 0.75
		  }, 400, function() {
			// Animation complete.
		  });
	  }
	);

	
	if ($('.banners').length > 0) {
			$('.banners').cycle({ 
					fx: 'fade', 
					timeout: 4000,
					speed: 3000
				});
			}
			
	if ($('#home .latest_blogs').length > 0) {
		$('#home .latest_blogs').cycle({ 
			fx: 'fade', 
			timeout: 0,
			speed: 1200,
			pause: 1,
			pauseOnPagerHover: 1,
			pager: ".latest_pager",
			backwards: false
		});
	}
	
	if ($('#trening .latest_drills').length > 0) {
		$('#trening .latest_drills').cycle({ 
			fx: 'fade', 
			timeout: 12000,
			speed: 1200,
			pause: 1,
			pauseOnPagerHover: 1,
			pager: ".latest_pager",
			backwards: false,
			next: '.next_item',
			prev: '.prev_item'
		});
	}
	
	if ($('#filmy .latest_films').length > 0) {
		$('#filmy .latest_films').cycle({ 
			fx: 'fade', 
			timeout: 12000,
			speed: 1200,
			pause: 1,
			pauseOnPagerHover: 1,
			pager: ".latest_pager",
			backwards: false,
			next: '.next_item',
			prev: '.prev_item'
		});
	}
	
	if ($('.news_page .images').length > 0) {
		$('.news_page .images').cycle({ 
			fx: 'fade', 
			timeout: 0,
			speed: 1200,
			pause: 1,
			pauseOnPagerHover: 1,
			pager: ".latest_pager",
			backwards: false,
			next: '.next_item',
			prev: '.prev_item'
		});
	}
			
	jQuery('.blogEntry').each(function(e){
		jQuery(this).find('p:last').css('marginBottom', 0);
	});

	jQuery('.showOnClick').click(function(e){
		e.preventDefault();								  
		jQuery('.showOnClickContainer').slideDown('slow');
		jQuery('#Password-_PasswordFieldVisible').attr('value', 1);
	});
	
	
	$('#home .latest_comments').newsScroll();
	
	$('.thumb, .playerResults').colorbox({
		initialWidth: "80px",
		initialHeight: "80px"
	});
	
	$('.btn_facebook').colorbox({
		initialWidth: "80px",
		initialHeight: "80px"
	});
	
	$('.btn_social').colorbox({
		initialWidth: "80px",
		initialHeight: "80px"
	});
	
	$('.news_page .images .img').colorbox({
		initialWidth: "80px",
		initialHeight: "80px"
	});
	
	//jQuery('#Password-_Password').attr('value', '');
	
	$('#PageCommentInterface_Form_PostCommentForm_Comment, #Form_AddNewKlubPageForm_Content, #Form_ContactForm_Comments').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 40
	});
	
});

})(jQuery);



