$(document).ready(function(){
	
	$('div.page-set').each(function(){
		
		if ($(this).attr('id') == 'activity-cont'){

			n_p_sets = 4;
		
		} else {
			
			n_p_sets = n_soft_pagination_sets;
		}

		$(this).data('n_sets', Math.ceil($(this).find('.block').length / n_p_sets));

		if ($(this).data('n_sets') > 1){

			var controls		= $(this).find('div.nav');
			var controls_list	= controls.find('ul:last');
			var controls_n		= controls_list.find('li:first');

			for (var i = 0 ; i < $(this).data('n_sets') ; i++){

				controls_n.clone().appendTo(controls_list);
			}

			controls_n.remove();
			
			controls_list.find('li a').each(function(i){
				
				$(this).attr('rel', i);
				$(this).html(i + 1);
			});

			$(this).data('curr_page', 0);
			$(this).find('a[rel=0]').addClass('active');
			$(this).find('a[rel=0]').parent().addClass('active');
			
			$(this).find('a.prev, a.next, a.number').click(function(){
				
				if (!$(this).hasClass('disabled')){
				
					var curr_page = $(this).closest('div.page-set').data('curr_page');

					if ($(this).hasClass('next')){

						$(this).closest('.nav').find('a.prev').show();
						var new_page = curr_page + 1;
					
					} else if ($(this).hasClass('prev')) {
						
						$(this).closest('.nav').find('a.next').show();
						var new_page = curr_page - 1;
					
					} else {
						
						var new_page = parseInt($(this).attr('rel'));
					}

					if (new_page == $(this).closest('div.page-set').data('n_sets') - 1){
						
						$(this).closest('.nav').find('a.next').addClass('disabled');
					
					} else {
						
						$(this).closest('.nav').find('a.next').removeClass('disabled');
					}

					if (new_page == 0){
						
						$(this).closest('.nav').find('a.prev').addClass('disabled');
					
					} else {
						
						$(this).closest('.nav').find('a.prev').removeClass('disabled');
					}
					
					$(this).closest('.nav').find('a.number').removeClass('active');
					$(this).closest('.nav').find('a.number').parent().removeClass('active');
					$(this).closest('.nav').find('a[rel=' + new_page + ']').addClass('active');
					$(this).closest('.nav').find('a[rel=' + new_page + ']').parent().addClass('active');
					$(this).closest('div.page-set').data('curr_page', new_page);
					
					$(this).closest('div.page-set').find('.block').hide();
					$(this).closest('div.page-set').find('.set-' + new_page).show();

				}

				$(this).blur();
				return false;
			});
		
		} else {
			
			$(this).find('div.nav').remove();
		}
	});

	$('#main .second .left li:last, #announcements li:last, #news li:last, #events li:last').addClass('last');

	$('input#eletter-subscribe, input#send-to-friend').click(function(){
		
		$(this).val('');
		$(this).css('color', '#000000');
	});

	$('#eletter-submit').submit(function(){
		
		$.post('/emails/submit/', { email: $('input#eletter-subscribe').val() }, function(data){
		
			$('input#eletter-subscribe').val(data.message);
			$('input#eletter-subscribe').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");

		return false;
	});

	$('#send-to-friend-submit').submit(function(){
		
		$.post('/emails/send_to_friend/', { email: $('input#send-to-friend').val(), link: window.location.href }, function(data){
		
			$('input#send-to-friend').val(data.message);
			$('input#send-to-friend').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");

		return false;
	});

	var n_books		= $('#books .book').length;
	var curr_book	= 0;

	$('#books-nav a.forward').click(function(){
		
		$('#books .book').hide();
		curr_book = curr_book >= n_books - 1 ? 0 : curr_book + 1;
		$('#books #book-' + curr_book).show();
		$(this).blur();
		return false;
	});

	$('#books-nav a.back').click(function(){
		
		$('#books .book').hide();
		curr_book = curr_book == 0 ? n_books - 1 : curr_book - 1;
		$('#books #book-' + curr_book).show();
		$(this).blur();
		return false;
	});

	
	
	var n_imgs		= $('#imgs .slide-img').length;
	var curr_img	= 0;

	$('#slide-nav a.forward').click(function(){
		
		$('#imgs .slide-img').hide();
		curr_img = curr_img >= n_imgs - 1 ? 0 : curr_img + 1;
		$('#imgs #img-' + curr_img).show();
		$(this).blur();
		return false;
	});

	$('#slide-nav a.back').click(function(){
		
		$('#imgs .slide-img').hide();
		curr_img = curr_img == 0 ? n_imgs - 1 : curr_img - 1;
		$('#imgs #img-' + curr_img).show();
		$(this).blur();
		return false;
	});

	$('#tweets').click(function(){
		
		window.open('http://twitter.com/fightthestupids', 'twitter');
	});
});