jQuery(function($){

  // jQuery UI datepicker for Hotel Search
  $('#hotel_check_in_date input, #hotel_check_out_date input').datepicker({ buttonImage: '/bgsg-assets/icons/calendar.png', buttonImageOnly: true, showOn: 'both' });
  
  // enable the photo gallery
  $('#photo-gallery a').zoomimage({caption: false});

  // Registration box
  $('#home_registration .button a').click(function(){
    $('#home_registration_form').toggleClass('drop');
    return false;
  });

  $.extend($.fn, {

    // randomizes child elements and puts sets of them in containers
    remix: function(){

      // randomize function: http://sedition.com/perl/javascript-fy.html
      function randomize(arr){
        var i = arr.length;
        if ( i == 0 ) return false;
        while ( --i ) {
          var j = Math.floor( Math.random() * ( i + 1 ) );
          var tempi = arr[i];
          var tempj = arr[j];
          arr[i] = tempj;
          arr[j] = tempi;
        }
      }

      var iterate = 3;

      var elements = [];
      $(this).children().each(function(i, element){
        elements.push(element);
      });

      randomize(elements);

      var container = $(this).clone().empty();
      var current_subcontainer;

      var counter = 0;

      $.each(elements, function(i, element){
        if(counter >= iterate | i == elements.length){
          counter = 0;
          container.append(current_subcontainer);
          current_subcontainer = null;
        } else {
          if(!current_subcontainer) current_subcontainer = $('<div></div>');
          current_subcontainer.append(element);
          counter++
        }
      });
      $(this).replaceWith(container);
    }
  });

  $('#home_sponsors .content').remix();
  $('#home_sponsors .content').cycle({
    timeout: 0,
    next: '#next'
  });

  var sto = STO({ userId: "93756", 
                  accountId: "288", 
                  accountKey: "vEhRR2RLgGKjqBPGi8GPwMddkb8ll6Ljg1rRuQVrPlAHJafPUxS8NgJpJ6hj5gjetyW6Rp5JJHBDt2KV7vIi4tGxZzZ45ZXIiFfC", 
                  userKey: "1T9c9dFENkzbeVy6z4xZwwkaqCpfxKk9axEnqAZfZPQUwstxTm5g9PuyuUSe8tIaACsLh2n4sQfSCDwq839jjLnAbRvShrXWbM3y" });

  var timestamp = sto.timestamp();

  $("#sto_search_form").attr("action", $("#sto_search_form").attr("action") + "&Timestamp=" + timestamp);
  $("#sto_search_form input[name=rcmwl]").val(sto.token(timestamp));

});
