$(document).ready(function(){
  $('.nav .sub-menu:eq(0)>li:gt(4)').wrapAll('<div class="toggle">');
  $('.nav .sub-menu:eq(0)>li:eq(4)').after('<li><a class="toggleLink" href="#">More</a></li>');
  $('.toggle').hide();

  $('a.toggleLink').click(function(event) {
    event.preventDefault();
    $(this).parent().next('.toggle').slideToggle('fast', function() {fixPos();});
    $(this).toggle();
  });
  
  $('.toggle>:last-child').append('<br /><a href="#" class="toggleClose">Less</a>');

  $('a.toggleClose').click(function(event) {
    event.preventDefault();
    $(this).parent().parent().slideToggle('fast',function() {
      fixPos();
    });
    $(this).parent().parent().prev().find('a.toggleLink').toggle();
  });
  
  $(function() {
    $(".caption a[rel]").overlay({
      mask: 'black',
      onBeforeLoad: function() {
        var wrap = this.getOverlay().find(".contentWrap");
        wrap.load(this.getTrigger().attr("href"));
      },
      onLoad: function() {
        initBindings();
      }
    });
    $(".entry-content a[rel]").overlay({
      mask: 'black',
      onBeforeLoad: function() {
        var wrap = this.getOverlay().find(".contentWrap");
        wrap.empty().append('<img alt="" class="large-img" src="' + this.getTrigger().attr("href") + '"/>');
      }
    });
  });
  if (!isiPhone()) {
  $('body.home .nav-wfora,body.home #sections a:not(.toggleLink,.toggleClose),body.wforc .nav-wfora').click(function() { 
    $('#architecture').css({'z-index':'99'}).animate({'left': '0.1%'}, 300, function() {
    });
  });

  $('body.home .nav-wforc, body.wfora .nav-wforc').click(function() { 
    $('#construction').css({'z-index':'99'}).animate({'left': '0.1%'}, 300, function() {
    });
  });
  $('body.wfora .nav-home, body.wforc .nav-home, .logo a, .logob a').click(function(event) { 
    event.preventDefault();
    link =$(this);
    $('#home').css({'z-index':'99'}).animate({'left': '0.1%'}, 300, function() {
      location.href=link.attr('href');
    });
  });
  }

  fixPos();

  $(window).resize(function () {
    fixPos();
  });

  $(window).scroll(function () {
    setPos();
  });

  function fixPos() {
    if ($(window).height() < $('ul#sections').height()+197) 
      $('div.logob').addClass('abs');
    else 
      $('div.logob').removeClass('abs').css('bottom','33px');
    setPos();
  }
  function setPos() {
    $('div.abs').css('bottom', $(window).height()-$('ul#sections').height()-162);
  }
});

function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPad") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}
function initBindings() {
  $('#overlay a').click(function(event) {
    event.preventDefault();
    work=$(this).children('img');
    work=work[0];
    if (work) {
      if ($(this).hasClass('open')) {
        work.src = work.attr('src',work.attr('src').replace('/./','/thumbs/thumbs_'));
        $(this).removeClass('open');
      } else {
        work.src = work.attr('src',work.attr('src').replace('/thumbs/thumbs_','/./'));
        work.removeAttr('height');
        work.removeAttr('width');
        $(this).addClass('open');
      }
    }
  });
}

function popInPlace(work) {
    work=$(work).children('img');
    if (work.hasClass('open')) {
      work.src = work.attr('src',work.attr('src').replace('/./','/thumbs/thumbs_'));
      work.removeClass('open');
    } else {
      work.src = work.attr('src',work.attr('src').replace('/thumbs/thumbs_','/./'));
      work.removeAttr('height');
      work.removeAttr('width');
      work.addClass('open');
    }
}

