$(document).ready(function(){

  $(window).resize(function() {
    setupParallax();
  });

  setupParallax();
  bounce($('.bubble'));
  
  $('#cloud1').pan({
    fps: 30, 
    speed: .5, 
    dir: 'right', 
    depth: 20
  });
  
  $('#cloud2').pan({
    fps: 30, 
    speed: 1, 
    dir: 'right', 
    depth: 50
  });
		
  $('.house').click(function() {
    var key = $(this).data('key');
    $('.clickon').hide();
    $('.bubble').fadeOut('fast');
    if(!$(this).hasClass('focused')){
      //$('.bubble').hide();
      $(this).data({
        top:$(this).css('top'), 
        left:$(this).css('left'), 
        width:$(this).children('.house-img').css('width'), 
        height:$(this).children('.house-img').css('height')
      });
      resetHouse();
      $(this)
      .animate({
        left: "47px", 
        top: "50px"
      }, 500, function(){
        $('.development-'+key).show();
      })
      .addClass('focused');
      $(this).children('.pointer').css("background-position","bottom");
      $(this).children('.pointer').animate({
        width: '129px', 
        height: '275px',  
        left: '10px', 
        top:'-275px'
      }, 500);
      $(this).children('.house-img').animate({
        width: "150px", 
        height: "100px"
      }, 500);
    }else{
      window.location.href = urlLinks[key];
    }
  });
		
  var urlLinks = new Array();
  urlLinks[1] = vrootdir+"/development-overview/pearce-leatherworks";
  urlLinks[2] = vrootdir+"/development-overview/long-meadow-walk-little-stanion-northants";
  urlLinks[3] = vrootdir+"/development-overview/biddenham-great-denham-bedford-mk40-4gp";
  urlLinks[4] = vrootdir+"/development-overview/riverside";
  urlLinks[5] = vrootdir+"/development-overview/ivel-road-shefford-bedfordshire";
		
  for(var i=1; i<=5; i++){
    $('.house'+i).data({
      'key':i
    });
    $('.development-'+i).hide();
    $('.development-'+i+' .close').click(function() {
      resetHouse();
      $('.bubble').fadeIn('fast');
    });
    $('.development-'+i+' .images').cycle({ 
      fx:     'scrollHorz', 
      speed:  'slow', 
      timeout: 0, 
      next:   '.development-'+i+' .next', 
      prev:   '.development-'+i+' .prev' 
    });
  }

  $(".slideshow").hover(
    function () {
      $('.pointer').stop(true, true).fadeOut();
    }, 
    function () {
      $('.pointer').stop(true, true).fadeIn();
    }
  );
		
});
      
function bounce(jqElement) {
  bouncespeed = 400;
  newx = Math.floor(10*Math.random());
  newy = Math.floor(10*Math.random());
  newspeed = bouncespeed + Math.floor(100*Math.random());
  //console.log(newx, newy, newspeed);
  
  $(jqElement).animate({
      'margin-top': newy + 'px',
      'margin-right': newy + 'px'
    }, newspeed, 'linear', function() { 
      bounce(jqElement);
    }
  );
}  

function setupParallax(){
  $('#parallax .parallax-layer').parallax({
    mouseport: $('#parallax'),
    yparallax: false, 
    triggerExposesEdges: true
  });
  $('#parallax').trigger('mouseenter');
}

function resetHouse(){
  $('.slideshow').hide();
  $('.focused').children('.house-img').animate({
    width: '100px', 
    height: '67px'
  }, 500);
  $('.focused').children('.pointer').animate({
    width: '129px', 
    left: '-15px', 
    top:'-80px'
  }, 500);
  $('.focused').children('.pointer').css({
    "background-position":"top", 
    height: '80px'
  });
  $('.focused')
  .animate({
    left: $('.focused').data('left'), 
    top: $('.focused').data('top')
  }, 500)
  .removeClass('focused');
}

