jQuery.fn.getBox = function() {
  return {
    left: $(this).offset().left,
    top: $(this).offset().top,
    width: $(this).outerWidth(),
    height: $(this).outerHeight()
  };
}

jQuery.fn.position = function(target, options) {
  var anchorOffsets = {t: 0, l: 0, c: 0.5, b: 1, r: 1};
  var defaults = {
    anchor: ['tl', 'tl'],
    animate: false,
    offset: [0, 0]
  };
  options = $.extend(defaults, options);

  var targetBox = $(target).getBox();
  var sourceBox = $(this).getBox();

  //origin is at the top-left of the target element
  var top = targetBox.top;
  var left = targetBox.left;

  //alignment with respect to source
  top -= anchorOffsets[options.anchor[0].charAt(0)] * sourceBox.height;
  left -= anchorOffsets[options.anchor[0].charAt(1)] * sourceBox.width;

  //alignment with respect to target
  top += anchorOffsets[options.anchor[1].charAt(0)] * targetBox.height;
  left += anchorOffsets[options.anchor[1].charAt(1)] * targetBox.width;

  //add offset to final coordinates
  top += options.offset[0];
  left += options.offset[1];

  $(this).css({
    top: top + 'px',
    left: left + 'px'
  }).show();

}

$(document).ready(function() {
		
	
  $('input[title], textarea[title]').each(function() {
    var title = $(this).attr('title');
    $(this).addClass('hinted');
    if ($(this).is('input')) {
      $(this).attr('value', title);
      $(this).focus(function() {
        if ($(this).attr('value') == title) {
          $(this).removeClass('hinted').attr('value', '');
        };
      });
      $(this).blur(function() {
        if ($(this).attr('value').length == 0) {
          $(this).addClass('hinted').attr('value', title);
        };
      });
    } else {
      $(this).html(title);
      $(this).focus(function() {
        if ($(this).val() == title) {
          $(this).removeClass('hinted').empty();
        };
      });
      $(this).blur(function() {
        if ($(this).val().length == 0) {
          $(this).addClass('hinted').html(title);
        };
      });
    };
  });
  
  $('.header .nav li').hover(function() {
    $(this).children('ul').show();
  }, function() {
    $(this).children('ul').hide();
  });
  
  var history = $('.history-list');
  if (history.length) {
    history.find('dd').each(function() {
      $(this).wrapInner('<div class="content hidden"></div>');
      if ($(this).find('p').length < 2 && $(this).find('p:first').text().split(' ').length < 38) {
        $(this).prev().addClass('compact');
        $(this).find('.content').show();
        return;
      };
      var p = $(this).find('p:first').text().split(' ').splice(0, 38).join(' ');
      p += '<span class="more">… <img src="/i/excerpt-more.gif" alt="" /></span>';
      $(this).prepend('<p class="excerpt">' + p + '</p>');
      $(this).find('.more img').click(function() {
        $(this).parents('dd:first').prev().click();
      });
    });
    history.find('dt').each(function() {
      if ($(this).is('.compact')) return;
      $(this).click(function() {
        if (!$(this).is('.expanded')) {
          $(this).addClass('expanded').next('dd').addClass('expanded').find('.excerpt').hide().next().show();
        } else {
          $(this).removeClass('expanded').next('dd').removeClass('expanded').find('.excerpt').show().next().hide();
        }
      });
    });
  };
  
  var carousel = $('.carousel');
  if (carousel.length) {
    carousel.find('.carousel-container').append('<span class="prev disabled"/><span class="next"/>').children('.items').jCarouselLite({
        scroll: 3,
        visible: 12,
        btnNext: '.next',
        btnPrev: '.prev',
        circular: false
      });
    carousel.find('a').each(function() {
      var target = $(this).clone();
      var index = $(this).parent().index();
      target.addClass('target-popup target-popup-' + index).find('img').attr({'width': 130, 'height': 130});
      if ($.browser.msie) {
        target.prepend('<b/>');
      };
      target.hover(function() {
        $(this).siblings('.target-popup:visible').hide();
      }, function() {
        $(this).hide();
      });
      $('body > .container-out > .container').append(target);
      $(this).hover(function() {
        $('body > .container-out > .container > .target-popup-' + index).position(this, {offset: [-30, -30]});
      });
    });
  };
  var asideGallery = $('.aside .gallery');
  if (asideGallery.length) {
    asideGallery.find('a').each(function() {
      var target = $(this).clone();
      var index = $(this).parent().index();
      target.addClass('target-popup target-popup-' + index).find('img').attr({'width': 130, 'height': 130});
      if ($.browser.msie) {
        target.prepend('<b/>');
      };
      target.mouseout(function() {
        $(this).hide();
      });
      $('body > .container-out > .container').append(target);
      $(this).hover(function() {
        $('body > .container-out > .container > .target-popup-' + index).position(this, {offset: [-30, -30]});
      });
    });
  };
});

