/*-----------------------------------------------------------------------------------*/
/*  Set everything up
/*-----------------------------------------------------------------------------------*/

jQuery(document).ready(function() {
  

/*-----------------------------------------------------------------------------------*/
/*  Dropdown functionality for main menu
/*-----------------------------------------------------------------------------------*/

  $('header .main-nav').superfish({
    delay: 200,
    animation: {opacity:'show', height:'show'},
    speed: 'fast',
    autoArrows: false,
    dropShadows: false
  });


/*-----------------------------------------------------------------------------------*/
/*  Disable Certain Nav Items
/*-----------------------------------------------------------------------------------*/

  $('.aboutus > a, .marketsectors > a, .jokakenews > a').click(function (e) {
    e.preventDefault();
  });
  
  

/*-----------------------------------------------------------------------------------*/
/*  Homepage Slider for Market Sectors
/*-----------------------------------------------------------------------------------*/
  
  if($('.slider').length > 0){
  
    $('.slider').cycle({
      fx:     'fade',
      speed:  'slow',
      timeout: 4000,
      speed: '1500',
      pager:  '.slider-nav',
      before:  function() {
        $('.caption').html( 
          "EVERY PROJECT HAS A STORY TO TELL" + 
          "<span>" + $(this).find('img').attr('alt') + "</span>" 
        )
      },
      pagerAnchorBuilder: function(idx, slide) {
        return '.slider-nav a:eq(' + (idx) + ') ';
      }
    });
    
  }
  

/*-----------------------------------------------------------------------------------*/
/*  Homepage Slider for Market Sectors
/*-----------------------------------------------------------------------------------*/
  
  if($('.sector-slider').length > 0){
  
    $('.sector-slider').cycle({
      fx:     'fade',
      timeout: 4000,
      speed: 600
    });
    
  }
  
  
/*-----------------------------------------------------------------------------------*/
/*  Adjust Styles
/*-----------------------------------------------------------------------------------*/
  
  /* about nav */
  $('.content section nav a:first-child').css('padding-left','0');
  $('.content section nav a:last-child').css('border','none');
  
  $('#leadership div:last').css('border','none');
  
  
  
/*-----------------------------------------------------------------------------------*/
/*  Slider for Mascot in Meet Joe section
/*-----------------------------------------------------------------------------------*/
  
  if($('.mascot-slider').length > 0){
  
    $('.mascot-slider').cycle({
      fx:     'fade',
      speed:  'slow',
      timeout: 10000,
      cleartype: false
    });
    
  }
  
  $('.meet-joe').click(function () {
    location.href = '/meet-joe/';
  })
  $('.meet-joe').hover(function () {
    $(this).css('cursor','pointer');
  })
  
  $('.rising-challenge').click(function () {
    location.href = '/rising-to-the-challenge/';
  })
  $('.rising-challenge').hover(function () {
    $(this).css('cursor','pointer');
  })
  

  
/*-----------------------------------------------------------------------------------*/
/*  Get column difference and apply it to the news section height
/*-----------------------------------------------------------------------------------*/

  function columnChecker() {
        
    var $mainHeight = $('.content').height();
    var $asideHeight = $('aside').height();
    var columnDifference;
    
    columnDifference = $('.content').height() - $('aside').height();
    
    if ($mainHeight > $asideHeight) {
      $('.news').height($('.news').height() + columnDifference);
    }
  
  }  
          
/*-----------------------------------------------------------------------------------*/
/*  Market Sector Display and Functionality
/*-----------------------------------------------------------------------------------*/
  
  // adjust display of elements on market sector list
  $('.project-list li:even').css({'float':'left','clear':'left'});
  $('.project-list li:odd').css({'float':'right','clear':'right'});
  
  
  $('.project-list')
    .children(':eq(0),:eq(1)')
    .addClass('market-list-item');
  
  // set pagination nav width
  var $pagLabel = $('.market-sector .pagination strong').outerWidth();
  var $pagNav = $('.market-sector .pagination nav').outerWidth();
  $('.market-sector .pagination').width($pagLabel + $pagNav + 26);

  //fade out li clicked when modal is launched
  $('.market-sector .project-list li a').click(function () {
    $(this).parent().addClass('selected').fadeTo('fast', 0.5);
  })

  //small adjustments
  $('.celebrating-service-list div:last').css('border','none');

/*-----------------------------------------------------------------------------------*/
/*  Modal Window
/*-----------------------------------------------------------------------------------*/
  
	$(".market-sector .project-list li a").click(function(e){
    
    id = jQuery(this).attr('rel');
    
    $('body')
      .css('overflow','hidden');
    
    $('<div id="lightbox_overlay"></div>')
      .css({
        'top': $(document).scrollTop(),
        'opacity':'0',
        'z-index' : 9000
      })
      .animate({'opacity':'0.9'},'normal')
      .appendTo('body');
    
    $('<div id="lightbox_container"></div>')
      .hide()
      .css({
        'z-index': 9500,
        'width': 700,
        'height':500
      })
      .appendTo('body');
    
    $('<a id="lightbox_close">Close</a>')
      .appendTo('#lightbox_container');
      
    $('<div id="ajax-content"></div>')
      .load('/modal/?id='+id,function () {
        positionLightBox();
        
        //initialize cycle plugin here
        $('#modal-slider div').cycle({
          fx: 'scrollHorz',
          easing: 'easeInOutQuint',
          timeout: 0,
          prev:   '#modal-prev',
          next:   '#modal-next',
          before:  function() {
            $('#modal-slider strong').html(this.alt);
          }//,
          //after: onAfter
        });


        /*
function onAfter(curr,next,opts) {
        	var caption = 'IMAGE ' + (opts.currSlide + 1) + ' OF ' + opts.slideCount;
        	$('#count').html(caption);
        }
*/
        
        // get nav item amount and set container width
        var modalNavLinkWidth = $('#pager-nav a').outerWidth();
        var modalNavLinkAmount = $('#pager-nav a').length;
        var modalNavBoxWidth = modalNavLinkWidth * modalNavLinkAmount;
        $('#pager-nav').css({
          width: modalNavBoxWidth,
          margin: '0 auto'
        })
        
      })
      .appendTo('#lightbox_container');
    
    // close with ESC key
    $(document).keyup(function(e) {
      if (e.keyCode == 27) {
        removeLightBox();
      }
    });
    
    $('#lightbox_close').live('click',function (e) {
      removeLightBox();
      e.preventDefault();
    })
    
    function removeLightBox(){
      $('#lightbox_overlay,#lightbox_container')
      .fadeOut('normal',function () {
        $(this).remove();
        $('body').css('overflow','auto');
        $('li.selected').css({ opacity: 1.0 }).removeClass('selected');
      })
    }
    
    function positionLightBox() {
      var top = ( $(window).height() - $('#lightbox_container').height() ) / 2;
      var left = ( $(window).width() - $('#lightbox_container').width() ) / 2;
      $('#lightbox_container')
      .css({
        'top': top + $(document).scrollTop(),
        'left': left
      })
      .fadeIn();
      
      
      
    }
    
    return false;
		
	});
	
	
	
/*-----------------------------------------------------------------------------------*/
/*  Columns for Testimonial Page
/*-----------------------------------------------------------------------------------*/
  $('.testimonials').masonry({
    itemSelector: '.testimonials div',
    columnWidth: 340,
    gutterWidth: 30
  });

	
/*-----------------------------------------------------------------------------------*/
/*  Twitter Feed
/*-----------------------------------------------------------------------------------*/

  //pull in twitter feed
  getTwitters('tweets', { 
    id: 'jokakecompany', 
    count: 2, 
    enableLinks: true, 
    ignoreReplies: true, 
    clearContents: true,
    newwindow: true,
    template: '<p><strong>%time%</strong>%text%</p>',
    callback: function () {
      $('#tweets li').addClass('dotted-spacer');
      $('#tweets li:last-child').removeClass('dotted-spacer');
      
      columnChecker();
 
      if($('.news').height() < $('#tweets').height()){
        $('.news').css('height',$('#tweets').height() + 80);
      }
      
    }
  });		

 /* ending ready tag - don't delete */
});





