class AdImage
  include Mongoid::Document
  include Mongoid::Timestamps
  include OrbitModel::TimeFrame
  include OrbitTag::Taggable

  mount_uploader :file, ImageUploader

  field :title, localize: true
  field :context, localize: true
  field :weight, type: Integer, default: 1
  field :out_link #the link itself
  field :link_open #how will the link be opened
  LINK_OPEN_TYPES = ["local", "new_window"]
    
  belongs_to :ad_banner

  before_validation :add_http
  validates :file, :presence => true
  validates :out_link, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :allow_blank => true
  validates :title, :at_least_one => true

  protected

  def add_http
    unless self.out_link.blank? || self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//]
      self.out_link = 'http://' + self.out_link
    end
  end
end
