// JavaScript Document
$(document).ready(function() {
  $('#jumppage-button').css('display','block'); // show button

  // these elements require JS to display so show them now
  var do_not_display_sticker_on = ['/community', '/category/news', '/contact-us', '/site-map', '/2009/', '/2010/', '/2011/', '/2012/', '/2013', '/arrange-viewing/', '/book-a-meeting-room', '/setup-virtual-office/', '/second-floor/'];
  if ( 
    ($('#main:contains("Sorry, but what you are looking for isn\'t here...")').length == 0) && // hide on 404 error
    ( jQuery.grep(do_not_display_sticker_on, function (url, i) { 
      return (document.location.href.toLowerCase().indexOf(url) >= 0);
    }).length == 0) ) // hide on pages matching url
  {
    $('#jumppage-sticker').prependTo('body'); // move sticker into top of body as IE does not like absolute positioning this element
    $('#jumppage-sticker').css('display','block');
    function align_sticker() { 
      $('#jumppage-sticker').css('left',$('#header_logo').offset().left-70);
    }
    align_sticker();
    $(window).resize(align_sticker);
  }

  var jp = (document.location.toString().indexOf('#2ndfloor')>0);
  // deeplink to the jumppage using the hash.
  showJumpPage(jp);

  // arm the jump page triggers
  $('.jumptrigger').click( function(e){
    e.preventDefault();
    showJumpPage(true);
  });

  // generic function for toggeling jump page content
  function showJumpPage( visible ) {
    // insert the container
    var b = $('body');
    var root = "/wp-content/themes/lemonstudios/";
    var jpurl = '/wp-content/uploads/2010/05/jumppage.html';
    var jpx = $('#jumppage-content').length;

    if ( !visible ) {
      // remove the jumppage content.
      if ( jpx > 0 ) { $('#jumppage-content').remove();  }
    } else {
      // prevent multiple prepends.
      if( jpx == 0 ) {
        b.prepend('<div id="jumppage-content"><div class="entry"></div></div>');
      } else { 
        return true; 
      }

      var settings = {
        tl: { radius: 15 },
        tr: { radius: 15 },
        bl: { radius: 15 },
        br: { radius: 15 },
        antiAlias: true
      }
      curvyCorners(settings, "#jumppage-content");
      
      $('#jumppage-content .entry').load(jpurl, function() { 
        // arm the close button
        $('#jumppage-content .entry').prepend('<a href="#" class="close-jp"><span>close</span></a>');
        $('a.close-jp').click( function(e){
          e.preventDefault();
          showJumpPage(false);
        });

        // this handles the image galley on the jumppage
        // (c) Bruce Thomas 2010 - "sliver gallery"
        // Accordian style collapsing image gallery & navigator.
        var x = $('.gallery').width();
        var ub = $('.gallery ul li').length;

        var border = 0;
        var shim = root + "jumppage/shim.gif";
        x = x - ((2*border)*ub) // compensation or border
        var b2 = border*2;
        var h = $('.gallery ul li a').eq(0).height();
        var rn = Math.floor(Math.random() * (ub));
        $('.gallery ul li a').eq(rn).children('img').show();
        $('.gallery ul li a').css('height',h-b2+'px');
        var si = $('.gallery ul li').eq(rn).find('a img').show().width();

        //alert('si'+si);
        var nw = Math.round((x-si)/(ub-1)) - b2;

        // replace images with BG image.
        $('.gallery ul li a').each( function(){
          var html = $(this).html();
          var img = $(this).find('img');
          var src = img.attr('src');
          $(this).css({'border-size': border+'px','background-image': 'url('+img.attr('src')+')'}).html('<img src="'+shim+'" height="'+img.height()+'" width="'+img.width()+'" />');
        });

        // show the default image.
        $('.gallery ul').width(x+300);
        $('.gallery ul li a').width( nw ).find('img').show();
        $('.gallery ul li a').eq(rn).animate({width:si},'slow').removeClass('selected');

        // handle the click event
        $('.gallery ul li a').click( function(e){
          e.preventDefault();
          $(this).addClass('selected');

          var me = $(this).find('img').width();
          anw  = Math.round(((x-me)/(ub-1)));
          $(this).animate( {width:me},"slow");
          $('.gallery ul li a').not('.selected').each( function() {
            $(this).animate({width:anw},'slow').parent('li').find('span').hide();
          });
          $(this).removeClass('selected').parent('li').find('span').show('slow');
        });
      });
    }
  }
});