(function( $ ){
  function CopterWL(){
    //Initial properties go here...
  }

  //Initialisation
  CopterWL.prototype.drawWidget = function(){
    document.write(
      '<label for="copter-wl-checkin">Check-in</label>' +
      '<span><input type="text" name="copter-wl-checkin" id="copter-wl-checkin" /></span>' +
      '<label for="copter-wl-checkout">Check-out</label>' +
      '<span><input type="text" name="copter-wl-checkout" id="copter-wl-checkout" /></span>' +
      '<label for="copter-wl-numrooms">Rooms</label>' +
      '<span>' +
      '  <select id="copter-wl-numrooms" name="copter-wl-numrooms">' +
      '    <option selected="selected">1</option><option>2</option><option>3</option><option>4</option>' +
      '  </select>' +
      '</span>' +
      '<label for="copter-wl-numguests">Guests</label>' +
      '<span>' +
      '  <select id="copter-wl-numguests" name="copter-wl-numguests">' +
      '    <option selected="selected">1</option><option>2</option><option>3</option><option>4</option>' +
      '  </select>' +
      '</span>' +
      '<a id="copter-wl-searchbtn" href="#">Find Hotels</a>' +
      '<a id="copter-powered" href="http://www.hotelicopter.com/" target="_blank" style="background:url(' + $('#copter-wl-widget').attr('src').replace('widget/search.js', 'shared/img/logos/hotelicopter/32x32-transparent.png') + ') no-repeat top left;width:auto;height:auto;padding:0 0 0 32px;line-height:38px;display:block;clear:both;text-align:left;">Super-powered by hotelicopter</a>'
    );
  };

  //Initialisation (on DOM Ready)
  CopterWL.prototype.init = function(){
    //Make assignments
    this.assignDatePickers();
    this.assignEventHandlers();

    //Trigger numrooms change event
    $('#copter-wl-numrooms').change();
  };

  //Assign jQueryUI date pickers to check in and check out fields
  CopterWL.prototype.assignDatePickers = function(){
    $('#copter-wl-checkin').datepicker({
      dateFormat: 'D, M d yy',
      numberOfMonths: 2,
      minDate: 0,
      onSelect: function(dateText, inst){
        var checkOutDate = $(this).datepicker('getDate');
        checkOutDate.setDate(checkOutDate.getDate() + 1);

        //Set checkout date picker date and min date
        $('#copter-wl-checkout').datepicker('setDate', checkOutDate);
        $('#copter-wl-checkout').datepicker('option', 'minDate', checkOutDate);
      }
    });

    //Create jquery datepicker for check-out date
    $('#copter-wl-checkout').datepicker({
      dateFormat: 'D, M d yy',
      numberOfMonths: 2,
      minDate: 0
    });
  };

  //Assign event handlers to DOM elements
  CopterWL.prototype.assignEventHandlers = function(){
    var self = this;
    $('#copter-wl-numrooms').change(function(e){
      //Work out min and max guests
      var minNumGuests = $(this).val();
      var maxNumGuests = minNumGuests * 4;
      var html = '';
      for(var i = minNumGuests; i <= maxNumGuests; i++){
        html += '<option value="' + i + '">' + i + '</option>';
      }
      $('#copter-wl-numguests').html(html);
    });

    $('#copter-wl-searchbtn').click(function(e){
      e.preventDefault();
      self.search();
    });
  };

  //Perform a search
  CopterWL.prototype.search = function(){
    //Retrieve URL from script src attribute
    var url = $('#copter-wl-widget').attr('src').replace('widget/search.js', '');

    //This check is only required for development mode
    if(url.length == 0){
      url = 'index.html';
    }

    //Construct url hash and navigate
    if($('#copter-wl-checkin').val().length > 0 && $('#copter-wl-checkout').val().length > 0){
      url += '#/search/n/';
      url += 'd1=' + $.datepicker.formatDate('yy-mm-dd', $('#copter-wl-checkin').datepicker('getDate'));
      url += '&d2=' + $.datepicker.formatDate('yy-mm-dd', $('#copter-wl-checkout').datepicker('getDate'));
      url += '&gs=' + $('#copter-wl-numguests').val();
      url += '&rm=' + $('#copter-wl-numrooms').val();
    }
    document.location.href = url;
  };

  // ----------------------------------------------------------------------- //

  // Create a new instance of the widget
  var copterWL = new CopterWL();

  // Write out widget html immediately
  copterWL.drawWidget();

  // When the DOM is ready, initialise the widget
  $(function(){
    copterWL.init();
  });

  // Return a new instance of copterWL
  return( copterWL );

})( jQuery );
