var popupClose = function() {
    $('.popup').addClass('j-off');
    $('.popup-overlay').addClass('j-off');

}

$(document).keydown(function(e) {
    if(e.which==27) popupClose();

});

$(document).ready(function(){

    $('.popup-handle').click(function() {
        $('#'+this.rel).removeClass('j-off');
        $('.popup-overlay').removeClass('j-off');
		return false;
	});

	$('.popup-close').click(function() {
        popupClose();
		return false;
	});

    $('body').click(function(e){
        if (!$(e.target).parents('.popup').size()) {
            popupClose();
        }
    });

    $('.click').live("mouseenter",function(){
        $(this).css('cursor', 'pointer');
        $(this).addClass('box-hover');
    }).live("mouseleave",function(){
		$(this).removeClass('box-hover');
    });

    $('.click').live("click", function(){
        window.location = $(this).find('.link').attr('href');
    });

    $('.controls, .controls-back, .controls-next').live("mouseenter",function(){
        $(this).addClass('controls-hover, .controls-back-hover, .controls-next-hover');
    }).live("mouseleave",function(){
		$(this).removeClass('controls-hover, .controls-back-hover, .controls-next-hover');
    });

    $('.submit').live("mouseenter",function(){
        $(this).addClass('submit-hover');
    }).live("mouseleave",function(){
		$(this).removeClass('submit-hover');
    });

    $('.client-op').live("mouseenter",function(){
        $(this).addClass('client-op-hover');
    }).live("mouseleave",function(){
		$(this).removeClass('client-op-hover');
    });


});


$(function(){

    var $masoned = $('.wrap');

    $masoned.imagesLoaded(function(){
      $masoned.masonry({
        itemSelector: '.box',
        columnWidth: 90,
        animate: true,
        animationOptions: {
            duration: 2000,
            queue: false
        }
      });
    });

    var $container = $('#page-load');

    $container.infinitescroll({
      navSelector  : '#page_nav',    // selector for the paged navigation
      nextSelector : '#page_nav a',  // selector for the NEXT link (to page 2)
      itemSelector : '.box',     // selector for all items you'll retrieve
      animate      : true,
      extraScrollPx: 180,
      bufferPx     : 10,
      localMode    : true,
      loadingImg : '/images/loader.gif',
      loadingText : '',
      donetext  : '',
      debug: false,
      errorCallback: function() {
        // fade out the error message after 2 seconds
        $('#infscr-loading').animate({opacity: .8},10).fadeOut('fast');
      }
      },
      // call Masonry as a callback
      function( newElements ) {
        var $newElems = $( newElements );
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
          $container.masonry( 'appended', $newElems, true );
        });
      }
    );

  });



