(function($, window, undefined) {

  var toggleLamp = function(timeouts) {
    $(timeouts).each(function(index, timeout) {
      setTimeout(function() {
        $('#lamp').toggleClass('on');
      }, timeout);
    });
  }

  var adjustWidths = function() {
    $('ul.children').each(function(index, el) {
      var parent = $(el).parent();
      $(el).width($(parent).width());
    });
  }

  $('body.first').each(function() {
    $('#curtain').slideDown(700);
    toggleLamp([400 + 200, 400 + 350, 400 + 580, 400 + 650, 400 + 690]);
  })

  $('body.home').each(function() {
    window.twitterCallback = function(tweets) {
      if (tweets.length == 1) {
        var username = tweets[0].user.screen_name;
        var status = tweets[0].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
          return '<a href="'+url+'" target="_blank">'+url+'</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
          return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
        });
        var html = '<a class="username" href="http://twitter.com/' + username + '" target="_blank">' + username + '</a>: ' + status;
        $('#last-tweet').html(html).slideDown();
      }
    }

    setTimeout(function() { $.getScript(THEME_URL + '_twitter.php'); }, 1000);
  });

  $('dl.gallery-item a').removeAttr('title');

  adjustWidths();
  setTimeout(adjustWidths, 500);
  setTimeout(adjustWidths, 1000);

  $('#menu li:has(ul)').hover(function() {
    $(this).find('ul').fadeIn(200);
  }, function() {
    $(this).find('ul').fadeOut(200);
  });

  $('.imasters-wp-faq-listing dt').toggle(function() {
    $(this).addClass('open').next('dd').slideDown(150);
  }, function() {
    $(this).removeClass('open').next('dd').slideUp(150);
  });

})(jQuery, this);
