if (typeof common == 'undefined') common = {};

(function($) {
  /**
   * Replaces the src attribute value with filename on the current
   * object.
   *
   * @var string filename The filename
   *
   * @return void
   *
   * @author Lars Hess <hess@twt.de>
   */
  common.initHoverButtonChangeImage = function(filename) {
    $(this).attr('src', filename);
  };

  common.initHoverButtons = function() {
    $('.hoverbutton').each(function(idx, elem) { common.initHoverButton($(elem)); });
  };

  /**
   * Init an hover button with the hover effect
   *
   * @var DOM-element element The button to animate
   *
   * @return void
   *
   * @author Lars Hess <hess@twt.de>
   */
  common.initHoverButton = function($element) {
    var src = common.xtractFileNameFromSrc($element.attr('src'));
    var filename = src.file.split('.');

    $element.bind('mouseover', common.initHoverButtonChangeImage.curry(src.path + '/' + filename[0]+'_hover.'+filename[1]));
    $element.bind('mouseout', common.initHoverButtonChangeImage.curry(src.path + '/' + filename[0]+'.'+filename[1]));

    if ($element.attr('disabled')) {
      $element.attr('src', src.path + '/' + filename[0]+'_hover.'+filename[1]);
    }
  };

  /**
   * Init rating links
   *
   * @return void
   *
   * @author Lars Hess <hess@twt.de>
   */
  common.initRatingLinks = function() {
    $('#rating_score.enabled').click(function(e) { e.stopPropagation(); e.preventDefault(); common.openRatingPopup($(this).find('a').attr('href')) });
  };
  common.openRatingPopup = function(url) {
    common.csspopup(url, 500, 290);
  };

  /**
   * Extract a path and filename from a given location in the form
   *
   * /path/to/file/filename.ext
   * or
   * http://path/to/file/filename.ext
   *
   * @param string A location string as described above
   *
   * @return object
   *   {path: /path/to/file, file: filename.ext}
   *
   * @author Jan Schumann <schumann.jan@guj.de>
   */
  common.xtractFileNameFromSrc = function(src) {
    var parts = src.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
    if (parts == null) {
      var ret = {path: '', file: src};
    }
    else {
      var ret = {path: parts[1], file: parts[2]};
    }

    return ret;
  };


  $(document).ready(function() {
    common.initHoverButtons();
    common.initRatingLinks();
  });

  /**
   * Opens an link always in top-frame
   *
   * @param  dom-objekt element An anchor dom element
   *
   * @return bool
   */
  common.openInTopFrame = function (element) {
    if (element.windows != top) {
      top.location = element.href;
      return false;
    }

    return true;
  }

  /**
   * Opens a lightbox for images
   *
   * @param string url    Url top open
   * @param int    width  Windows-width
   * @param int    height Window-height
   *
   * @return bool
   */
  common.imagepopup = function(url, width, height)
  {
    if (csspopup.getCurrentInstance() != null) {
      return;
    }

    Tips.hideAll()
    try {
      new csspopup(height, width, {
        type: 'image',
        options: {
          src: url
        }
      });
    }
    catch(e) {}

    return false;
  }
})(jQuery);

