//background fade in

(function ($) {
  $(window).load(function () {
    $('#bg').fadeIn(600);
  });
})(jQuery);

jQuery(document).ready(function($) {

//more background fade in
  $('#bg').bind('load', function (event) {
    $(this).fadeIn(600);
  });

  // contact animation start
  $("#contact").css({right: ( -($("#contact").width() ) + 28 ) });

  $("#contact").mouseleave(function() {
    if ($(this).hasClass('active')) {
      //$("#contact").removeClass('active');
      $("#contact").animate({
          right: ( -($("#contact").width() ) + 28 )
        },
        function() {
          $("#contact").removeClass('active');
        }
      );
    }
  });

  $("#contact").mouseenter(function(){
    if (!$(this).hasClass('active')) {
      $("#contact").addClass('active');
      $("#contact").animate({right: 0 });
    }
  });
  // contact animation end


  // gallery code for the blog by Hamilton

  $('.post .entry .gallery-img').hide();
  $('.post .entry').find('.gallery-img:first').show();

  $('a.gallery-link').click(function(){
    $(this).parent().parent().find('.gallery-img').hide();

    var clickedimgid = $(this).attr("href");
    $(clickedimgid).show();

    return false;
  });

  // end gallery


  //project rollovers

  $('#projects .overlay, #projects  h2, #projects h3').css("opacity", "0");

  $("#projects .project").hover(function () {
    $(this).find('.overlay, h2, h3').animate({opacity:1}, 150);
  },
  function(){
    $(this).find('.overlay, h2, h3').animate({opacity:0}, 150);
  });


  //full screen background code

  var theWindow = $(window)

  function resizeBg() {
    var ratio_w = ( theWindow.width()/1600*100);
    var ratio_h = ( theWindow.height()/1000*100);
    var ratio_used = Math.max(ratio_h,ratio_w);

    var leftpos = Math.round( ( theWindow.width() - 16 * ratio_used ) / 2 );
    var toppos = Math.round( ( theWindow.height() - 10 * ratio_used ) / 2 );

    $("#bg").css("left", leftpos).css("top", toppos);

    if ( (theWindow.width() / theWindow.height()) < 1.6 ) {
      $("#bg")
      .removeClass()
      .addClass('bgheight');
    } else {
      $("#bg")
      .removeClass()
      .addClass('bgwidth');
    }
  }

  theWindow.resize(function() {
    if ( ! $('#bg').hasClass('intransition') ) resizeBg();
  }).trigger("resize");


  //project nav code

  if ( $('#project-images').length ) {
    $('.prevlink-full .slidelink, .nextlink-full .slidelink').hide();

    var imgcount = $("#project-images img").length;
    if ( imgcount > 1 ) {
      $('.nextlink-full .slidelink').show();
    }

    $('.nextlink-full .slidelink, .prevlink-full .slidelink').click(function() {
      if (!$('#project-images').hasClass('intransition')) {
        // execute transition if one is in not progress

		$(window).trigger("resize");
		
        $('#project-images').addClass('intransition');

        var currentclass = $('#bg').attr('class');
        var currenttop = $('#bg').css('top');
        var currentleft = $('#bg').offset().left;
        var currentwidth = $('#bg').width();

        if ( $(this).attr('id') == 'next-slide') { // when next button is clicked
		  
		  var nextImg = $('#bg').next('img');
          $('#bg').attr('id', 'oldbg');
          $('#oldbg').animate( { left: (-( $(window).width() ) ) }, 1700);
          nextImg.
            attr('id', 'bg').
            addClass(currentclass).
            css('top', currenttop).
            css('left', $(window).width() ).
            show().
		    animate( { left: currentleft }, 1600, function() {
              $('#oldbg').attr('id', '').hide();
              $('#project-images').removeClass('intransition');
              $(window).trigger("resize");
            });

          if ( $('#slidenum').text() == 1 ) {
            $('.prevlink-full .slidelink').show();
          }

          if ( parseInt( $('#slidenum').text() ) == ( imgcount - 1 ) ) {
            $('.nextlink-full .slidelink').hide();
          }

          $('#slidenum').text( parseInt( $('#slidenum').text() ) + 1 );
        } else { // when previous button is clicked
        
          var prevImg = $('#bg').prev('img');
          $('#bg').attr('id', 'oldbg');
          $('#oldbg').animate( { left: ( $(window).width() ) }, 1700);
          prevImg.
            attr('id', 'bg').
            addClass(currentclass).
            css('top', currenttop).
            css('left', currentleft-currentwidth ).
            show().
		    animate( { left: currentleft }, 1600, function() {
              $('#oldbg').attr('id', '').hide();
              $('#project-images').removeClass('intransition');
              $(window).trigger("resize");
            });


          if ($('#slidenum').text() == 2) {
            $('.prevlink-full .slidelink').hide();
          }

          if (parseInt($('#slidenum').text()) == imgcount) {
            $('.nextlink-full .slidelink').show();
          }

          $('#slidenum').text( parseInt( $('#slidenum').text() ) - 1 );
        }
      }

      return false;
    });

    $('#infobutton a').click( function() {
      $('#info-overlay').fadeIn(600);

      //column functionality

      if (! $('.column.first').length ) { // if has not been columnized yet
        $('.entry').columnize({
          buildOnce : true,
          overflow : {
            height : 243,
            doneFunc : function () {}
          }
        });
      }
      return false;		
    });

    $('#info-overlay').click( function() {
      $('#info-overlay').fadeOut(600);
      return false;
    });
  } //end project nav code



  //project nav drop down animation

  $("#menu-item-437 a").mouseenter(function(){
    clearTimeout($(this).data('timeoutId'));
    $('.projectlist').slideDown();
  }).mouseleave(function(){
    var someelement = this;
    var timeoutId = setTimeout(function(){
      $('.projectlist').slideUp();
    }, 300);
    $(someelement).data('timeoutId', timeoutId); //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
  });

  $(".projectlist").mouseenter(function(){
    clearTimeout($("#menu-item-437 a").data('timeoutId'));
  }).mouseleave(function(){
    var someelement = $("#menu-item-437 a");
    var timeoutId = setTimeout(function(){
      $('.projectlist').slideUp();
    }, 300);
    $(someelement).data('timeoutId', timeoutId); //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
  });
  
  
  //project landing page navigation
  if ( $('#projectscroll').length ) {
  
  	var numofprojpages = $('.projectpage').length;
  	var numofprojects = $('.projectpage:last .project').length;
  	var colsinlastprojpage = Math.min(numofprojects, 4);
	var numofcols = ( numofprojpages - 1 ) * 4 + colsinlastprojpage; // number of project pages minus last one times number of columns per page. add number of columns in last project page
	var currentcol = 1;

	$('.arrow-nav.right a').click( function() {
		if ( currentcol + 4 < numofcols ) {
			currentcol = currentcol + 4;
			$('#projectscroll').animate( { marginLeft: '-=964' } );
		}
		return false;
	});
	  
	$('.arrow-nav.left a').click( function() {
		if ( currentcol > 1 ) {
			currentcol =currentcol -4;
			$('#projectscroll').animate( { marginLeft: '+=964' } );
		}
		return false;
	});
  
  
  }
  

 //home page slider code

  if ( $('#home-images').length ) {

    var imgcount = $("#home-images img").length;

    function imageSlide() {

        var currentclass = $('#bg').attr('class');
        var currenttop = $('#bg').css('top');
        var currentleft = $('#bg').css('left');
        
        if ( $('#bg').next('img').length ) {
        
        	var nextImg = $('#bg').next('img');
        
        
        
        } //end if -- if there is a next image
        
        else {
        	var nextImg = $('#home-images img:first');
        } //end if -- if there aint no more images

		  $(window).trigger("resize");
		  
          $('#bg').attr('id', 'oldbg');
          $('#oldbg').animate( { left: (-( $(window).width() ) ) }, 1600);
          nextImg.
            attr('id', 'bg').
            addClass(currentclass).
            css('top', currenttop).
            css('left', $(window).width() ).
            show().
            addClass('intransition').
		    animate( { left: currentleft }, 1600, function() {
              $('#oldbg').attr('id', '').hide();
              $(this).removeClass('intransition');
              $(window).trigger("resize");
            });
            
           


    }// end function imageSlide
    
    if ( imgcount > 1 ) {
		window.setInterval(imageSlide, 8000);
    }
    
  } //end is there a #home-images div/ is this the home page




  
  
});

