
//
// Custom Behaviour for Image Design Kitchens
// Matthew Smith-Stubbs 2008-12-22

$(function() {
  // custom scrolling behaviour for content areas
  var scrollValue = {
      up:    "+=150px",
      down:  "-=150px"
    }

  var viewport = $('.viewport');
  var content = $('.viewport p');

  function showOrHideControls() { // show or hide up/down arrows to prevent user overscrolling
    if ( content.offset().top + content.height() < viewport.offset().top + viewport.height()  ) {
      $('a.down').hide();
    } else {
      $('a.down').show();
    }

    if ( content.offset().top >= viewport.offset().top ) {
      $('a.up').hide();
    } else {
      $('a.up').show();
    }
  }

  if ( $('a.up, a.down').length > 0) {
    showOrHideControls(); 
  }
  
  $('a.down, a.up').click(function() { // scroll content
    content.animate({
      marginTop: $(this).hasClass('up') ? scrollValue.up : scrollValue.down
    }, {
      speed: "fast",
      easing: "swing",
      complete: showOrHideControls
    });
    return false;
  });
  
  
  
  // hover effect for homepage
  $('ul.main-menu li a').hover(function() {
    $('span', this).addClass('selected');
  }, function() {
    $('span', this).removeClass('selected');
  });
  
  $('.gallery a.popup').click(function() { // open gallery images in a new window
    window.open('/gallery_popup.html?'+this, 'gallery_popup', 'scrolling=auto,location=no,height=650,width=900,status=no,location=no,toolbar=no')
    return false;
  });
  
  $('a.external').click(function() {
    window.open( this.href );
    return false;
  })
  
  $('.close a').click(function() {
    window.close();
  });
  
  $('.popup p img').each(function() { // show correct gallery image from URL parameter
    this.src = window.location.search.substring(1);
  });
  
  $('textarea').focus(function() {
     this.select();
  })
  
});