/*

           ::::::::  :::::::::: ::::::::   ::::::::  ::::    ::: :::::::::  
          :+:    :+: :+:       :+:    :+: :+:    :+: :+:+:   :+: :+:    :+: 
          +:+        +:+       +:+        +:+    +:+ :+:+:+  +:+ +:+    +:+ 
          +#++:++#++ +#++:++#  +#+        +#+    +:+ +#+ +:+ +#+ +#+    +:+ 
                 +#+ +#+       +#+        +#+    +#+ +#+  +#+#+# +#+    +#+ 
          #+#    #+# #+#       #+#    #+# #+#    #+# #+#   #+#+# #+#    #+# 
           ########  ########## ########   ########  ###    #### #########  

     ::::::::   ::::::::  :::    :::  :::::::: ::::::::::: ::::    :::  ::::::::  
    :+:    :+: :+:    :+: :+:    :+: :+:    :+:    :+:     :+:+:   :+: :+:    :+: 
    +:+        +:+    +:+ +:+    +:+ +:+           +:+     :+:+:+  +:+ +:+        
    +#+        +#+    +:+ +#+    +:+ +#++:++#++    +#+     +#+ +:+ +#+ +#++:++#++ 
    +#+        +#+    +#+ +#+    +#+        +#+    +#+     +#+  +#+#+#        +#+ 
    #+#    #+# #+#    #+# #+#    #+# #+#    #+#    #+#     #+#   #+#+# #+#    #+# 
     ########   ########   ########   ######## ########### ###    ####  ########  


                /           /       / /  /      /     /  /       / /
                    / /      /    /   /     ===================  
                         /    /   /___  /    secondcousins.org   /
                  /   /       /  ,;(  /-,   ===================
                    /      /    /(((\   (    /   /  /    /   /
                /      /      / \\\\(.__,'           /       /   /
                   /  /   /     /\)\\:._`.__/   /      /   /
                / /     /   /    ,)'        `._   /     /    /
                     /         ,'           \  `-..___)\__,,     /
                 /       /  ,-'    ,         `._     (__.-'   /
                 ,.-'`,_..-'    .-' \         \ ``---''   /
                /  ``-/      _.-'   / \         `. /    /       /
                 /   ``---''    /     \       _,(   /      /
                      /     /           \ _.-''   \ /   /     /
                  /   /      /    /  /  (     .   \      /  
                        /        /       \     \   \ /    /    /
                /  /    /     /      /   / )     )   )    /    
                          /     /     _/_/     /   /  /    /    /
                /  _/__...---/--'''/''  /     /   /```--/-..._/__
                  < >   _.-.    -   _  /    _(  _(_   - _   < >  
                      .   _-  <  >   -'..-''  `'   .-     _   .
                   `--...__     .-.  __.   <  >      <  >
                           ``---..__        ..-._  .   _.-.   <  >
                                    ```----....-._________________   
                                                      

*	Herr Frau
*	by Second Cousins
*	http://secondcousins.org
*	v2.0
*/

$(document).ready(function() {
	
	jQuery.fn.afterCall = function() {
		$(this).each(function(){
			$(this).find('img').removeAttr('title');
		});
		
		$('.sort a').not('#all').each(function(){
			if ($(this).hasClass('current')) {
				var sortLink = $(this).attr('id');
				$('.temp li').not('.' + sortLink).hide();
				$('.temp li.' + sortLink).show();	
			}
		});
		
		if ($('.extra a').hasClass('active-nsfw')) {
			$('li.nsfw').show();	
		}
		else {
			$('li.nsfw').hide();	
		}
		
	};
	
	jQuery.fn.rdmLoc = function() {
		
		//$('.container li').each(function(){
			
			function getPlusMinus() {
				var plusMinus = ['-', '+'];
				return plusMinus[Math.floor(Math.random() * plusMinus.length)];
			}
		
			var docWidth = $(document).width();
			var posx = (Math.random() * (docWidth - 300));
			var posy = (Math.random() * 5800);
			var rota = (Math.random() * 45);
			$(this).css({'left':posx+'px', 'top':posy+'px', '-webkit-transform': 'rotate(' + getPlusMinus() + rota + 'deg)', '-moz-transform': 'rotate(' + getPlusMinus() + rota + 'deg)'});
		//});
	};
	
	$('li').live('hover', function(){
		var index_highest = 0;
		// more effective to have a class for the div you want to search
		$('li').each(function() {
		// always use a radix when using parseInt
			var index_current = parseInt($(this).css('zIndex'), 10);
			if (index_current > index_highest) {
			index_highest = index_current;
			}
		});
		$(this).css({zIndex: index_highest+1});
	});
	
	$('.container').children().afterCall();
	$('.container li').each(function(){
		$(this).rdmLoc();
	});
	$('.container li').draggable();
	
	$(window).scroll(function(){
		if($(window).scrollTop() + $(window).height() > $(document).height() - $(window).height()*2){
			if ($('.nav').length) { 
			$('.loading').show();
			var theLink = $('.nav').attr('href');
			$('.nav').remove();
			$('.container-clear').remove();
			$('.temp').load(theLink + ' .container, .nav', function(){
				$('.temp').afterCall();
				$('.temp .container li').each(function(){
					$(this).rdmLoc();
				});
				//fix for infinite scroll
				//$('html').css({'min-height':$('html').height()+1});
				$('body').append($('.temp').html());
				$('.loading').fadeOut(300);
				$('.container li').draggable();
			});
			$('.temp').empty();
			}
        }
	});
		
		
	$('a.larger').live('click', function(e){
		$('.post-larger').remove();
		$('.post').show();
		var largerLink = $(this).attr('href');
		$(this).parent().parent().after('<a href="#" class="post-larger" title="Click to close"><img src="' + largerLink + '"></a>');
		$(this).parent().parent().hide();
		e.preventDefault();
	});
	
	
	$('.post-larger').live('click', function(e) {
		$(this).remove();
		$('.post').show();
		e.preventDefault();
	});
	
	$('.sort a').live('click', (function(e){
	
		var sortLink = $(this).attr('id');
		
		$('.sort a').not(this).removeClass('current');
		$(this).addClass('current');
		
		if (sortLink == 'all') {
			$('.container li').show(200);
		}
		else {
					
			$('.container li').not('.' + sortLink).hide(200);
			$('.container li.' + sortLink).show(200);
			$('html,body').scrollTop($(document).scrollTop()+1);
		}
		e.preventDefault();
		})
	);
	
	$('.container li.nsfw').hide();
	
	$('#nsfw').toggle(function(e){
		$(this).addClass('active-nsfw');
		$('.container li.nsfw').show();
		e.preventDefault();
	}, function(){
		$(this).removeClass('active-nsfw');
		$('.container li.nsfw').hide();
		e.preventDefault();
	});
	
	
});
