$(document).ready(function(){


    /* ======================================== */
    
    welcomeVisible = true;
    welcomeAnimating = false;
    
    $("#site-search #searchform #s").corner("5px left");
    $("#site-search #searchform #searchsubmit").corner("5px right");
    
    
    $(".magic-arrow a").corner("5px");
    
    $(".cat-links a").corner("3px");
    $("#site-categories ul li").corner("3px");
    
    
    
    /* ======================================== */
    
    
	if ($("#welcome-wrapper").css("display") !== "none") {
	
	
		$("#baaarr").delay(2500).fadeOut(200, function(){
			$("#baaarr-up").fadeIn(300);
			
			if (!$("#welcome-wrapper").is(':animated')) {
			
				if (!welcomeAnimating) {
					$("#sheep-particles #particle-1").delay(300).fadeIn(350);
					$("#sheep-particles #particle-2").delay(450).fadeIn(350);
					$("#sheep-particles #particle-3").delay(600).fadeIn(350);
				}
				
			}
			
			
			
		});
		
	}
    
    
    /* ======================================== */
    
    
    $("#welcome #close").click(function(){
        welcomeAnimating = true;
        
        $("#sheep-particles #particle-3").fadeOut(200);
        $("#sheep-particles #particle-2").delay(100).fadeOut(200);
        $("#sheep-particles #particle-1").delay(200).fadeOut(200);
        
        $("#welcome").fadeOut(300, function(){
        
            $("#welcome-wrapper").delay(300).slideUp({
                duration: 400,
                easing: "easeInOutQuad",
                complete: function(){
                
                    $("#baaarr").fadeIn(300);
                    $("#baaarr-up").fadeOut(300, function(){
                    
                        welcomeAnimating = false;
                        
                    });
                    
                }
            });
            
        });
        
        
        
        
    });
    
    
    /* ======================================== */
    
    
    $("#site-header #site-sheep #baaarr").click(function(){
        $("#welcome-wrapper").slideDown({
            duration: 400,
            easing: "easeInOutQuad",
            complete: function(){
            
            
                $("#sheep-particles #particle-1").fadeIn(150);
                $("#sheep-particles #particle-2").delay(100).fadeIn(150, function(){
                    $("#welcome").fadeIn(200);
                });
                
                $("#sheep-particles #particle-3").delay(200).fadeIn(150);
                
                
            }
        });
        
        
        $(this).fadeOut(300);
        $("#baaarr-up").fadeIn(300);
        
        
    });
    
    
    
    /* ======================================== */
    
    $(window).resize(function(){
    
    
        checkWindow(false); // not first run, so dont call any setup-ish functionality!
    });
    
    
    /* ======================================== */
    
    $(window).scroll(function(){
    
        if ($(window).width() >= 1000) {
        
            if ($(this).scrollTop() > 300) {
                $(".magic-arrow").fadeIn(400);
            }
            
            
            if ($(this).scrollTop() < 300) {
                $(".magic-arrow").fadeOut(400);
            }
            
        }
       
    });
    
    
    /* ======================================== */
    
    checkWindow = function(firstRun){
    
        w = $(window).width();
        s = $(window).scrollTop();
		v = "6%"; //distance from left and right screen edges.
		
		showArrows = false;
		if (s > 300) showArrows = true;
		
		
        if (w < 1000) {
        
            $(".magic-arrow").stop().fadeOut(400);
            
        }// w<1000 if
        else {
        
      			if(w <= 1200) v = 20; // if they have a tiny screen, make it abs px value
				
				if (firstRun) {
					$("#magic-arrow-left").css("left", v);
					$("#magic-arrow-right").css("right", v);
					
				}
				else {
					if(showArrows) $("#magic-arrow-left, #magic-arrow-right").css("display", "block");
					
					$("#magic-arrow-left").stop().animate({
						left: v,
						opacity: 100
					}, {duration: 500, easing: "easeInOutQuad"});
					$("#magic-arrow-right").stop().animate({
						right: v,
						opacity: 100
					}, {duration:500, easing: "easeInOutQuad"});
					
				}
		            
       }// w<1000 else
        
    }//checkWindow
	
    checkWindow(true); //first run, set it automatically, otherwise, animate smoothly!
});

