/**
 *  @author MRaichelson
 */

// Reassign jQuery $() to avoid namespace collisions
$j = jQuery.noConflict();

var LBG = {};
// variables
LBG.vars = {
  animspeed:500
};
// reuseable functions
LBG.func = {
  manageDropdowns:function(clickedItem){
    if(clickedItem){
      if($j(clickedItem).is('.dropdownOpen')){ // this item is open: close it
        LBG.func.closeOpenDropdowns();
      }else{ // this item is closed: open it
        // find any open items: close them
        LBG.func.closeOpenDropdowns();
        // find the selected item: open it
        $j(clickedItem).addClass('dropdownOpen').find('div.flyout').slideDown(LBG.vars.animspeed);
      }
    }else{ // click on the body: close any open item
      LBG.func.closeOpenDropdowns();
    }
  },
  closeOpenDropdowns:function(){
    $j('div.dropdownOpen').removeClass('dropdownOpen').find('div.flyout').slideUp(LBG.vars.animspeed);
  }
};
// page setup functions
LBG.init = function(){
  // manipulate page elements
  $j('#doc').removeClass('noJs');
  
  // related Content slideshow
  $j('#docProjects div.panels').cycle(
    {
      fx:'scrollUp', // animation effect
      speed:500,
      timeout:0,
      pager:'#docProjects div.ctrl',
      activePagerClass:'active',
      pagerAnchorBuilder:function(idx,slide){
        var theLink = '<a href="#">0'+(idx+1)+'<\/a>';
        return theLink;
      }
    }
  );
  
  // homepage slideshow
  $j('#hpInteractive div.slides div.slide').each(function(i){
    var theContent = '<span rel="'+i+'" class="item">'+$j(this).find('div.txt').html()+'<\/span>';
    $j(theContent).appendTo('#hpInteractive div.ctrl');
  });
  $j('#hpInteractive div.slide:eq(0)').show();
  $j('#hpInteractive span.item:eq(0)').addClass('itemActive');
  $j('#hpInteractive span.item:not(span.itemActive)').live(
    'click',
    function(){
      // index of the slide that was selected
      var theSlide = $j(this).attr('rel');
      
      $j('#hpInteractive div.slideTop').removeClass('slideTop');
      $j('#hpInteractive div.slide:eq('+theSlide+')').addClass('slideTop').fadeIn(
        500,
        function(){
          $j('#hpInteractive div.slide:not(:eq('+theSlide+'))').hide();
        }
      );
      
      $j('#hpInteractive span.itemActive').removeClass('itemActive');
      $j('#hpInteractive span.item:eq('+theSlide+')').addClass('itemActive');
    }
  );
  
  // tabbed content
  $j('div.tabbed').tabs();
  
  // expandables
  $j('div.expander div.ctrl a').wrapInner('<span><\/span>').append('<img src="/_res/img/x.gif" alt="" />');
  $j('div.expander div.ctrl a').toggle(
    function(){
      $j(this).parents('div.expander').find('div.content').slideDown(
        500,
        function(){
          $j(this).parents('div.expander').addClass('expanderOpen');
        }
      );
      this.blur();
      return false;
    },
    function(){
      $j(this).parents('div.expander').find('div.content').slideUp(
        500,
        function(){
          $j(this).parents('div.expander').removeClass('expanderOpen');
        }
      );
      this.blur();
      return false;
    }
  );
  
  // manage dropdown controls
  $j('div.dropdown > div.ctrl').bind(
    'click',
    function(){
      var thisItem = $j(this).parents('div.dropdown');
      LBG.func.manageDropdowns(thisItem);
    }
  );
  $j('div.dropdown').bind('click',function(event){ event.stopPropagation(); });
  $j('body').bind('click',function(){ LBG.func.closeOpenDropdowns(); });
  
  // map page collapse tool
  $j('div.collapseWrap a').wrapInner('<span><\/span>').append('<img src="/_res/img/x.gif" alt="" />');
  $j('div.collapseWrap a').toggle(
    function(){
      $j(this).parents('div.collapseWrap').find('img').css('background-position','0 -31px');
      $j(this).find('span').text('Expand Map');
      $j('#mapComponent').slideUp(500);
    },
    function(){
      $j(this).parents('div.collapseWrap').find('img').css('background-position','-11px -31px');
      $j(this).find('span').text('Collapse Map');
      $j('#mapComponent').slideDown(500);
    }
  );
  // map page filters
  $j('#mapFilters select').each(function(i){
    $j(this).bind(
      'change',
      function(){
        var theUrl = document.location;
        var targetUrl = theUrl.toString().split('?')[0];
        targetUrl = targetUrl.split('#')[0];
        $j('#mapFilters select:not(:eq('+i+')) option:selected').attr('selected','');
        $j('#mapFilters select:not(:eq('+i+')) option:first').attr('selected','selected');
        var filterBy = escape($j(this).attr('name'));
        var filterCriteria = escape($j(this).val());
        location.href = targetUrl +'?filterBy='+ filterBy +'&filter='+ filterCriteria;
      }
    );
  });
  
  // video lightbox
  $j("a.video").fancybox({
    //'padding':0,
    'autoScale':false,
    'transitionIn':'none',
    'transitionOut':'none'
  });
  
  // photo gallery
  $j('a.galleryLaunch').wrapInner('<span><\/span>').prepend('<img class="galleryIcon" src="/_res/img/x.gif" />');
  $j('a.galleryLaunch').bind(
    'click',
    function(){
      $j('a[rel=gallery]:first').trigger('click');
      this.blur();
      return false;
    }
  );
  $j("a[rel=gallery]").fancybox({
    'transitionIn':'none',
    'transitionOut':'none',
    'titlePosition':'over',
    'titleFormat':function(title, currentArray, currentIndex, currentOpts) {
      var theText = '<span class="galleryItem"><span class="galleryTitle">'+ $j('span.galleryImages a:eq('+currentIndex+') span.imgTitle').text() +'<\/span>';
          theText +='<span class="galleryDesc">'+ $j('span.galleryImages a:eq('+currentIndex+') span.imgDesc').text() +'<\/span>';
          theText +='<span class="galleryState">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '<\/span>';
          theText +='<span class="galleryCredit">'+ $j('span.galleryImages a:eq('+currentIndex+') span.imgCredit').text() +'<\/span><\/span>';
      return '<span id="fancybox-title-over">'+theText+'<\/span>';
    }
  });
  
  // Sitemap headers contain A tags with no HREF attribute
  $j('div.sitemap a:not([href])').css(
    {
      'text-decoration':'none',
      'color':'#000'
    }
  );

  // sectionNavigation
  if($j('div.sectionNavigation li').size() > 0){
    $j('div.sectionNavigation').addClass('sectionNavigation-links');
    $j('div.sectionNavigation').hover(
      function(){ $j(this).addClass('sectionNavigation-hover'); },
      function(){ $j(this).removeClass('sectionNavigation-hover'); }
    );
    $j('div.sectionNavigation li').hover(
      function(){ $j(this).addClass('hover'); },
      function(){ $j(this).removeClass('hover'); }
    );
  }

  // IE-specific code
  if($j.browser.msie){
    $j('div.dropdown > div.ctrl').hover(
      function(){ $j(this).addClass('ctrl-hover'); },
      function(){ $j(this).removeClass('ctrl-hover'); }
    );
    $j('div.projectBox:nth-child(3n+4)').addClass('project4th');
    $j('div.officeBox:nth-child(3n+4)').addClass('office4th');
    $j('div.pt01 div.clearfix div.grid_4:nth-child(3n+5),div.pt02 div.clearfix div.grid_4:nth-child(3n+5),div.pt03 div.clearfix div.grid_4:nth-child(3n+5)').addClass('grid4th');
    $j('ul.services').each(function(i){
      $j('li:nth-child(even)',this).addClass('even');
      $j('li:nth-child(odd)',this).addClass('odd');
    });
    // use classes to emulate :nth-child(even/odd) in table.zebra
    $j('table.zebra').each(function(i){
      $j('tr:nth-child(even)',this).addClass('even');
      $j('tr:nth-child(odd)',this).addClass('odd');
    });
    // add class to first and last list items to emulate :first-child/:last-child
    $j('ul,ol').each(function(i){
      $j('li:first',this).addClass('first-child');
      $j('li:last',this).addClass('last-child');
    });
    // IE6-specific code
    if($j.browser.version < 7){
      $j('div.pt01 > div.clearfix').wrapInner('<div id="ie6hpWrapper"><\/div>');
    }
  }
  // end of IE-specific code
};

/*  Application of code */
$j(document).ready(function(){
  LBG.init();
});


