jQuery(document).ready(function() {

    /* Init Functions */
    initFeaturedWorkHover();
    initFeaturedWorkAnim();
    initEvents();           
            
    /* Featured Work */
    function initFeaturedWorkHover() {
        var projects = jQuery('#middle_section ul li .pic_sec a.tall');
        projects.hover(
            function () { 
                jQuery(this).children('.hover').removeClass('offscreen'); 
            }, 
            function () { 
                jQuery(this).children('.hover').addClass('offscreen');
            }
        );

    };

    function initFeaturedWorkAnim() {
        var project = jQuery('#middle_section ul li .pic_sec a.tall');
        var projectTxt = project.children('.hover');

        project.find('.hover').css({left: '0px'})
        project.find('.hover-bg').fadeTo(1, 0.0)
        project.find('.hover-content').css({left: '-300px'})

        project.hover(
            function () {
                jQuery(this).find('.hover-content').css({left: '-260px'});
                jQuery(this).find('.hover-bg').stop().fadeTo(100, 1);
                jQuery(this).find('.hover-content').stop().animate({left: '20px'},200 );
            }, 
            function () {
                jQuery(this).find('.hover-bg').stop().fadeTo(400 , 0.0);
                jQuery(this).find('.hover-content').stop().animate({left: '300px'},100 );
            }
        );
    };
    
    function initSlideBox() {

        jQuery(window).scroll(function(){
            
            var distanceTop = jQuery('#slideboxplacer').offset().top - jQuery(window).height();

            if  (jQuery(window).scrollTop() > distanceTop)
                jQuery('#slidebox').animate({'right':'0px'},100);
            else
                jQuery('#slidebox').stop(true).animate({'right':'-430px'},100); 
        });

        /* remove the slidebox when clicking the cross */
        jQuery('#slidebox .close').bind('click',function(){
            jQuery(this).parent().remove();
        });
    };
    
    function initEvents() {
        
        /* Preloader */
		if (typeof preloader == "function") {
			jQuery("div#featured-work").preloader();
			jQuery("div.showcase").preloader();
			jQuery("ul.row").preloader();
        }
		
        /* Pretty Photo */
        jQuery("a.modal").attr('rel','gallery[modal]');
        jQuery("a.modal").prettyPhoto({animationSpeed:'normal',theme:'pp_default', show_title : false});

        /* Image Over */
        jQuery('div.showcase img').css({ opacity: 1 });
        jQuery('div.showcase img').hover(function(){
            jQuery(this).stop().animate({ opacity: .7 }, 250);
        }, function(){
            jQuery(this).stop().animate({ opacity: 1 }, 350);
        });
        
        /* IE FIX */
        jQuery("div#featured-work ul li.project:nth-child(3n)").css("margin-right", "0px");
        jQuery("div.post ul.row li:nth-child(3n)").css("margin-right", "0px");
        
    };
})
