var zj_headeron=true;



//Stuff to execute when DOM completes
$(document).ready(function($){

  $('#introduction, nav a, h1, h2, h3, p').addClass('hide_fontload');

  if ( $('#introduction.innerpage').length > 0 ) {
    zj_headeron = false;
  }  

  
  setTimeout( function() {
  	enable_floating_nav();
  }, 1 );

});


//Stuff to execute after all images have loaded
$(window).load( function() {


  setTimeout( function() {
     enable_hidden_header();
     adjust_nav_position();
  },1);

});

function enable_hidden_header() {
  $('#introduction.innerpage').each( function() {
    var currentScroll = $(window).scrollTop(); 
    $(this).css('display','block');
    var height = ( currentScroll > 0 ) ? currentScroll : $(this).outerHeight();
    //alert( height );
    //setTimeout( function() { $(window).scrollTop( height ) }, 1 );
    $(window).scrollTop( height );  
    zj_headeron=true;
  });
}

function columnize_text()
{
  //Automatically columnize text
  $('.content_section .three_column').columnize({column: 'one_third', columns: 3});
  $('.content_section .two_column').columnize({column: 'one_third', columns: 2});
  $('.content_section .one_column').columnize({column: 'one_third', columns: 1});
}

//This is a major cheat since text-align-last isn't available in most browsers
function justify_lastlines()
{
  $('.content_section .one_third p:last-child').each( function() {
    var innertext = $(this).text();
    innertext = innertext.replace(/^\s+|\s+$/g, '');
    
    if ( innertext.match( /[\w,]$/ ) )
    {
      var orig = $(this).html();
      $(this).html( orig + "<span style='display:inline-block;height:0px;line-height:0;overflow:hidden;padding:0;margin:0;'> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
      if ( $.browser.msie && parseInt($.browser.version, 10) < 8 ) {
        $(this).css('margin-bottom','-2px');
      } else {
        $(this).css('margin-bottom','-13px');
      }
    }

  });
}

function enable_imagebox_hovers( parent )
{

  var firstload=false;
  if ( parent == null ) {
    firstload = true;
    parent = $('body');
  }




  //Apply blue desaturated hover state to case study and person preview images
  //only do this after image load
  $(parent).find('A.casestudy_preview .preview IMG, A.person_preview .preview IMG, A.page_preview.withimage .preview IMG').not('.img_original').each( function() {

    var loadtowatch = window;
    if ( !firstload )
       loadtowatch = this;

    var item = this;

 
    $(loadtowatch).load( function() {
      var duplicate = $(item).clone();
      $(duplicate)
        .css('display','block')
        .addClass('img_original');
        
      try { 
        var altered = $(item)
            .pixastic("desaturate")
            //.pixastic("coloradjust", {red:-0.1,green:-.05,blue:0})
            .css('display','block')
            .addClass('img_hoverstate');

          $(altered)
            .after('<div class="img_hoverstate_cover" style="display:block"></div>')
            .after( duplicate );

      }
      catch ( e ) {
      }

   });

  });
  
  
  
  $(parent).find('A.casestudy_preview, A.person_preview, A.page_preview.withimage')
	.unbind()
	.mouseover( function() {
		$(this).find('.preview p').stop().animate({ 'color': '#738D97' }, 500);
		$(this).find('.preview .img_original').fadeOut(1000);
	})
	.mouseleave( function() {
		$(this).find('.preview p').stop().animate({ 'color': '#454438' }, 200);
		$(this).find('.preview .img_original').fadeIn(300);
	});
  $(parent).find('A.page_preview.noimage, A.blog_preview')
	.unbind()
	.mouseover( function() {
		$(this).find('.preview p').stop().animate({ 'color': '#ffffff' }, 200);
		$(this).find('div.preview').stop().animate({ 'background-color': '#98AFB7'}, 500 );
	})
	.mouseleave( function() {
		$(this).find('.preview p').not('.byline, .date').stop().animate({ 'color': '#454438' }, 300);
		$(this).find('.preview p.date').stop().animate({ 'color': '#65808A' }, 300);
		$(this).find('.preview p.byline').stop().animate({ 'color': '#000000' }, 300);
		$(this).find('div.preview').stop().animate({ 'background-color': '#F5F4EF'}, 300 );
	});

/*    .mouseover( function() {
      $(this).find('.preview .img_hoverstate')
        .css('display','inline-block');
      $(this).find('.preview .img_hoverstate_cover')
        .css('display','block');
      $(this).find('.preview .img_original')
        .css('display','none'); 
    })
    .mouseleave( function() {
      $(this).find('.preview .img_hoverstate')
        .css('display','none');
      $(this).find('.preview .img_hoverstate_cover')
        .css('display','none');
      $(this).find('.preview .img_original')
        .css('display','inline-block');
    });
*/

}


function enable_floating_nav()
{
  //push navigation down if it moves above page top
  $(window).scroll(function () {
    adjust_nav_position();
  });
  adjust_nav_position();

}
function adjust_nav_position() {
    navBaseLoc = $("#introduction").outerHeight();


    var hideAmount = 20;
    var exposeHeight = 45;
    var fullHeight = 60;
 
    var scrollY = $(window).scrollTop();
    
    if ( scrollY > navBaseLoc || zj_headeron==false ) {
      var headerAdjust = 0;
      if ( zj_headeron == false ) {
        headerAdjust = navBaseLoc;
      }

      headerAdjust = scrollY - navBaseLoc;
      if ( headerAdjust > hideAmount ) {
        headerAdjust = hideAmount;
      }

      $("#navarea").css('top', '-' + headerAdjust + 'px').css('position', 'fixed');
      var bgheight = fullHeight;
      //if ( scrollY > navBaseLoc ) {
        var slidepct = (scrollY - navBaseLoc -10 ) / (10+ (fullHeight * .50));
        var slide = fullHeight - Math.floor((fullHeight-exposeHeight) * slidepct);
        if ( slide < exposeHeight ) {
            slide = exposeHeight;
        } else if ( slide > fullHeight ) {
            slide = fullHeight;
        }
        $("#navarea_bg").css('height', slide + 'px' );
      //}
      //else {
      //  $("#navarea_bg").css('height', fullHeight + 'px' );
      //}
    }
    else {
      $("#navarea_bg").css('height', fullHeight + 'px' );
      $("#navarea").css('top', '').css('position', 'absolute');
    }
}

function enable_infinite_scroll()
{

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

    if ( $('#content .autoscrollable').length > 0 )
    {

      $('#content .autoscrollable').infinitescroll({
        navSelector : ".content_section.paging",
        nextSelector : "div.box_paging .nav-next A",
        itemSelector : "#content .autoscrollable .content_section",
	loadingImg : "/wp-content/themes/zeusjones/assets/images/loading.gif",
	loadingText : "",
	debug : false,
	animate : false,
	donetext : "" 
      }, function( newElements ) {
      
	for ( var elemidx in newElements ) {
        	enable_imagebox_hovers( newElements[elemidx] );
	}

      });

    }  

  });

}

function enable_search_bar()
{
  //
  // Handle search functionality
  //
  $('#searchquery').focus( function() {
  	
	if ( zj_searchtouched == false ) {
		$(this).val('');
		zj_searchtouched = true;
	}
  });  
  $('#search_button').click( function() {	
	if ( zj_searchtouched ==true  && $('#searchquery').val() != '' ) {
		$('#searchform').submit();
	}
	return false;
  });
  
  $('#content_searchquery').focus( function() {
	if ( content_searchtouched == false ) {
		$(this).val('');
		content_searchtouched = true;
	}
  });  
  $('#content_search_button').click( function() {	
	if ( content_searchtouched ==true  && $('#content_searchquery').val() != '' ) {
		$('#content_searchform').submit();
	}
	return false;
  });
  
}

function applyIEFixes()
{
    //Procedurally apply styles for IE's lack of last-child selectors
    $('.full_width p:last-child, .one_third p:last-child, .two_thirds p:last-child').each( function() {
      $(this).addClass('last-child');
    });
}


