function Sound(opt_loop) {
  var self_ = this;
  var context_ = null;
  var source_ = null;
  var loop_ = opt_loop || false;
  window.AudioContext = window.AudioContext || window.webkitAudioContext;
  if (window.AudioContext) {
    context_ = new window.AudioContext();
  }
  this.load = function(url, mixToMono, opt_callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
      if (context_) {
	self_.sample = context_.createBuffer(this.response, mixToMono);
	if (opt_callback) {
	  opt_callback();
	}
      }
    };
    xhr.send();
  };
  this.play = function() {
    if (context_) {
       source_ = context_.createBufferSource();
       source_.buffer = self_.sample;
       source_.looping = loop_;
       source_.connect(context_.destination);
       source_.noteOn(0);
    }
  };
  this.stop = function() {
    if (source_) {
      source_.noteOff(0);
      source_.disconnect(0);
    }
  };
}

var moan1 = document.createElement('audio');
    moan1.setAttribute('src', 'audio/moan1.ogg');
    moan1.load();
var moan2 = document.createElement('audio');
    moan2.setAttribute('src', 'audio/moan2.ogg');
    moan2.load();
var moan3 = document.createElement('audio');
    moan3.setAttribute('src', 'audio/moan3.ogg');
    moan3.load();

function rand(n,b){
  var num = 0;
  if (b){
    while(num < 2){
      num = Math.floor(Math.random()*n+1);
   }
  }else {
    num = Math.floor(Math.random()*n+1);
  }
    return num;
}

var moan_timer = function () {
  var keep_counting=1;
  var time_left = 10;
  function countdown() {            
    if(time_left < 2) {
      keep_counting = 0;
    }
    time_left = time_left - 1;
  }
  function show_time_left() {
    return time_left;
  }
  function no_time_left() {
  }
  return {
    count: function () {
      countdown();
      //show_time_left();
    },
    timer: function () {
      moan_timer.count();
      if(keep_counting) {
        setTimeout("moan_timer.timer();", 1000);
      } else {
        no_time_left();
      }
    },
    init: function (t) {
      time_left = t;
      keep_counting = 1;
      moan_timer.timer();
    },
    return: function () {
      return show_time_left();
    }
  };
}();

$(function() {
  moan_timer.init(1);
  $( "#dragContainer" ).draggable({ 
    axis: "y",
    containment: "#wrapper",
    scroll: false,
    drag: function() {
      var offset = $('#dragContainer').offset();
      var top= offset.top;
      //console.log(top);
      if(top < 50){
        $('#tightPussy2').css("visibility","visible");
        if(moan_timer.return() < 1){
          switch(rand(3,0)){
	    case 1:
	      moan1.play();
	      break;
	    case 2:
	      moan2.play();
	      break;
	    case 3:
	      moan3.play();
	      break;
	    default:
	      moan1.play();
	      break;
	  }
          moan_timer.init(rand(4,1));
        }
        go = 0;
      }else {
        $('#tightPussy2').css("visibility","hidden");
      }
    }
  });
});





