class AdBanner
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  
  field :title
  field :picture_position
  field :post_date,type: Date
  field :unpost_date,type: Date
  field :context
  field :direct_to_after_click,type: Boolean
  field :ad_fx  #TODO Design should explain
  
  
  before_save :save_or_destroy
  
  embeds_many :ad_images, :cascade_callbacks => true
  
  FX_TYPES = ["blindX","blindY","blindZ","cover","curtainX","curtainY","fade","fadeZoom","growX","growY","scrollUp","scrollDown","scrollLeft","scrollRight","scrollHorz","scrollVert","shuffle","slideX","slideY","toss","turnUp","turnDown","turnLeft","turnRight","uncover","wipe","zoom"]
  
  def display?
    if (self.post_date <= Date.today && (self.unpost_date.nil? || self.unpost_date>= Date.today))     
      return true
    end
      return false
  end
  
  def new_ad_images=(*attrs)
     attrs[0].each  do |attr|   #Loop by JSs,Themes,Imgs
       unless attr[:file].nil?
         self.ad_images << AdImage.new(attr)        
       end
      end
  end
  
  def existing_ad_images=(*attrs)
     attrs[0].each  do |attr|   #Loop by JSs,Themes,Imgs
         ad_image = self.ad_images.find attr[0]
         ad_image.update_attributes(attr[1])
    end
  end
  
  def save_or_destroy
    self.ad_images.each do |ad_image|
      if ad_image.to_save?
        ad_image.save
      end
      if ad_image.to_destroy?
        ad_image.destroy
      end
    end
  end
end