// Global behaviour

$(document).ready(function() {

});

// AdLocation model

function AdLocation(ad_location_json, form_authenticity_token) {
  
  this.id           = ad_location_json.id;
  this.name         = ad_location_json.name;
  this.reload_rate  = ad_location_json.reload_rate * 1000;
  this.kind         = ad_location_json.kind;
  this.form_authenticity_token = form_authenticity_token;
  
  this.is_visual = function() {
    return (this.kind ==  "AdLocation::Visual");
  };

  this.is_sponsored = function() {
    return (this.kind ==  "AdLocation::SponsoredLinks");
  };
  
  this.is_firm_visual = function() {
    return (this.kind ==  "AdLocation::FirmVisual");
  };
  
  this.element = function() {
    return $("#ad_location_" + this.name);
  };
  
  this.reload = function() {
    
    var ad_location = this;
    
    $.ajax({
      url: "/ad_locations/reload/" + this.id,
      type: 'GET',
      data: {authenticity_token: this.form_authenticity_token, 'category_ids[]': this.categories},
      success: function(data, status) {
        if (ad_location.is_visual()) {
          var campaign = new Campaign(data.campaign);
          campaign.ad_location = ad_location;
          campaigns_controller.show(campaign);
        } else if (ad_location.is_sponsored()) {
          campaigns_controller.index(data, ad_location);
        }
      },
      dataType: "json"
    });
  };

};

// Campaign model

function Campaign(campaign_json) {
  this.id                   = campaign_json.id;
  this.public_filename      = campaign_json.public_filename;
  this.width                = campaign_json.width;
  this.height               = campaign_json.height;
  this.link_url             = campaign_json.link_url;
  this.announcer            = campaign_json.announcer;
  this.commercial_message   = campaign_json.commercial_message;
  
  this.is_image = function() {
    return (this.public_filename.indexOf(".swf") == -1);
  };
  
  this.attachment = function() {
    
    if (this.ad_location.is_visual()) {
      return this.attachment_for_visual();
    } else if (this.ad_location.is_sponsored()) {
      return this.attachment_for_sponsored();
    } else if (this.ad_location.is_firm_visual()) {
      return this.attachment_for_visual();
    }
    
  };
  
  this.attachment_for_visual = function() {
    
    var campaign = this;
    var link = $(document.createElement("a"));
    link.attr('href', "/campaigns/" + campaign.id + "/click");
    link.attr('target', '_blank');
    
    if (this.is_image()) {
      link.append(this.image_attachment());
      return link;
    } else {
      return this.flash_placeholder();
    }
    
  };
  
  this.attachment_for_sponsored = function() {

    var campaign = this;
    
    var attachment = $(document.createElement("p"));
    attachment.addClass('campaign');

    var announcer_link = $(document.createElement("a"));
    announcer_link.attr('target', '_blank');

    announcer_link.attr('href', '/campaigns/' + campaign.id + "/click");
    announcer_link.html(this.announcer.usual_corporate_name);
    
    var commercial_message = $(document.createElement("span"));
    commercial_message.html(this.commercial_message);
    
    attachment.append(announcer_link).append("<br />").append(commercial_message);
    
    return attachment;
  };
  
  this.image_attachment = function() {
    var img = $(document.createElement("img"));
    img.attr('alt', this.public_filename);
    img.attr('src', this.public_filename);

    return img;
  };
  
  this.flash_placeholder = function() {
    return $('<div id="campaign_' + this.id + '"></div>');
  };
  
  this.firm_visual_element = function() {
    return $("#firm_" + this.announcer.public_id + " .campaign.firm_visual");
  };
  
};

// Controller

var ad_locations_controller = {
  
  show: function(ad_location) {

    var ad_location_element = $(document.createElement("div"));
    ad_location_element.addClass('ad_location').addClass(ad_location.name).attr("id", "ad_location_" + ad_location.name);

    if (ad_location.is_visual()) {
      ad_location_element.addClass('visual');
    } else if (ad_location.is_sponsored()) {
      ad_location_element.addClass('sponsored');
    }
    
    if (ad_location.is_sponsored()) {
      if (ad_location.campaigns.length > 0)
        $('script#ad_location_src_' + ad_location.name).after(ad_location_element);
    } else {
      $('script#ad_location_src_' + ad_location.name).after(ad_location_element);
    }
    
    if (ad_location.is_visual()) {
      var campaign = new Campaign(ad_location.campaigns.campaign);
      campaign.ad_location = ad_location;
      campaigns_controller.show(campaign);
    } else if (ad_location.is_sponsored()) {
      campaigns_controller.index(ad_location.campaigns, ad_location);
    }
    
    // to avoid cycle references
    ad_location.campaigns = null;
    
    if (ad_location.is_visual() && ad_location.reload_rate) {
      setInterval(function() {
        ad_location.reload();
      }, ad_location.reload_rate);
    }
  }
  
};

var campaigns_controller = {

  index: function(campaigns_json, ad_location){
    
    ad_location_element = ad_location.element();
    ad_location_element.empty();
    
    if (ad_location.is_sponsored()) {
      var title = $(document.createElement("span"));
      title.addClass('title');
      title.html('Liens Sponsorisés');
      ad_location_element.append(title);
    }
    
    $.each(campaigns_json, function(index, campaign_json) {
      var campaign = new Campaign(campaign_json.campaign);
      campaign.ad_location = ad_location;
      ad_location_element.append(campaign.attachment());
    });
    
  },
  
  show: function(campaign) {

    if (campaign.ad_location.is_visual() || campaign.ad_location.is_sponsored()) {
      var ad_location_element = campaign.ad_location.element();
      ad_location_element.empty();

      var campaign_attachment = campaign.attachment();
      ad_location_element.append(campaign_attachment);
      
      // we must call swfobject.embedSWF AFTER appending the placeholder element in the ad_location
      // or the flash won't show up
      if (!campaign.is_image()) {
        swfobject.embedSWF(campaign.public_filename, campaign_attachment.attr('id'), campaign.width, campaign.height, "9.0.0");
      }

    } else if (campaign.ad_location.is_firm_visual()) {
      campaign.firm_visual_element().append(campaign.attachment());

      if (!campaign.is_image()) {
        swfobject.embedSWF(campaign.public_filename, 'campaign_' + campaign.id, campaign.width, campaign.height, "9.0.0");
      }
    }
  }
  
};