/**** INDEX PAGE ************************************************************/


$(window).load(function() {
 
  // if( typeof(fadeInElements) != 'undefined' ) {
  //  $(fadeInElements).each(function(n) { var $thiz = $(this);
  //    setTimeout(function() {
  //      $thiz.fadeIn("slow");
  //    }, n*100);
  //  });
  // }

   $('#loader').animate({opacity:0},500)
  // $('.content').animate({opacity:1},1500);
 
  // $(".content").fadeIn();
    $(".portfolio_scroll, #portfolio-grid, .contact_right, .bio, .portfolio_nav, .img_cycle").fadeIn();
 
  resizeElements();
 
});

$(document).ready(function() {
  
  


  
    $('.img_cycle').cycle({
       fx: 'fade',
       speed:  1000, 
       timeout: 3000
  });  
  



/** Browser Fix **********************************************************/
 if ( $.browser.webkit ) {
      $(".portfolio_scroll").css( "height", 505);
   }
   
   if($.browser.msie && parseInt($.browser.version) == 7) {
       $(".portfolio_scroll").css( "margin-left", 1);
   }

   if($.browser.msie && parseInt($.browser.version) == 7) {
       $(".portfolio_scroll").css( "margin-left", 1);
   }

   if($.browser.msie && parseInt($.browser.version) == 8) {
      $("#portfolio_scroll").css( "height", 495);
   }


   if($.browser.msie && parseInt($.browser.version) == 7) {
      $("#portfolio_scroll").css( "height", 495);
   }
   
   if($.browser.msie && parseInt($.browser.version) == 8) {
      $("#video_info").css( "margin-left", 175);
   }
   
   if($.browser.msie && parseInt($.browser.version) == 9) {
      $(".top_nav").css( "padding-left", 0);
   }
   
    if($.browser.msie && parseInt($.browser.version) == 9) {
       $("#portfolio_scroll").css( "height", 495);
    }
/** Portfolio page **********************************************************/
  // $(".portfolio_scroll").animate({opacity:0});

  $("#portfolio-grid > a:nth-child(4n)").css({'margin-right': 0,'float':'right'});
  // $("#scroll_controls").animate({opacity:0});
	
	// $(".portfolio_detail").animate({ opacity: "hide" }, "fast");
  // $("#instructions").animate({ opacity: "hide"}, "fast");


  
  $(".thumbnails").click(function(){






   // index = $(this).parent().parent().index() + 1 ;
   
   index =3;

   var id_array = $(this).attr('id');

   var verb_id = id_array.split('_').pop();
   // console.log(verb_id);

   var album_id = '#album_' + verb_id;

   var functionCallback = function(){
     
     $("#portfolio-grid").animate({ opacity: "hide" }, "slow");
     $(".portfolio_scroll").animate({opacity:1},"slow");
   }

   sliderScroll($(album_id),functionCallback);
   }); 

  $(".t_images").click(function(){
  $(".portfolio_detail").animate({ opacity: "show" }, "slow");
  $("#scroll_controls").animate({opacity:1},"slow");
  $("#instructions").animate({ opacity: "show"}, "slow");
  });

  // $("#thumb_nav").click(function(){
  // $(".portfolio_scroll").animate({opacity:0}, "slow");
  // $("#portfolio-grid").animate({ opacity: "show" }, "slow");    
  // $(".portfolio_detail").animate({ opacity: "hide" }, "slow");
  // $("#ajax_video").animate({ opacity: "hide" }, "fast");
  // $(".video").animate({ opacity: "hide" }, "fast");
  //  
  // });

  $("#previous-slide, #next-slide").click(function(){
    $("#instructions").animate({ opacity: "hide"}, 1000);
  
  });
  

	  // $(".portfolio_detail").animate({ opacity: "show" }, "slow");
	 //  $("#instructions").animate({ opacity: "show"}, "slow");


  var index = 1;

  $('#next-slide').click(function() {
    
    index = $(".current").index() + 2;

    if(index > $('#horizontalwrap a').length)
      index = 1;

    sliderScroll($('#horizontalwrap a:nth-child(' + index + ')'));

    return false;

  });


  $('#previous-slide').click(function() {

    index = $(".current").index();

    if(index <= 0)
      index = $('#portfolio_scroll #horizontalwrap a').length;

    sliderScroll($('#portfolio_scroll #horizontalwrap a:nth-child(' + index + ')'));

    return false;

  });




});


$(document).keydown(function(e) {


  if (e.keyCode == 39) { 

  $('#next-slide').click();

  }

  if (e.keyCode == 37) { 

  $('#previous-slide').click();

  }


  if (e.keyCode == 39) { 

  href= $('#next-slide').attr('href');                        
  window.location = href;

  }

  if (e.keyCode == 37) { 

  href= $('#previous-slide').attr('href');                        
  window.location = href;

  }                        


})
/**** WINDOW>RESIZE *********************************************************/




function sliderScroll(target,functionCallback){
  // console.log('sliderScroll triggered');

	if(target.length < 1)
		return false;
	$("#portfolio_scroll #horizontalwrap a").removeClass("current");
	target.addClass("current");
	$("#portfolio_scroll").stop().scrollTo(target,'fast',{axis:'x',onAfter:functionCallback});
  
	var image_id = $(target).attr('id').substring(5);


	return target;
}

/***************************************************************************/

function resizeElements() {
  
  // Set top margins of thumbnail grid in portfolio section
 $('.thumbnails').each(function(){
 
    var this_thumbnail = $(this);
 
    var height = this_thumbnail.width();
 
    this_thumbnail.css('height',height);
 
    image_width = this_thumbnail.find('img').width();
    // console.log(image_width);
    
    image_height = this_thumbnail.find('img').height();
    // console.log(image_height);
    
    if(image_width > image_height) {
 
      // landscape
      var top_margin = (height - image_height)/2;
      this_thumbnail.find('img').css({'margin-top':top_margin,'height':'auto','display':'block'});
 
    } else if(image_width < image_height) {
 
      // portrait: image_width < image_height      
      this_thumbnail.find('img').css({'display':'block','margin':'auto','height':height,'width':'auto'});
    }
 
 
 });
  
}  



 

