/* 
 * Widget CE
 * by Franck D'agostini
 * franck.dagostini@gmail.com
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 *
 * NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
 *
 *
 * USAGE : $("#widgetCE").widgetCE({sector: 'buy', location: 'paris', limit: 10});
 *  sector: see sector list here : http://www.connexion-emploi.com/offers
 *  location : both french and german name are working see here : http://www.connexion-emploi.com/offers/mapfrance/fr
 *  limit : set the max number of offer to retrieve (max is 10)
 *
 */

jQuery.fn.widgetCE = function(options) {
  var config = $.extend({}, $.fn.widgetCE.defaults, options);

  var offer_url = 'http://www.connexion-emploi.com/' + config.lang + '/offers/'
  var json_url = 'http://www.connexion-emploi.com/offers/feed2?callback=?';

  return this.each(function() {
    var element = jQuery(this);
    
    $.getJSON(json_url, 
      { sector: config.sector, location: config.location, limit: config.limit, title: config.title}, function(data){ 
      if(data.length>0){
        if(config.styling){
          element
            .css("border", "1px solid #E7911B")
            .css("font-size", config.font_size)
            .css("font-family", "verdana,arial,helvetica,sans-serif")
            .css("width", config.box_width)
        }
        
        var title = config.title
        
        if(config.lang=="fr"){
          var lien = $("<a class='ce-title' href='http://www.connexion-emploi.com/fr' alt='Connexion Emploi, le site emploi franco-allemand' title='Connexion Emploi, le site emploi franco-allemand'>" + config.title + "</a>");
        }else{
          var lien = $("<a class='ce-title' href='http://www.connexion-emploi.com/de' alt='Connexion-Emploi, Das Deutsch-Französische Jobportal' title='Connexion-Emploi, Das Deutsch-Französische Jobportal'>" + config.title + "</a>");
        }
        
        
        //var title = $("<h4></h4>").text("Connexion Emploi");
        if(config.styling){
          lien
            .css("display", "block")
            .css("background", "#E7911B")
            .css("color", "#FFFFFF")
            .css("margin","0")
            .css("padding","5px 10px")
            .css("font-weight", "bold")
            .css("text-decoration", "none")
        }
        lien.appendTo(element);
            
        var ul = $("<ul></ul>");
        if(config.styling){
          ul
            .css("margin", "0")
            .css("padding", config.box_padding)
            .css("padding-bottom", "0")
            .css("list-style", "none")
        }
        ul.appendTo(element);
        
        if(config.subtitle){
          var subtitle = $("<h2></h2>").text(config.subtitle)
          if(config.styling){
            subtitle
              .css("font-size", "1em")
              .css("margin", "0 0 1em 0")
              .css("color", "#333")
          }
          subtitle.appendTo(ul)
        }
                
        $.each(data, function(i, item){
          var li = $("<li></li>");
          var a = $("<a></a>")
          
          a.text(item.offer.title)
            .attr("href", offer_url + item.offer.permalink)
            .attr("target", "_blank");
              
          if(config.styling){
            li.css("margin-bottom", config.li_margin)
            a
              .css("text-decoration", "none")
              .css("color", "#336699")
          }
          a.appendTo(li);  
          li.appendTo(element.find("ul"));
        });
        
        
        if(config.lang=="fr"){
          var more = $("<a target='_blank' href='http://www.connexion-emploi.com/fr' alt='Connexion Emploi, le site emploi franco-allemand' title='Connexion Emploi, le site emploi franco-allemand'></a>");
        }else{
          var more = $("<a target='_blank' href='http://www.connexion-emploi.com/de' alt='Connexion-Emploi, Das Deutsch-Französische Jobportal' title='Connexion-Emploi, Das Deutsch-Französische Jobportal'></a>");
        }
        more.text(config.more_text);
                
          
        
        if(config.styling){
          more
            .css("margin","0")
            .css("text-decoration", "none")
            .css("color", "#336699")
            .css("font-weight", "bold")
            .css("font-size", "1em")
        }
        li = $("<li class='all-offers'></li>");
        
        if(config.styling){
          li.css("margin-bottom", config.li_margin);
        }
        
        more.appendTo(li);
        li.appendTo(element.find("ul"));
        
      }
    });
  });
};

jQuery.fn.widgetCE.defaults = {
  'sector'      : '',
  'location'    : '',
  'limit'       : 5,
  'title'       : 'Connexion Emploi',
  'styling'     : true,
  'box_width'   : '400px',
  'box_padding' : '10px',
  'font_size'   : '0.7em',
  'li_margin'   : '10px',
  'more_text'   : 'Voir toutes les offres',
  'lang'        : 'fr'
};
