var nowe_newsy = 2;

var main = {
	init: function() {
		this.mainNav();
		
		if($('.client-box').length > 0) {
			this.clientBox();
		}
		
		if($('#contact-form button').length > 0) {
			this.contactForm();
		}
		
		if($('#promo').length > 0) {
			this.promoRotator();
		}
		
		if($('a.back').length > 0) {
			this.topScroll();
		}
		
		if($('aside nav').length > 0) {
			this.navFloat();
			
			this.navHovers();
		}
		$('aside nav > ul li ul li ul ul li:last-child').addClass('last-child');
		if($('.load-more').length > 0) {
			this.loader();
		}
	},
	
	loader: function() {
		var $handler 	= $('.load-more'),
			path 		= '',
			count		= 1;
		
		$('a.load-more').live('click',function(){
			//path = $handler.attr('href');
			path = $handler.attr('href') + '/' + nowe_newsy;
			nowe_newsy++;

			//alert(path);
			
			$('a.load-more').addClass('active').text('Ładuję starsze aktualności...');
			
	
			var id;
			id = setInterval(function() {
			$.ajax({
				url: path,
				cache: false,
				success: function(html){
					//$handler.before(html).removeClass('active').text('Załaduj starsze aktulaności');
					$('a.load-more.active').addClass('toRemove').before('<div class="loaded c-'+count+'">'+html+'</div>');
					count++;
					$('a.load-more.toRemove').remove();
					return false;
				}
			}); clearInterval(id); }, 2000);
			
			return false;
		});
	},
	
	clientBox: function() {
		var $handler = $('.client-box .login-button a');
		
		$handler.mouseenter(function(){
			$(this).stop().animate({'opacity': 0.80});
		}).mouseleave(function(){
			$(this).stop().animate({'opacity': 1});
		});
	},
	
	contactForm: function() {
		var $handler = $('#contact-form form button');
		
		$handler.mouseenter(function(){
			$(this).stop().animate({'opacity': 0.80});
		}).mouseleave(function(){
			$(this).stop().animate({'opacity': 1});
		});
	},
	
	mainNav: function() {
		var $handler = $('#navigation ul.main li:not(.active) a');
		
		$handler.mouseenter(function(){
			$(this).stop().animate({'background-color': '#124482'});
		}).mouseleave(function(){
			$(this).stop().animate({'background-color': '#005a9a'});
		});
	},
	
	navHovers: function() {
		var $handler = $('aside nav > ul > li > a');
		/*
		$handler.mouseenter(function(){
			$(this).stop().animate({'opacity': 0.9});
		}).mouseleave(function(){
			$(this).stop().animate({'opacity': 1});
		});
		*/
	},
	
	navFloat: function() {
		var pos = null,
			$handler = $('aside nav'),
			gap = 315;
		
		$(window).scroll(function () {
			var offset = $(document).scrollTop();  
		
			if(offset > gap)
				$handler.animate({'margin-top': offset-gap+'px'}, {duration: 500, queue: false});				
			else
				$handler.animate({'margin-top': 0}, {duration: 500, queue: false});
				
			pos = offset;
		});  
	},
	
	promoRotator: function() {
		var $handler = $('#promo > ul li'),
			$box = $('#promo'),
			count = $handler.length,
        	act = 0,
        	$nextB = $('#promo .next'),
        	$prevB = $('#promo .prev'),
			speed = 10000,
        	play = true,
			btnOp = 0.7;

        $handler.eq(act).siblings().children().hide();
		
        if(count < 2)
        {
            $nextB.hide();
			$prevB.hide();
        }
		
		$nextB.mouseenter(function() {
			$(this).stop().animate({opacity: btnOp});
		});
		$nextB.mouseleave(function() {
			$(this).stop().animate({opacity: 1});
		});
		$prevB.mouseenter(function() {
			$(this).stop().animate({opacity: btnOp});
		});
		$prevB.mouseleave(function() {
			$(this).stop().animate({opacity: 1});
		});
		
        $nextB.click(function(){
            move(1);
            return false;
        });
			
        $prevB.click(function(){
            move(-1);
            return false;
        });
		        
        function move(step)
        {
        	newAct = act + step;
        	if(newAct < 0)
        		newAct = count-1;
        	
        	if(newAct >= count)
        		newAct = 0;
			        		
        	$handler.eq(act).children('img').stop().hide('puff', {}, 300, function(){
		 		$handler.eq(newAct).children('img').fadeIn('fast');
				
				$(this).siblings('h2, p').hide();
				$handler.eq(newAct).children('h2, p').show();
			});	   		
			        	
		  	act = newAct;
		}
		
		$box.mouseenter(function() { play = false; })
        $box.mouseleave(function() { play = true; })

        if(count > 1)
        {          
            interval = setInterval(function(){
                if(play)
                {
                    $nextB.trigger('click');
                }
            }, speed);
        }
	},
	
	topScroll: function() {
		$('a.back').click(function() {
			var $handler = $('html, body');
			
			if($.browser.opera)
				$handler = $('html');
				
			$handler.animate({
				scrollTop: $("#seiko").offset().top
			}, 500);
			
			return false;
		});
	}
};

$(document).ready(function() {
	main.init();
});
